2013-07-09 13:44:36 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2021-10-11 14:40:15 +00:00
|
|
|
* Project Info: https://plantuml.com
|
2014-02-06 17:28:06 +00:00
|
|
|
*
|
2013-07-09 13:44:36 +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;
|
|
|
|
|
2013-11-29 14:58:23 +00:00
|
|
|
import java.io.BufferedReader;
|
2013-07-09 13:44:36 +00:00
|
|
|
import java.io.IOException;
|
2013-11-29 14:58:23 +00:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.MalformedURLException;
|
2013-07-09 13:44:36 +00:00
|
|
|
import java.net.URL;
|
2021-10-15 13:11:55 +00:00
|
|
|
import java.security.cert.Certificate;
|
|
|
|
import java.util.List;
|
2013-07-09 13:44:36 +00:00
|
|
|
|
2021-10-15 13:11:55 +00:00
|
|
|
import javax.imageio.IIOException;
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
import javax.net.ssl.SSLPeerUnverifiedException;
|
|
|
|
|
|
|
|
import jakarta.servlet.ServletException;
|
|
|
|
import jakarta.servlet.http.HttpServlet;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
2013-07-09 13:44:36 +00:00
|
|
|
|
2013-12-02 11:45:11 +00:00
|
|
|
import net.sourceforge.plantuml.BlockUml;
|
2013-07-09 13:44:36 +00:00
|
|
|
import net.sourceforge.plantuml.FileFormat;
|
2019-09-26 17:13:07 +00:00
|
|
|
import net.sourceforge.plantuml.OptionFlags;
|
2013-07-09 13:44:36 +00:00
|
|
|
import net.sourceforge.plantuml.SourceStringReader;
|
2013-12-02 11:45:11 +00:00
|
|
|
import net.sourceforge.plantuml.core.Diagram;
|
|
|
|
import net.sourceforge.plantuml.core.UmlSource;
|
2013-07-09 13:44:36 +00:00
|
|
|
|
2021-10-11 14:40:15 +00:00
|
|
|
/**
|
2013-07-09 13:44:36 +00:00
|
|
|
* Proxy servlet of the webapp.
|
|
|
|
* This servlet retrieves the diagram source of a web resource (web html page)
|
|
|
|
* and renders it.
|
|
|
|
*/
|
2021-10-11 14:40:15 +00:00
|
|
|
@SuppressWarnings("SERIAL")
|
2013-07-09 13:44:36 +00:00
|
|
|
public class ProxyServlet 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 17:42:54 +00:00
|
|
|
public static boolean forbiddenURL(String full) {
|
2022-12-06 17:52:30 +00:00
|
|
|
if (full == null) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-06 17:42:54 +00:00
|
|
|
if (full.startsWith("https://") == false && full.startsWith("http://") == false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (full.matches("^https?://[-#.0-9:\\[\\]+]+/.*")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (full.matches("^https?://[^.]+/.*")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (full.matches("^https?://[^.]+$")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-09 13:44:36 +00:00
|
|
|
@Override
|
2013-07-10 15:07:24 +00:00
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
2013-07-09 13:44:36 +00:00
|
|
|
|
2015-12-10 15:43:56 +00:00
|
|
|
final String fmt = request.getParameter("fmt");
|
2013-11-25 21:35:58 +00:00
|
|
|
final String source = request.getParameter("src");
|
|
|
|
final String index = request.getParameter("idx");
|
2022-12-06 17:42:54 +00:00
|
|
|
if (forbiddenURL(source)) {
|
|
|
|
response.setStatus(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-29 14:58:23 +00:00
|
|
|
final URL srcUrl;
|
|
|
|
// Check if the src URL is valid
|
|
|
|
try {
|
|
|
|
srcUrl = new URL(source);
|
|
|
|
} catch (MalformedURLException mue) {
|
|
|
|
mue.printStackTrace();
|
2021-10-11 10:38:37 +00:00
|
|
|
response.setStatus(400);
|
2013-11-29 14:58:23 +00:00
|
|
|
return;
|
2014-02-06 17:28:06 +00:00
|
|
|
}
|
2013-11-29 14:58:23 +00:00
|
|
|
|
2013-11-25 21:35:58 +00:00
|
|
|
// generate the response
|
2013-11-29 14:58:23 +00:00
|
|
|
String diagmarkup = getSource(srcUrl);
|
|
|
|
SourceStringReader reader = new SourceStringReader(diagmarkup);
|
2013-11-25 21:35:58 +00:00
|
|
|
int n = index == null ? 0 : Integer.parseInt(index);
|
2013-12-02 11:45:11 +00:00
|
|
|
List<BlockUml> blocks = reader.getBlocks();
|
|
|
|
BlockUml block = blocks.get(n);
|
|
|
|
Diagram diagram = block.getDiagram();
|
|
|
|
UmlSource umlSrc = diagram.getSource();
|
|
|
|
String uml = umlSrc.getPlainString();
|
2015-12-10 15:43:56 +00:00
|
|
|
//System.out.println("uml=" + uml);
|
2014-02-06 17:28:06 +00:00
|
|
|
|
2013-12-02 11:45:11 +00:00
|
|
|
// generate the response
|
2017-04-05 19:57:35 +00:00
|
|
|
DiagramResponse dr = new DiagramResponse(response, getOutputFormat(fmt), request);
|
2013-12-02 11:45:11 +00:00
|
|
|
try {
|
2022-06-29 20:32:51 +00:00
|
|
|
// special handling for the MAP since it's not using "#sendDiagram()" like the other types
|
|
|
|
if ("map".equals(fmt)) {
|
|
|
|
dr.sendMap(uml, 0);
|
|
|
|
} else {
|
|
|
|
dr.sendDiagram(uml, 0);
|
|
|
|
}
|
2021-10-11 14:40:15 +00:00
|
|
|
} catch (IIOException e) {
|
2013-12-02 11:45:11 +00:00
|
|
|
// Browser has closed the connection, so the HTTP OutputStream is closed
|
2014-02-06 17:28:06 +00:00
|
|
|
// Silently catch the exception to avoid annoying log
|
2013-12-02 11:45:11 +00:00
|
|
|
}
|
2014-02-06 17:28:06 +00:00
|
|
|
dr = null;
|
2013-07-09 13:44:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 15:51:35 +00:00
|
|
|
/**
|
|
|
|
* Get textual uml diagram source from URL.
|
|
|
|
*
|
|
|
|
* @param url source URL
|
|
|
|
*
|
|
|
|
* @return textual uml diagram source
|
|
|
|
*
|
|
|
|
* @throws IOException if an input or output exception occurred
|
|
|
|
*/
|
2014-02-06 17:28:06 +00:00
|
|
|
private String getSource(final URL url) throws IOException {
|
2013-11-29 14:58:23 +00:00
|
|
|
String line;
|
|
|
|
BufferedReader rd;
|
|
|
|
StringBuilder sb;
|
2013-07-09 13:44:36 +00:00
|
|
|
try {
|
2013-11-29 14:58:23 +00:00
|
|
|
HttpURLConnection con = getConnection(url);
|
|
|
|
rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
|
|
sb = new StringBuilder();
|
|
|
|
|
|
|
|
while ((line = rd.readLine()) != null) {
|
|
|
|
sb.append(line + '\n');
|
|
|
|
}
|
|
|
|
rd.close();
|
|
|
|
return sb.toString();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2014-02-06 17:28:06 +00:00
|
|
|
} finally {
|
2013-11-29 14:58:23 +00:00
|
|
|
rd = null;
|
2013-07-09 13:44:36 +00:00
|
|
|
}
|
2013-11-29 14:58:23 +00:00
|
|
|
return "";
|
2013-07-09 13:44:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 15:51:35 +00:00
|
|
|
/**
|
|
|
|
* Get {@link FileFormat} instance from string.
|
|
|
|
*
|
|
|
|
* @param format file format name
|
|
|
|
*
|
|
|
|
* @return corresponding file format instance,
|
|
|
|
* if {@code format} is null or unknown the default {@link FileFormat#PNG} will be returned
|
|
|
|
*/
|
2015-12-10 18:38:37 +00:00
|
|
|
private FileFormat getOutputFormat(String format) {
|
2013-07-10 15:07:24 +00:00
|
|
|
if (format == null) {
|
2013-07-09 13:44:36 +00:00
|
|
|
return FileFormat.PNG;
|
|
|
|
}
|
|
|
|
if (format.equals("svg")) {
|
|
|
|
return FileFormat.SVG;
|
|
|
|
}
|
2017-06-28 10:59:27 +00:00
|
|
|
if (format.equals("eps")) {
|
|
|
|
return FileFormat.EPS;
|
|
|
|
}
|
|
|
|
if (format.equals("epstext")) {
|
|
|
|
return FileFormat.EPS_TEXT;
|
|
|
|
}
|
2013-07-09 13:44:36 +00:00
|
|
|
if (format.equals("txt")) {
|
2013-11-25 21:35:58 +00:00
|
|
|
return FileFormat.UTXT;
|
2013-07-09 13:44:36 +00:00
|
|
|
}
|
2022-06-29 20:32:51 +00:00
|
|
|
if (format.equals("map")) {
|
|
|
|
return FileFormat.UTXT;
|
|
|
|
}
|
|
|
|
|
2013-07-09 13:44:36 +00:00
|
|
|
return FileFormat.PNG;
|
2013-07-10 15:07:24 +00:00
|
|
|
}
|
2013-07-09 13:44:36 +00:00
|
|
|
|
2021-10-12 15:51:35 +00:00
|
|
|
/**
|
|
|
|
* Get open http connection from URL.
|
|
|
|
*
|
|
|
|
* @param url URL to open connection
|
|
|
|
*
|
|
|
|
* @return open http connection
|
|
|
|
*
|
|
|
|
* @throws IOException if an input or output exception occurred
|
|
|
|
*/
|
2014-02-06 17:28:06 +00:00
|
|
|
private HttpURLConnection getConnection(final URL url) throws IOException {
|
2019-05-17 14:22:28 +00:00
|
|
|
final HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
2021-10-11 14:40:15 +00:00
|
|
|
//if (con instanceof HttpsURLConnection) {
|
|
|
|
// printHttpsCert((HttpsURLConnection) con);
|
|
|
|
//}
|
2019-05-17 14:22:28 +00:00
|
|
|
con.setRequestMethod("GET");
|
|
|
|
String token = System.getenv("HTTP_AUTHORIZATION");
|
|
|
|
if (token != null) {
|
|
|
|
con.setRequestProperty("Authorization", token);
|
|
|
|
}
|
|
|
|
con.setReadTimeout(10000); // 10 seconds
|
|
|
|
con.connect();
|
|
|
|
return con;
|
2013-11-29 14:58:23 +00:00
|
|
|
}
|
2014-02-06 17:28:06 +00:00
|
|
|
|
2013-11-29 14:58:23 +00:00
|
|
|
/**
|
2021-10-12 15:51:35 +00:00
|
|
|
* Debug method used to dump the certificate info.
|
|
|
|
*
|
2013-11-29 14:58:23 +00:00
|
|
|
* @param con the https connection
|
|
|
|
*/
|
2014-02-06 17:28:06 +00:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private void printHttpsCert(final HttpsURLConnection con) {
|
2013-11-29 14:58:23 +00:00
|
|
|
if (con != null) {
|
|
|
|
try {
|
|
|
|
System.out.println("Response Code : " + con.getResponseCode());
|
|
|
|
System.out.println("Cipher Suite : " + con.getCipherSuite());
|
|
|
|
System.out.println("\n");
|
|
|
|
|
|
|
|
Certificate[] certs = con.getServerCertificates();
|
|
|
|
for (Certificate cert : certs) {
|
|
|
|
System.out.println("Cert Type : " + cert.getType());
|
|
|
|
System.out.println("Cert Hash Code : " + cert.hashCode());
|
|
|
|
System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
|
|
|
|
System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat());
|
|
|
|
System.out.println("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (SSLPeerUnverifiedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 13:44:36 +00:00
|
|
|
}
|