From ad09f193f40edcf3a575f5ba9463efabf61992dd Mon Sep 17 00:00:00 2001 From: Rob Oxspring Date: Wed, 18 Apr 2018 23:30:33 +0100 Subject: [PATCH] Add POST support --- .../plantuml/servlet/DiagramResponse.java | 5 ++- .../plantuml/servlet/ProxyServlet.java | 2 +- .../plantuml/servlet/UmlDiagramService.java | 44 ++++++++++++++++--- .../sourceforge/plantuml/servlet/TestSVG.java | 44 +++++++++++++++++++ 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/sourceforge/plantuml/servlet/DiagramResponse.java b/src/main/java/net/sourceforge/plantuml/servlet/DiagramResponse.java index e290ec2..cae74c8 100644 --- a/src/main/java/net/sourceforge/plantuml/servlet/DiagramResponse.java +++ b/src/main/java/net/sourceforge/plantuml/servlet/DiagramResponse.java @@ -76,7 +76,7 @@ class DiagramResponse { request = rq; } - void sendDiagram(String uml, int idx) throws IOException { + void sendDiagram(String uml, int idx, boolean badRequestOnError) throws IOException { response.addHeader("Access-Control-Allow-Origin", "*"); response.setContentType(getContentType()); SourceStringReader reader = new SourceStringReader(uml); @@ -99,6 +99,9 @@ class DiagramResponse { addHeaderForCache(blockUml); } final Diagram diagram = blockUml.getDiagram(); + if (diagram instanceof PSystemError) { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + } final ImageData result = diagram.exportDiagram(response.getOutputStream(), idx, new FileFormatOption(format)); } diff --git a/src/main/java/net/sourceforge/plantuml/servlet/ProxyServlet.java b/src/main/java/net/sourceforge/plantuml/servlet/ProxyServlet.java index c124122..f99d8e2 100644 --- a/src/main/java/net/sourceforge/plantuml/servlet/ProxyServlet.java +++ b/src/main/java/net/sourceforge/plantuml/servlet/ProxyServlet.java @@ -85,7 +85,7 @@ public class ProxyServlet extends HttpServlet { // generate the response DiagramResponse dr = new DiagramResponse(response, getOutputFormat(fmt), request); try { - dr.sendDiagram(uml, 0); + dr.sendDiagram(uml, 0, false); } catch (IIOException iioe) { // Browser has closed the connection, so the HTTP OutputStream is closed // Silently catch the exception to avoid annoying log diff --git a/src/main/java/net/sourceforge/plantuml/servlet/UmlDiagramService.java b/src/main/java/net/sourceforge/plantuml/servlet/UmlDiagramService.java index 2481201..21a76d4 100644 --- a/src/main/java/net/sourceforge/plantuml/servlet/UmlDiagramService.java +++ b/src/main/java/net/sourceforge/plantuml/servlet/UmlDiagramService.java @@ -23,17 +23,18 @@ */ package net.sourceforge.plantuml.servlet; -import java.io.IOException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import net.sourceforge.plantuml.FileFormat; +import net.sourceforge.plantuml.servlet.utility.UmlExtractor; + import javax.imageio.IIOException; 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.servlet.utility.UmlExtractor; +import java.io.BufferedReader; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Common service servlet to produce diagram from compressed UML source contained in the end part of the requested URI. @@ -59,7 +60,36 @@ public abstract class UmlDiagramService extends HttpServlet { DiagramResponse dr = new DiagramResponse(response, getOutputFormat(), request); final int idx = Integer.parseInt(sourceAndIdx[1]); try { - dr.sendDiagram(uml, idx); + dr.sendDiagram(uml, idx, false); + } catch (IIOException iioe) { + // Browser has closed the connection, so the HTTP OutputStream is closed + // Silently catch the exception to avoid annoying log + } + dr = null; + } + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { + + // build the UML source from the compressed request parameter + final String[] sourceAndIdx = getSourceAndIdx(request); +// final String uml; + + final StringBuilder uml = new StringBuilder(); + final BufferedReader in = request.getReader(); + while (true) { + final String line = in.readLine(); + if (line == null) { + break; + } + uml.append(line).append('\n'); + } + + // generate the response + DiagramResponse dr = new DiagramResponse(response, getOutputFormat(), request); + final int idx = Integer.parseInt(sourceAndIdx[1]); + try { + dr.sendDiagram(uml.toString(), idx, true); } catch (IIOException iioe) { // Browser has closed the connection, so the HTTP OutputStream is closed // Silently catch the exception to avoid annoying log diff --git a/src/test/java/net/sourceforge/plantuml/servlet/TestSVG.java b/src/test/java/net/sourceforge/plantuml/servlet/TestSVG.java index 526baab..4928df6 100644 --- a/src/test/java/net/sourceforge/plantuml/servlet/TestSVG.java +++ b/src/test/java/net/sourceforge/plantuml/servlet/TestSVG.java @@ -1,10 +1,13 @@ package net.sourceforge.plantuml.servlet; import com.meterware.httpunit.GetMethodWebRequest; +import com.meterware.httpunit.PostMethodWebRequest; import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebResponse; +import java.io.ByteArrayInputStream; +import java.nio.charset.Charset; import java.util.Scanner; public class TestSVG extends WebappTestCase { @@ -25,6 +28,47 @@ public class TestSVG extends WebappTestCase { assertTrue(diagramLen < 3000); } + /** + * Verifies the generation of the SVG for the Bob -> Alice sample + */ + public void testPostedSequenceDiagram() throws Exception { + WebConversation conversation = new WebConversation(); + PostMethodWebRequest request = new PostMethodWebRequest( + getServerUrl() + "svg/", + new ByteArrayInputStream("@startuml\nBob -> Alice\n@enduml".getBytes(Charset.defaultCharset())), + "text/plain"); + + WebResponse response = conversation.getResource(request); + + assertEquals(200, response.getResponseCode()); + + // Analyze response + // Verifies the Content-Type header + assertEquals("Response content type is not SVG", "image/svg+xml", response.getContentType()); + // Get the content and verify its size + + String diagram = response.getText(); + + int diagramLen = diagram.length(); + assertTrue(diagramLen > 1000); + assertTrue(diagramLen < 3000); + } + + /** + * Verifies the generation of the SVG for the Bob -> Alice sample + */ + public void testPostedInvalidSequenceDiagram() throws Exception { + WebConversation conversation = new WebConversation(); + PostMethodWebRequest request = new PostMethodWebRequest( + getServerUrl() + "svg/", + new ByteArrayInputStream("@startuml\n[Bob\n@enduml".getBytes(Charset.defaultCharset())), + "text/plain"); + + WebResponse response = conversation.getResource(request); + + assertEquals(400, response.getResponseCode()); + } + /** * Check the content of the SVG */