mirror of
https://github.com/octoleo/plantuml-server.git
synced 2025-01-03 05:00:14 +00:00
Merge pull request #74 from roxspring/support-post-requests
Add POST support plus PSystemError diagrams results generate 400 Bad Request status codes
This commit is contained in:
commit
cb2cb80931
@ -99,6 +99,9 @@ class DiagramResponse {
|
|||||||
addHeaderForCache(blockUml);
|
addHeaderForCache(blockUml);
|
||||||
}
|
}
|
||||||
final Diagram diagram = blockUml.getDiagram();
|
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));
|
final ImageData result = diagram.exportDiagram(response.getOutputStream(), idx, new FileFormatOption(format));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,17 +23,18 @@
|
|||||||
*/
|
*/
|
||||||
package net.sourceforge.plantuml.servlet;
|
package net.sourceforge.plantuml.servlet;
|
||||||
|
|
||||||
import java.io.IOException;
|
import net.sourceforge.plantuml.FileFormat;
|
||||||
import java.util.regex.Matcher;
|
import net.sourceforge.plantuml.servlet.utility.UmlExtractor;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import javax.imageio.IIOException;
|
import javax.imageio.IIOException;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.BufferedReader;
|
||||||
import net.sourceforge.plantuml.FileFormat;
|
import java.io.IOException;
|
||||||
import net.sourceforge.plantuml.servlet.utility.UmlExtractor;
|
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.
|
* Common service servlet to produce diagram from compressed UML source contained in the end part of the requested URI.
|
||||||
@ -46,6 +47,7 @@ public abstract class UmlDiagramService extends HttpServlet {
|
|||||||
|
|
||||||
// build the UML source from the compressed request parameter
|
// build the UML source from the compressed request parameter
|
||||||
final String[] sourceAndIdx = getSourceAndIdx(request);
|
final String[] sourceAndIdx = getSourceAndIdx(request);
|
||||||
|
final int idx = Integer.parseInt(sourceAndIdx[1]);
|
||||||
final String uml;
|
final String uml;
|
||||||
try {
|
try {
|
||||||
uml = UmlExtractor.getUmlSource(sourceAndIdx[0]);
|
uml = UmlExtractor.getUmlSource(sourceAndIdx[0]);
|
||||||
@ -55,9 +57,38 @@ public abstract class UmlDiagramService extends HttpServlet {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doDiagramResponse(request, response, uml, idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 int idx = Integer.parseInt(sourceAndIdx[1]);
|
||||||
|
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
|
doDiagramResponse(request, response, uml.toString(), idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doDiagramResponse(
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
String uml,
|
||||||
|
int idx)
|
||||||
|
throws IOException {
|
||||||
|
|
||||||
// generate the response
|
// generate the response
|
||||||
DiagramResponse dr = new DiagramResponse(response, getOutputFormat(), request);
|
DiagramResponse dr = new DiagramResponse(response, getOutputFormat(), request);
|
||||||
final int idx = Integer.parseInt(sourceAndIdx[1]);
|
|
||||||
try {
|
try {
|
||||||
dr.sendDiagram(uml, idx);
|
dr.sendDiagram(uml, idx);
|
||||||
} catch (IIOException iioe) {
|
} catch (IIOException iioe) {
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package net.sourceforge.plantuml.servlet;
|
package net.sourceforge.plantuml.servlet;
|
||||||
|
|
||||||
import com.meterware.httpunit.GetMethodWebRequest;
|
import com.meterware.httpunit.GetMethodWebRequest;
|
||||||
|
import com.meterware.httpunit.PostMethodWebRequest;
|
||||||
import com.meterware.httpunit.WebConversation;
|
import com.meterware.httpunit.WebConversation;
|
||||||
import com.meterware.httpunit.WebRequest;
|
import com.meterware.httpunit.WebRequest;
|
||||||
import com.meterware.httpunit.WebResponse;
|
import com.meterware.httpunit.WebResponse;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class TestSVG extends WebappTestCase {
|
public class TestSVG extends WebappTestCase {
|
||||||
@ -25,6 +28,47 @@ public class TestSVG extends WebappTestCase {
|
|||||||
assertTrue(diagramLen < 3000);
|
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
|
* Check the content of the SVG
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user