Skip to content

Commit

Permalink
Introduced parameter PLANTUML_CONFIG_FILE, that allows to specify a P…
Browse files Browse the repository at this point in the history
…lantUML config file.
  • Loading branch information
tmons committed Jul 26, 2022
1 parent 820fcca commit e2e6776
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ You can set all the following variables:
* `BASE_URL`
* PlantUML Base URL path
* Default value: `ROOT`
* `PLANTUML_CONFIG_FILE`
* Local path to a PlantUML configuration file (identical to the `-config` flag on the CLI)
* Default value: `null`
* `PLANTUML_LIMIT_SIZE`
* Limits image width and height
* Default value: `4096`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
*/
package net.sourceforge.plantuml.servlet;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
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;
Expand All @@ -46,6 +50,7 @@
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.error.PSystemError;
import net.sourceforge.plantuml.preproc.Defines;
import net.sourceforge.plantuml.version.Version;

/**
Expand All @@ -63,6 +68,8 @@ public class DiagramResponse {
*/
private static final String POWERED_BY = "PlantUML Version " + Version.versionString();

private static final List<String> CONFIG = new ArrayList<>();

static {
OptionFlags.ALLOW_INCLUDE = false;
if ("true".equalsIgnoreCase(System.getenv("ALLOW_PLANTUML_INCLUDE"))) {
Expand Down Expand Up @@ -114,7 +121,28 @@ public DiagramResponse(HttpServletResponse res, FileFormat fmt, HttpServletReque
public void sendDiagram(String uml, int idx) throws IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType(getContentType());
SourceStringReader reader = new SourceStringReader(uml);

if (CONFIG.size() == 0 && System.getenv("PLANTUML_CONFIG_FILE") != null) {
// Read config
final BufferedReader br = new BufferedReader(new FileReader(System.getenv("PLANTUML_CONFIG_FILE")));
if (br == null) {
return;
}
try {
String s = null;
while ((s = br.readLine()) != null) {
CONFIG.add(s);
}
} finally {
br.close();
}
}

SourceStringReader reader = new SourceStringReader(Defines.createEmpty(), uml, CONFIG);
if (CONFIG.size() > 0 && reader.getBlocks().get(0).getDiagram().getWarningOrError() != null) {
reader = new SourceStringReader(uml);
}

if (format == FileFormat.BASE64) {
byte[] imageBytes;
try (ByteArrayOutputStream outstream = new ByteArrayOutputStream()) {
Expand Down

0 comments on commit e2e6776

Please sign in to comment.