1
0
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:
Maxime Sinclair 2011-03-29 18:15:53 +02:00
parent 225ef45c37
commit 01a16339be
6 changed files with 170 additions and 2 deletions

View File

@ -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>

View 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);
}
}

View 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;
}
}

View File

@ -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();
} }

View 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;
}
}

View 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();
}
}