[gatein-commits] gatein SVN: r2132 - in components/wsrp/trunk: consumer/src/main/java/org/gatein/wsrp/consumer and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Mar 10 17:19:32 EST 2010


Author: chris.laprun at jboss.com
Date: 2010-03-10 17:19:31 -0500 (Wed, 10 Mar 2010)
New Revision: 2132

Modified:
   components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPResourceURL.java
   components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java
   components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java
   components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/RenderHandlerTestCase.java
   components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/WSRPPortletInvocationContext.java
Log:
- Added WSRPResourceURL.createAbsoluteURLFrom method for better code reuse.
- Reverted resource handling to previous behavior of not generating an actual resource URL but creating a URL
  that we might be able to work with (only works for static resources, of course).


Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPResourceURL.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPResourceURL.java	2010-03-10 22:11:47 UTC (rev 2131)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPResourceURL.java	2010-03-10 22:19:31 UTC (rev 2132)
@@ -25,6 +25,7 @@
 
 import org.gatein.common.net.URLTools;
 import org.gatein.common.net.media.MediaType;
+import org.gatein.common.util.ParameterValidation;
 import org.gatein.pc.api.Mode;
 import org.gatein.pc.api.PortletContext;
 import org.gatein.pc.api.ResourceURL;
@@ -55,10 +56,12 @@
    private boolean requiresRewrite = false;
    private URL resourceURL;
 
-   private static final Map<String, MediaType> SUPPORTED_RESOURCE_TYPES = new HashMap<String, MediaType>(8);
+   private static final Map<String, MediaType> SUPPORTED_RESOURCE_TYPES = new HashMap<String, MediaType>(11);
 
    static
    {
+      SUPPORTED_RESOURCE_TYPES.put("html", MediaType.TEXT_HTML);
+      SUPPORTED_RESOURCE_TYPES.put("htm", MediaType.TEXT_HTML);
       SUPPORTED_RESOURCE_TYPES.put("css", MediaType.TEXT_CSS);
       SUPPORTED_RESOURCE_TYPES.put("js", MediaType.TEXT_JAVASCRIPT);
       SUPPORTED_RESOURCE_TYPES.put("png", MediaType.create("image/png"));
@@ -203,19 +206,13 @@
     */
    public void buildURLWith(HttpServletRequest request, PortletContext portletContext)
    {
-      String url = URLTools.getServerAddressFrom(request) + URLTools.SLASH + portletContext.getApplicationName();
-
-      if (resourceId != null)
-      {
-         url += resourceId;
-      }
-
+      String url = createAbsoluteURLFrom(resourceId, URLTools.getServerAddressFrom(request), portletContext.getApplicationName());
       try
       {
          resourceURL = new URL(url);
          String extension = URLTools.getFileExtensionOrNullFrom(resourceURL);
          MediaType type = SUPPORTED_RESOURCE_TYPES.get(extension);
-         if (MediaType.TEXT_CSS.equals(type) || MediaType.TEXT_JAVASCRIPT.equals(type))
+         if (MediaType.TEXT_CSS.equals(type) || MediaType.TEXT_JAVASCRIPT.equals(type) || MediaType.TEXT_HTML.equals(type))
          {
             requiresRewrite = true;
          }
@@ -228,6 +225,31 @@
       log.info("Attempted to build resource URL that could be accessed directly from consumer: " + resourceURL);
    }
 
+   public static String createAbsoluteURLFrom(String initial, String serverAddress, String portletApplicationName)
+   {
+      String url = serverAddress;
+
+      if (portletApplicationName != null)
+      {
+         url += URLTools.SLASH + portletApplicationName;
+      }
+
+      if (!ParameterValidation.isNullOrEmpty(initial))
+      {
+         if (initial.startsWith(URLTools.SLASH))
+         {
+            url += initial;
+         }
+         else
+         {
+            url += URLTools.SLASH + initial;
+         }
+      }
+
+      return url;
+
+   }
+
    /**
     * @return
     * @deprecated

Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java	2010-03-10 22:11:47 UTC (rev 2131)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java	2010-03-10 22:19:31 UTC (rev 2132)
@@ -481,6 +481,24 @@
       return localizedString;
    }
 
+   public static String getAbsoluteURLFor(String url, boolean checkWSRPToken, String serverAddress)
+   {
+      // We don't encode URL through this API when it is a wsrp URL
+      if (checkWSRPToken && url.startsWith(WSRPRewritingConstants.BEGIN_WSRP_REWRITE))
+      {
+         return url;
+      }
+
+      if (!URLTools.isNetworkURL(url) && url.startsWith(URLTools.SLASH))
+      {
+         return serverAddress + url;
+      }
+      else
+      {
+         return url;
+      }
+   }
+
    /**
     * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
     * @version $Revision$
@@ -507,25 +525,7 @@
        */
       public String getAbsoluteURLFor(String url)
       {
-         return getAbsoluteURLFor(url, true, serverAddress);
+         return WSRPUtils.getAbsoluteURLFor(url, true, serverAddress);
       }
-
-      public static String getAbsoluteURLFor(String url, boolean checkWSRPToken, String serverAddress)
-      {
-         // We don't encode URL through this API when it is a wsrp URL
-         if (checkWSRPToken && url.startsWith(WSRPRewritingConstants.BEGIN_WSRP_REWRITE))
-         {
-            return url;
-         }
-
-         if (!URLTools.isNetworkURL(url) && url.startsWith(URLTools.SLASH))
-         {
-            return serverAddress + url;
-         }
-         else
-         {
-            return url;
-         }
-      }
    }
 }

