Add Base64 support

This commit is contained in:
Arnaud Roques 2018-03-19 22:28:19 +01:00
parent 353b089904
commit 7ad8b8033d
3 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,40 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* Project Info: http://plantuml.sourceforge.net
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
package net.sourceforge.plantuml.servlet;
import net.sourceforge.plantuml.FileFormat;
/*
* Base64 servlet of the webapp.
* This servlet produces the UML diagram in Base64 format.
*/
@SuppressWarnings("serial")
public class Base64Servlet extends UmlDiagramService {
@Override
public FileFormat getOutputFormat() {
return FileFormat.BASE64;
}
}

View File

@ -25,6 +25,7 @@ package net.sourceforge.plantuml.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.ByteArrayOutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -38,6 +39,7 @@ import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.NullOutputStream;
import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.code.Base64Coder;
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.ImageData;
@ -64,6 +66,7 @@ class DiagramResponse {
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);
}
@ -76,6 +79,15 @@ class DiagramResponse {
void sendDiagram(String uml, int idx) throws IOException {
response.setContentType(getContentType());
SourceStringReader reader = new SourceStringReader(uml);
if (format == FileFormat.BASE64) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final DiagramDescription result = reader.outputImage(baos, idx, new FileFormatOption(FileFormat.PNG));
baos.close();
final String encodedBytes = "data:image/png;base64,"
+ Base64Coder.encodeLines(baos.toByteArray()).replaceAll("\\s", "");
response.getOutputStream().write(encodedBytes.getBytes());
return;
}
final BlockUml blockUml = reader.getBlocks().get(0);
if (notModified(blockUml)) {
addHeaderForCache(blockUml);

View File

@ -31,6 +31,10 @@
<servlet-name>epstextservlet</servlet-name>
<servlet-class>net.sourceforge.plantuml.servlet.EpsTextServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>base64servlet</servlet-name>
<servlet-class>net.sourceforge.plantuml.servlet.Base64Servlet</servlet-class>
</servlet>
<servlet>
<servlet-name>asciiservlet</servlet-name>
<servlet-class>net.sourceforge.plantuml.servlet.AsciiServlet</servlet-class>
@ -84,6 +88,10 @@
<servlet-name>epstextservlet</servlet-name>
<url-pattern>/epstext/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>base64servlet</servlet-name>
<url-pattern>/base64/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>asciiservlet</servlet-name>
<url-pattern>/txt/*</url-pattern>