[gatein-commits] gatein SVN: r5158 - in components/wsrp/trunk: common/src/test/java/org/gatein/wsrp and 7 other directories.
do-not-reply at jboss.org
do-not-reply at jboss.org
Thu Nov 18 06:55:45 EST 2010
Author: chris.laprun at jboss.com
Date: 2010-11-18 06:55:43 -0500 (Thu, 18 Nov 2010)
New Revision: 5158
Added:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/ResourceServingUtil.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/DirectResourceServingHandler.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/resources/
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/resources/ResourceServingServlet.java
Modified:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPActionURL.java
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletURL.java
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderURL.java
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPResourceURL.java
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java
components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderURLTestCase.java
components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/other/WSRPPortletURLTestCase.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/WSRPConsumerImpl.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationHandler.java
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/ResourceHandler.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/WSRPPortletInvocationContext.java
components/wsrp/trunk/wsrp-producer-war/src/main/webapp/WEB-INF/web.xml
Log:
- GTNWSRP-146: Use a servlet to serve wsrp v1 resource:
+ Improved invocation dispatching of resources.
+ Refactored handling of direct resource serving: now uses a proper handler, subclassing ResourceHandler.
+ Added WSRPPortletURL.URLContext as a parameter to build WSRPPortletURL so that contextual informtation can be passed to the URL.
+ Added ResourceServingUtil to deal with encoding and decoding of direct resource requests.
+ Had WSRPPortletInvocationContext.renderURL use URLContext to transmit data to URLs.
+ WSRPResourceURL now computes a resourceURL if it is provided with the information to do so.
- Moved namespace generation and portlet instance key extraction methods to WSRPTypeFactory so that they can be reused across modules with the same logic.
Added: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/ResourceServingUtil.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/ResourceServingUtil.java (rev 0)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/ResourceServingUtil.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -0,0 +1,181 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is 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 Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.gatein.wsrp;
+
+import org.gatein.common.net.URLTools;
+import org.gatein.common.util.ParameterValidation;
+import org.gatein.pc.api.Mode;
+import org.gatein.pc.api.StateString;
+import org.gatein.pc.api.WindowState;
+import org.gatein.pc.api.cache.CacheLevel;
+import org.oasis.wsrp.v2.GetResource;
+import org.oasis.wsrp.v2.NamedString;
+import org.oasis.wsrp.v2.PortletContext;
+import org.oasis.wsrp.v2.RegistrationContext;
+import org.oasis.wsrp.v2.ResourceParams;
+import org.oasis.wsrp.v2.RuntimeContext;
+import org.oasis.wsrp.v2.StateChange;
+
+import javax.servlet.http.HttpServletRequest;
+import java.net.URI;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
+ * @version $Revision$
+ */
+public class ResourceServingUtil
+{
+ private static final String REG_HANDLE = "regHandle";
+ private static final String INSTANCE_KEY = "instanceKey";
+ private static final String NS = "ns";
+ private static final String MODE = "mode";
+ private static final String WINDOW_STATE = "windowState";
+ private static final String RESOURCE_STATE = "resourceState";
+ private static final String NAV_STATE = "navState";
+ private static final String SLASH_REPLACEMENT = "__";
+ private static final String QMARK = "?";
+
+ public static GetResource decode(HttpServletRequest req)
+ {
+ String path = req.getPathInfo();
+
+ int portletHandleEnd = path.indexOf(URLTools.SLASH, 1);
+ String portletHandle = path.substring(1, portletHandleEnd);
+ PortletContext portletContext = decode(portletHandle);
+
+ String resourceId = path.substring(portletHandleEnd);
+
+ String registrationHandle = req.getParameter(REG_HANDLE);
+ RegistrationContext registrationContext = null;
+ if (!ParameterValidation.isNullOrEmpty(registrationHandle))
+ {
+ registrationContext = WSRPTypeFactory.createRegistrationContext(registrationHandle);
+ }
+
+ String instanceKey = req.getParameter(INSTANCE_KEY);
+ String ns = req.getParameter(NS);
+ RuntimeContext runtimeContext = WSRPTypeFactory.createRuntimeContext(WSRPConstants.NONE_USER_AUTHENTICATION, instanceKey, ns);
+
+ Enumeration reqLocales = req.getLocales();
+ List<String> locales = WSRPUtils.convertLocalesToRFC3066LanguageTags(Collections.list(reqLocales));
+ List<String> mimeTypes = WSRPConstants.getDefaultMimeTypes();
+
+ ResourceParams resourceParams = WSRPTypeFactory.createResourceParams(req.isSecure(), locales, mimeTypes,
+ WSRPConstants.VIEW_MODE, WSRPConstants.NORMAL_WINDOW_STATE, resourceId, StateChange.READ_ONLY);
+ resourceParams.setResourceState(req.getParameter(RESOURCE_STATE));
+
+ String navState = req.getParameter(NAV_STATE);
+ if (!ParameterValidation.isNullOrEmpty(navState))
+ {
+ resourceParams.setNavigationalContext(WSRPTypeFactory.createNavigationalContext(navState, Collections.<NamedString>emptyList()));
+ }
+
+ return WSRPTypeFactory.createGetResource(registrationContext, portletContext, runtimeContext, null, resourceParams);
+ }
+
+ public static URL encode(Mode mode, WindowState windowState, boolean secure, StateString navigationalState, StateString resourceState, String resourceId, CacheLevel cacheability, WSRPPortletURL.URLContext context)
+ {
+ // Generate a resource URL based on the resource Id
+ String serverAddress = (String)context.getValueFor(WSRPPortletURL.URLContext.SERVER_ADDRESS);
+ org.gatein.pc.api.PortletContext portletContext = (org.gatein.pc.api.PortletContext)context.getValueFor(WSRPPortletURL.URLContext.PORTLET_CONTEXT);
+ try
+ {
+ StringBuilder sb = new StringBuilder(createAbsoluteURLFrom(resourceId, serverAddress, portletContext));
+ appendParameter(sb, MODE, mode.toString());
+ appendParameter(sb, WINDOW_STATE, windowState.toString());
+ appendParameter(sb, INSTANCE_KEY, context.getValueFor(WSRPPortletURL.URLContext.INSTANCE_KEY));
+ appendParameter(sb, NS, context.getValueFor(WSRPPortletURL.URLContext.NAMESPACE));
+ appendParameter(sb, REG_HANDLE, context.getValueFor(WSRPPortletURL.URLContext.REGISTRATION_HANDLE));
+ if (resourceState != null)
+ {
+ appendParameter(sb, RESOURCE_STATE, resourceState.getStringValue());
+ }
+ if (navigationalState != null)
+ {
+ appendParameter(sb, NAV_STATE, navigationalState.getStringValue());
+ }
+
+ return new URI(sb.toString()).toURL();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Couldn't create an absolute URL from resourceId: " + resourceId + ", server address: " + serverAddress
+ + ", portlet: " + portletContext, e);
+ }
+ }
+
+ private static String createAbsoluteURLFrom(String resourceId, String serverAddress, org.gatein.pc.api.PortletContext portletContext)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(resourceId, "resource ID", null);
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(serverAddress, "server address", null);
+ ParameterValidation.throwIllegalArgExceptionIfNull(portletContext, "portlet context");
+
+ String url = serverAddress + "/wsrp-producer/resource/";
+
+ url += encode(portletContext);
+
+ if (resourceId.startsWith(URLTools.SLASH))
+ {
+ url += resourceId + QMARK;
+ }
+ else
+ {
+ url += URLTools.SLASH + resourceId + QMARK;
+ }
+
+ return url;
+ }
+
+ private static String encode(org.gatein.pc.api.PortletContext portletContext)
+ {
+ String id = portletContext.getId();
+ if (id.startsWith(URLTools.SLASH))
+ {
+ id = id.replace(URLTools.SLASH, SLASH_REPLACEMENT);
+ }
+ return id;
+ }
+
+ private static PortletContext decode(String encodedPortletContext)
+ {
+ if (encodedPortletContext.startsWith(SLASH_REPLACEMENT))
+ {
+ encodedPortletContext = encodedPortletContext.replace(SLASH_REPLACEMENT, URLTools.SLASH);
+ }
+
+ return WSRPTypeFactory.createPortletContext(encodedPortletContext);
+ }
+
+ private static void appendParameter(StringBuilder builder, String name, Object value)
+ {
+ if (value != null)
+ {
+ builder.append("&").append(name).append("=").append(value);
+ }
+ }
+}
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPActionURL.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPActionURL.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPActionURL.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -40,9 +40,9 @@
{
private StateString interactionState;
- protected WSRPActionURL(Mode mode, WindowState windowState, boolean secure, StateString navigationalState, StateString interactionState)
+ protected WSRPActionURL(Mode mode, WindowState windowState, boolean secure, StateString navigationalState, StateString interactionState, URLContext context)
{
- super(mode, windowState, secure, navigationalState);
+ super(mode, windowState, secure, navigationalState, context);
this.interactionState = interactionState;
}
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletURL.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletURL.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletURL.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -49,7 +49,7 @@
*/
public abstract class WSRPPortletURL implements ContainerURL
{
- private static final Logger log = LoggerFactory.getLogger(WSRPPortletURL.class);
+ protected static final Logger log = LoggerFactory.getLogger(WSRPPortletURL.class);
protected static final String EQUALS = "=";
protected static final String AMPERSAND = "&";
@@ -85,7 +85,7 @@
log.debug("Using " + (strict ? "strict" : "lenient") + " rewriting parameters validation mode.");
}
- public static WSRPPortletURL create(ContainerURL containerURL, boolean secure)
+ public static WSRPPortletURL create(ContainerURL containerURL, boolean secure, URLContext context)
{
if (containerURL == null)
{
@@ -100,17 +100,17 @@
if (containerURL instanceof ActionURL)
{
StateString interactionState = ((ActionURL)containerURL).getInteractionState();
- url = new WSRPActionURL(mode, windowState, secure, navigationalState, interactionState);
+ url = new WSRPActionURL(mode, windowState, secure, navigationalState, interactionState, context);
}
else if (containerURL instanceof RenderURL)
{
- url = new WSRPRenderURL(mode, windowState, secure, navigationalState, ((RenderURL)containerURL).getPublicNavigationalStateChanges());
+ url = new WSRPRenderURL(mode, windowState, secure, navigationalState, ((RenderURL)containerURL).getPublicNavigationalStateChanges(), context);
}
else if (containerURL instanceof ResourceURL)
{
ResourceURL resource = (ResourceURL)containerURL;
url = new WSRPResourceURL(mode, windowState, secure, navigationalState, resource.getResourceState(),
- resource.getResourceId(), resource.getCacheability());
+ resource.getResourceId(), resource.getCacheability(), context);
}
else
{
@@ -283,7 +283,7 @@
return create(encodedURL, Collections.<String>emptySet(), Collections.<String>emptySet());
}
- protected WSRPPortletURL(Mode mode, WindowState windowState, boolean secure, StateString navigationalState)
+ protected WSRPPortletURL(Mode mode, WindowState windowState, boolean secure, StateString navigationalState, URLContext context)
{
this.mode = mode;
this.windowState = windowState;
@@ -597,4 +597,50 @@
{
return Collections.emptyMap();
}
+
+ public static class URLContext
+ {
+ public static final String SERVER_ADDRESS = "org.gatein.wsrp.server.address";
+ public static final String PORTLET_CONTEXT = "org.gatein.wsrp.portlet.context";
+ public static final String REGISTRATION_HANDLE = "org.gatein.wsrp.registration";
+ public static final String INSTANCE_KEY = "org.gatein.wsrp.instance.key";
+ public static final String NAMESPACE = "org.gatein.wsrp.namespace";
+
+ public static final URLContext EMPTY = new URLContext()
+ {
+ @Override
+ public Object getValueFor(String name)
+ {
+ return null;
+ }
+
+ @Override
+ public void setValueFor(String name, Object value)
+ {
+ }
+ };
+ private Map<String, Object> context;
+
+ public URLContext()
+ {
+ context = new HashMap<String, Object>(7);
+ }
+
+ public URLContext(String name1, Object value1, String name2, Object value2)
+ {
+ this();
+ context.put(name1, value1);
+ context.put(name2, value2);
+ }
+
+ public Object getValueFor(String name)
+ {
+ return context.get(name);
+ }
+
+ public void setValueFor(String name, Object value)
+ {
+ context.put(name, value);
+ }
+ }
}
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderURL.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderURL.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderURL.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -40,9 +40,9 @@
{
private Map<String, String[]> publicNSChanges;
- protected WSRPRenderURL(Mode mode, WindowState windowState, boolean secure, StateString navigationalState, Map<String, String[]> publicNavigationalStateChanges)
+ protected WSRPRenderURL(Mode mode, WindowState windowState, boolean secure, StateString navigationalState, Map<String, String[]> publicNavigationalStateChanges, URLContext context)
{
- super(mode, windowState, secure, navigationalState);
+ super(mode, windowState, secure, navigationalState, context);
this.publicNSChanges = publicNavigationalStateChanges;
}
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-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPResourceURL.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -59,15 +59,25 @@
{
}
- public WSRPResourceURL(Mode mode, WindowState windowState, boolean secure, StateString navigationalState, StateString resourceState, String resourceId, CacheLevel cacheability)
+ public WSRPResourceURL(Mode mode, WindowState windowState, boolean secure, StateString navigationalState, StateString resourceState, String resourceId, CacheLevel cacheability, URLContext context)
{
- super(mode, windowState, secure, navigationalState);
+ super(mode, windowState, secure, navigationalState, context);
if (resourceId == null)
{
// if the container didn't provide us with a resource id, fake one so that we can still build a correct WSRP URL.
resourceId = DEFAULT_RESOURCE_ID;
}
+
+ if (context != null && !URLContext.EMPTY.equals(context))
+ {
+ resourceURL = ResourceServingUtil.encode(mode, windowState, secure, navigationalState, resourceState, resourceId, cacheability, context);
+ }
+ else
+ {
+ resourceURL = null;
+ }
+
this.resourceId = resourceId;
this.resourceState = resourceState;
this.cacheability = cacheability;
@@ -252,7 +262,7 @@
public static Map<String, String> decodeResource(String resourceInfo)
{
Map<String, String> resource = new HashMap<String, String>();
-
+
if (resourceInfo != null && resourceInfo.startsWith(StateString.JBPNS_PREFIX))
{
Map<String, String[]> resourceParameters = StateString.decodeOpaqueValue(resourceInfo);
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPTypeFactory.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -37,7 +37,9 @@
import org.gatein.pc.api.URLFormat;
import org.gatein.pc.api.WindowState;
import org.gatein.pc.api.cache.CacheLevel;
+import org.gatein.pc.api.spi.InstanceContext;
import org.gatein.pc.api.spi.PortletInvocationContext;
+import org.gatein.pc.api.spi.WindowContext;
import org.gatein.wsrp.payload.PayloadUtils;
import org.gatein.wsrp.spec.v2.ErrorCodes;
import org.gatein.wsrp.spec.v2.WSRP2RewritingConstants;
@@ -419,7 +421,7 @@
private static void checkPortletHandle(String portletHandle)
{
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletHandle, "portlet handle", "PortletDescription");
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletHandle, "portlet handle", null);
if (portletHandle.length() > 255)
{
throw new IllegalArgumentException("Portlet handles must be less than 255 characters long. Was "
@@ -586,10 +588,10 @@
public static MarkupContext createMarkupContext(String mediaType, String markupString, byte[] markupBinary, Boolean useCachedItem)
{
boolean isUseCachedItem = (useCachedItem == null) ? false : useCachedItem.booleanValue();
-
+
MarkupContext markupContext = new MarkupContext();
markupContext.setMimeType(mediaType);
-
+
if (isUseCachedItem)
{
markupContext.setUseCachedItem(useCachedItem);
@@ -597,7 +599,7 @@
else
{
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(mediaType, "Media type", "MarkupContext");
-
+
if (markupBinary != null)
{
markupContext.setItemBinary(markupBinary);
@@ -1811,4 +1813,30 @@
{
return new NamedStringArray();
}
+
+ public static String getPortletInstanceKey(InstanceContext instanceContext)
+ {
+ return instanceContext.getId();
+ }
+
+ public static String getNamespacePrefix(WindowContext windowContext, String portletHandle)
+ {
+ String namespacePrefix = getNamespaceFrom(windowContext);
+ if (namespacePrefix == null)
+ {
+ return portletHandle;
+ }
+
+ return namespacePrefix;
+ }
+
+ public static String getNamespaceFrom(WindowContext windowContext)
+ {
+ if (windowContext != null)
+ {
+ return windowContext.getNamespace();
+ }
+
+ return null;
+ }
}
Modified: components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderURLTestCase.java
===================================================================
--- components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderURLTestCase.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderURLTestCase.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -39,13 +39,13 @@
{
public void testNullPublicNavigationalState()
{
- WSRPRenderURL url = new WSRPRenderURL(null, null, false, null, null);
+ WSRPRenderURL url = new WSRPRenderURL(null, null, false, null, null, WSRPPortletURL.URLContext.EMPTY);
assertFalse(url.toString().contains(WSRP2RewritingConstants.NAVIGATIONAL_VALUES));
}
public void testEmptyPublicNavigationalState()
{
- WSRPRenderURL url = new WSRPRenderURL(null, null, false, null, Collections.<String, String[]>emptyMap());
+ WSRPRenderURL url = new WSRPRenderURL(null, null, false, null, Collections.<String, String[]>emptyMap(), WSRPPortletURL.URLContext.EMPTY);
assertFalse(url.toString().contains(WSRP2RewritingConstants.NAVIGATIONAL_VALUES));
}
Modified: components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/other/WSRPPortletURLTestCase.java
===================================================================
--- components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/other/WSRPPortletURLTestCase.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/other/WSRPPortletURLTestCase.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -96,16 +96,16 @@
assertNull(resourceMap.get(WSRPRewritingConstants.RESOURCE_URL));
assertEquals("false", resourceMap.get(WSRP2RewritingConstants.RESOURCE_PREFER_OPERATION));
}
-
+
public void testResourceIDToken()
{
//Test what happens in the case where the url is created via token wsrp rewriting on the producer side
//In this case the resource ID will not be an encoded resource map.
-
+
Map<String, String> resourceMap = WSRPResourceURL.decodeResource("resource_123");
String resourceID = resourceMap.get(WSRP2RewritingConstants.RESOURCE_ID);
assertEquals("resource_123", resourceID);
-
+
assertNull(resourceMap.get(WSRPRewritingConstants.RESOURCE_URL));
assertNull(resourceMap.get(WSRP2RewritingConstants.RESOURCE_PREFER_OPERATION));
}
@@ -197,7 +197,7 @@
{
// create URL from container
ResourceURL resourceURL = new TestResourceURL("resparam", "resvalue");
- WSRPPortletURL url = WSRPPortletURL.create(resourceURL, false);
+ WSRPPortletURL url = WSRPPortletURL.create(resourceURL, false, WSRPPortletURL.URLContext.EMPTY);
assertTrue(url instanceof WSRPResourceURL);
WSRPResourceURL resource = (WSRPResourceURL)url;
Modified: components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/WSRPConsumerImpl.java
===================================================================
--- components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/WSRPConsumerImpl.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/WSRPConsumerImpl.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -121,6 +121,8 @@
private final static Logger log = LoggerFactory.getLogger(WSRPConsumer.class);
+ private final static String PORTLET_INFO_KEY = "wsrp_portlet_info";
+
static
{
REGISTRATION_NOT_NEEDED.setConsumerAgent("INVALID AGENT");
@@ -493,7 +495,17 @@
public WSRPPortletInfo getPortletInfo(PortletInvocation invocation) throws PortletInvokerException
{
- return (WSRPPortletInfo)getWSRPPortlet(getPortletContext(invocation)).getInfo();
+ // first try to get the info from the invocation
+ Object info = invocation.getAttribute(PORTLET_INFO_KEY);
+
+ // if the portlet info is not in the invocation, set it so that it can be used in further calls
+ if (info == null)
+ {
+ info = getWSRPPortlet(getPortletContext(invocation)).getInfo();
+ invocation.setAttribute(PORTLET_INFO_KEY, info);
+ }
+
+ return (WSRPPortletInfo)info;
}
WSRPPortlet getWSRPPortlet(PortletContext portletContext) throws PortletInvokerException
Added: components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/DirectResourceServingHandler.java
===================================================================
--- components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/DirectResourceServingHandler.java (rev 0)
+++ components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/DirectResourceServingHandler.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -0,0 +1,142 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is 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 Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.gatein.wsrp.consumer.handlers;
+
+import org.apache.commons.httpclient.Cookie;
+import org.gatein.common.io.IOTools;
+import org.gatein.common.net.media.MediaType;
+import org.gatein.common.net.media.SubtypeDef;
+import org.gatein.common.net.media.TypeDef;
+import org.gatein.common.util.MultiValuedPropertyMap;
+import org.gatein.common.util.Tools;
+import org.gatein.pc.api.invocation.response.ResponseProperties;
+import org.gatein.wsrp.WSRPTypeFactory;
+import org.gatein.wsrp.consumer.WSRPConsumerImpl;
+import org.gatein.wsrp.handler.CookieUtil;
+import org.oasis.wsrp.v2.GetResource;
+import org.oasis.wsrp.v2.ResourceContext;
+import org.oasis.wsrp.v2.ResourceResponse;
+
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
+ * @version $Revision$
+ */
+public class DirectResourceServingHandler extends ResourceHandler
+{
+ protected DirectResourceServingHandler(WSRPConsumerImpl consumer)
+ {
+ super(consumer);
+ }
+
+ @Override
+ protected ResourceResponse performRequest(GetResource getResource) throws Exception
+ {
+ // if we perform the request with this handler, that means that the invocation was dispatched this way
+ // and the initiliazation of the request done in ResourceHandler.prepareRequest put the URL in the resource ID
+ String resourceURL = getResource.getResourceParams().getResourceID();
+
+ URL url = new URL(resourceURL);
+ URLConnection urlConnection = url.openConnection();
+ String contentType = urlConnection.getContentType();
+
+ // init ResponseProperties for ContentResponse result
+ Map<String, List<String>> headers = urlConnection.getHeaderFields();
+ ResponseProperties props = new ResponseProperties();
+ MultiValuedPropertyMap<String> transportHeaders = props.getTransportHeaders();
+ for (Map.Entry<String, List<String>> entry : headers.entrySet())
+ {
+ String key = entry.getKey();
+ if (key != null)
+ {
+ List<String> values = entry.getValue();
+ if (values != null)
+ {
+ if (CookieUtil.SET_COOKIE.equals(key))
+ {
+ Cookie[] cookies = CookieUtil.extractCookiesFrom(url, values.toArray(new String[values.size()]));
+ List<javax.servlet.http.Cookie> propCookies = props.getCookies();
+ for (Cookie cookie : cookies)
+ {
+ propCookies.add(CookieUtil.convertFrom(cookie));
+ }
+ }
+ else
+ {
+ for (String value : values)
+ {
+ transportHeaders.addValue(key, value);
+ }
+ }
+ }
+ }
+ }
+
+ int length = urlConnection.getContentLength();
+ // if length is not known, use a default value
+ length = (length > 0 ? length : Tools.DEFAULT_BUFFER_SIZE * 8);
+ byte[] bytes = IOTools.getBytes(urlConnection.getInputStream(), length);
+
+
+ ResourceContext resourceContext;
+ MediaType type = MediaType.create(contentType);
+ if (TypeDef.TEXT.equals(type.getType()))
+ {
+ // determine the charset of the content, if any
+ String charset = "UTF-8";
+ if (contentType != null)
+ {
+ for (String part : contentType.split(";"))
+ {
+ if (part.startsWith("charset="))
+ {
+ charset = part.substring("charset=".length());
+ }
+ }
+ }
+
+ String markup = new String(bytes, charset);
+
+ resourceContext = WSRPTypeFactory.createResourceContext(contentType, markup, null);
+
+ // process markup if needed
+ SubtypeDef subtype = type.getSubtype();
+ if (SubtypeDef.HTML.equals(subtype) || SubtypeDef.CSS.equals(subtype) || SubtypeDef.JAVASCRIPT.equals(subtype) || SubtypeDef.XML.equals(subtype))
+ {
+ resourceContext.setRequiresRewriting(true);
+ }
+ }
+ else
+ {
+ resourceContext = WSRPTypeFactory.createResourceContext(contentType, null, bytes);
+ resourceContext.setRequiresRewriting(false);
+ }
+
+ return WSRPTypeFactory.createResourceResponse(resourceContext);
+ }
+}
Modified: components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java
===================================================================
--- components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -23,13 +23,7 @@
package org.gatein.wsrp.consumer.handlers;
-import org.apache.commons.httpclient.Cookie;
-import org.gatein.common.io.IOTools;
-import org.gatein.common.net.media.MediaType;
-import org.gatein.common.net.media.TypeDef;
-import org.gatein.common.util.MultiValuedPropertyMap;
import org.gatein.common.util.ParameterValidation;
-import org.gatein.common.util.Tools;
import org.gatein.pc.api.PortletInvokerException;
import org.gatein.pc.api.invocation.ActionInvocation;
import org.gatein.pc.api.invocation.EventInvocation;
@@ -37,21 +31,13 @@
import org.gatein.pc.api.invocation.PortletInvocation;
import org.gatein.pc.api.invocation.RenderInvocation;
import org.gatein.pc.api.invocation.ResourceInvocation;
-import org.gatein.pc.api.invocation.response.ContentResponse;
import org.gatein.pc.api.invocation.response.ErrorResponse;
import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
-import org.gatein.pc.api.invocation.response.ResponseProperties;
import org.gatein.wsrp.WSRPResourceURL;
import org.gatein.wsrp.WSRPRewritingConstants;
import org.gatein.wsrp.consumer.WSRPConsumerImpl;
-import org.gatein.wsrp.handler.CookieUtil;
import org.gatein.wsrp.spec.v2.WSRP2RewritingConstants;
-import java.io.IOException;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.Collections;
-import java.util.List;
import java.util.Map;
/**
@@ -65,6 +51,7 @@
private final ResourceHandler resourceHandler;
private final EventHandler eventHandler;
private final WSRPConsumerImpl consumer;
+ private final DirectResourceServingHandler directResourceHandler;
public InvocationDispatcher(WSRPConsumerImpl consumer)
{
@@ -73,6 +60,7 @@
renderHandler = new RenderHandler(consumer);
resourceHandler = new ResourceHandler(consumer);
eventHandler = new EventHandler(consumer);
+ directResourceHandler = new DirectResourceServingHandler(consumer);
}
public PortletInvocationResponse dispatchAndHandle(PortletInvocation invocation) throws PortletInvokerException
@@ -101,11 +89,16 @@
resourceId = resourceMap.get(WSRP2RewritingConstants.RESOURCE_ID);
resourceURL = resourceMap.get(WSRPRewritingConstants.RESOURCE_URL);
preferOperationAsString = resourceMap.get(WSRP2RewritingConstants.RESOURCE_PREFER_OPERATION);
+
+ // put the values in the invocation so that we don't need to redecode the resource id
+ resourceInvocation.setAttribute(WSRPRewritingConstants.RESOURCE_URL, resourceURL);
+ resourceInvocation.setAttribute(WSRP2RewritingConstants.RESOURCE_ID, resourceId);
+ resourceInvocation.setAttribute(WSRP2RewritingConstants.RESOURCE_PREFER_OPERATION, preferOperationAsString);
}
else
{
// GateIn-specific: WSRP-specific URL parameters might also be put as attributes by UIPortlet when the invocation is created
- resourceId = (String)resourceInvocation.getAttribute(WSRP2RewritingConstants.RESOURCE_ID);
+ resourceId = null;
resourceURL = (String)resourceInvocation.getAttribute(WSRPRewritingConstants.RESOURCE_URL);
preferOperationAsString = (String)resourceInvocation.getAttribute(WSRP2RewritingConstants.RESOURCE_PREFER_OPERATION);
}
@@ -118,7 +111,7 @@
}
else if (resourceURL != null)
{
- return performDirectURLRequest(resourceURL);
+ handler = directResourceHandler;
}
else
{
@@ -136,83 +129,4 @@
return handler.handle(invocation);
}
-
- private PortletInvocationResponse performDirectURLRequest(String resourceURL)
- {
- try
- {
- URL url = new URL(resourceURL);
- URLConnection urlConnection = url.openConnection();
- String contentType = urlConnection.getContentType();
-
- // init ResponseProperties for ContentResponse result
- Map<String, List<String>> headers = urlConnection.getHeaderFields();
- ResponseProperties props = new ResponseProperties();
- MultiValuedPropertyMap<String> transportHeaders = props.getTransportHeaders();
- for (Map.Entry<String, List<String>> entry : headers.entrySet())
- {
- String key = entry.getKey();
- if (key != null)
- {
- List<String> values = entry.getValue();
- if (values != null)
- {
- if (CookieUtil.SET_COOKIE.equals(key))
- {
- Cookie[] cookies = CookieUtil.extractCookiesFrom(url, values.toArray(new String[values.size()]));
- List<javax.servlet.http.Cookie> propCookies = props.getCookies();
- for (Cookie cookie : cookies)
- {
- propCookies.add(CookieUtil.convertFrom(cookie));
- }
- }
- else
- {
- for (String value : values)
- {
- transportHeaders.addValue(key, value);
- }
- }
- }
- }
- }
-
- int length = urlConnection.getContentLength();
- // if length is not known, use a default value
- length = (length > 0 ? length : Tools.DEFAULT_BUFFER_SIZE * 8);
- byte[] bytes = IOTools.getBytes(urlConnection.getInputStream(), length);
-
- ContentResponse result;
- MediaType type = MediaType.create(contentType);
- if (TypeDef.TEXT.equals(type.getType()))
- {
- // determine the charset of the content, if any
- String charset = "UTF-8";
- if (contentType != null)
- {
- for (String part : contentType.split(";"))
- {
- if (part.startsWith("charset="))
- {
- charset = part.substring("charset=".length());
- }
- }
- }
-
- // build a String-based content response
- result = new ContentResponse(props, Collections.<String, Object>emptyMap(), contentType, null, new String(bytes, charset), null);
- }
- else
- {
- // build a byte-based content response
- result = new ContentResponse(props, Collections.<String, Object>emptyMap(), contentType, bytes, null, null);
- }
-
- return result;
- }
- catch (IOException e)
- {
- return new ErrorResponse(e);
- }
- }
}
Modified: components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationHandler.java
===================================================================
--- components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationHandler.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationHandler.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -145,7 +145,7 @@
if (runtimeContext != null)
{
WindowContext windowContext = invocation.getWindowContext();
- runtimeContext.setNamespacePrefix(getNamespaceFrom(windowContext));
+ runtimeContext.setNamespacePrefix(WSRPTypeFactory.getNamespaceFrom(windowContext));
InstanceContext instanceContext = invocation.getInstanceContext();
runtimeContext.setPortletInstanceKey(instanceContext == null ? null : instanceContext.getId());
@@ -197,16 +197,6 @@
return response;
}
- protected static String getNamespaceFrom(WindowContext windowContext)
- {
- if (windowContext != null)
- {
- return windowContext.getNamespace();
- }
-
- return null;
- }
-
/**
* Deals with common error conditions.
*
@@ -347,13 +337,9 @@
ParameterValidation.throwIllegalArgExceptionIfNull(securityContext, SECURITY_CONTEXT);
String authType = WSRPUtils.convertRequestAuthTypeToWSRPAuthType(securityContext.getAuthType());
- String portletInstanceKey = invocation.getInstanceContext().getId();
- String namespacePrefix = getNamespaceFrom(invocation.getWindowContext());
+ String portletInstanceKey = WSRPTypeFactory.getPortletInstanceKey(invocation.getInstanceContext());
- if (namespacePrefix == null)
- {
- namespacePrefix = getPortletHandle();
- }
+ String namespacePrefix = WSRPTypeFactory.getNamespacePrefix(invocation.getWindowContext(), getPortletHandle());
runtimeContext = WSRPTypeFactory.createRuntimeContext(authType, portletInstanceKey, namespacePrefix);
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-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -24,6 +24,7 @@
package org.gatein.wsrp.consumer.handlers;
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;
@@ -36,8 +37,8 @@
import org.gatein.wsrp.WSRPConstants;
import org.gatein.wsrp.WSRPConsumer;
import org.gatein.wsrp.WSRPPortletURL;
-import org.gatein.wsrp.WSRPResourceURL;
import org.gatein.wsrp.WSRPRewritingConstants;
+import org.gatein.wsrp.WSRPTypeFactory;
import org.gatein.wsrp.consumer.ProducerInfo;
import org.gatein.wsrp.consumer.WSRPConsumerImpl;
import org.oasis.wsrp.v2.CacheControl;
@@ -92,13 +93,13 @@
}
}
- if (markup != null && markup.length() > 0)
+ if (!ParameterValidation.isNullOrEmpty(markup))
{
if (Boolean.TRUE.equals(mimeResponse.isRequiresRewriting()))
{
markup = processMarkup(
markup,
- getNamespaceFrom(invocation.getWindowContext()),
+ WSRPTypeFactory.getNamespaceFrom(invocation.getWindowContext()),
invocation.getContext(),
invocation.getTarget(),
new URLFormat(invocation.getSecurityContext().isSecure(), invocation.getSecurityContext().isAuthenticated(), true, true),
@@ -108,7 +109,7 @@
}
String mimeType = mimeResponse.getMimeType();
- if (mimeType == null || mimeType.length() == 0)
+ if (ParameterValidation.isNullOrEmpty(mimeType))
{
return new ErrorResponse(new IllegalArgumentException("No MIME type was provided for portlet content."));
}
@@ -216,19 +217,5 @@
WSRPPortletURL portletURL = WSRPPortletURL.create(match, supportedCustomModes, supportedCustomWindowStates, true);
return context.renderURL(portletURL, format);
}
-
-
- static String getResourceURL(String urlAsString, WSRPResourceURL resource)
- {
- String resourceURL = resource.getResourceURL().toExternalForm();
- if (log.isDebugEnabled())
- {
- log.debug("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/main/java/org/gatein/wsrp/consumer/handlers/ResourceHandler.java
===================================================================
--- components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ResourceHandler.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ResourceHandler.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -28,7 +28,7 @@
import org.gatein.pc.api.invocation.ResourceInvocation;
import org.gatein.pc.api.spi.InstanceContext;
import org.gatein.pc.api.state.AccessMode;
-import org.gatein.wsrp.WSRPResourceURL;
+import org.gatein.wsrp.WSRPRewritingConstants;
import org.gatein.wsrp.WSRPTypeFactory;
import org.gatein.wsrp.WSRPUtils;
import org.gatein.wsrp.consumer.WSRPConsumerImpl;
@@ -92,10 +92,21 @@
@Override
protected GetResource prepareRequest(RequestPrecursor<ResourceInvocation> requestPrecursor, ResourceInvocation invocation)
{
- String resourceInvocationId = invocation.getResourceId();
+ /*String resourceInvocationId = invocation.getResourceId();
Map<String, String> resourceMap = WSRPResourceURL.decodeResource(resourceInvocationId);
- String resourceId = resourceMap.get(WSRP2RewritingConstants.RESOURCE_ID);
+ String resourceId = resourceMap.get(WSRP2RewritingConstants.RESOURCE_ID);*/
+ // get the resource id from the invocation attributes so that we don't need to redecode the original encoded resource id
+ String resourceId = (String)invocation.getAttribute(WSRP2RewritingConstants.RESOURCE_ID);
+
+ // if we didn't get a resource id, that means we need to use a resource URL, so get it and use it as the resource id
+ // note that the InvocationDispatcher should properly handle which handler will be used so this should be safe
+ // if we had a resource id anyway but are using WSRP 1, we need to reset it to use the URL instead
+ if (resourceId == null || !consumer.isUsingWSRP2())
+ {
+ resourceId = (String)invocation.getAttribute(WSRPRewritingConstants.RESOURCE_URL);
+ }
+
PortletContext portletContext = requestPrecursor.getPortletContext();
// since we actually extracted the data into MarkupParams in the RequestPrecursor, use that! :)
Modified: components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/WSRPPortletInvocationContext.java
===================================================================
--- components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/WSRPPortletInvocationContext.java 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/handlers/processors/WSRPPortletInvocationContext.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -36,8 +36,11 @@
import org.gatein.pc.portlet.impl.spi.AbstractClientContext;
import org.gatein.pc.portlet.impl.spi.AbstractPortletInvocationContext;
import org.gatein.pc.portlet.impl.spi.AbstractServerContext;
+import org.gatein.registration.Registration;
+import org.gatein.registration.RegistrationLocal;
import org.gatein.wsrp.WSRPPortletURL;
import org.gatein.wsrp.WSRPRewritingConstants;
+import org.gatein.wsrp.WSRPTypeFactory;
import org.gatein.wsrp.WSRPUtils;
import org.gatein.wsrp.servlet.ServletAccess;
@@ -131,7 +134,18 @@
{
Boolean wantSecureBool = urlFormat.getWantSecure();
boolean wantSecure = (wantSecureBool != null ? wantSecureBool : false);
- WSRPPortletURL url = WSRPPortletURL.create(containerURL, wantSecure);
+ WSRPPortletURL.URLContext context = new WSRPPortletURL.URLContext(WSRPPortletURL.URLContext.SERVER_ADDRESS,
+ URLTools.getServerAddressFrom(request), WSRPPortletURL.URLContext.PORTLET_CONTEXT, instanceContext.getPortletContext());
+
+ Registration registration = RegistrationLocal.getRegistration();
+ if (registration != null)
+ {
+ context.setValueFor(WSRPPortletURL.URLContext.REGISTRATION_HANDLE, registration.getRegistrationHandle());
+ }
+ context.setValueFor(WSRPPortletURL.URLContext.INSTANCE_KEY, WSRPTypeFactory.getPortletInstanceKey(instanceContext));
+ context.setValueFor(WSRPPortletURL.URLContext.NAMESPACE, WSRPTypeFactory.getNamespacePrefix(windowContext, instanceContext.getPortletContext().getId()));
+
+ WSRPPortletURL url = WSRPPortletURL.create(containerURL, wantSecure, context);
return url.toString();
}
return null;
Added: components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/resources/ResourceServingServlet.java
===================================================================
--- components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/resources/ResourceServingServlet.java (rev 0)
+++ components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/resources/ResourceServingServlet.java 2010-11-18 11:55:43 UTC (rev 5158)
@@ -0,0 +1,78 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is 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 Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.gatein.wsrp.producer.resources;
+
+import org.gatein.common.util.ParameterValidation;
+import org.gatein.wsrp.ResourceServingUtil;
+import org.gatein.wsrp.producer.ProducerHolder;
+import org.gatein.wsrp.producer.v2.WSRP2Producer;
+import org.oasis.wsrp.v2.GetResource;
+import org.oasis.wsrp.v2.ResourceContext;
+import org.oasis.wsrp.v2.ResourceResponse;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a>
+ * @version $Revision$
+ */
+public class ResourceServingServlet extends HttpServlet
+{
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ WSRP2Producer producer = ProducerHolder.getProducer();
+
+ GetResource getResource = ResourceServingUtil.decode(req);
+
+ try
+ {
+ ResourceResponse resource = producer.getResource(getResource);
+ ResourceContext resourceContext = resource.getResourceContext();
+ byte[] itemBinary = resourceContext.getItemBinary();
+ String itemString = resourceContext.getItemString();
+
+ resp.setContentType(resourceContext.getMimeType());
+
+ if (itemBinary != null && itemBinary.length > 0)
+ {
+ resp.getOutputStream().write(itemBinary);
+ }
+
+ if (!ParameterValidation.isNullOrEmpty(itemString))
+ {
+ resp.getWriter().write(itemString);
+ }
+ }
+ catch (Exception exception)
+ {
+ throw new ServletException("Couldn't get resource " + getResource.getResourceParams().getResourceID()
+ + " for portlet " + getResource.getPortletContext(), exception);
+ }
+ }
+}
Modified: components/wsrp/trunk/wsrp-producer-war/src/main/webapp/WEB-INF/web.xml
===================================================================
--- components/wsrp/trunk/wsrp-producer-war/src/main/webapp/WEB-INF/web.xml 2010-11-18 11:22:08 UTC (rev 5157)
+++ components/wsrp/trunk/wsrp-producer-war/src/main/webapp/WEB-INF/web.xml 2010-11-18 11:55:43 UTC (rev 5158)
@@ -54,7 +54,18 @@
<load-on-startup>1</load-on-startup>
</servlet>-->
+ <!-- Resource serving -->
+ <servlet>
+ <servlet-name>ResourceService</servlet-name>
+ <servlet-class>org.gatein.wsrp.producer.resources.ResourceServingServlet</servlet-class>
+ </servlet>
+ <servlet-mapping>
+ <servlet-name>ResourceService</servlet-name>
+ <url-pattern>/resource/*</url-pattern>
+ </servlet-mapping>
+
+
<!-- WSRP 2 Endpoints -->
<servlet>
<servlet-name>V2ServiceDescriptionService</servlet-name>
More information about the gatein-commits
mailing list