Modified: components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java
===================================================================
--- components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java	2010-03-10 22:11:47 UTC (rev 2131)
+++ components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java	2010-03-10 22:19:31 UTC (rev 2132)
@@ -25,7 +25,6 @@
 
 import org.gatein.common.net.URLTools;
 import org.gatein.common.text.TextTools;
-import org.gatein.common.util.ParameterValidation;
 import org.gatein.pc.api.PortletInvokerException;
 import org.gatein.pc.api.URLFormat;
 import org.gatein.pc.api.cache.CacheScope;
@@ -52,9 +51,8 @@
 import org.oasis.wsrp.v1.UserContext;
 
 import javax.xml.ws.Holder;
-import java.net.URI;
-import java.net.URL;
 import java.util.List;
+import java.util.Set;
 
 /**
  * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
@@ -195,9 +193,15 @@
          URLFormat format = new URLFormat(invocation.getSecurityContext().isSecure(),
             invocation.getSecurityContext().isAuthenticated(), true, true);
 
-         // replace URL marked for rewriting by proper ones 
-         markup = TextTools.replaceBoundedString(markup, WSRPRewritingConstants.BEGIN_WSRP_REWRITE,
-            WSRPRewritingConstants.END_WSRP_REWRITE, new ResourceURLStringReplacementGenerator(invocation.getContext(), format, consumer), true, false);
+         // replace URL marked for rewriting by proper ones
+         markup = TextTools.replaceBoundedString(
+            markup,
+            WSRPRewritingConstants.BEGIN_WSRP_REWRITE,
+            WSRPRewritingConstants.END_WSRP_REWRITE,
+            new ResourceURLStringReplacementGenerator(invocation.getContext(), format, consumer, invocation.getTarget()),
+            true,
+            false
+         );
       }
 
       return markup;
@@ -241,30 +245,44 @@
 
    static class ResourceURLStringReplacementGenerator implements TextTools.StringReplacementGenerator
    {
-      private PortletInvocationContext context;
-      private URLFormat format;
-      private WSRPConsumer consumer;
+      private final PortletInvocationContext context;
+      private final URLFormat format;
+      private final Set<String> supportedCustomModes;
+      private final Set<String> supportedCustomWindowStates;
+      private final String serverAddress;
+      private final String portletApplicationName;
 
-      private ResourceURLStringReplacementGenerator(PortletInvocationContext context, URLFormat format, WSRPConsumer consumer)
+      private ResourceURLStringReplacementGenerator(PortletInvocationContext context, URLFormat format, WSRPConsumer consumer, org.gatein.pc.api.PortletContext target)
       {
          this.context = context;
          this.format = format;
-         this.consumer = consumer;
+         ProducerInfo info = consumer.getProducerInfo();
+         supportedCustomModes = info.getSupportedCustomModes();
+         supportedCustomWindowStates = info.getSupportedCustomWindowStates();
+         serverAddress = info.getEndpointConfigurationInfo().getRemoteHostAddress();
+         portletApplicationName = target.getApplicationName();
       }
 
       public String getReplacementFor(String match)
       {
-         ProducerInfo info = consumer.getProducerInfo();
-         WSRPPortletURL portletURL = WSRPPortletURL.create(match, info.getSupportedCustomModes(), info.getSupportedCustomWindowStates());
+
+         WSRPPortletURL portletURL = WSRPPortletURL.create(match, supportedCustomModes, supportedCustomWindowStates);
          if (portletURL instanceof WSRPResourceURL)
          {
-            if (log.isDebugEnabled())
+            WSRPResourceURL resource = (WSRPResourceURL)portletURL;
+            String replacement = getResourceURL(match, resource);
+
+            // if the URL starts with /, prepend the remote host address and the portlet application name so that we
+            // can attempt to create a remotely available URL
+            if (replacement.startsWith(URLTools.SLASH))
             {
-               log.debug("URL '" + match + "' seems to refer to a resource which are not currently well supported.");
+               replacement = WSRPResourceURL.createAbsoluteURLFrom(replacement, serverAddress, portletApplicationName);
             }
 
-            WSRPResourceURL resource = (WSRPResourceURL)portletURL;
+            return replacement;
 
+            /*
+            todo: use this code to reactivate primitive use of resources
             // get the parsed URL and add marker to it so that the consumer can know it needs to be intercepted
             URL url = resource.getResourceURL();
             String query = url.getQuery();
@@ -289,12 +307,20 @@
             catch (Exception e)
             {
                throw new IllegalArgumentException("Cannot parse specified Resource as a URI: " + url);
-            }
+            }*/
          }
 
          return context.renderURL(portletURL, format);
       }
    }
 
