1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-11-15 16:57:09 +00:00

Merge branch 'hotproxy2'

This commit is contained in:
Maxime Sinclair 2013-12-03 18:11:42 +01:00
commit 414db01d79
9 changed files with 328 additions and 73 deletions

View File

@ -0,0 +1,119 @@
/* ========================================================================
* 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 java.io.IOException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import HTTPClient.CookieModule;
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.ModuleException;
import HTTPClient.ParseException;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
/*
* 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 OldProxyServlet extends HttpServlet {
private static final Pattern proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
private String format;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
final String uri = request.getRequestURI();
Matcher proxyMatcher = proxyPattern.matcher(uri);
if (proxyMatcher.matches()) {
String num = proxyMatcher.group(2); // Optional number of the diagram source
format = proxyMatcher.group(4); // Expected format of the generated diagram
String sourceURL = proxyMatcher.group(5);
handleImageProxy(response, num, sourceURL);
} else {
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", "ERROR Invalid proxy syntax : " + uri);
request.removeAttribute("net.sourceforge.plantuml.servlet.encoded");
// forward to index.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}
}
private void handleImageProxy(HttpServletResponse response, String num, String source) throws IOException {
SourceStringReader reader = new SourceStringReader(getSource(source));
int n = num == null ? 0 : Integer.parseInt(num);
reader.generateImage(response.getOutputStream(), n, new FileFormatOption(getOutputFormat(), false));
}
private String getSource(String uri) throws IOException {
CookieModule.setCookiePolicyHandler(null);
final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
final Matcher m = p.matcher(uri);
if (m.find() == false) {
throw new IOException(uri);
}
final URL url = new URL(uri);
final HTTPConnection httpConnection = new HTTPConnection(url);
try {
final HTTPResponse resp = httpConnection.Get(m.group(1));
return resp.getText();
} catch (ModuleException e) {
throw new IOException(e.toString());
} catch (ParseException e) {
throw new IOException(e.toString());
}
}
private FileFormat getOutputFormat() {
if (format == null) {
return FileFormat.PNG;
}
if (format.equals("svg")) {
return FileFormat.SVG;
}
if (format.equals("txt")) {
return FileFormat.ATXT;
}
return FileFormat.PNG;
}
}

View File

@ -23,26 +23,30 @@
*/ */
package net.sourceforge.plantuml.servlet; package net.sourceforge.plantuml.servlet;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import HTTPClient.CookieModule; import net.sourceforge.plantuml.BlockUml;
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.ModuleException;
import HTTPClient.ParseException;
import net.sourceforge.plantuml.FileFormat; import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader; import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.UmlSource;
import java.security.cert.Certificate;
import java.util.List;
import javax.imageio.IIOException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
/* /*
* Proxy servlet of the webapp. * Proxy servlet of the webapp.
@ -52,55 +56,65 @@ import net.sourceforge.plantuml.SourceStringReader;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ProxyServlet extends HttpServlet { public class ProxyServlet extends HttpServlet {
private static final Pattern proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
private String format; private String format;
@Override @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
final String uri = request.getRequestURI(); final String source = request.getParameter("src");
final String index = request.getParameter("idx");
Matcher proxyMatcher = proxyPattern.matcher(uri); final URL srcUrl;
if (proxyMatcher.matches()) { // Check if the src URL is valid
String num = proxyMatcher.group(2); // Optional number of the diagram source
format = proxyMatcher.group(4); // Expected format of the generated diagram
String sourceURL = proxyMatcher.group(5);
handleImageProxy(response, num, sourceURL);
} else {
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", "ERROR Invalid proxy syntax : " + uri);
request.removeAttribute("net.sourceforge.plantuml.servlet.encoded");
// forward to index.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}
}
private void handleImageProxy(HttpServletResponse response, String num, String source) throws IOException {
SourceStringReader reader = new SourceStringReader(getSource(source));
int n = num == null ? 0 : Integer.parseInt(num);
reader.generateImage(response.getOutputStream(), n, new FileFormatOption(getOutputFormat(), false));
}
private String getSource(String uri) throws IOException {
CookieModule.setCookiePolicyHandler(null);
final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
final Matcher m = p.matcher(uri);
if (m.find() == false) {
throw new IOException(uri);
}
final URL url = new URL(uri);
final HTTPConnection httpConnection = new HTTPConnection(url);
try { try {
final HTTPResponse resp = httpConnection.Get(m.group(1)); srcUrl = new URL(source);
return resp.getText(); } catch (MalformedURLException mue) {
} catch (ModuleException e) { mue.printStackTrace();
throw new IOException(e.toString()); return;
} catch (ParseException e) {
throw new IOException(e.toString());
} }
// generate the response
String diagmarkup = getSource(srcUrl);
System.out.println("getSource=>" + diagmarkup);
SourceStringReader reader = new SourceStringReader(diagmarkup);
int n = index == null ? 0 : Integer.parseInt(index);
List<BlockUml> blocks = reader.getBlocks();
BlockUml block = blocks.get(n);
Diagram diagram = block.getDiagram();
UmlSource umlSrc = diagram.getSource();
String uml = umlSrc.getPlainString();
System.out.println("uml="+uml);
// generate the response
DiagramResponse dr = new DiagramResponse(response, getOutputFormat());
try {
dr.sendDiagram(uml);
} catch (IIOException iioe) {
// Browser has closed the connection, so the HTTP OutputStream is closed
// Silently catch the exception to avoid annoying log
}
dr = null;
}
private String getSource(URL url) throws IOException {
String line;
BufferedReader rd;
StringBuilder sb;
try {
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;
}
return "";
} }
private FileFormat getOutputFormat() { private FileFormat getOutputFormat() {
@ -111,9 +125,53 @@ public class ProxyServlet extends HttpServlet {
return FileFormat.SVG; return FileFormat.SVG;
} }
if (format.equals("txt")) { if (format.equals("txt")) {
return FileFormat.ATXT; return FileFormat.UTXT;
} }
return FileFormat.PNG; return FileFormat.PNG;
} }
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();
}
}
}
} }

View File

@ -22,7 +22,8 @@ UmlDiagramService <|-- PngServlet
UmlDiagramService <|-- SvgServlet UmlDiagramService <|-- SvgServlet
UmlDiagramService <|-- AsciiServlet UmlDiagramService <|-- AsciiServlet
UmlDiagramService o- DiagramResponse UmlDiagramService o- DiagramResponse
MapServlet o- DiagramResponse MapServlet o-- DiagramResponse
ProxyServlet o-- DiagramResponse
@enduml @enduml
## Sequence diagram ## ## Sequence diagram ##

View File

@ -6,7 +6,7 @@
- Service servlets : ImgServlet, SvgServlet, AsciiServlet, ProxyServlet that only produce a diagram as output.<br> - Service servlets : ImgServlet, SvgServlet, AsciiServlet, ProxyServlet that only produce a diagram as output.<br>
<br> <br>
Structure of the service part of the PlantUmlServer: <br> Structure of the service part of the PlantUmlServer: <br>
<img src="http://www.plantuml.com/plantuml/img/XP51ReCm44Ntd6AMH0etwAPIbNPJjIhg0OoPm4WsTiPZrAZDtGk514ZIP3b_dlx_7jTK8g3riWUBja0EIJsNf7RbGjeIcavHHH1MMa0R5G9yMlD4gc9bS-IMDC9t0k1ZOKX3wwY4qZsZf2yYlYSCoWVk8WO1tgrX8GVlce30mQywZrFGQ9OBKrD1XPAxo1hJenAP5lo636uSMoKz_1R5HndcT9KSag7tMFeKshS-qzBhxTRpW6sV_FVCW4qv6apDh5diNvLVxWxJEMTPyF2JPPUNlS5snkDy0tfdzxK_OfV_DZ1DTOV8stl4Oz14_pCkEpzqCjM_ilq5" /> <img src="http://www.plantuml.com/plantuml/img/XP51ReCm44Ntd6AMH0etwAPIbNPJjIhg0OoPm4WsTiPZrAZDtGk5913IP3b_dlx_7jTK8g3riWUBja0EIJsLf7RbJDeIcavHHH1MMa0R5G9yMlD4gc9bS-IMDC9t0k1ZOKX3wwY4qZsZf2yYlYSCoWVk8WO1tgrX9WVlce30mQywZrFGQ9OBKrD1XPAxo1hJenAPPlo636uSMoKz_1R5HndcT9KSag7tMFeKshU-qDBhxTRJW6sV_FVCW4qv6foRMJFRloe_tntEvvnamSDFbYqlUuFjZCVv1lJExcj_n9R_DZ1DTOV8stl4Oz14_pCkkpnqSgxVRPVhQV5hm2y0" />
</p> </p>
<p> <p>
<img src="http://www.plantuml.com/plantuml/img/XP1DZi8m38NtdCA23RFe0OfGLr24n4y5uW2cU2fBQL8vBeBRup8ZHEc2LPJtNhuNMraTmOey2Ie73-4N48hT2hZ6Ye2TQwEQHvTHuQiZoTMHGfB1ssq65Uanj5BIzESZTghTycQ0KeFy1KrvPNjkqgD-gTktshIQ1wbH1wKBnagmFb1iWezaB-RpKiYcoBAlqKZ-ygyQk45HBhb1hp0kd1sdxGOSdmNbFWQCiE4pJD8qpzDqz4cpWixkVlpSCAsxhHgsKvDX_H3G6_q1" /> <img src="http://www.plantuml.com/plantuml/img/XP1DZi8m38NtdCA23RFe0OfGLr24n4y5uW2cU2fBQL8vBeBRup8ZHEc2LPJtNhuNMraTmOey2Ie73-4N48hT2hZ6Ye2TQwEQHvTHuQiZoTMHGfB1ssq65Uanj5BIzESZTghTycQ0KeFy1KrvPNjkqgD-gTktshIQ1wbH1wKBnagmFb1iWezaB-RpKiYcoBAlqKZ-ygyQk45HBhb1hp0kd1sdxGOSdmNbFWQCiE4pJD8qpzDqz4cpWixkVlpSCAsxhHgsKvDX_H3G6_q1" />

View File

@ -31,6 +31,10 @@
<servlet-name>proxyservlet</servlet-name> <servlet-name>proxyservlet</servlet-name>
<servlet-class>net.sourceforge.plantuml.servlet.ProxyServlet</servlet-class> <servlet-class>net.sourceforge.plantuml.servlet.ProxyServlet</servlet-class>
</servlet> </servlet>
<servlet>
<servlet-name>oldproxyservlet</servlet-name>
<servlet-class>net.sourceforge.plantuml.servlet.OldProxyServlet</servlet-class>
</servlet>
<servlet> <servlet>
<servlet-name>mapservlet</servlet-name> <servlet-name>mapservlet</servlet-name>
<servlet-class>net.sourceforge.plantuml.servlet.MapServlet</servlet-class> <servlet-class>net.sourceforge.plantuml.servlet.MapServlet</servlet-class>
@ -69,9 +73,13 @@
<url-pattern>/start/*</url-pattern> <url-pattern>/start/*</url-pattern>
</servlet-mapping> </servlet-mapping>
<servlet-mapping> <servlet-mapping>
<servlet-name>proxyservlet</servlet-name> <servlet-name>oldproxyservlet</servlet-name>
<url-pattern>/proxy/*</url-pattern> <url-pattern>/proxy/*</url-pattern>
</servlet-mapping> </servlet-mapping>
<servlet-mapping>
<servlet-name>proxyservlet</servlet-name>
<url-pattern>/proxy</url-pattern>
</servlet-mapping>
<error-page> <error-page>
<exception-type>java.lang.Throwable</exception-type> <exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location> <location>/error.jsp</location>

View File

@ -73,11 +73,6 @@
</p> </p>
</c:if> </c:if>
</div> </div>
<!-- This comment is used by the TestProxy class
@startuml
Bob -> Alice : hello
@enduml
-->
<%-- FOOTER --%> <%-- FOOTER --%>
<%@ include file="footer.jspf" %> <%@ include file="footer.jspf" %>
</body> </body>

View File

@ -0,0 +1,9 @@
This file is used by the TestProxy unit test. It contains 2 diagrams description.
@startuml
Bob -> Alice : hello
@enduml
@startuml
version
@enduml

View File

@ -0,0 +1,67 @@
package net.sourceforge.plantuml.servlet;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
public class TestOldProxy extends WebappTestCase {
/**
* Verifies the proxified reception of the default Bob and Alice diagram
*/
public void testDefaultProxy() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/" + getServerUrl() + "resource/test2diagrams.txt");
WebResponse response = conversation.getResource(request);
// Analyze response
// Verifies the Content-Type header
// assertEquals( "Response content type is not PNG", "image/png", response.getContentType());
// Get the image and verify its size (~2000 bytes)
InputStream responseStream = response.getInputStream();
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while ((n = responseStream.read(buf)) != -1) {
imageStream.write(buf, 0, n);
}
imageStream.close();
responseStream.close();
byte[] inMemoryImage = imageStream.toByteArray();
int diagramLen = inMemoryImage.length;
assertTrue(diagramLen > 1500);
assertTrue(diagramLen < 2500);
}
public void testProxyWithFormat() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/svg/" + getServerUrl() + "resource/test2diagrams.txt");
WebResponse response = conversation.getResource(request);
// Analyze response
// Verifies the Content-Type header
// TODO assertEquals( "Response content type is not SVG", "image/svg+xml", response.getContentType());
// Get the content and verify its size
String diagram = response.getText();
int diagramLen = diagram.length();
assertTrue(diagramLen > 1000);
assertTrue(diagramLen < 3000);
}
/**
* Verifies that the HTTP header of a diagram incites the browser to cache it.
*/
public void testInvalidUrl() throws Exception {
WebConversation conversation = new WebConversation();
// Try to proxify an invalid address
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/invalidURL");
WebResponse response = conversation.getResource(request);
// Analyze response, it must be the empty form
// Verifies the Content-Type header
assertEquals("Response content type is not HTML", "text/html", response.getContentType());
WebForm forms[] = response.getForms();
assertEquals(2, forms.length);
}
}

