1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-06-01 16:10:47 +00:00

Add Date header

This commit is contained in:
Arnaud Roques 2011-01-30 15:23:08 +01:00
parent 6a93bdad66
commit e55fea2359

View File

@ -32,171 +32,184 @@ import HTTPClient.ParseException;
*/ */
public class PlantUmlServlet extends HttpServlet { public class PlantUmlServlet extends HttpServlet {
private static final Pattern startumlPattern = Pattern private static final Pattern startumlPattern = Pattern
.compile("/\\w+/uml/startuml/(.*)"); .compile("/\\w+/uml/startuml/(.*)");
private static final Pattern imagePattern = Pattern private static final Pattern imagePattern = Pattern
.compile("/\\w+/uml/image/(.*)"); .compile("/\\w+/uml/image/(.*)");
private static final Pattern proxyPattern = Pattern private static final Pattern proxyPattern = Pattern
.compile("/\\w+/uml/proxy/((\\d+)/)?(http://.*)"); .compile("/\\w+/uml/proxy/((\\d+)/)?(http://.*)");
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException { throws IOException, ServletException {
final String uri = request.getRequestURI(); final String uri = request.getRequestURI();
Matcher startumlMatcher = startumlPattern.matcher(uri); Matcher startumlMatcher = startumlPattern.matcher(uri);
Matcher imageMatcher = imagePattern.matcher(uri); Matcher imageMatcher = imagePattern.matcher(uri);
Matcher proxyMatcher = proxyPattern.matcher(uri); Matcher proxyMatcher = proxyPattern.matcher(uri);
if (startumlMatcher.matches()) { if (startumlMatcher.matches()) {
String source = startumlMatcher.group(1); String source = startumlMatcher.group(1);
handleImage(response, source); handleImage(response, source);
} else if (imageMatcher.matches()) { } else if (imageMatcher.matches()) {
String source = imageMatcher.group(1); String source = imageMatcher.group(1);
handleImageDecompress(response, source); handleImageDecompress(response, source);
} else if (proxyMatcher.matches()) { } else if (proxyMatcher.matches()) {
String num = proxyMatcher.group(2); String num = proxyMatcher.group(2);
String source = proxyMatcher.group(3); String source = proxyMatcher.group(3);
handleImageProxy(response, num, source); handleImageProxy(response, num, source);
} else { } else {
doPost(request, response); doPost(request, response);
} }
} }
@Override @Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) protected void doPost(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException { throws ServletException, IOException {
PrintWriter writer = resp.getWriter(); PrintWriter writer = resp.getWriter();
writer.print("<html>"); writer.print("<html>");
writer.print("<head>"); writer.print("<head>");
writer.print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"); writer
writer.print("<meta http-equiv=\"expires\" content=\"0\">"); .print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
writer.print("<meta http-equiv=\"pragma\" content=\"no-cache\">"); writer.print("<meta http-equiv=\"expires\" content=\"0\">");
writer.print("<meta http-equiv=\"cache-control\" content=\"no-cache, must-revalidate\">"); writer.print("<meta http-equiv=\"pragma\" content=\"no-cache\">");
writer.print("<link rel=\"SHORTCUT ICON\" href=\"/plantuml/favicon.ico\">"); writer
writer.print("</head>"); .print("<meta http-equiv=\"cache-control\" content=\"no-cache, must-revalidate\">");
writer.print("<body>"); writer
writer.print("<h1>PlantUMLServer</h1><p>This application provides a servlet which serves images createdby <a href=\"http://plantuml.sourceforge.net\">PlantUML</a>.</p>"); .print("<link rel=\"SHORTCUT ICON\" href=\"/plantuml/favicon.ico\">");
writer.print("</head>");
writer.print("<body>");
writer
.print("<h1>PlantUMLServer</h1><p>This application provides a servlet which serves images createdby <a href=\"http://plantuml.sourceforge.net\">PlantUML</a>.</p>");
String text = request.getParameter("text"); String text = request.getParameter("text");
String url = request.getParameter("url"); String url = request.getParameter("url");
String encode = ""; String encode = "";
Transcoder transcoder = getTranscoder(); Transcoder transcoder = getTranscoder();
if (url != null) { if (url != null) {
Pattern p = Pattern.compile(".*/(.*)"); Pattern p = Pattern.compile(".*/(.*)");
Matcher m = p.matcher(url); Matcher m = p.matcher(url);
if (m.find()) { if (m.find()) {
url = m.group(1); url = m.group(1);
} }
text = transcoder.decode(url); text = transcoder.decode(url);
} }
writer.print("<form method=post action=\"/plantuml/uml/post\"><textarea name=\"text\" cols=\"120\" rows=\"10\">"); writer
if (text != null) { .print("<form method=post action=\"/plantuml/uml/post\"><textarea name=\"text\" cols=\"120\" rows=\"10\">");
encode = transcoder.encode(text); if (text != null) {
writer.print(text); encode = transcoder.encode(text);
} writer.print(text);
writer.print("</textarea><br><input type=\"submit\"></form>"); }
writer.print("<hr>"); writer.print("</textarea><br><input type=\"submit\"></form>");
writer.print("You can enter here a previously generated URL:<p>"); writer.print("<hr>");
writer.print("You can enter here a previously generated URL:<p>");
String host = "http://" + request.getServerName()+ ":" + request.getServerPort(); String host = "http://" + request.getServerName() + ":"
String total = host + "/plantuml/uml/image/" + encode; + request.getServerPort();
String total = host + "/plantuml/uml/image/" + encode;
writer.print("<form method=\"post\" action=\"/plantuml/uml/post\"><input name=\"url\" type=\"text\" size=\"150\" value=\"" writer
+ total + "\">"); .print("<form method=\"post\" action=\"/plantuml/uml/post\"><input name=\"url\" type=\"text\" size=\"150\" value=\""
writer.print("<br><input type=\"submit\"></form>"); + total + "\">");
writer.print("<br><input type=\"submit\"></form>");
if (text != null) { if (text != null) {
writer.print("<hr>"); writer.print("<hr>");
writer.print("You can use the following URL:<p>"); writer.print("You can use the following URL:<p>");
String urlPart = "\"" + total + "\""; String urlPart = "\"" + total + "\"";
writer.print("<a href=" + urlPart + " >"); writer.print("<a href=" + urlPart + " >");
writer.print("<code>"); writer.print("<code>");
writer.print("&lt;img src=" + urlPart + " &gt;"); writer.print("&lt;img src=" + urlPart + " &gt;");
writer.print("</code></a><p>"); writer.print("</code></a><p>");
writer.print("<a href=" + urlPart + " >"); writer.print("<a href=" + urlPart + " >");
writer.print("<img src=\"/plantuml/uml/image/" + encode + "\" >"); writer.print("<img src=\"/plantuml/uml/image/" + encode + "\" >");
writer.print("</a>"); writer.print("</a>");
} }
writer.print("</body></html>"); writer.print("</body></html>");
writer.flush(); writer.flush();
} }
private Transcoder getTranscoder() { private Transcoder getTranscoder() {
return TranscoderUtil.getDefaultTranscoder(); return TranscoderUtil.getDefaultTranscoder();
} }
private void handleImage(HttpServletResponse response, String source)
throws IOException {
source = URLDecoder.decode(source, "UTF-8");
StringBuilder plantUmlSource = new StringBuilder();
StringTokenizer tokenizer = new StringTokenizer(source, "/@"); private void handleImage(HttpServletResponse response, String source)
while (tokenizer.hasMoreTokens()) { throws IOException {
String token = tokenizer.nextToken(); source = URLDecoder.decode(source, "UTF-8");
plantUmlSource.append(token).append("\n"); StringBuilder plantUmlSource = new StringBuilder();
}
sendImage(response, plantUmlSource.toString());
} StringTokenizer tokenizer = new StringTokenizer(source, "/@");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
plantUmlSource.append(token).append("\n");
}
sendImage(response, plantUmlSource.toString());
private void handleImageDecompress(HttpServletResponse response, }
String source) throws IOException {
source = URLDecoder.decode(source, "UTF-8");
Transcoder transcoder = getTranscoder();
String text2 = transcoder.decode(source);
sendImage(response, text2);
}
private void handleImageProxy(HttpServletResponse response, String num, private void handleImageDecompress(HttpServletResponse response,
String source) throws IOException { String source) throws IOException {
String s = getContent(source); source = URLDecoder.decode(source, "UTF-8");
SourceStringReader reader = new SourceStringReader(s); Transcoder transcoder = getTranscoder();
int n = num == null ? 0 : Integer.parseInt(num); String text2 = transcoder.decode(source);
// Write the first image to "os" sendImage(response, text2);
reader.generateImage(response.getOutputStream(), n); }
}
private void sendImage(HttpServletResponse response, String text) private void handleImageProxy(HttpServletResponse response, String num,
throws IOException { String source) throws IOException {
StringBuilder plantUmlSource = new StringBuilder(); String s = getContent(source);
plantUmlSource.append("@startuml\n"); SourceStringReader reader = new SourceStringReader(s);
plantUmlSource.append(text); int n = num == null ? 0 : Integer.parseInt(num);
plantUmlSource.append("\n@enduml"); // Write the first image to "os"
reader.generateImage(response.getOutputStream(), n);
}
SourceStringReader reader = new SourceStringReader(plantUmlSource private void sendImage(HttpServletResponse response, String text)
.toString()); throws IOException {
// Write the first image to "os" StringBuilder plantUmlSource = new StringBuilder();
response.setContentType("image/png"); plantUmlSource.append("@startuml\n");
reader.generateImage(response.getOutputStream()); plantUmlSource.append(text);
response.flushBuffer(); plantUmlSource.append("\n@enduml");
}
public String getContent(String adress) throws IOException { SourceStringReader reader = new SourceStringReader(plantUmlSource
// HTTPConnection.setProxyServer("proxy", 8080); .toString());
CookieModule.setCookiePolicyHandler(null); // Write the first image to "os"
long today = System.currentTimeMillis();
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("image/png");
reader.generateImage(response.getOutputStream());
final Pattern p = Pattern.compile("http://[^/]+(/?.*)"); response.flushBuffer();
final Matcher m = p.matcher(adress); }
if (m.find() == false) {
throw new IOException(adress); public String getContent(String adress) throws IOException {
} // HTTPConnection.setProxyServer("proxy", 8080);
final URL url = new URL(adress); CookieModule.setCookiePolicyHandler(null);
final HTTPConnection httpConnection = new HTTPConnection(url);
try { final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
final HTTPResponse resp = httpConnection.Get(m.group(1)); final Matcher m = p.matcher(adress);
return resp.getText(); if (m.find() == false) {
} catch (ModuleException e) { throw new IOException(adress);
throw new IOException(e.toString()); }
} catch (ParseException e) { final URL url = new URL(adress);
throw new IOException(e.toString()); final HTTPConnection httpConnection = new HTTPConnection(url);
} try {
} final HTTPResponse resp = httpConnection.Get(m.group(1));
return resp.getText();
} catch (ModuleException e) {
throw new IOException(e.toString());
} catch (ParseException e) {
throw new IOException(e.toString());
}
}
} }