1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-12-22 08:48:54 +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 {
private static final Pattern startumlPattern = Pattern
.compile("/\\w+/uml/startuml/(.*)");
private static final Pattern startumlPattern = Pattern
.compile("/\\w+/uml/startuml/(.*)");
private static final Pattern imagePattern = Pattern
.compile("/\\w+/uml/image/(.*)");
private static final Pattern imagePattern = Pattern
.compile("/\\w+/uml/image/(.*)");
private static final Pattern proxyPattern = Pattern
.compile("/\\w+/uml/proxy/((\\d+)/)?(http://.*)");
private static final Pattern proxyPattern = Pattern
.compile("/\\w+/uml/proxy/((\\d+)/)?(http://.*)");
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
final String uri = request.getRequestURI();
Matcher startumlMatcher = startumlPattern.matcher(uri);
Matcher imageMatcher = imagePattern.matcher(uri);
Matcher proxyMatcher = proxyPattern.matcher(uri);
final String uri = request.getRequestURI();
Matcher startumlMatcher = startumlPattern.matcher(uri);
Matcher imageMatcher = imagePattern.matcher(uri);
Matcher proxyMatcher = proxyPattern.matcher(uri);
if (startumlMatcher.matches()) {
String source = startumlMatcher.group(1);
handleImage(response, source);
} else if (imageMatcher.matches()) {
String source = imageMatcher.group(1);
handleImageDecompress(response, source);
} else if (proxyMatcher.matches()) {
String num = proxyMatcher.group(2);
String source = proxyMatcher.group(3);
handleImageProxy(response, num, source);
} else {
doPost(request, response);
}
}
if (startumlMatcher.matches()) {
String source = startumlMatcher.group(1);
handleImage(response, source);
} else if (imageMatcher.matches()) {
String source = imageMatcher.group(1);
handleImageDecompress(response, source);
} else if (proxyMatcher.matches()) {
String num = proxyMatcher.group(2);
String source = proxyMatcher.group(3);
handleImageProxy(response, num, source);
} else {
doPost(request, response);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.print("<html>");
writer.print("<head>");
writer.print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
writer.print("<meta http-equiv=\"expires\" content=\"0\">");
writer.print("<meta http-equiv=\"pragma\" content=\"no-cache\">");
writer.print("<meta http-equiv=\"cache-control\" content=\"no-cache, must-revalidate\">");
writer.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>");
PrintWriter writer = resp.getWriter();
writer.print("<html>");
writer.print("<head>");
writer
.print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
writer.print("<meta http-equiv=\"expires\" content=\"0\">");
writer.print("<meta http-equiv=\"pragma\" content=\"no-cache\">");
writer
.print("<meta http-equiv=\"cache-control\" content=\"no-cache, must-revalidate\">");
writer
.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 url = request.getParameter("url");
String encode = "";
String text = request.getParameter("text");
String url = request.getParameter("url");
String encode = "";
Transcoder transcoder = getTranscoder();
if (url != null) {
Pattern p = Pattern.compile(".*/(.*)");
Matcher m = p.matcher(url);
if (m.find()) {
url = m.group(1);
}
text = transcoder.decode(url);
}
writer.print("<form method=post action=\"/plantuml/uml/post\"><textarea name=\"text\" cols=\"120\" rows=\"10\">");
if (text != null) {
encode = transcoder.encode(text);
writer.print(text);
}
writer.print("</textarea><br><input type=\"submit\"></form>");
writer.print("<hr>");
writer.print("You can enter here a previously generated URL:<p>");
Transcoder transcoder = getTranscoder();
if (url != null) {
Pattern p = Pattern.compile(".*/(.*)");
Matcher m = p.matcher(url);
if (m.find()) {
url = m.group(1);
}
text = transcoder.decode(url);
}
writer
.print("<form method=post action=\"/plantuml/uml/post\"><textarea name=\"text\" cols=\"120\" rows=\"10\">");
if (text != null) {
encode = transcoder.encode(text);
writer.print(text);
}
writer.print("</textarea><br><input type=\"submit\"></form>");
writer.print("<hr>");
writer.print("You can enter here a previously generated URL:<p>");
String host = "http://" + request.getServerName()+ ":" + request.getServerPort();
String total = host + "/plantuml/uml/image/" + encode;
String host = "http://" + request.getServerName() + ":"
+ 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=\""
+ total + "\">");
writer.print("<br><input type=\"submit\"></form>");
writer
.print("<form method=\"post\" action=\"/plantuml/uml/post\"><input name=\"url\" type=\"text\" size=\"150\" value=\""
+ total + "\">");
writer.print("<br><input type=\"submit\"></form>");
if (text != null) {
writer.print("<hr>");
writer.print("You can use the following URL:<p>");
if (text != null) {
writer.print("<hr>");
writer.print("You can use the following URL:<p>");
String urlPart = "\"" + total + "\"";
writer.print("<a href=" + urlPart + " >");
writer.print("<code>");
writer.print("&lt;img src=" + urlPart + " &gt;");
writer.print("</code></a><p>");
String urlPart = "\"" + total + "\"";
writer.print("<a href=" + urlPart + " >");
writer.print("<code>");
writer.print("&lt;img src=" + urlPart + " &gt;");
writer.print("</code></a><p>");
writer.print("<a href=" + urlPart + " >");
writer.print("<img src=\"/plantuml/uml/image/" + encode + "\" >");
writer.print("</a>");
}
writer.print("</body></html>");
writer.flush();
}
writer.print("<a href=" + urlPart + " >");
writer.print("<img src=\"/plantuml/uml/image/" + encode + "\" >");
writer.print("</a>");
}
writer.print("</body></html>");
writer.flush();
}
private Transcoder getTranscoder() {
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, "/@");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
plantUmlSource.append(token).append("\n");
}
sendImage(response, plantUmlSource.toString());
private void handleImage(HttpServletResponse response, String source)
throws IOException {
source = URLDecoder.decode(source, "UTF-8");
StringBuilder plantUmlSource = new StringBuilder();
}
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,
String source) throws IOException {
String s = getContent(source);
SourceStringReader reader = new SourceStringReader(s);
int n = num == null ? 0 : Integer.parseInt(num);
// Write the first image to "os"
reader.generateImage(response.getOutputStream(), n);
}
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 sendImage(HttpServletResponse response, String text)
throws IOException {
StringBuilder plantUmlSource = new StringBuilder();
plantUmlSource.append("@startuml\n");
plantUmlSource.append(text);
plantUmlSource.append("\n@enduml");
private void handleImageProxy(HttpServletResponse response, String num,
String source) throws IOException {
String s = getContent(source);
SourceStringReader reader = new SourceStringReader(s);
int n = num == null ? 0 : Integer.parseInt(num);
// Write the first image to "os"
reader.generateImage(response.getOutputStream(), n);
}
SourceStringReader reader = new SourceStringReader(plantUmlSource
.toString());
// Write the first image to "os"
response.setContentType("image/png");
reader.generateImage(response.getOutputStream());
response.flushBuffer();
}
private void sendImage(HttpServletResponse response, String text)
throws IOException {
StringBuilder plantUmlSource = new StringBuilder();
plantUmlSource.append("@startuml\n");
plantUmlSource.append(text);
plantUmlSource.append("\n@enduml");
public String getContent(String adress) throws IOException {
// HTTPConnection.setProxyServer("proxy", 8080);
CookieModule.setCookiePolicyHandler(null);
SourceStringReader reader = new SourceStringReader(plantUmlSource
.toString());
// 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://[^/]+(/?.*)");
final Matcher m = p.matcher(adress);
if (m.find() == false) {
throw new IOException(adress);
}
final URL url = new URL(adress);
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());
}
}
response.flushBuffer();
}
public String getContent(String adress) throws IOException {
// HTTPConnection.setProxyServer("proxy", 8080);
CookieModule.setCookiePolicyHandler(null);
final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
final Matcher m = p.matcher(adress);
if (m.find() == false) {
throw new IOException(adress);
}
final URL url = new URL(adress);
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());
}
}
}