View File

@ -5,7 +5,6 @@ import java.io.InputStream;
import com.meterware.httpunit.GetMethodWebRequest; import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse; import com.meterware.httpunit.WebResponse;
@ -15,11 +14,12 @@ public class TestProxy extends WebappTestCase {
*/ */
public void testDefaultProxy() throws Exception { public void testDefaultProxy() throws Exception {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/" + getServerUrl() + "welcome"); WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "proxy?src=" + getServerUrl() + "resource/test2diagrams.txt");
WebResponse response = conversation.getResource(request); WebResponse response = conversation.getResource(request);
// Analyze response // Analyze response
// Verifies the Content-Type header // Verifies the Content-Type header
// assertEquals( "Response content type is not PNG", "image/png", response.getContentType()); assertEquals( "Response content type is not PNG", "image/png", response.getContentType());
// Get the image and verify its size (~2000 bytes) // Get the image and verify its size (~2000 bytes)
InputStream responseStream = response.getInputStream(); InputStream responseStream = response.getInputStream();
ByteArrayOutputStream imageStream = new ByteArrayOutputStream(); ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
@ -35,14 +35,15 @@ public class TestProxy extends WebappTestCase {
assertTrue(diagramLen > 1500); assertTrue(diagramLen > 1500);
assertTrue(diagramLen < 2500); assertTrue(diagramLen < 2500);
} }
/*
public void testProxyWithFormat() throws Exception { public void testProxyWithFormat() throws Exception {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/svg/" + getServerUrl() + "welcome"); WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "proxy?format=svg&src=" + getServerUrl() + "resource/test2diagrams.txt");
WebResponse response = conversation.getResource(request); WebResponse response = conversation.getResource(request);
// Analyze response // Analyze response
// Verifies the Content-Type header // Verifies the Content-Type header
// TODO assertEquals( "Response content type is not SVG", "image/svg+xml", response.getContentType()); assertEquals( "Response content type is not SVG", "image/svg+xml", response.getContentType());
// Get the content and verify its size // Get the content and verify its size
String diagram = response.getText(); String diagram = response.getText();
int diagramLen = diagram.length(); int diagramLen = diagram.length();
@ -56,12 +57,9 @@ public class TestProxy extends WebappTestCase {
public void testInvalidUrl() throws Exception { public void testInvalidUrl() throws Exception {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
// Try to proxify an invalid address // Try to proxify an invalid address
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/invalidURL"); WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy?src=invalidURL");
WebResponse response = conversation.getResource(request); WebResponse response = conversation.getResource(request);
// Analyze response, it must be the empty form // Analyze response, it must be HTTP error 500
// Verifies the Content-Type header assertEquals("Bad HTTP status received", 500, response.getResponseCode());
assertEquals("Response content type is not HTML", "text/html", response.getContentType());
WebForm forms[] = response.getForms();
assertEquals(2, forms.length);
} }
} }