1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-06-01 16:10:47 +00:00
plantuml-server/src/main/java/net/sourceforge/plantuml/servlet/PlantUmlServlet.java

241 lines
8.7 KiB
Java
Raw Normal View History

2011-03-30 15:53:11 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* Project Info: http://plantuml.sourceforge.net
*
2011-03-30 15:53:11 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
2011-01-30 14:00:47 +00:00
package net.sourceforge.plantuml.servlet;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.RequestDispatcher;
2011-01-30 14:00:47 +00:00
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.FileFormatOption;
2011-01-30 14:00:47 +00:00
import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.StringUtils;
2011-01-30 14:00:47 +00:00
import net.sourceforge.plantuml.code.Transcoder;
import net.sourceforge.plantuml.code.TranscoderUtil;
import HTTPClient.CookieModule;
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.ModuleException;
import HTTPClient.ParseException;
/*
2011-01-30 14:00:47 +00:00
* Original idea from Achim Abeling for Confluence macro
* See http://www.banapple.de/display/BANAPPLE/plantuml+user+macro
*
* This class is the old all-in-one historic implementation of the PlantUml server.
* See package.html for the new design. It's a work in progress.
*
2011-01-30 14:00:47 +00:00
* Modified by Arnaud Roques
* Modified by Pablo Lalloni
2011-01-30 14:00:47 +00:00
* Packaged by Maxime Sinclair
*
2011-01-30 14:00:47 +00:00
*/
2011-03-30 15:53:11 +00:00
@SuppressWarnings("serial")
2011-01-30 14:00:47 +00:00
public class PlantUmlServlet extends HttpServlet {
private static final Pattern startumlPattern = Pattern.compile("/\\w+/start/(.*)");
private static final Pattern proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
private static final Pattern oldStartumlPattern = Pattern.compile("/\\w+/uml/startuml/(.*)");
private static final Pattern oldImagePattern = Pattern.compile("/\\w+/uml/image/(.*)");
private static final Pattern oldProxyPattern = Pattern.compile("/\\w+/uml/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
2011-01-30 14:23:08 +00:00
@Override
2011-01-30 14:23:08 +00:00
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
final String uri = request.getRequestURI();
Matcher startumlMatcher = startumlPattern.matcher(uri);
Matcher proxyMatcher = proxyPattern.matcher(uri);
Matcher oldStartumlMatcher = oldStartumlPattern.matcher(uri);
Matcher oldImageMatcher = oldImagePattern.matcher(uri);
Matcher oldProxyMatcher = oldProxyPattern.matcher(uri);
2011-01-30 14:23:08 +00:00
if (startumlMatcher.matches()) {
String source = startumlMatcher.group(1);
2011-02-20 16:05:48 +00:00
handleImage(response, source, uri);
2011-01-30 14:23:08 +00:00
} else if (proxyMatcher.matches()) {
String num = proxyMatcher.group(2);
String format = proxyMatcher.group(4);
String source = proxyMatcher.group(5);
handleImageProxy(response, num, source, format, uri);
} else if (oldStartumlMatcher.matches()) {
String source = oldStartumlMatcher.group(1);
2011-02-20 16:05:48 +00:00
handleImage(response, source, uri);
} else if (oldImageMatcher.matches()) {
String source = oldImageMatcher.group(1);
2011-02-20 16:05:48 +00:00
handleImageDecompress(response, source, uri);
} else if (oldProxyMatcher.matches()) {
String num = oldProxyMatcher.group(2);
String format = oldProxyMatcher.group(4);
String source = oldProxyMatcher.group(5);
handleImageProxy(response, num, source, format, uri);
2011-01-30 14:23:08 +00:00
} else {
doPost(request, response);
}
}
2011-02-20 16:05:48 +00:00
2011-01-30 14:23:08 +00:00
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
2011-01-30 14:23:08 +00:00
throws ServletException, IOException {
2011-02-20 16:05:48 +00:00
request.setCharacterEncoding("UTF-8");
2011-01-30 14:23:08 +00:00
String text = request.getParameter("text");
String url = request.getParameter("url");
String encoded = "";
2011-01-30 14:23:08 +00:00
Transcoder transcoder = getTranscoder();
// the URL form has been submitted
if (url != null && !url.trim().isEmpty()) {
// TODO Verify the url is correct
2011-01-30 14:23:08 +00:00
Pattern p = Pattern.compile(".*/(.*)");
Matcher m = p.matcher(url);
if (m.find()) {
url = m.group(1);
text = transcoder.decode(url);
2011-01-30 14:23:08 +00:00
}
}
// the Text form has been submitted
if (text != null && !text.trim().isEmpty()) {
encoded = transcoder.encode(text);
2011-01-30 14:23:08 +00:00
}
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", text);
request.setAttribute("net.sourceforge.plantuml.servlet.encoded", encoded);
// forward to index.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
2013-05-27 21:45:38 +00:00
return;
2011-01-30 14:23:08 +00:00
}
2011-01-30 14:00:47 +00:00
private Transcoder getTranscoder() {
return TranscoderUtil.getDefaultTranscoder();
}
2011-01-30 14:23:08 +00:00
2011-02-20 16:05:48 +00:00
private void handleImage(HttpServletResponse response, String source, String uri)
2011-01-30 14:23:08 +00:00
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");
}
2011-02-20 16:05:48 +00:00
sendImage(response, plantUmlSource.toString(), uri);
2011-01-30 14:23:08 +00:00
}
private void handleImageDecompress(HttpServletResponse response,
2011-02-20 16:05:48 +00:00
String source, String uri) throws IOException {
2011-01-30 14:23:08 +00:00
source = URLDecoder.decode(source, "UTF-8");
Transcoder transcoder = getTranscoder();
String text2 = transcoder.decode(source);
2011-02-20 16:05:48 +00:00
sendImage(response, text2, uri);
2011-01-30 14:23:08 +00:00
}
private void handleImageProxy(HttpServletResponse response, String num,
String source, String format, String uri) throws IOException {
SourceStringReader reader = new SourceStringReader( getContent(source));
2011-01-30 14:23:08 +00:00
int n = num == null ? 0 : Integer.parseInt(num);
reader.generateImage(response.getOutputStream(), n, getFormat(format));
}
private FileFormatOption getFormat(String f) {
if (f==null) {
return new FileFormatOption(FileFormat.PNG);
}
if (f.equals("svg")) {
return new FileFormatOption(FileFormat.SVG);
}
if (f.equals("txt")) {
return new FileFormatOption(FileFormat.ATXT);
}
return new FileFormatOption(FileFormat.PNG);
2011-01-30 14:23:08 +00:00
}
2011-02-20 16:05:48 +00:00
private void sendImage(HttpServletResponse response, String text, String uri)
2011-01-30 14:23:08 +00:00
throws IOException {
final String uml;
if (text.startsWith("@startuml")) {
uml = text;
} else {
StringBuilder plantUmlSource = new StringBuilder();
plantUmlSource.append("@startuml\n");
plantUmlSource.append(text);
if (text.endsWith("\n") == false) {
plantUmlSource.append("\n");
}
plantUmlSource.append("@enduml");
uml = plantUmlSource.toString();
}
2011-01-30 14:23:08 +00:00
// Write the first image to "os"
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");
}
2011-01-30 14:23:08 +00:00
response.setContentType("image/png");
2011-02-20 16:05:48 +00:00
SourceStringReader reader = new SourceStringReader(uml);
reader.generateImage(response.getOutputStream(), new FileFormatOption(FileFormat.PNG));
2011-01-30 14:23:08 +00:00
response.flushBuffer();
}
2011-03-10 14:57:37 +00:00
private String getContent(String adress) throws IOException {
2011-01-30 14:23:08 +00:00
// 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());
}
}
2011-01-30 14:00:47 +00:00
2011-02-17 09:30:21 +00:00
}