1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-06-17 07:32:23 +00:00
plantuml-server/src/main/java/net/sourceforge/plantuml/servlet/UmlDiagramService.java

123 lines
4.2 KiB
Java
Raw Normal View History

2011-03-30 15:53:11 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* Project Info: https://plantuml.com
*
2011-03-30 15:53:11 +00:00
* 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;
2018-04-18 22:30:33 +00:00
import java.io.BufferedReader;
import java.io.IOException;
import javax.imageio.IIOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
2021-10-18 10:50:50 +00:00
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.OptionFlags;
import net.sourceforge.plantuml.servlet.utility.UmlExtractor;
2021-10-18 10:50:50 +00:00
import net.sourceforge.plantuml.servlet.utility.UrlDataExtractor;
2011-03-30 15:53:11 +00:00
/**
2013-07-10 15:07:24 +00:00
* Common service servlet to produce diagram from compressed UML source contained in the end part of the requested URI.
2011-03-30 15:53:11 +00:00
*/
@SuppressWarnings("SERIAL")
public abstract class UmlDiagramService extends HttpServlet {
2019-09-26 17:08:48 +00:00
static {
OptionFlags.ALLOW_INCLUDE = false;
if ("true".equalsIgnoreCase(System.getenv("ALLOW_PLANTUML_INCLUDE"))) {
OptionFlags.ALLOW_INCLUDE = true;
}
}
@Override
2013-07-10 15:07:24 +00:00
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
2021-10-18 10:50:50 +00:00
final String url = request.getRequestURI();
final String encoded = UrlDataExtractor.getEncodedDiagram(url, "");
final int idx = UrlDataExtractor.getIndex(url, 0);
2011-03-31 14:12:10 +00:00
// build the UML source from the compressed request parameter
2017-04-05 19:57:35 +00:00
final String uml;
try {
2021-10-18 10:50:50 +00:00
uml = UmlExtractor.getUmlSource(encoded);
2017-04-05 19:57:35 +00:00
} catch (Exception e) {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Request");
return;
}
doDiagramResponse(request, response, uml, idx);
2018-04-18 22:30:33 +00:00
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
2021-10-18 10:50:50 +00:00
final int idx = UrlDataExtractor.getIndex(request.getRequestURI(), 0);
2018-04-18 22:30:33 +00:00
2021-10-18 10:50:50 +00:00
// read textual diagram source from request body
2018-04-18 22:30:33 +00:00
final StringBuilder uml = new StringBuilder();
2021-10-18 10:50:50 +00:00
try (BufferedReader in = request.getReader()) {
String line;
while ((line = in.readLine()) != null) {
uml.append(line).append('\n');
2018-04-18 22:30:33 +00:00
}
}
doDiagramResponse(request, response, uml.toString(), idx);
}
/**
* Send diagram response.
*
* @param request html request
* @param response html response
* @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
*/
private void doDiagramResponse(
HttpServletRequest request,
HttpServletResponse response,
String uml,
int idx
) throws IOException {
2018-04-18 22:30:33 +00:00
// generate the response
DiagramResponse dr = new DiagramResponse(response, getOutputFormat(), request);
try {
dr.sendDiagram(uml, idx);
} catch (IIOException e) {
// Browser has closed the connection, so the HTTP OutputStream is closed
// Silently catch the exception to avoid annoying log
}
}
2013-07-10 15:07:24 +00:00
2011-03-30 15:53:11 +00:00
/**
2013-07-10 15:07:24 +00:00
* Gives the wished output format of the diagram. This value is used by the DiagramResponse class.
*
2011-03-30 15:53:11 +00:00
* @return the format
*/
2011-03-31 14:12:10 +00:00
abstract public FileFormat getOutputFormat();
2013-07-10 15:07:24 +00:00
}