Commit d7ecaaa9 authored by Maxime Sinclair's avatar Maxime Sinclair
Browse files

Refactoring of the Proxy service

parent cbb6eb0a
/* ========================================================================
* 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 ProxyServlet 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()));
}
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;
}
}
......@@ -3,10 +3,10 @@
<p>This package is in charge of the JEE PlantUml Server.</p>
<p>there are 2 kind of servlets in this package :<br>
- Interactive servlets : Welcome, PlantUmlServlet that are in charge of the web pages dedicated to human users.<br>
- Service servlets : ImgServlet, SvgServlet that only produce a diagram as output.<br>
- Service servlets : ImgServlet, SvgServlet, AsciiServlet, ProxyServlet that only produce a diagram as output.<br>
<br>
Structure of the service part of the PlantUmlServer: <br>
<img src="http://www.plantuml.com/plantuml/img/XP3DJiCm48JlaV8EUmnIymPSAg723KJ40pZsDbd9ZbtlEX8gl3laXv2q_1mz-dPcF2qP17H1Ni6Xgp5odhLhJLfljjgHq0wIgbcYqWBQAcPuSVQEL1ELgp3sf17EUGOGKcr9G-_9WF7tACM3I1WGY_ACfuGi44yxsCWSVCS8aSFDOB94pMwLHEeQQ50gdwB6uaj9aRO71x9uyD4f6UZ79279z2u-mVSycyhFpPVWiVg5MFnSSRVEE8xfusSPEpCxVDTpTafT-gqiuVQjBAzdpBFhPKVogMlcor-HglyNsRCc-WFovUKE7m00" />
<img src="http://www.plantuml.com/plantuml/img/XP11ReCm44NtFiKiYHHkq4sbAEskLL4EOCm4HhAnCnwZHcdltb0eXIHjbdoU_d-UXYe4T46EuD2Lo54ryqWQNosYFQ4z9xgCmje86AjZX9sp6z8rQrSWTsGGJWeWfDAIWSvJ1PL_GicUHK9FM9GtF2IC0dnOmoAEmRSHWMDOdJkceR6i4AQsXXgby8aDPZz9ObhmMxAuzUoqzF1Baf1a-CvMwbCDtgFqDDfRNsJPRRlrV63VBty_Cs9RZGM_PixsYvFPh0yUFlBy-dh_iA_ituzZegxFFZ1jVOSekdl4OpH4VpC-lxoJcflT-Fy5" />
</p>
</body>
</html>
\ No newline at end of file
......@@ -27,6 +27,10 @@
<servlet-name>asciiservlet</servlet-name>
<servlet-class>net.sourceforge.plantuml.servlet.AsciiServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>proxyservlet</servlet-name>
<servlet-class>net.sourceforge.plantuml.servlet.ProxyServlet</servlet-class>
</servlet>
<!-- Patterns of the servlet -->
<servlet-mapping>
<servlet-name>welcome</servlet-name>
......@@ -57,7 +61,7 @@
<url-pattern>/start/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>plantumlservlet</servlet-name>
<servlet-name>proxyservlet</servlet-name>
<url-pattern>/proxy/*</url-pattern>
</servlet-mapping>
<error-page>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment