Code formatted, encoded text checked and warning added
The old url syntax is announced as deprecated.
This commit is contained in:
@@ -57,184 +57,190 @@ import HTTPClient.ParseException;
|
|||||||
*
|
*
|
||||||
* Modified by Arnaud Roques
|
* Modified by Arnaud Roques
|
||||||
* Modified by Pablo Lalloni
|
* Modified by Pablo Lalloni
|
||||||
* Packaged by Maxime Sinclair
|
* Modified by Maxime Sinclair
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class PlantUmlServlet extends HttpServlet {
|
public class PlantUmlServlet extends HttpServlet {
|
||||||
|
|
||||||
private static final Pattern startumlPattern = Pattern.compile("/\\w+/start/(.*)");
|
private static final Pattern urlPattern = Pattern.compile(".*/(.*)"); // Last part of the URL
|
||||||
|
private static final Pattern encodedPattern = Pattern.compile("^[a-zA-Z0-9\\-\\_]+$"); // Format of a compressed diagram
|
||||||
|
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 proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
|
||||||
private static final Pattern oldStartumlPattern = Pattern.compile("/\\w+/uml/startuml/(.*)");
|
private static final Pattern oldStartumlPattern = Pattern.compile("/\\w+/uml/startuml/(.*)");
|
||||||
private static final Pattern oldImagePattern = Pattern.compile("/\\w+/uml/image/(.*)");
|
private static final Pattern oldImagePattern = Pattern.compile("/\\w+/uml/image/(.*)");
|
||||||
private static final Pattern oldProxyPattern = Pattern.compile("/\\w+/uml/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
|
private static final Pattern oldProxyPattern = Pattern.compile("/\\w+/uml/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
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 proxyMatcher = proxyPattern.matcher(uri);
|
Matcher proxyMatcher = proxyPattern.matcher(uri);
|
||||||
Matcher oldStartumlMatcher = oldStartumlPattern.matcher(uri);
|
Matcher oldStartumlMatcher = oldStartumlPattern.matcher(uri);
|
||||||
Matcher oldImageMatcher = oldImagePattern.matcher(uri);
|
Matcher oldImageMatcher = oldImagePattern.matcher(uri);
|
||||||
Matcher oldProxyMatcher = oldProxyPattern.matcher(uri);
|
Matcher oldProxyMatcher = oldProxyPattern.matcher(uri);
|
||||||
if (startumlMatcher.matches()) {
|
if (startumlMatcher.matches()) {
|
||||||
String source = startumlMatcher.group(1);
|
String source = startumlMatcher.group(1);
|
||||||
handleImage(response, source, uri);
|
handleImage(response, source, uri);
|
||||||
} else if (proxyMatcher.matches()) {
|
} else if (proxyMatcher.matches()) {
|
||||||
String num = proxyMatcher.group(2);
|
String num = proxyMatcher.group(2);
|
||||||
String format = proxyMatcher.group(4);
|
String format = proxyMatcher.group(4);
|
||||||
String source = proxyMatcher.group(5);
|
String source = proxyMatcher.group(5);
|
||||||
handleImageProxy(response, num, source, format, uri);
|
handleImageProxy(response, num, source, format, uri);
|
||||||
} else if (oldStartumlMatcher.matches()) {
|
} else if (oldStartumlMatcher.matches()) {
|
||||||
String source = oldStartumlMatcher.group(1);
|
System.out.println("PlantUML WARNING This URL syntax is deprecated, please delete the '/uml/' part.");
|
||||||
handleImage(response, source, uri);
|
String source = oldStartumlMatcher.group(1);
|
||||||
} else if (oldImageMatcher.matches()) {
|
handleImage(response, source, uri);
|
||||||
String source = oldImageMatcher.group(1);
|
} else if (oldImageMatcher.matches()) {
|
||||||
handleImageDecompress(response, source, uri);
|
System.out.println("PlantUML WARNING This URL syntax is deprecated, please delete the '/uml/' part.");
|
||||||
} else if (oldProxyMatcher.matches()) {
|
String source = oldImageMatcher.group(1);
|
||||||
String num = oldProxyMatcher.group(2);
|
handleImageDecompress(response, source, uri);
|
||||||
|
} else if (oldProxyMatcher.matches()) {
|
||||||
|
System.out.println("PlantUML WARNING This URL syntax is deprecated, please delete the '/uml/' part.");
|
||||||
|
String num = oldProxyMatcher.group(2);
|
||||||
String format = oldProxyMatcher.group(4);
|
String format = oldProxyMatcher.group(4);
|
||||||
String source = oldProxyMatcher.group(5);
|
String source = oldProxyMatcher.group(5);
|
||||||
handleImageProxy(response, num, source, format, uri);
|
handleImageProxy(response, num, source, format, uri);
|
||||||
} else {
|
} else {
|
||||||
doPost(request, response);
|
doPost(request, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
|
||||||
|
IOException {
|
||||||
|
|
||||||
@Override
|
request.setCharacterEncoding("UTF-8");
|
||||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
String text = request.getParameter("text");
|
||||||
throws ServletException, IOException {
|
String url = request.getParameter("url");
|
||||||
|
String encoded = "";
|
||||||
|
|
||||||
request.setCharacterEncoding("UTF-8");
|
Transcoder transcoder = getTranscoder();
|
||||||
String text = request.getParameter("text");
|
// the URL form has been submitted
|
||||||
String url = request.getParameter("url");
|
if (url != null && !url.trim().isEmpty()) {
|
||||||
String encoded = "";
|
// Catch the last part of the URL
|
||||||
|
Matcher m1 = urlPattern.matcher(url);
|
||||||
|
if (m1.find()) {
|
||||||
|
// Check it's a valid compressed text
|
||||||
|
url = m1.group(1);
|
||||||
|
Matcher m2 = encodedPattern.matcher(url);
|
||||||
|
if (m2.find()) {
|
||||||
|
url = m2.group(0);
|
||||||
|
text = transcoder.decode(url);
|
||||||
|
} else {
|
||||||
|
System.out.println("PlantUML ERROR Not a valid compressed string : " + url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// the Text form has been submitted
|
||||||
|
if (text != null && !text.trim().isEmpty()) {
|
||||||
|
encoded = transcoder.encode(text);
|
||||||
|
}
|
||||||
|
|
||||||
Transcoder transcoder = getTranscoder();
|
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", text);
|
||||||
// the URL form has been submitted
|
request.setAttribute("net.sourceforge.plantuml.servlet.encoded", encoded);
|
||||||
if (url != null && !url.trim().isEmpty()) {
|
|
||||||
// TODO Verify the url is correct
|
|
||||||
Pattern p = Pattern.compile(".*/(.*)");
|
|
||||||
Matcher m = p.matcher(url);
|
|
||||||
if (m.find()) {
|
|
||||||
url = m.group(1);
|
|
||||||
text = transcoder.decode(url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// the Text form has been submitted
|
|
||||||
if (text != null && !text.trim().isEmpty()) {
|
|
||||||
encoded = transcoder.encode(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", text);
|
// forward to index.jsp
|
||||||
request.setAttribute("net.sourceforge.plantuml.servlet.encoded", encoded);
|
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
|
||||||
|
dispatcher.forward(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// forward to index.jsp
|
private Transcoder getTranscoder() {
|
||||||
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
|
return TranscoderUtil.getDefaultTranscoder();
|
||||||
dispatcher.forward(request, response);
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Transcoder getTranscoder() {
|
private void handleImage(HttpServletResponse response, String source, String uri) throws IOException {
|
||||||
return TranscoderUtil.getDefaultTranscoder();
|
source = URLDecoder.decode(source, "UTF-8");
|
||||||
}
|
StringBuilder plantUmlSource = new StringBuilder();
|
||||||
|
|
||||||
private void handleImage(HttpServletResponse response, String source, String uri)
|
StringTokenizer tokenizer = new StringTokenizer(source, "/@");
|
||||||
throws IOException {
|
while (tokenizer.hasMoreTokens()) {
|
||||||
source = URLDecoder.decode(source, "UTF-8");
|
String token = tokenizer.nextToken();
|
||||||
StringBuilder plantUmlSource = new StringBuilder();
|
plantUmlSource.append(token).append("\n");
|
||||||
|
}
|
||||||
|
sendImage(response, plantUmlSource.toString(), uri);
|
||||||
|
|
||||||
StringTokenizer tokenizer = new StringTokenizer(source, "/@");
|
}
|
||||||
while (tokenizer.hasMoreTokens()) {
|
|
||||||
String token = tokenizer.nextToken();
|
|
||||||
plantUmlSource.append(token).append("\n");
|
|
||||||
}
|
|
||||||
sendImage(response, plantUmlSource.toString(), uri);
|
|
||||||
|
|
||||||
}
|
private void handleImageDecompress(HttpServletResponse response, String source, String uri) throws IOException {
|
||||||
|
source = URLDecoder.decode(source, "UTF-8");
|
||||||
|
Transcoder transcoder = getTranscoder();
|
||||||
|
String text2 = transcoder.decode(source);
|
||||||
|
sendImage(response, text2, uri);
|
||||||
|
}
|
||||||
|
|
||||||
private void handleImageDecompress(HttpServletResponse response,
|
private void handleImageProxy(HttpServletResponse response, String num, String source, String format, String uri)
|
||||||
String source, String uri) throws IOException {
|
throws IOException {
|
||||||
source = URLDecoder.decode(source, "UTF-8");
|
SourceStringReader reader = new SourceStringReader(getContent(source));
|
||||||
Transcoder transcoder = getTranscoder();
|
int n = num == null ? 0 : Integer.parseInt(num);
|
||||||
String text2 = transcoder.decode(source);
|
|
||||||
sendImage(response, text2, uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleImageProxy(HttpServletResponse response, String num,
|
|
||||||
String source, String format, String uri) throws IOException {
|
|
||||||
SourceStringReader reader = new SourceStringReader( getContent(source));
|
|
||||||
int n = num == null ? 0 : Integer.parseInt(num);
|
|
||||||
|
|
||||||
reader.generateImage(response.getOutputStream(), n, getFormat(format));
|
reader.generateImage(response.getOutputStream(), n, getFormat(format));
|
||||||
}
|
}
|
||||||
|
|
||||||
private FileFormatOption getFormat(String f) {
|
private FileFormatOption getFormat(String f) {
|
||||||
if (f==null) {
|
if (f == null) {
|
||||||
return new FileFormatOption(FileFormat.PNG);
|
return new FileFormatOption(FileFormat.PNG);
|
||||||
}
|
}
|
||||||
if (f.equals("svg")) {
|
if (f.equals("svg")) {
|
||||||
return new FileFormatOption(FileFormat.SVG);
|
return new FileFormatOption(FileFormat.SVG);
|
||||||
}
|
}
|
||||||
if (f.equals("txt")) {
|
if (f.equals("txt")) {
|
||||||
return new FileFormatOption(FileFormat.ATXT);
|
return new FileFormatOption(FileFormat.ATXT);
|
||||||
}
|
}
|
||||||
return new FileFormatOption(FileFormat.PNG);
|
return new FileFormatOption(FileFormat.PNG);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendImage(HttpServletResponse response, String text, String uri)
|
private void sendImage(HttpServletResponse response, String text, String uri) throws IOException {
|
||||||
throws IOException {
|
final String uml;
|
||||||
final String uml;
|
if (text.startsWith("@startuml")) {
|
||||||
if (text.startsWith("@startuml")) {
|
uml = text;
|
||||||
uml = text;
|
} else {
|
||||||
} else {
|
StringBuilder plantUmlSource = new StringBuilder();
|
||||||
StringBuilder plantUmlSource = new StringBuilder();
|
plantUmlSource.append("@startuml\n");
|
||||||
plantUmlSource.append("@startuml\n");
|
plantUmlSource.append(text);
|
||||||
plantUmlSource.append(text);
|
if (text.endsWith("\n") == false) {
|
||||||
if (text.endsWith("\n") == false) {
|
plantUmlSource.append("\n");
|
||||||
plantUmlSource.append("\n");
|
}
|
||||||
}
|
plantUmlSource.append("@enduml");
|
||||||
plantUmlSource.append("@enduml");
|
uml = plantUmlSource.toString();
|
||||||
uml = plantUmlSource.toString();
|
}
|
||||||
}
|
// Write the first image to "os"
|
||||||
// Write the first image to "os"
|
long today = System.currentTimeMillis();
|
||||||
long today = System.currentTimeMillis();
|
if (StringUtils.isDiagramCacheable(uml)) {
|
||||||
if ( StringUtils.isDiagramCacheable( uml)) {
|
// Add http headers to force the browser to cache the image
|
||||||
// Add http headers to force the browser to cache the image
|
response.addDateHeader("Expires", today + 31536000000L);
|
||||||
response.addDateHeader("Expires", today + 31536000000L);
|
// today + 1 year
|
||||||
// today + 1 year
|
response.addDateHeader("Last-Modified", 1261440000000L);
|
||||||
response.addDateHeader("Last-Modified", 1261440000000L);
|
// 2009 dec 22 constant date in the past
|
||||||
// 2009 dec 22 constant date in the past
|
response.addHeader("Cache-Control", "public");
|
||||||
response.addHeader("Cache-Control", "public");
|
}
|
||||||
}
|
response.setContentType("image/png");
|
||||||
response.setContentType("image/png");
|
SourceStringReader reader = new SourceStringReader(uml);
|
||||||
SourceStringReader reader = new SourceStringReader(uml);
|
reader.generateImage(response.getOutputStream(), new FileFormatOption(FileFormat.PNG));
|
||||||
reader.generateImage(response.getOutputStream(), new FileFormatOption(FileFormat.PNG));
|
response.flushBuffer();
|
||||||
response.flushBuffer();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private String getContent(String adress) throws IOException {
|
private String getContent(String adress) throws IOException {
|
||||||
// HTTPConnection.setProxyServer("proxy", 8080);
|
// HTTPConnection.setProxyServer("proxy", 8080);
|
||||||
CookieModule.setCookiePolicyHandler(null);
|
CookieModule.setCookiePolicyHandler(null);
|
||||||
|
|
||||||
final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
|
final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
|
||||||
final Matcher m = p.matcher(adress);
|
final Matcher m = p.matcher(adress);
|
||||||
if (m.find() == false) {
|
if (m.find() == false) {
|
||||||
throw new IOException(adress);
|
throw new IOException(adress);
|
||||||
}
|
}
|
||||||
final URL url = new URL(adress);
|
final URL url = new URL(adress);
|
||||||
final HTTPConnection httpConnection = new HTTPConnection(url);
|
final HTTPConnection httpConnection = new HTTPConnection(url);
|
||||||
try {
|
try {
|
||||||
final HTTPResponse resp = httpConnection.Get(m.group(1));
|
final HTTPResponse resp = httpConnection.Get(m.group(1));
|
||||||
return resp.getText();
|
return resp.getText();
|
||||||
} catch (ModuleException e) {
|
} catch (ModuleException e) {
|
||||||
throw new IOException(e.toString());
|
throw new IOException(e.toString());
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
throw new IOException(e.toString());
|
throw new IOException(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user