From 5c149977fbd88790741e76353c5dd226538c746f Mon Sep 17 00:00:00 2001 From: Maxime Sinclair Date: Wed, 9 Mar 2011 16:55:57 +0100 Subject: [PATCH 1/6] Refactoring of the welcome diagram. --- content/WEB-INF/web.xml | 10 ++++++- content/index.jsp | 6 ++-- .../sourceforge/plantuml/servlet/Welcome.java | 30 +++++++++++++++++++ .../plantuml/servlet/TestForm.java | 12 ++++---- 4 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/net/sourceforge/plantuml/servlet/Welcome.java diff --git a/content/WEB-INF/web.xml b/content/WEB-INF/web.xml index fa2c97b..e24540f 100644 --- a/content/WEB-INF/web.xml +++ b/content/WEB-INF/web.xml @@ -5,7 +5,15 @@ plantumlservlet net.sourceforge.plantuml.servlet.PlantUmlServlet + + welcome + net.sourceforge.plantuml.servlet.Welcome + + + welcome + /welcome + plantumlservlet /uml/* @@ -27,7 +35,7 @@ /proxy/* - index.jsp + welcome java.lang.Throwable diff --git a/content/index.jsp b/content/index.jsp index fea6937..cf8fe7a 100644 --- a/content/index.jsp +++ b/content/index.jsp @@ -14,10 +14,12 @@ if (encodedAttribute != null) { } } Object decodedAttribute = request.getAttribute("net.sourceforge.plantuml.servlet.decoded"); -if (decodedAttribute == null) { +/*if (decodedAttribute == null) { umltext = "Bob -> Alice : hello"; imgurl = host + contextRoot + "/img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"; -} else { +} else { */ + +if (decodedAttribute != null) { umltext = decodedAttribute.toString(); } %> diff --git a/src/net/sourceforge/plantuml/servlet/Welcome.java b/src/net/sourceforge/plantuml/servlet/Welcome.java new file mode 100644 index 0000000..0b1cc84 --- /dev/null +++ b/src/net/sourceforge/plantuml/servlet/Welcome.java @@ -0,0 +1,30 @@ +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. + */ +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); + } + +} diff --git a/test/src/net/sourceforge/plantuml/servlet/TestForm.java b/test/src/net/sourceforge/plantuml/servlet/TestForm.java index 5953eb6..d8811dd 100644 --- a/test/src/net/sourceforge/plantuml/servlet/TestForm.java +++ b/test/src/net/sourceforge/plantuml/servlet/TestForm.java @@ -73,7 +73,7 @@ public class TestForm extends TestCase { public void testEmptyUrl() throws Exception { WebConversation conversation = new WebConversation(); // Fill the form and submit it - WebRequest request = new GetMethodWebRequest( "http://localhost/plantuml/" ); + WebRequest request = new GetMethodWebRequest( TestUtils.getServerUrl() ); WebResponse response = TestUtils.tryGetResponse(conversation, request ); WebForm formUrl = response.getForms()[1]; formUrl.setParameter("url", ""); @@ -82,11 +82,11 @@ public class TestForm extends TestCase { WebForm forms[] = response.getForms(); assertEquals( 2, forms.length ); // Ensure the Text field is empty - assertTrue( forms[0].getParameterValue("text").startsWith("Bob")); - // Ensure the URL field is correct - assertTrue( forms[1].getParameterValue("url").endsWith("/img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000")); - // Ensure the image is present - assertEquals( 1, response.getImages().length); + assertNull( forms[0].getParameterValue("text")); + // Ensure the URL field is empty + assertTrue( forms[1].getParameterValue("url").isEmpty()); + // Ensure there is no image + assertEquals( 0, response.getImages().length); } } From 57425f770de585e3a6852f99297840a42ed7a2e1 Mon Sep 17 00:00:00 2001 From: Maxime Sinclair Date: Thu, 10 Mar 2011 15:54:36 +0100 Subject: [PATCH 2/6] Prevent the creation of the session. --- content/index.jsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/index.jsp b/content/index.jsp index cf8fe7a..dbed1ec 100644 --- a/content/index.jsp +++ b/content/index.jsp @@ -1,4 +1,4 @@ -<%@ page info="index" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> +<%@ page info="index" contentType="text/html; charset=utf-8" pageEncoding="utf-8" session="false" %> <% String contextRoot = request.getContextPath(); From 9e55d93ab8b1a5c1277c243e856a310abaf74fb9 Mon Sep 17 00:00:00 2001 From: Maxime Sinclair Date: Thu, 10 Mar 2011 15:57:37 +0100 Subject: [PATCH 3/6] getContent() now private. --- src/net/sourceforge/plantuml/servlet/PlantUmlServlet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/sourceforge/plantuml/servlet/PlantUmlServlet.java b/src/net/sourceforge/plantuml/servlet/PlantUmlServlet.java index d49fee5..ee0a45c 100644 --- a/src/net/sourceforge/plantuml/servlet/PlantUmlServlet.java +++ b/src/net/sourceforge/plantuml/servlet/PlantUmlServlet.java @@ -170,7 +170,7 @@ public class PlantUmlServlet extends HttpServlet { response.flushBuffer(); } - public String getContent(String adress) throws IOException { + private String getContent(String adress) throws IOException { // HTTPConnection.setProxyServer("proxy", 8080); CookieModule.setCookiePolicyHandler(null); From 4eb0dfc879c94a503f5c4da82d20ee10bdbfbe53 Mon Sep 17 00:00:00 2001 From: Maxime Sinclair Date: Thu, 10 Mar 2011 16:17:30 +0100 Subject: [PATCH 4/6] Test of the HTTP header added. --- .../plantuml/servlet/TestImage.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/src/net/sourceforge/plantuml/servlet/TestImage.java b/test/src/net/sourceforge/plantuml/servlet/TestImage.java index 746df00..ab4e936 100644 --- a/test/src/net/sourceforge/plantuml/servlet/TestImage.java +++ b/test/src/net/sourceforge/plantuml/servlet/TestImage.java @@ -5,6 +5,9 @@ import com.meterware.httpunit.*; import java.io.ByteArrayOutputStream; import java.io.InputStream; +import java.util.Date; +import java.util.Locale; +import java.text.SimpleDateFormat; public class TestImage extends TestCase { /** @@ -16,7 +19,7 @@ public class TestImage extends TestCase { WebResponse response = conversation.getResource( request); // Analyze response // Verifies the Content-Type header - assertEquals( "image/png", response.getContentType()); + assertEquals( "Response content type is not PNG", "image/png", response.getContentType()); // Get the image and verify its size InputStream responseStream = response.getInputStream(); ByteArrayOutputStream imageStream = new ByteArrayOutputStream(); @@ -32,4 +35,25 @@ public class TestImage extends TestCase { assertTrue( diagramLen > 10000); assertTrue( diagramLen < 20000); } + + /** + * Verifies that the HTTP header of a diagram incites the browser to cache it. + */ + public void testDiagramHttpHeader() throws Exception { + WebConversation conversation = new WebConversation(); + // Bob -> Alice : hello + WebRequest request = new GetMethodWebRequest( TestUtils.getServerUrl()+"img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"); + WebResponse response = conversation.getResource( request); + // Analyze response + // Verifies the Content-Type header + assertEquals( "Response content type is not PNG", "image/png", response.getContentType()); + // Verifies the availability of the Expires entry in the response header + assertNotNull( response.getHeaderField( "Expires")); + // Verifies the availability of the Last-Modified entry in the response header + assertNotNull( response.getHeaderField( "Last-Modified")); + // Verifies the Last-Modified value is in the past + SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZ", Locale.ENGLISH); + Date lastModified = format.parse( response.getHeaderField( "Last-Modified")); + assertTrue( "Last-Modified is not in the past", lastModified.before( new Date())); + } } From b924e9936081bd7d6e887d1af6547f8820753227 Mon Sep 17 00:00:00 2001 From: Maxime Sinclair Date: Fri, 18 Mar 2011 16:29:22 +0100 Subject: [PATCH 5/6] Session creation avoided --- content/error.jsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/error.jsp b/content/error.jsp index 2a49c54..0582c9a 100644 --- a/content/error.jsp +++ b/content/error.jsp @@ -1,4 +1,4 @@ -<%@ page isErrorPage="true" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> +<%@ page isErrorPage="true" contentType="text/html; charset=utf-8" pageEncoding="utf-8" session="false" %> <% String contextRoot = request.getContextPath(); %> From 225ef45c370bc432c46c2369c81b1bd813a18ba2 Mon Sep 17 00:00:00 2001 From: Maxime Sinclair Date: Fri, 18 Mar 2011 16:35:45 +0100 Subject: [PATCH 6/6] Unit test of the proxy feature. --- content/index.jsp | 11 ++-- .../plantuml/servlet/AllTests.java | 1 + .../plantuml/servlet/TestProxy.java | 51 +++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 test/src/net/sourceforge/plantuml/servlet/TestProxy.java diff --git a/content/index.jsp b/content/index.jsp index dbed1ec..3a21b2a 100644 --- a/content/index.jsp +++ b/content/index.jsp @@ -14,11 +14,6 @@ if (encodedAttribute != null) { } } Object decodedAttribute = request.getAttribute("net.sourceforge.plantuml.servlet.decoded"); -/*if (decodedAttribute == null) { - umltext = "Bob -> Alice : hello"; - imgurl = host + contextRoot + "/img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"; -} else { */ - if (decodedAttribute != null) { umltext = decodedAttribute.toString(); } @@ -70,7 +65,11 @@ if (decodedAttribute != null) {

<% } //endif %> - + <%-- FOOTER <%@ include file="util/footer.jspf" %> --%> diff --git a/test/src/net/sourceforge/plantuml/servlet/AllTests.java b/test/src/net/sourceforge/plantuml/servlet/AllTests.java index ad6f081..ece24b5 100644 --- a/test/src/net/sourceforge/plantuml/servlet/AllTests.java +++ b/test/src/net/sourceforge/plantuml/servlet/AllTests.java @@ -10,6 +10,7 @@ public class AllTests extends TestSuite { //$JUnit-BEGIN$ suite.addTestSuite(TestForm.class); suite.addTestSuite(TestImage.class); + suite.addTestSuite(TestProxy.class); //$JUnit-END$ return suite; } diff --git a/test/src/net/sourceforge/plantuml/servlet/TestProxy.java b/test/src/net/sourceforge/plantuml/servlet/TestProxy.java new file mode 100644 index 0000000..b36d9b1 --- /dev/null +++ b/test/src/net/sourceforge/plantuml/servlet/TestProxy.java @@ -0,0 +1,51 @@ +package net.sourceforge.plantuml.servlet; + +import junit.framework.TestCase; +import com.meterware.httpunit.*; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +public class TestProxy extends TestCase { + /** + * Verifies the proxified reception of the default Bob and Alice diagram + */ + public void testDefaultProxy() throws Exception { + WebConversation conversation = new WebConversation(); + WebRequest request = new GetMethodWebRequest( TestUtils.getServerUrl()+"proxy/" + +TestUtils.getServerUrl()+"welcome"); + 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 (~1533 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 < 1600); + } + + /** + * 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( TestUtils.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 ); + } +}