[gatein-commits] gatein SVN: r5599 - in components/wsrp/trunk/consumer/src: test/java/org/gatein/wsrp/consumer/handlers and 3 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Dec 16 23:15:26 EST 2010


Author: mwringe
Date: 2010-12-16 23:15:25 -0500 (Thu, 16 Dec 2010)
New Revision: 5599

Modified:
   components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java
   components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandlerTestCase.java
   components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v1/MarkupTestCase.java
   components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v2/MarkupTestCase.java
   components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/test/support/TestPortletInvocationContext.java
Log:
GTNWSRP-187, GTNWSRP-144, GTNWSRP-186, GTNWSRP-183, GTNWSRP-185: Update how we handle url escaping, we need to make sure we are not changing the encoding type when recreating the portlet resource url. Multiple issues occur if we assume that everything needs to be xhtml escaped (especially things like css and javascript urls) Update tests.

Modified: components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java
===================================================================
--- components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java	2010-12-16 15:52:17 UTC (rev 5598)
+++ components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java	2010-12-17 04:15:25 UTC (rev 5599)
@@ -250,6 +250,7 @@
       private final Set<String> supportedCustomWindowStates;
       private final String namespace;
 
+      //TODO: the URLFormat here doesn't make any sense, the escaping needs to be unique for each url processed.
       protected MarkupProcessor(String namespace, PortletInvocationContext context, org.gatein.pc.api.PortletContext target, URLFormat format, ProducerInfo info)
       {
          this.namespace = namespace;
@@ -261,11 +262,45 @@
 
       public String getReplacementFor(String match, String prefix, String suffix)
       {
+         // We run into some issues with url encoding. We should not be making assumptions about
+         // what url encoding we should be using. For example, we may be dealing with html encoding (ampersand as &)
+         // or xhtml/xml encoding (ampersand as &amp;) or javascript encoding (ampersand as \x26).
+         // When we recreate the WSRP url as a portlet url, we have to use whatever encoding was used in the original wsrp url,
+         // we need to assume that is the correct encoding for the situation.
+
+         // NOTE: there may be other encoding situations we are not currently dealing with :(
+         
+         boolean useJavaScriptEscaping = false;  
          // work around for GTNWSRP-93:
-         match = match.replaceAll("\\\\x2D", "-").replaceAll("\\\\x26", "&amp;");
+         if (match.contains("\\x2D") || match.contains("\\x26"))
+         {
+            useJavaScriptEscaping = true;
+            match = match.replaceAll("\\\\x2D", "-").replaceAll("\\\\x26", "&amp;");
+         }
 
          WSRPPortletURL portletURL = WSRPPortletURL.create(match, supportedCustomModes, supportedCustomWindowStates, true);
-         return context.renderURL(portletURL, format);
+         
+         URLFormat urlFormat;
+         // If the current url is using &amp; then specify we want to use xml escaped ampersands
+         if (match.contains("&amp;"))
+         {
+            urlFormat = new URLFormat(format.getWantSecure(), format.getWantAuthenticated(), format.getWantRelative(), true);
+         }
+         else
+         {
+            urlFormat = new URLFormat(format.getWantSecure(), format.getWantAuthenticated(), format.getWantRelative(), false);
+         }
+         
+         String value = context.renderURL(portletURL, urlFormat);
+         
+         // we now need to add back the javascript url encoding if it was originally used
+         // NOTE: we should fix this by specifying the escaping to be used in URLFormat when it supported (see GTNPC-41)
+         if (useJavaScriptEscaping)
+         {
+            value = value.replaceAll("-", "\\\\x2D").replaceAll("&amp;", "\\\\x26");
+         }
+         
+         return value;
       }
    }
 }

Modified: components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandlerTestCase.java
===================================================================
--- components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandlerTestCase.java	2010-12-16 15:52:17 UTC (rev 5598)
+++ components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandlerTestCase.java	2010-12-17 04:15:25 UTC (rev 5599)
@@ -30,6 +30,7 @@
 import org.gatein.pc.api.invocation.PortletInvocation;
 import org.gatein.pc.api.invocation.response.ContentResponse;
 import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
+import org.gatein.wsrp.WSRPResourceURL;
 import org.gatein.wsrp.WSRPRewritingConstants;
 import org.gatein.wsrp.WSRPTypeFactory;
 import org.gatein.wsrp.test.ExtendedAssert;
@@ -43,6 +44,7 @@
 import org.oasis.wsrp.v2.ResourceContext;
 
 import java.io.UnsupportedEncodingException;
