mirror of
https://github.com/octoleo/plantuml-server.git
synced 2024-11-11 15:20:58 +00:00
New servlet structure with a dedicated class UmlDiagramService to
produce diagram from a compressed source.
This commit is contained in:
parent
225ef45c37
commit
01a16339be
@ -9,6 +9,14 @@
|
|||||||
<servlet-name>welcome</servlet-name>
|
<servlet-name>welcome</servlet-name>
|
||||||
<servlet-class>net.sourceforge.plantuml.servlet.Welcome</servlet-class>
|
<servlet-class>net.sourceforge.plantuml.servlet.Welcome</servlet-class>
|
||||||
</servlet>
|
</servlet>
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>imgservlet</servlet-name>
|
||||||
|
<servlet-class>net.sourceforge.plantuml.servlet.ImgServlet</servlet-class>
|
||||||
|
</servlet>
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>svgservlet</servlet-name>
|
||||||
|
<servlet-class>net.sourceforge.plantuml.servlet.SvgServlet</servlet-class>
|
||||||
|
</servlet>
|
||||||
<!-- Patterns of the servlet -->
|
<!-- Patterns of the servlet -->
|
||||||
<servlet-mapping>
|
<servlet-mapping>
|
||||||
<servlet-name>welcome</servlet-name>
|
<servlet-name>welcome</servlet-name>
|
||||||
@ -23,9 +31,13 @@
|
|||||||
<url-pattern>/form</url-pattern>
|
<url-pattern>/form</url-pattern>
|
||||||
</servlet-mapping>
|
</servlet-mapping>
|
||||||
<servlet-mapping>
|
<servlet-mapping>
|
||||||
<servlet-name>plantumlservlet</servlet-name>
|
<servlet-name>imgservlet</servlet-name>
|
||||||
<url-pattern>/img/*</url-pattern>
|
<url-pattern>/img/*</url-pattern>
|
||||||
</servlet-mapping>
|
</servlet-mapping>
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>svgservlet</servlet-name>
|
||||||
|
<url-pattern>/svg/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
<servlet-mapping>
|
<servlet-mapping>
|
||||||
<servlet-name>plantumlservlet</servlet-name>
|
<servlet-name>plantumlservlet</servlet-name>
|
||||||
<url-pattern>/start/*</url-pattern>
|
<url-pattern>/start/*</url-pattern>
|
||||||
|
54
src/net/sourceforge/plantuml/servlet/DiagramResponse.java
Normal file
54
src/net/sourceforge/plantuml/servlet/DiagramResponse.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package net.sourceforge.plantuml.servlet;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import net.sourceforge.plantuml.FileFormat;
|
||||||
|
import net.sourceforge.plantuml.FileFormatOption;
|
||||||
|
import net.sourceforge.plantuml.SourceStringReader;
|
||||||
|
import net.sourceforge.plantuml.StringUtils;
|
||||||
|
|
||||||
|
|
||||||
|
class DiagramResponse {
|
||||||
|
private HttpServletResponse response;
|
||||||
|
private FileFormat format;
|
||||||
|
private static final Map<FileFormat, String> contentType;
|
||||||
|
static {
|
||||||
|
Map<FileFormat, String> map = new HashMap<FileFormat, String>();
|
||||||
|
map.put(FileFormat.PNG, "image/png");
|
||||||
|
map.put(FileFormat.SVG, "image/svg+xml");
|
||||||
|
contentType = Collections.unmodifiableMap(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
DiagramResponse( HttpServletResponse r, FileFormat f) {
|
||||||
|
response = r;
|
||||||
|
format = f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void sendDiagram( String uml) throws IOException {
|
||||||
|
long today = System.currentTimeMillis();
|
||||||
|
if ( StringUtils.isDiagramCacheable( uml)) {
|
||||||
|
// Add http headers to force the browser to cache the image
|
||||||
|
response.addDateHeader( "Expires", today + 31536000000L);
|
||||||
|
// today + 1 year
|
||||||
|
response.addDateHeader( "Last-Modified", 1261440000000L);
|
||||||
|
// 2009 dec 22 constant date in the past
|
||||||
|
response.addHeader( "Cache-Control", "public");
|
||||||
|
}
|
||||||
|
response.setContentType( getContentType());
|
||||||
|
SourceStringReader reader = new SourceStringReader( uml);
|
||||||
|
reader.generateImage( response.getOutputStream(), new FileFormatOption(format));
|
||||||
|
response.flushBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getContentType() {
|
||||||
|
return contentType.get( format);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
27
src/net/sourceforge/plantuml/servlet/ImgServlet.java
Normal file
27
src/net/sourceforge/plantuml/servlet/ImgServlet.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package net.sourceforge.plantuml.servlet;
|
||||||
|
|
||||||
|
import net.sourceforge.plantuml.FileFormat;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* SVG servlet of the webapp.
|
||||||
|
* TODO.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
|
public class ImgServlet extends UmlDiagramService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSource( String uri) {
|
||||||
|
String[] result = uri.split("/img/", 2);
|
||||||
|
if (result.length != 2) {
|
||||||
|
return "";
|
||||||
|
} else {
|
||||||
|
return result[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
FileFormat getOutputFormat() {
|
||||||
|
return FileFormat.PNG;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -13,6 +13,8 @@ import javax.servlet.http.HttpServlet;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import net.sourceforge.plantuml.FileFormat;
|
||||||
|
import net.sourceforge.plantuml.FileFormatOption;
|
||||||
import net.sourceforge.plantuml.SourceStringReader;
|
import net.sourceforge.plantuml.SourceStringReader;
|
||||||
import net.sourceforge.plantuml.StringUtils;
|
import net.sourceforge.plantuml.StringUtils;
|
||||||
import net.sourceforge.plantuml.code.Transcoder;
|
import net.sourceforge.plantuml.code.Transcoder;
|
||||||
@ -166,7 +168,7 @@ public class PlantUmlServlet extends HttpServlet {
|
|||||||
}
|
}
|
||||||
response.setContentType("image/png");
|
response.setContentType("image/png");
|
||||||
SourceStringReader reader = new SourceStringReader(uml);
|
SourceStringReader reader = new SourceStringReader(uml);
|
||||||
reader.generateImage(response.getOutputStream());
|
reader.generateImage(response.getOutputStream(), new FileFormatOption(FileFormat.PNG));
|
||||||
response.flushBuffer();
|
response.flushBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
27
src/net/sourceforge/plantuml/servlet/SvgServlet.java
Normal file
27
src/net/sourceforge/plantuml/servlet/SvgServlet.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package net.sourceforge.plantuml.servlet;
|
||||||
|
|
||||||
|
import net.sourceforge.plantuml.FileFormat;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* SVG servlet of the webapp.
|
||||||
|
* TODO.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
|
public class SvgServlet extends UmlDiagramService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSource( String uri) {
|
||||||
|
String[] result = uri.split("/svg/", 2);
|
||||||
|
if (result.length != 2) {
|
||||||
|
return "";
|
||||||
|
} else {
|
||||||
|
return result[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
FileFormat getOutputFormat() {
|
||||||
|
return FileFormat.SVG;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
46
src/net/sourceforge/plantuml/servlet/UmlDiagramService.java
Normal file
46
src/net/sourceforge/plantuml/servlet/UmlDiagramService.java
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package net.sourceforge.plantuml.servlet;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import net.sourceforge.plantuml.FileFormat;
|
||||||
|
import net.sourceforge.plantuml.code.Transcoder;
|
||||||
|
import net.sourceforge.plantuml.code.TranscoderUtil;
|
||||||
|
|
||||||
|
@SuppressWarnings("serial")
|
||||||
|
public abstract class UmlDiagramService extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
|
||||||
|
// build the uml source from the compressed request parameter
|
||||||
|
String text = URLDecoder.decode( getSource( request.getRequestURI()), "UTF-8");
|
||||||
|
Transcoder transcoder = getTranscoder();
|
||||||
|
text = transcoder.decode(text);
|
||||||
|
StringBuilder plantUmlSource = new StringBuilder();
|
||||||
|
plantUmlSource.append("@startuml\n");
|
||||||
|
plantUmlSource.append( text);
|
||||||
|
if (text.endsWith("\n") == false) {
|
||||||
|
plantUmlSource.append("\n");
|
||||||
|
}
|
||||||
|
plantUmlSource.append("@enduml");
|
||||||
|
final String uml = plantUmlSource.toString();
|
||||||
|
|
||||||
|
// generate the response
|
||||||
|
DiagramResponse dr = new DiagramResponse( response, getOutputFormat());
|
||||||
|
dr.sendDiagram( uml);
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract public String getSource( String uri);
|
||||||
|
abstract FileFormat getOutputFormat();
|
||||||
|
|
||||||
|
private Transcoder getTranscoder() {
|
||||||
|
return TranscoderUtil.getDefaultTranscoder();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user