Project fully mavenized with functionality exceeding previous Ant build.xml implemented (except deployment to servlet container, which can be easily added).

Improved unit tests to be independent of externally running servlet container, now every test runs its own embedded jetty server (and stops it afterward).

Removed all eclipse artifacts (.project, .classpath, .settings, etc.) and added to .gitignore to be independent of development environment (can be autogenerated by maven, or better yet use m2eclipse).

Removed embedded dependency jars since they are handled automatically by maven.
This commit is contained in:
Pablo Lalloni
2011-11-02 19:11:52 -03:00
parent 4f80244a2b
commit 89c4d91b41
45 changed files with 202 additions and 259 deletions

View File

@@ -0,0 +1,50 @@
/* ========================================================================
* 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 net.sourceforge.plantuml.FileFormat;
/*
* ASCII servlet of the webapp.
* This servlet produces the UML sequence diagram in text format.
*/
@SuppressWarnings("serial")
public class AsciiServlet extends UmlDiagramService {
@Override
public String getSource( String uri) {
String[] result = uri.split("/txt/", 2);
if (result.length != 2) {
return "";
} else {
return result[1];
}
}
@Override
public FileFormat getOutputFormat() {
return FileFormat.ATXT;
}
}

View File

@@ -0,0 +1,82 @@
/* ========================================================================
* 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.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.StringUtils;
/**
* Delegates the diagram generation from the UML source and
* the filling of the HTTP response with the diagram in the right format.
* Its own responsibility is to produce the right HTTP headers.
*/
class DiagramResponse {
private HttpServletResponse response;
private FileFormat format;
private static final Map<FileFormat, String> contentType;
static {
Map<FileFormat, String> map = new HashMap<FileFormat, String>();
map.put(FileFormat.PNG, "image/png");
map.put(FileFormat.SVG, "image/svg+xml");
map.put(FileFormat.ATXT, "text/plain");
contentType = Collections.unmodifiableMap(map);
}
DiagramResponse( HttpServletResponse r, FileFormat f) {
response = r;
format = f;
}
void sendDiagram( String uml) throws IOException {
long today = System.currentTimeMillis();
if ( StringUtils.isDiagramCacheable( uml)) {
// Add http headers to force the browser to cache the image
response.addDateHeader( "Expires", today + 31536000000L);
// today + 1 year
response.addDateHeader( "Last-Modified", 1261440000000L);
// 2009 dec 22 constant date in the past
response.addHeader( "Cache-Control", "public");
}
response.setContentType( getContentType());
SourceStringReader reader = new SourceStringReader( uml);
reader.generateImage( response.getOutputStream(), new FileFormatOption(format));
response.flushBuffer();
}
private String getContentType() {
return contentType.get( format);
}
}

View File

@@ -0,0 +1,50 @@
/* ========================================================================
* 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 net.sourceforge.plantuml.FileFormat;
/*
* Image servlet of the webapp.
* This servlet produces the UML diagram in PNG format.
*/
@SuppressWarnings("serial")
public class ImgServlet extends UmlDiagramService {
@Override
public String getSource( String uri) {
String[] result = uri.split("/img/", 2);
if (result.length != 2) {
return "";
} else {
return result[1];
}
}
@Override
public FileFormat getOutputFormat() {
return FileFormat.PNG;
}
}

View File

