[TASK] Full coding style clean up
This commit is contained in:
@@ -33,12 +33,12 @@ import net.sourceforge.plantuml.FileFormat;
|
||||
public class AsciiServlet extends UmlDiagramService {
|
||||
|
||||
@Override
|
||||
public String getSource( String uri) {
|
||||
public String getSource(String uri) {
|
||||
String[] result = uri.split("/txt/", 2);
|
||||
if (result.length != 2) {
|
||||
return "";
|
||||
} else {
|
||||
return result[1];
|
||||
return result[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,10 +35,9 @@ 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.
|
||||
/**
|
||||
* 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;
|
||||
@@ -52,31 +51,29 @@ class DiagramResponse {
|
||||
contentType = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
DiagramResponse( HttpServletResponse r, FileFormat f) {
|
||||
DiagramResponse(HttpServletResponse r, FileFormat f) {
|
||||
response = r;
|
||||
format = f;
|
||||
}
|
||||
|
||||
|
||||
void sendDiagram( String uml) throws IOException {
|
||||
|
||||
void sendDiagram(String uml) throws IOException {
|
||||
long today = System.currentTimeMillis();
|
||||
if ( StringUtils.isDiagramCacheable( uml)) {
|
||||
if (StringUtils.isDiagramCacheable(uml)) {
|
||||
// Add http headers to force the browser to cache the image
|
||||
response.addDateHeader( "Expires", today + 31536000000L);
|
||||
response.addDateHeader("Expires", today + 31536000000L);
|
||||
// today + 1 year
|
||||
response.addDateHeader( "Last-Modified", 1261440000000L);
|
||||
response.addDateHeader("Last-Modified", 1261440000000L);
|
||||
// 2009 dec 22 constant date in the past
|
||||
response.addHeader( "Cache-Control", "public");
|
||||
response.addHeader("Cache-Control", "public");
|
||||
}
|
||||
response.setContentType( getContentType());
|
||||
SourceStringReader reader = new SourceStringReader( uml);
|
||||
reader.generateImage( response.getOutputStream(), new FileFormatOption(format));
|
||||
response.setContentType(getContentType());
|
||||
SourceStringReader reader = new SourceStringReader(uml);
|
||||
reader.generateImage(response.getOutputStream(), new FileFormatOption(format));
|
||||
response.flushBuffer();
|
||||
}
|
||||
|
||||
|
||||
private String getContentType() {
|
||||
return contentType.get( format);
|
||||
return contentType.get(format);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -33,12 +33,12 @@ import net.sourceforge.plantuml.FileFormat;
|
||||
public class ImgServlet extends UmlDiagramService {
|
||||
|
||||
@Override
|
||||
public String getSource( String uri) {
|
||||
public String getSource(String uri) {
|
||||
String[] result = uri.split("/img/", 2);
|
||||
if (result.length != 2) {
|
||||
return "";
|
||||
} else {
|
||||
return result[1];
|
||||
return result[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,8 @@ import HTTPClient.ParseException;
|
||||
public class PlantUmlServlet extends HttpServlet {
|
||||
|
||||
private static final Pattern urlPattern = Pattern.compile(".*/(.*)"); // Last part of the URL
|
||||
private static final Pattern encodedPattern = Pattern.compile("^[a-zA-Z0-9\\-\\_]+$"); // Format of a compressed diagram
|
||||
private static final Pattern encodedPattern = Pattern.compile("^[a-zA-Z0-9\\-\\_]+$"); // Format of a compressed
|
||||
// diagram
|
||||
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/(.*)");
|
||||
|
||||
@@ -54,10 +54,9 @@ 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 {
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
|
||||
final String uri = request.getRequestURI();
|
||||
|
||||
@@ -76,15 +75,15 @@ public class ProxyServlet extends HttpServlet {
|
||||
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 {
|
||||
private String getSource(String uri) throws IOException {
|
||||
CookieModule.setCookiePolicyHandler(null);
|
||||
|
||||
final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
|
||||
@@ -105,7 +104,7 @@ public class ProxyServlet extends HttpServlet {
|
||||
}
|
||||
|
||||
private FileFormat getOutputFormat() {
|
||||
if (format==null) {
|
||||
if (format == null) {
|
||||
return FileFormat.PNG;
|
||||
}
|
||||
if (format.equals("svg")) {
|
||||
@@ -115,6 +114,6 @@ public class ProxyServlet extends HttpServlet {
|
||||
return FileFormat.ATXT;
|
||||
}
|
||||
return FileFormat.PNG;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,12 +33,12 @@ import net.sourceforge.plantuml.FileFormat;
|
||||
public class SvgServlet extends UmlDiagramService {
|
||||
|
||||
@Override
|
||||
public String getSource( String uri) {
|
||||
public String getSource(String uri) {
|
||||
String[] result = uri.split("/svg/", 2);
|
||||
if (result.length != 2) {
|
||||
return "";
|
||||
} else {
|
||||
return result[1];
|
||||
return result[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,60 +37,60 @@ 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.
|
||||
* 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 {
|
||||
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");
|
||||
String text = URLDecoder.decode(getSource(request.getRequestURI()), "UTF-8");
|
||||
Transcoder transcoder = getTranscoder();
|
||||
text = transcoder.decode(text);
|
||||
|
||||
// encapsulate the UML syntax if necessary
|
||||
|
||||
// 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("@startuml\n");
|
||||
plantUmlSource.append(text);
|
||||
if (text.endsWith("\n") == false) {
|
||||
plantUmlSource.append("\n");
|
||||
}
|
||||
plantUmlSource.append( "@enduml");
|
||||
plantUmlSource.append("@enduml");
|
||||
uml = plantUmlSource.toString();
|
||||
}
|
||||
|
||||
// generate the response
|
||||
DiagramResponse dr = new DiagramResponse( response, getOutputFormat());
|
||||
DiagramResponse dr = new DiagramResponse(response, getOutputFormat());
|
||||
try {
|
||||
dr.sendDiagram(uml);
|
||||
} catch (IIOException iioe) {
|
||||
// Browser has closed the connection, do nothing
|
||||
// Browser has closed the connection, do nothing
|
||||
}
|
||||
dr = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extracts the compressed UML source from the HTTP URI.
|
||||
* @param uri the complete URI as returned by request.getRequestURI()
|
||||
*
|
||||
* @param uri
|
||||
* the complete URI as returned by request.getRequestURI()
|
||||
* @return the compressed UML source
|
||||
*/
|
||||
abstract public String getSource( String uri);
|
||||
|
||||
abstract public String getSource(String uri);
|
||||
|
||||
/**
|
||||
* Gives the wished output format of the diagram.
|
||||
* This value is used by the DiagramResponse class.
|
||||
* 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();
|
||||
}
|
||||
|
||||
@@ -38,17 +38,16 @@ import javax.servlet.http.HttpServletResponse;
|
||||
@SuppressWarnings("serial")
|
||||
public class Welcome extends HttpServlet {
|
||||
|
||||
@Override
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException {
|
||||
@Override
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
|
||||
// set the sample
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user