1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-12-22 08:48:54 +00:00

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

@ -74,7 +74,7 @@
<!-- Testing --> <!-- Testing -->
<junit.version>5.9.3</junit.version> <junit.version>5.9.3</junit.version>
<junit-suite.version>1.9.3</junit-suite.version> <junit-suite.version>1.9.3</junit-suite.version>
<selenium.version>4.9.0</selenium.version> <selenium.version>4.9.1</selenium.version>
<selenium-webdrivermanager.version>5.3.2</selenium-webdrivermanager.version> <selenium-webdrivermanager.version>5.3.2</selenium-webdrivermanager.version>
<commons-io.version>2.11.0</commons-io.version> <commons-io.version>2.11.0</commons-io.version>
<jetty-server.version>${jetty.version}</jetty-server.version> <jetty-server.version>${jetty.version}</jetty-server.version>
@ -86,7 +86,7 @@
<maven-resources-plugin.version>3.3.1</maven-resources-plugin.version> <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version> <maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<versions-maven-plugin.version>2.15.0</versions-maven-plugin.version> <versions-maven-plugin.version>2.15.0</versions-maven-plugin.version>
<maven-surefire-plugin.version>3.0.0</maven-surefire-plugin.version> <maven-surefire-plugin.version>3.1.0</maven-surefire-plugin.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version> <maven-war-plugin.version>3.3.2</maven-war-plugin.version>
<maven-install-plugin.version>3.1.1</maven-install-plugin.version> <maven-install-plugin.version>3.1.1</maven-install-plugin.version>
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version> <maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>
@ -108,11 +108,13 @@
<groupId>net.sourceforge.plantuml</groupId> <groupId>net.sourceforge.plantuml</groupId>
<artifactId>plantuml</artifactId> <artifactId>plantuml</artifactId>
<version>${plantuml.version}</version> <version>${plantuml.version}</version>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.webjars.npm</groupId> <groupId>org.webjars.npm</groupId>
<artifactId>monaco-editor</artifactId> <artifactId>monaco-editor</artifactId>
<version>${monaco-editor.version}</version> <version>${monaco-editor.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>jakarta.servlet</groupId> <groupId>jakarta.servlet</groupId>
@ -153,31 +155,44 @@
<groupId>org.scilab.forge</groupId> <groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath</artifactId> <artifactId>jlatexmath</artifactId>
<version>${jlatexmath.version}</version> <version>${jlatexmath.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.scilab.forge</groupId> <groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath-font-greek</artifactId> <artifactId>jlatexmath-font-greek</artifactId>
<version>${jlatexmath-font-greek.version}</version> <version>${jlatexmath-font-greek.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.scilab.forge</groupId> <groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath-font-cyrillic</artifactId> <artifactId>jlatexmath-font-cyrillic</artifactId>
<version>${jlatexmath-font-cyrillic.version}</version> <version>${jlatexmath-font-cyrillic.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<!-- PDF --> <!-- PDF
PlantUMLs PDF generation requires:
- batik-dom
- batik-svgrasterizer (includes batik-dom)
- batik-svggen
- fop
-->
<dependency> <dependency>
<!-- batik-all generally tends to provide duplicate resources on the classpath -->
<groupId>org.apache.xmlgraphics</groupId> <groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-all</artifactId> <artifactId>batik-svgrasterizer</artifactId>
<version>${batik-all.version}</version> <version>${batik-all.version}</version>
<type>pom</type> <scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-svggen</artifactId>
<version>${batik-all.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<!-- fop generally tends to provide duplicate resources on the classpath -->
<groupId>org.apache.xmlgraphics</groupId> <groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId> <artifactId>fop</artifactId>
<version>${fop.version}</version> <version>${fop.version}</version>
<type>pom</type> <scope>runtime</scope>
</dependency> </dependency>
<!-- Testing --> <!-- Testing -->
@ -205,12 +220,6 @@
<version>${selenium-webdrivermanager.version}</version> <version>${selenium-webdrivermanager.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId> <artifactId>jetty-server</artifactId>

37
pom.xml
View File

@ -74,7 +74,7 @@
<!-- Testing --> <!-- Testing -->
<junit.version>5.9.3</junit.version> <junit.version>5.9.3</junit.version>
<junit-suite.version>1.9.3</junit-suite.version> <junit-suite.version>1.9.3</junit-suite.version>
<selenium.version>4.9.0</selenium.version> <selenium.version>4.9.1</selenium.version>
<selenium-webdrivermanager.version>5.3.2</selenium-webdrivermanager.version> <selenium-webdrivermanager.version>5.3.2</selenium-webdrivermanager.version>
<commons-io.version>2.11.0</commons-io.version> <commons-io.version>2.11.0</commons-io.version>
<jetty-server.version>${jetty.version}</jetty-server.version> <jetty-server.version>${jetty.version}</jetty-server.version>
@ -86,7 +86,7 @@
<maven-resources-plugin.version>3.3.1</maven-resources-plugin.version> <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version> <maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<versions-maven-plugin.version>2.15.0</versions-maven-plugin.version> <versions-maven-plugin.version>2.15.0</versions-maven-plugin.version>
<maven-surefire-plugin.version>3.0.0</maven-surefire-plugin.version> <maven-surefire-plugin.version>3.1.0</maven-surefire-plugin.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version> <maven-war-plugin.version>3.3.2</maven-war-plugin.version>
<maven-install-plugin.version>3.1.1</maven-install-plugin.version> <maven-install-plugin.version>3.1.1</maven-install-plugin.version>
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version> <maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>
@ -108,11 +108,13 @@
<groupId>net.sourceforge.plantuml</groupId> <groupId>net.sourceforge.plantuml</groupId>
<artifactId>plantuml</artifactId> <artifactId>plantuml</artifactId>
<version>${plantuml.version}</version> <version>${plantuml.version}</version>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.webjars.npm</groupId> <groupId>org.webjars.npm</groupId>
<artifactId>monaco-editor</artifactId> <artifactId>monaco-editor</artifactId>
<version>${monaco-editor.version}</version> <version>${monaco-editor.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
@ -131,31 +133,44 @@
<groupId>org.scilab.forge</groupId> <groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath</artifactId> <artifactId>jlatexmath</artifactId>
<version>${jlatexmath.version}</version> <version>${jlatexmath.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.scilab.forge</groupId> <groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath-font-greek</artifactId> <artifactId>jlatexmath-font-greek</artifactId>
<version>${jlatexmath-font-greek.version}</version> <version>${jlatexmath-font-greek.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.scilab.forge</groupId> <groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath-font-cyrillic</artifactId> <artifactId>jlatexmath-font-cyrillic</artifactId>
<version>${jlatexmath-font-cyrillic.version}</version> <version>${jlatexmath-font-cyrillic.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<!-- PDF --> <!-- PDF
PlantUMLs PDF generation requires:
- batik-dom
- batik-svgrasterizer (includes batik-dom)
- batik-svggen
- fop
-->
<dependency> <dependency>
<!-- batik-all generally tends to provide duplicate resources on the classpath -->
<groupId>org.apache.xmlgraphics</groupId> <groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-all</artifactId> <artifactId>batik-svgrasterizer</artifactId>
<version>${batik-all.version}</version> <version>${batik-all.version}</version>
<type>pom</type> <scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-svggen</artifactId>
<version>${batik-all.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<!-- fop generally tends to provide duplicate resources on the classpath -->
<groupId>org.apache.xmlgraphics</groupId> <groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId> <artifactId>fop</artifactId>
<version>${fop.version}</version> <version>${fop.version}</version>
<type>pom</type> <scope>runtime</scope>
</dependency> </dependency>
<!-- Testing --> <!-- Testing -->
@ -183,12 +198,6 @@
<version>${selenium-webdrivermanager.version}</version> <version>${selenium-webdrivermanager.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId> <artifactId>jetty-server</artifactId>

View File

@ -29,10 +29,7 @@ import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
@ -59,10 +56,6 @@ import net.sourceforge.plantuml.version.Version;
*/ */
public class DiagramResponse { 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. * 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"))) { if ("true".equalsIgnoreCase(System.getenv("ALLOW_PLANTUML_INCLUDE"))) {
OptionFlags.ALLOW_INCLUDE = true; 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 * @return response content type
*/ */
private String getContentType() { private String getContentType() {
return CONTENT_TYPE.get(format); return format.getMimeType();
} }
} }

View File

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