update + restructure pom and add missing javadoc

This commit is contained in:
Florian
2021-10-12 17:51:35 +02:00
committed by arnaudroques
parent 098e630a28
commit deda3c2256
18 changed files with 867 additions and 310 deletions

View File

@@ -48,41 +48,69 @@ import net.sourceforge.plantuml.version.Version;
import net.sourceforge.plantuml.error.PSystemError;
import net.sourceforge.plantuml.ErrorUml;
/**
* Delegates the diagram generation from the UML source and the filling of the HTTP response with the diagram in the
* right format. Its own responsibility is to produce the right HTTP headers.
*/
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.
*/
private static final String POWERED_BY = "PlantUML Version " + Version.versionString();
private HttpServletResponse response;
private FileFormat format;
private HttpServletRequest request;
private static final Map<FileFormat, String> CONTENT_TYPE;
static {
Map<FileFormat, String> map = new HashMap<FileFormat, String>();
map.put(FileFormat.PNG, "image/png");
map.put(FileFormat.SVG, "image/svg+xml");
map.put(FileFormat.EPS, "application/postscript");
map.put(FileFormat.UTXT, "text/plain;charset=UTF-8");
map.put(FileFormat.BASE64, "text/plain; charset=x-user-defined");
CONTENT_TYPE = Collections.unmodifiableMap(map);
}
static {
OptionFlags.ALLOW_INCLUDE = false;
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");
}});
}
public DiagramResponse(HttpServletResponse r, FileFormat f, HttpServletRequest rq) {
response = r;
format = f;
request = rq;
/**
* Response format.
*/
private FileFormat format;
/**
* Http request.
*/
private HttpServletRequest request;
/**
* Http response.
*/
private HttpServletResponse response;
/**
* Create new diagram response instance.
*
* @param res http response
* @param fmt target file format
* @param req http request
*/
public DiagramResponse(HttpServletResponse res, FileFormat fmt, HttpServletRequest req) {
response = res;
format = fmt;
request = req;
}
/**
* Render and send a specific uml diagram.
*
* @param uml textual UML diagram(s) source
* @param idx diagram index of {@code uml} to send
*
* @throws IOException if an input or output exception occurred
*/
public void sendDiagram(String uml, int idx) throws IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType(getContentType());
@@ -114,6 +142,13 @@ public class DiagramResponse {
diagram.exportDiagram(response.getOutputStream(), idx, new FileFormatOption(format));
}
/**
* Is block uml unmodified?
*
* @param blockUml block uml
*
* @return true if unmodified; otherwise false
*/
private boolean notModified(BlockUml blockUml) {
final String ifNoneMatch = request.getHeader("If-None-Match");
final long ifModifiedSince = request.getDateHeader("If-Modified-Since");
@@ -127,7 +162,13 @@ public class DiagramResponse {
return ifNoneMatch.contains(etag);
}
/**
* Produce and send the image map of the uml diagram in HTML format.
*
* @param uml textual UML diagram source
*
* @throws IOException if an input or output exception occurred
*/
public void sendMap(String uml) throws IOException {
response.setContentType(getContentType());
SourceStringReader reader = new SourceStringReader(uml);
@@ -145,15 +186,29 @@ public class DiagramResponse {
}
}
/**
* Check the syntax of the diagram and send a report in TEXT format.
*
* @param uml textual UML diagram source
*
* @throws IOException if an input or output exception occurred
*/
public void sendCheck(String uml) throws IOException {
response.setContentType(getContentType());
SourceStringReader reader = new SourceStringReader(uml);
DiagramDescription desc = reader.outputImage(
new NullOutputStream(), new FileFormatOption(FileFormat.PNG, false));
new NullOutputStream(),
new FileFormatOption(FileFormat.PNG, false)
);
PrintWriter httpOut = response.getWriter();
httpOut.print(desc.getDescription());
}
/**
* Add default header including cache headers to response.
*
* @param blockUml response block uml
*/
private void addHeaderForCache(BlockUml blockUml) {
long today = System.currentTimeMillis();
// Add http headers to force the browser to cache the image
@@ -177,12 +232,22 @@ public class DiagramResponse {
addHeaders(response);
}
/**
* Add default headers to response.
*
* @param response http response
*/
public static void addHeaders(HttpServletResponse response) {
response.addHeader("X-Powered-By", POWERED_BY);
response.addHeader("X-Patreon", "Support us on https://plantuml.com/patreon");
response.addHeader("X-Donate", "https://plantuml.com/paypal");
}
/**
* Get response content type.
*
* @return response content type
*/
private String getContentType() {
return CONTENT_TYPE.get(format);
}