fix: PDF servlet

Since the update of `bastik` the PDF servlet didn't work anymore.

Problems:
- No PDF UnitTest existed
- Pom dependency `org.apache.xmlgraphics.batik-all` change nearly every dependency to `optional` starting with version `1.15`, hence some important dependencies like the SVG converter were missing
- `DiagramResponse.CONTENT_TYPE` had no mime type value for PDF

Changes:
- add PDF UnitTest
- remove `batik-all` dependency and only include the dependencies PlantUML requires for the PDF generation:
  * batik-dom
  * batik-svgrasterizer (includes batik-dom)
  * batik-svggen
  * fop
- remove own `DiagramResponse.CONTENT_TYPE` mapping and use `FileFormat.getMimeType()`
This commit is contained in:
Florian
2023-05-07 20:45:29 +02:00
committed by PlantUML
parent a6e69b33eb
commit 6538be2047
5 changed files with 85 additions and 44 deletions

View File

@@ -29,10 +29,7 @@ import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@@ -59,10 +56,6 @@ import net.sourceforge.plantuml.version.Version;
*/
public class DiagramResponse {
/**
* {@link FileFormat} to http content type mapping.
*/
private static final Map<FileFormat, String> CONTENT_TYPE;
/**
* X-Powered-By http header value included in every response by default.
*/
@@ -75,13 +68,6 @@ public class DiagramResponse {
if ("true".equalsIgnoreCase(System.getenv("ALLOW_PLANTUML_INCLUDE"))) {
OptionFlags.ALLOW_INCLUDE = true;
}
CONTENT_TYPE = Collections.unmodifiableMap(new HashMap<FileFormat, String>() {{
put(FileFormat.PNG, "image/png");
put(FileFormat.SVG, "image/svg+xml");
put(FileFormat.EPS, "application/postscript");
put(FileFormat.UTXT, "text/plain;charset=UTF-8");
put(FileFormat.BASE64, "text/plain; charset=x-user-defined");
}});
}
/**
@@ -282,7 +268,7 @@ public class DiagramResponse {
* @return response content type
*/
private String getContentType() {
return CONTENT_TYPE.get(format);
return format.getMimeType();
}
}

View File

@@ -91,7 +91,6 @@ public class ProxyServlet extends HttpServlet {
try {
srcUrl = new URL(source);
} catch (MalformedURLException mue) {
mue.printStackTrace();
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "URL malformed.");
return;
}

View File

@@ -0,0 +1,38 @@
package net.sourceforge.plantuml.servlet;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import net.sourceforge.plantuml.servlet.utils.TestUtils;
import net.sourceforge.plantuml.servlet.utils.WebappTestCase;
public class TestPDF extends WebappTestCase {
/**
* Verifies the generation of the PDF for the Bob -> Alice sample
*/
@Test
public void testSimpleSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/pdf/" + TestUtils.SEQBOB);
final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// Analyze response
// Verifies HTTP status code and the Content-Type
Assertions.assertEquals(200, conn.getResponseCode(), "Bad HTTP status received");
Assertions.assertEquals(
"application/pdf",
conn.getContentType().toLowerCase(),
"Response content type is not PDF"
);
// Get the content and verify its size
String diagram = getContentText(conn);
int diagramLen = diagram.length();
Assertions.assertTrue(diagramLen > 1500);
Assertions.assertTrue(diagramLen < 2000);
}
}