@@ -0,0 +1,227 @@
/* ========================================================================
* 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.net.URLDecoder;
import java.util.StringTokenizer;
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 net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.code.Transcoder;
import net.sourceforge.plantuml.code.TranscoderUtil;
import HTTPClient.CookieModule;
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.ModuleException;
import HTTPClient.ParseException;
/*
* Original idea from Achim Abeling for Confluence macro
* See http://www.banapple.de/display/BANAPPLE/plantuml+user+macro
*
* Modified by Arnaud Roques
* Modified by Pablo Lalloni
* Packaged by Maxime Sinclair
*
*/
@SuppressWarnings("serial")
public class PlantUmlServlet extends HttpServlet {
private static final Pattern startumlPattern = Pattern.compile("/\\w+/start/(.*)");
private static final Pattern proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
private static final Pattern oldStartumlPattern = Pattern.compile("/\\w+/uml/startuml/(.*)");
private static final Pattern oldImagePattern = Pattern.compile("/\\w+/uml/image/(.*)");
private static final Pattern oldProxyPattern = Pattern.compile("/\\w+/uml/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
final String uri = request.getRequestURI();
Matcher startumlMatcher = startumlPattern.matcher(uri);
Matcher proxyMatcher = proxyPattern.matcher(uri);
Matcher oldStartumlMatcher = oldStartumlPattern.matcher(uri);
Matcher oldImageMatcher = oldImagePattern.matcher(uri);
Matcher oldProxyMatcher = oldProxyPattern.matcher(uri);
if (startumlMatcher.matches()) {
String source = startumlMatcher.group(1);
handleImage(response, source, uri);
} else if (proxyMatcher.matches()) {
String num = proxyMatcher.group(2);
String format = proxyMatcher.group(4);
String source = proxyMatcher.group(5);
handleImageProxy(response, num, source, format, uri);
} else if (oldStartumlMatcher.matches()) {
String source = oldStartumlMatcher.group(1);
handleImage(response, source, uri);
} else if (oldImageMatcher.matches()) {
String source = oldImageMatcher.group(1);
handleImageDecompress(response, source, uri);
} else if (oldProxyMatcher.matches()) {
String num = oldProxyMatcher.group(2);
String format = oldProxyMatcher.group(4);
String source = oldProxyMatcher.group(5);
handleImageProxy(response, num, source, format, uri);
} else {
doPost(request, response);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String text = request.getParameter("text");
String url = request.getParameter("url");
String encoded = "";
Transcoder transcoder = getTranscoder();
// the URL form has been submitted
if (url != null && !url.trim().isEmpty()) {
// TODO Verify the url is correct
Pattern p = Pattern.compile(".*/(.*)");
Matcher m = p.matcher(url);
if (m.find()) {
url = m.group(1);
text = transcoder.decode(url);
}
}
// the Text form has been submitted
if (text != null && !text.trim().isEmpty()) {
encoded = transcoder.encode(text);
}
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", text);
request.setAttribute("net.sourceforge.plantuml.servlet.encoded", encoded);
// forward to index.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}
private Transcoder getTranscoder() {
return TranscoderUtil.getDefaultTranscoder();
}
private void handleImage(HttpServletResponse response, String source, String uri)
throws IOException {
source = URLDecoder.decode(source, "UTF-8");
StringBuilder plantUmlSource = new StringBuilder();
StringTokenizer tokenizer = new StringTokenizer(source, "/@");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
plantUmlSource.append(token).append("\n");
}
sendImage(response, plantUmlSource.toString(), uri);
}
private void handleImageDecompress(HttpServletResponse response,
String source, String uri) throws IOException {
source = URLDecoder.decode(source, "UTF-8");
Transcoder transcoder = getTranscoder();
String text2 = transcoder.decode(source);
sendImage(response, text2, uri);
}
private void handleImageProxy(HttpServletResponse response, String num,
String source, String format, String uri) throws IOException {
SourceStringReader reader = new SourceStringReader( getContent(source));
int n = num == null ? 0 : Integer.parseInt(num);
// Write the requested image to "os"
if (format != null) {
reader.generateImage(response.getOutputStream(), n, new FileFormatOption(FileFormat.valueOf(format)));
} else {
reader.generateImage(response.getOutputStream(), n);
}
}
private void sendImage(HttpServletResponse response, String text, String uri)
throws IOException {
final String uml;
if (text.startsWith("@startuml")) {
uml = text;
} else {
StringBuilder plantUmlSource = new StringBuilder();
plantUmlSource.append("@startuml\n");
plantUmlSource.append(text);
if (text.endsWith("\n") == false) {
plantUmlSource.append("\n");
}
plantUmlSource.append("@enduml");
uml = plantUmlSource.toString();
}
// Write the first image to "os"
long today = System.currentTimeMillis();
if ( StringUtils.isDiagramCacheable( uml)) {
// Add http headers to force the browser to cache the image
response.addDateHeader("Expires", today + 31536000000L);
// today + 1 year
response.addDateHeader("Last-Modified", 1261440000000L);
// 2009 dec 22 constant date in the past
response.addHeader("Cache-Control", "public");
}
response.setContentType("image/png");
SourceStringReader reader = new SourceStringReader(uml);
reader.generateImage(response.getOutputStream(), new FileFormatOption(FileFormat.PNG));
response.flushBuffer();
}
private String getContent(String adress) throws IOException {
// HTTPConnection.setProxyServer("proxy", 8080);
CookieModule.setCookiePolicyHandler(null);
final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
final Matcher m = p.matcher(adress);
if (m.find() == false) {
throw new IOException(adress);
}
final URL url = new URL(adress);
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());
}
}
}

