2013-07-09 13:44:36 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* 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;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import net.sourceforge.plantuml.FileFormat;
|
|
|
|
import net.sourceforge.plantuml.FileFormatOption;
|
|
|
|
import net.sourceforge.plantuml.SourceStringReader;
|
|
|
|
|
2013-11-29 14:58:23 +00:00
|
|
|
import java.security.cert.Certificate;
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
import javax.net.ssl.SSLPeerUnverifiedException;
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
public class ProxyServlet extends HttpServlet {
|
|
|
|
|
|
|
|
private String format;
|
2013-07-10 15:07:24 +00:00
|
|
|
|
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
|
|
|
|
2013-11-25 21:35:58 +00:00
|
|
|
final String source = request.getParameter("src");
|
|
|
|
final String index = request.getParameter("idx");
|
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();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-25 21:35:58 +00:00
|
|
|
// generate the response
|
2013-11-29 14:58:23 +00:00
|
|
|
String diagmarkup = getSource(srcUrl);
|
|
|
|
System.out.println("getSource=>" + diagmarkup);
|
|
|
|
SourceStringReader reader = new SourceStringReader(diagmarkup);
|
2013-11-25 21:35:58 +00:00
|
|
|
int n = index == null ? 0 : Integer.parseInt(index);
|
2013-11-29 14:58:23 +00:00
|
|
|
|
2013-10-31 21:54:53 +00:00
|
|
|
reader.generateImage(response.getOutputStream(), n, new FileFormatOption(getOutputFormat(), false));
|
2013-07-09 13:44:36 +00:00
|
|
|
}
|
|
|
|
|
2013-11-29 14:58:23 +00:00
|
|
|
private String getSource(URL url) throws IOException {
|
|
|
|
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();
|
|
|
|
} finally{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private FileFormat getOutputFormat() {
|
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;
|
|
|
|
}
|
|
|
|
if (format.equals("txt")) {
|
2013-11-25 21:35:58 +00:00
|
|
|
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
|
|
|
|
2013-11-29 14:58:23 +00:00
|
|
|
private HttpURLConnection getConnection(URL url) throws IOException {
|
|
|
|
if (url.getProtocol().startsWith("https")) {
|
|
|
|
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
con.setReadTimeout(10000); // 10 seconds
|
|
|
|
// printHttpsCert(con);
|
|
|
|
con.connect();
|
|
|
|
return con;
|
|
|
|
} else {
|
|
|
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
con.setReadTimeout(10000); // 10 seconds
|
|
|
|
con.connect();
|
|
|
|
return con;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Debug method used to dump the certificate info
|
|
|
|
* @param con the https connection
|
|
|
|
*/
|
|
|
|
private void printHttpsCert(HttpsURLConnection con) {
|
|
|
|
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
|
|
|
}
|