Improve multipage (index) handling

This commit is contained in:
Florian
2021-10-18 12:50:50 +02:00
committed by arnaudroques
parent db31315485
commit 3763ee737e
10 changed files with 500 additions and 134 deletions

View File

@@ -25,8 +25,6 @@ package net.sourceforge.plantuml.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.IIOException;
@@ -34,9 +32,11 @@ import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.OptionFlags;
import net.sourceforge.plantuml.servlet.utility.UmlExtractor;
import net.sourceforge.plantuml.servlet.utility.UrlDataExtractor;
/**
* Common service servlet to produce diagram from compressed UML source contained in the end part of the requested URI.
@@ -44,11 +44,6 @@ import net.sourceforge.plantuml.servlet.utility.UmlExtractor;
@SuppressWarnings("SERIAL")
public abstract class UmlDiagramService extends HttpServlet {
/**
* Regex pattern to fetch encoded uml text from an URL.
*/
private static final Pattern RECOVER_UML_PATTERN = Pattern.compile("/\\w+/(\\d+/)?(.*)");
static {
OptionFlags.ALLOW_INCLUDE = false;
if ("true".equalsIgnoreCase(System.getenv("ALLOW_PLANTUML_INCLUDE"))) {
@@ -58,12 +53,14 @@ public abstract class UmlDiagramService extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
final String url = request.getRequestURI();
final String encoded = UrlDataExtractor.getEncodedDiagram(url, "");
final int idx = UrlDataExtractor.getIndex(url, 0);
// build the UML source from the compressed request parameter
final String[] sourceAndIdx = getSourceAndIdx(request);
final int idx = Integer.parseInt(sourceAndIdx[1]);
final String uml;
try {
uml = UmlExtractor.getUmlSource(sourceAndIdx[0]);
uml = UmlExtractor.getUmlSource(encoded);
} catch (Exception e) {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Request");
@@ -75,18 +72,15 @@ public abstract class UmlDiagramService extends HttpServlet {
@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 int idx = UrlDataExtractor.getIndex(request.getRequestURI(), 0);
// read textual diagram source from request body
final StringBuilder uml = new StringBuilder();
final BufferedReader in = request.getReader();
while (true) {
final String line = in.readLine();
if (line == null) {
break;
try (BufferedReader in = request.getReader()) {
String line;
while ((line = in.readLine()) != null) {
uml.append(line).append('\n');
}
uml.append(line).append('\n');
}
doDiagramResponse(request, response, uml.toString(), idx);
@@ -118,33 +112,6 @@ public abstract class UmlDiagramService extends HttpServlet {
}
}
/**
* Extracts the UML source text and its index from the HTTP request.
*
* @param request http request
*
* @return the UML source text and its index
*/
public final String[] getSourceAndIdx(HttpServletRequest request) {
final Matcher recoverUml = RECOVER_UML_PATTERN.matcher(
request.getRequestURI().substring(
request.getContextPath().length()));
// the URL form has been submitted
if (recoverUml.matches()) {
final String data = recoverUml.group(2);
if (data.length() >= 4) {
String idx = recoverUml.group(1);
if (idx == null) {
idx = "0";
} else {
idx = idx.substring(0, idx.length() - 1);
}
return new String[]{data, idx };
}
}
return new String[]{"", "0"};
}
/**
* Gives the wished output format of the diagram. This value is used by the DiagramResponse class.
*