View File

@@ -0,0 +1,50 @@
/* ========================================================================
* 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 net.sourceforge.plantuml.FileFormat;
/*
* SVG servlet of the webapp.
* This servlet produces the UML diagram in SVG format.
*/
@SuppressWarnings("serial")
public class SvgServlet extends UmlDiagramService {
@Override
public String getSource( String uri) {
String[] result = uri.split("/svg/", 2);
if (result.length != 2) {
return "";
} else {
return result[1];
}
}
@Override
public FileFormat getOutputFormat() {
return FileFormat.SVG;
}
}

View File

@@ -0,0 +1,92 @@
/* ========================================================================
* 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.URLDecoder;
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.code.Transcoder;
import net.sourceforge.plantuml.code.TranscoderUtil;
/**
* Common service servlet to produce diagram from compressed UML source
* contained in the end part of the requested URI.
*/
@SuppressWarnings("serial")
public abstract class UmlDiagramService extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// build the UML source from the compressed request parameter
String text = URLDecoder.decode( getSource(request.getRequestURI()), "UTF-8");
Transcoder transcoder = getTranscoder();
text = transcoder.decode(text);
// encapsulate the UML syntax if necessary
String uml;
if (text.startsWith("@start")) {
uml = text;
} else {
StringBuilder plantUmlSource = new StringBuilder();
plantUmlSource.append( "@startuml\n");
plantUmlSource.append( text);
if (text.endsWith( "\n") == false) {
plantUmlSource.append( "\n");
}
plantUmlSource.append( "@enduml");
uml = plantUmlSource.toString();
}
// generate the response
DiagramResponse dr = new DiagramResponse( response, getOutputFormat());
dr.sendDiagram(uml);
dr = null;
}
/**
* Extracts the compressed UML source from the HTTP URI.
* @param uri the complete URI as returned by request.getRequestURI()
* @return the compressed UML source
*/
abstract public String getSource( String uri);
/**
* Gives the wished output format of the diagram.
* This value is used by the DiagramResponse class.
* @return the format
*/
abstract public FileFormat getOutputFormat();
private Transcoder getTranscoder() {
return TranscoderUtil.getDefaultTranscoder();
}
}

View File

@@ -0,0 +1,54 @@
/* ========================================================================
* 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 javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* Welcome servlet of the webapp.
* Displays the sample Bob and Alice sequence diagram.
*/
@SuppressWarnings("serial")
public class Welcome extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// set the sample
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", "Bob -> Alice : hello");
request.setAttribute("net.sourceforge.plantuml.servlet.encoded", "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000");
// forward to index.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}
}

View File

@@ -0,0 +1,12 @@
<html>
<body>
<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>
<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" />
</p>
</body>
</html>