+   private static String getResourceURL(String urlAsString, WSRPResourceURL resource)
+   {
+      String resourceURL = resource.getResourceURL().toExternalForm();
+      log.info("URL '" + urlAsString + "' refers to a resource which are not currently well supported. " +
+         "Attempting to craft a URL that we might be able to work with: '" + resourceURL + "'");
 
+      // right now the resourceURL should be output as is, because it will be used directly but it really should be encoded 
+      return resourceURL;
+   }
 }

Modified: components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/RenderHandlerTestCase.java
===================================================================
--- components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/RenderHandlerTestCase.java	2010-03-10 22:11:47 UTC (rev 2131)
+++ components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/RenderHandlerTestCase.java	2010-03-10 22:19:31 UTC (rev 2132)
@@ -40,7 +40,7 @@
          "&ns=_next%3D%2Fdk%2Fskat%2Fportal%2Ffront%2Fportlets%2Fexample%2Findex.jsp" +
          "&is=_action%3D%252Fdk%252Fskat%252Fportal%252Ffront%252Fportlets%252Fexample%252FprocessLink" +
          "%26jbpns_2fdefault_2fTest_2fEXAMPLE_2fEXAMPLEsnpbjname%3DChris\">Press to use default name.</a>";
-      String result = URLTools.replaceURLsBy(markup, new RenderHandler.ResourceURLRewriter());
+      String result = URLTools.replaceURLsBy(markup, new RenderHandler.WSRPURLRewriter());
       assertEquals(markup, result);*/
    }
 }

Modified: components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/WSRPPortletInvocationContext.java
===================================================================
--- components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/WSRPPortletInvocationContext.java	2010-03-10 22:11:47 UTC (rev 2131)
+++ components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/WSRPPortletInvocationContext.java	2010-03-10 22:19:31 UTC (rev 2132)
@@ -94,8 +94,8 @@
    {
       if (url != null && !url.startsWith(WSRPRewritingConstants.BEGIN_WSRP_REWRITE))
       {
-         // make root relative URLs absolute. Optimization: we don't recheck the precense of the WSRP token.
-         url = WSRPUtils.AbsoluteURLReplacementGenerator.getAbsoluteURLFor(url, false, URLTools.getServerAddressFrom(getClientRequest()));
+         // make root relative URLs absolute. Optimization: we don't recheck the presence of the WSRP token.
+         url = WSRPUtils.getAbsoluteURLFor(url, false, URLTools.getServerAddressFrom(getClientRequest()));
 
          // properly encode the URL
          url = URLTools.encodeXWWWFormURL(url);



More information about the gatein-commits mailing list