From 13ac55e09ef3dd980fcb7d28c08c22ab3a521a3d Mon Sep 17 00:00:00 2001 From: Maxime Sinclair Date: Mon, 19 Aug 2013 16:07:38 +0200 Subject: [PATCH] [FEATURE] Map service totally implemented --- .../plantuml/servlet/DiagramResponse.java | 22 +++++--- .../sourceforge/plantuml/servlet/diagrams.txt | 31 ++++++----- .../sourceforge/plantuml/servlet/package.html | 3 ++ .../servlet/utility/NullOutputStream.java | 54 +++++++++++++++++++ .../plantuml/servlet/utility/package.html | 9 ++++ .../plantuml/servlet/AllTests.java | 1 + .../sourceforge/plantuml/servlet/TestMap.java | 7 +-- 7 files changed, 104 insertions(+), 23 deletions(-) create mode 100644 src/main/java/net/sourceforge/plantuml/servlet/utility/NullOutputStream.java create mode 100644 src/main/java/net/sourceforge/plantuml/servlet/utility/package.html diff --git a/src/main/java/net/sourceforge/plantuml/servlet/DiagramResponse.java b/src/main/java/net/sourceforge/plantuml/servlet/DiagramResponse.java index a0e02a0..930b72f 100644 --- a/src/main/java/net/sourceforge/plantuml/servlet/DiagramResponse.java +++ b/src/main/java/net/sourceforge/plantuml/servlet/DiagramResponse.java @@ -28,12 +28,14 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import javax.servlet.ServletOutputStream; 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.servlet.utility.NullOutputStream; /** * Delegates the diagram generation from the UML source and the filling of the HTTP response with the diagram in the @@ -47,7 +49,7 @@ class DiagramResponse { Map map = new HashMap(); map.put(FileFormat.PNG, "image/png"); map.put(FileFormat.SVG, "image/svg+xml"); - map.put(FileFormat.ATXT, "text/plain"); + map.put(FileFormat.ATXT, "text/plain;charset=ISO-8859-1"); contentType = Collections.unmodifiableMap(map); } @@ -67,14 +69,18 @@ class DiagramResponse { } void sendMap(String uml) throws IOException { - /* SourceStringReader reader = new SourceStringReader(uml); - String map = reader.generateImage(response.getOutputStream(), new FileFormatOption(FileFormat.PNG)); - response.flushBuffer(); - System.out.println( "map !!!" + map + "!!!"); + if (StringUtils.isDiagramCacheable(uml)) { + addHeaderForCache(); + } + response.setContentType(getContentType()); + SourceStringReader reader = new SourceStringReader(uml); + String map = reader.generateImage(new NullOutputStream(), new FileFormatOption(FileFormat.PNG)); String[] mapLines = map.split("[\\r\\n]"); - for (int i=2; (i+1) PngServlet : getSource() +PngServlet -> UmlExtractor : getUmlSource() +UmlExtractor --> PngServlet +PngServlet -> PngServlet : getOutputFormat() +PngServlet -> DiagramResponse : <> +PngServlet -> DiagramResponse : sendDiagram() +participant "PlantUML library" as Lib #99FF99 +DiagramResponse -> Lib : generateImage() +Lib --> DiagramResponse +DiagramResponse --> PngServlet @enduml \ No newline at end of file diff --git a/src/main/java/net/sourceforge/plantuml/servlet/package.html b/src/main/java/net/sourceforge/plantuml/servlet/package.html index 91061b9..c3c9bb3 100644 --- a/src/main/java/net/sourceforge/plantuml/servlet/package.html +++ b/src/main/java/net/sourceforge/plantuml/servlet/package.html @@ -8,5 +8,8 @@ Structure of the service part of the PlantUmlServer:

+

+ +

\ No newline at end of file diff --git a/src/main/java/net/sourceforge/plantuml/servlet/utility/NullOutputStream.java b/src/main/java/net/sourceforge/plantuml/servlet/utility/NullOutputStream.java new file mode 100644 index 0000000..1accbaf --- /dev/null +++ b/src/main/java/net/sourceforge/plantuml/servlet/utility/NullOutputStream.java @@ -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.utility; + +import java.io.IOException; +import java.io.OutputStream; + +public class NullOutputStream extends OutputStream { + + /** + * Writes to nowhere + */ + @Override + public void write(int b) throws IOException { + // Do nothing silently + } + + /** + * Overridden for performance reason + */ + @Override + public void write(byte b[]) throws IOException { + // Do nothing silently + } + + /** + * Overridden for performance reason + */ + @Override + public void write(byte b[], int off, int len) throws IOException { + // Do nothing silently + } +} diff --git a/src/main/java/net/sourceforge/plantuml/servlet/utility/package.html b/src/main/java/net/sourceforge/plantuml/servlet/utility/package.html new file mode 100644 index 0000000..570e7b4 --- /dev/null +++ b/src/main/java/net/sourceforge/plantuml/servlet/utility/package.html @@ -0,0 +1,9 @@ + + +

This package contains utility classes of the JEE PlantUml Server.

+
    +
  • NullOutputStream is used by the Map feature.
  • +
  • UmlExtractor encapsulates the PlantUML library to decode the UML compressed source.
  • +
+ + \ No newline at end of file diff --git a/src/test/java/net/sourceforge/plantuml/servlet/AllTests.java b/src/test/java/net/sourceforge/plantuml/servlet/AllTests.java index f32d9e8..e6db354 100644 --- a/src/test/java/net/sourceforge/plantuml/servlet/AllTests.java +++ b/src/test/java/net/sourceforge/plantuml/servlet/AllTests.java @@ -13,6 +13,7 @@ public class AllTests extends TestSuite { suite.addTestSuite(TestAsciiArt.class); suite.addTestSuite(TestSVG.class); suite.addTestSuite(TestProxy.class); + suite.addTestSuite(TestMap.class); // $JUnit-END$ return suite; } diff --git a/src/test/java/net/sourceforge/plantuml/servlet/TestMap.java b/src/test/java/net/sourceforge/plantuml/servlet/TestMap.java index 82e1f40..af7612d 100644 --- a/src/test/java/net/sourceforge/plantuml/servlet/TestMap.java +++ b/src/test/java/net/sourceforge/plantuml/servlet/TestMap.java @@ -18,7 +18,8 @@ public class TestMap extends WebappTestCase { WebResponse response = conversation.getResource(request); // Analyze response // Verifies the Content-Type header - assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType()); + assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType()); + assertEquals("Response character set is not ISO-8859-1", "ISO-8859-1", response.getCharacterSet()); // Get the content, check its first characters and verify its size String diagram = response.getText(); assertTrue("Response content is not starting with ]+)>)+")); // list of tags - assertTrue(map.matches(".*(area shape=\".+\" id=\".+\" href=\".+\").*")); // area structure + assertTrue("Response is not a list of tags", map.matches("(<([^<>]+)>)+")); + assertTrue("Response doesn't contain the area structure", map.matches(".*(area shape=\".+\" id=\".+\" href=\".+\").*")); } /**