Code formatted, encoded text checked and warning added

The old url syntax is announced as deprecated.
This commit is contained in:
Maxime Sinclair
2013-07-09 16:53:45 +02:00
parent d7ecaaa992
commit 90fe8924f7

View File

@@ -57,12 +57,14 @@ 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 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 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/(.*)");
@@ -70,8 +72,7 @@ public class PlantUmlServlet extends HttpServlet {
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);
@@ -88,12 +89,15 @@ public class PlantUmlServlet extends HttpServlet {
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()) {
System.out.println("PlantUML WARNING This URL syntax is deprecated, please delete the '/uml/' part.");
String source = oldStartumlMatcher.group(1); String source = oldStartumlMatcher.group(1);
handleImage(response, source, uri); handleImage(response, source, uri);
} else if (oldImageMatcher.matches()) { } else if (oldImageMatcher.matches()) {
System.out.println("PlantUML WARNING This URL syntax is deprecated, please delete the '/uml/' part.");
String source = oldImageMatcher.group(1); String source = oldImageMatcher.group(1);
handleImageDecompress(response, source, uri); handleImageDecompress(response, source, uri);
} else if (oldProxyMatcher.matches()) { } 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 num = oldProxyMatcher.group(2);
String format = oldProxyMatcher.group(4); String format = oldProxyMatcher.group(4);
String source = oldProxyMatcher.group(5); String source = oldProxyMatcher.group(5);
@@ -103,10 +107,9 @@ public class PlantUmlServlet extends HttpServlet {
} }
} }
@Override @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
throws ServletException, IOException { IOException {
request.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8");
String text = request.getParameter("text"); String text = request.getParameter("text");
@@ -116,12 +119,18 @@ public class PlantUmlServlet extends HttpServlet {
Transcoder transcoder = getTranscoder(); Transcoder transcoder = getTranscoder();
// the URL form has been submitted // the URL form has been submitted
if (url != null && !url.trim().isEmpty()) { if (url != null && !url.trim().isEmpty()) {
// TODO Verify the url is correct // Catch the last part of the URL
Pattern p = Pattern.compile(".*/(.*)"); Matcher m1 = urlPattern.matcher(url);
Matcher m = p.matcher(url); if (m1.find()) {
if (m.find()) { // Check it's a valid compressed text
url = m.group(1); url = m1.group(1);
Matcher m2 = encodedPattern.matcher(url);
if (m2.find()) {
url = m2.group(0);
text = transcoder.decode(url); text = transcoder.decode(url);
} else {
System.out.println("PlantUML ERROR Not a valid compressed string : " + url);
}
} }
} }
// the Text form has been submitted // the Text form has been submitted
@@ -142,8 +151,7 @@ public class PlantUmlServlet extends HttpServlet {
return TranscoderUtil.getDefaultTranscoder(); return TranscoderUtil.getDefaultTranscoder();
} }
private void handleImage(HttpServletResponse response, String source, String uri) private void handleImage(HttpServletResponse response, String source, String uri) throws IOException {
throws IOException {
source = URLDecoder.decode(source, "UTF-8"); source = URLDecoder.decode(source, "UTF-8");
StringBuilder plantUmlSource = new StringBuilder(); StringBuilder plantUmlSource = new StringBuilder();
@@ -156,24 +164,23 @@ public class PlantUmlServlet extends HttpServlet {
} }
private void handleImageDecompress(HttpServletResponse response, private void handleImageDecompress(HttpServletResponse response, String source, String uri) throws IOException {
String source, String uri) throws IOException {
source = URLDecoder.decode(source, "UTF-8"); source = URLDecoder.decode(source, "UTF-8");
Transcoder transcoder = getTranscoder(); Transcoder transcoder = getTranscoder();
String text2 = transcoder.decode(source); String text2 = transcoder.decode(source);
sendImage(response, text2, uri); sendImage(response, text2, uri);
} }
private void handleImageProxy(HttpServletResponse response, String num, private void handleImageProxy(HttpServletResponse response, String num, String source, String format, String uri)
String source, String format, String uri) throws IOException { throws IOException {
SourceStringReader reader = new SourceStringReader( getContent(source)); SourceStringReader reader = new SourceStringReader(getContent(source));
int n = num == null ? 0 : Integer.parseInt(num); 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")) {
@@ -185,8 +192,7 @@ public class PlantUmlServlet extends HttpServlet {
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;
@@ -202,7 +208,7 @@ public class PlantUmlServlet extends HttpServlet {
} }
// 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