+import java.net.URL;
 
 /**
  * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
@@ -104,6 +106,29 @@
       processMarkupAndCheck(markup, expected);
    }
 
+   public void testURLEscaping() throws Exception
+   {
+      String markup;
+      String expected;
+      
+      String resourceID = WSRPResourceURL.encodeResource(null, new URL("http://localhost:8080/test-resource-portlet/gif/logo.gif"), false);
+      
+      //test with &amp;
+      markup = "<img src='wsrp_rewrite?wsrp-urlType=resource&amp;wsrp-url=http%3A%2F%2Flocalhost%3A8080%2Ftest-resource-portlet%2Fgif%2Flogo.gif&amp;wsrp-requiresRewrite=true/wsrp_rewrite'/>";
+      expected = "<img src='http://test/mock:type=resource?mock:ComponentID=foobar&amp;mock:resourceID=" + resourceID + "'/>";      
+      processMarkupAndCheck(markup, expected);
+      
+      //test with &
+      markup = "<img src='wsrp_rewrite?wsrp-urlType=resource&wsrp-url=http%3A%2F%2Flocalhost%3A8080%2Ftest-resource-portlet%2Fgif%2Flogo.gif&wsrp-requiresRewrite=true/wsrp_rewrite'/>";
+      expected = "<img src='http://test/mock:type=resource?mock:ComponentID=foobar&mock:resourceID=" + resourceID + "'/>";     
+      processMarkupAndCheck(markup, expected);
+      
+      //test with /x26
+      markup = "<img src='wsrp_rewrite?wsrp-urlType=resource\\x26wsrp-url=http%3A%2F%2Flocalhost%3A8080%2Ftest-resource-portlet%2Fgif%2Flogo.gif\\x26wsrp-requiresRewrite=true/wsrp_rewrite'/>";
+      expected = "<img src='http://test/mock:type=resource?mock:ComponentID=foobar\\x26mock:resourceID=" + resourceID + "'/>";     
+      processMarkupAndCheck(markup, expected);
+   }
+   
    /*public void testResourceURLs()
    {
       String markup;

Modified: components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v1/MarkupTestCase.java
===================================================================
--- components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v1/MarkupTestCase.java	2010-12-16 15:52:17 UTC (rev 5598)
+++ components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v1/MarkupTestCase.java	2010-12-17 04:15:25 UTC (rev 5599)
@@ -222,7 +222,7 @@
       PortletInvocationResponse response = consumer.invoke(render);
 
       String resourceID = WSRPResourceURL.encodeResource(null, new URL("http://localhost:8080/test-resource-portlet/gif/logo.gif"), false);
-      String expectedResult = "<img src='Resource id=" + resourceID + " ns=null ws=null m=null'/>";
+      String expectedResult = "<img src='http://test/mock:type=resource?mock:ComponentID=foobar&amp;mock:resourceID=" + resourceID + "'/>";
 
       //NOTE: the value we get back is from the TestPortletInvocationContext, not what we would normally receive
       checkRenderResult(response, expectedResult);

Modified: components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v2/MarkupTestCase.java
===================================================================
--- components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v2/MarkupTestCase.java	2010-12-16 15:52:17 UTC (rev 5598)
+++ components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v2/MarkupTestCase.java	2010-12-17 04:15:25 UTC (rev 5599)
@@ -225,7 +225,7 @@
       PortletInvocationResponse response = consumer.invoke(render);
 
       String resourceID = WSRPResourceURL.encodeResource(null, new URL("http://localhost:8080/test-resource-portlet/gif/logo.gif"), false);
-      String expectedResult = "<img src='Resource id=" + resourceID + " ns=null ws=null m=null'/>";
+      String expectedResult = "<img src='http://test/mock:type=resource?mock:ComponentID=foobar&amp;mock:resourceID=" + resourceID + "'/>";;
 
       //NOTE: the value we get back is from the TestPortletInvocationContext, not what we would normally receive
       checkRenderResult(response, expectedResult);

Modified: components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/test/support/TestPortletInvocationContext.java
===================================================================
--- components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/test/support/TestPortletInvocationContext.java	2010-12-16 15:52:17 UTC (rev 5598)
+++ components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/test/support/TestPortletInvocationContext.java	2010-12-17 04:15:25 UTC (rev 5599)
@@ -101,9 +101,24 @@
       {
          result = "Render";
       }
-      else
+      else //dealing with a resource
       {
-         result = "Resource id=" + ((ResourceURL)containerURL).getResourceId();
+         //fake setup which approximates what the actual PortletInvocationContext should be doing.
+         String url = "http://test/mock:type=resource?mock:ComponentID=foobar";
+         ResourceURL resourceURL = ((ResourceURL)containerURL);
+         
+         if (urlFormat.getWantEscapeXML())
+         {
+            url += "&amp;";
+         }
+         else
+         {
+            url += "&";  
+         }
+         
+         url += "mock:resourceID=" + resourceURL.getResourceId();
+         
+         return url;
       }
 
       StateString ns = containerURL.getNavigationalState();



More information about the gatein-commits mailing list