Author: chris.laprun(a)jboss.com
Date: 2010-08-10 11:35:03 -0400 (Tue, 10 Aug 2010)
New Revision: 3793
Added:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java
Modified:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPResourceURL.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/InvocationHandler.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/ActionHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.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/NavigationalStateUpdatingHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/RenderHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ResourceHandler.java
Log:
- GTNWSRP-58: Refactored InvocationHandler hierarchy to use generics.
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-08-10
13:22:57 UTC (rev 3792)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPResourceURL.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -250,7 +250,7 @@
}
/**
- * Decodes the resource information specified by the encodeResource back into proper
resource values
+ * Decodes the resource information specified by the encodeResource back into proper
resource values todo: improve
*
* @param resourceInfo
*/
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/InvocationHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/InvocationHandler.java 2010-08-10
13:22:57 UTC (rev 3792)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/InvocationHandler.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -59,7 +59,7 @@
* @version $Revision: 13121 $
* @since 2.4 (May 31, 2006)
*/
-public abstract class InvocationHandler
+public abstract class InvocationHandler<Invocation extends PortletInvocation, Request,
Response>
{
protected final WSRPConsumerImpl consumer;
@@ -81,28 +81,49 @@
this.consumer = consumer;
}
- PortletInvocationResponse handle(PortletInvocation invocation) throws
PortletInvokerException
+ public PortletInvocationResponse handle(Invocation invocation) throws
PortletInvokerException
{
// Extracts basic required information from invocation
- RequestPrecursor requestPrecursor = new RequestPrecursor(consumer, invocation);
+ RequestPrecursor<Invocation> requestPrecursor = new
RequestPrecursor<Invocation>(consumer, invocation);
// create the specific request
- Object request = prepareRequest(requestPrecursor, invocation);
+ Request request = prepareRequest(requestPrecursor, invocation);
// Perform the request
- Object response = performRequest(request, invocation);
- if (response instanceof ErrorResponse)
+ try
{
+ Response response = performRequest(request, invocation);
+ return processResponse(response, invocation, requestPrecursor);
+ }
+ catch (Exception e)
+ {
+ if (!(e instanceof PortletInvokerException))
+ {
+ ErrorResponse errorResponse = dealWithError(e, invocation,
getRuntimeContextFrom(request));
+ if (errorResponse != null)
+ {
+ return unwrapWSRPError(errorResponse);
+ }
+ else
+ {
+ return new ErrorResponse(e);
+ }
+ }
+ else
+ {
+ throw (PortletInvokerException)e;
+ }
+ }
+ /*if (response instanceof ErrorResponse)
+ {
return unwrapWSRPError((ErrorResponse)response);
- }
-
- return processResponse(response, invocation, requestPrecursor);
+ }*/
}
- protected Object performRequest(Object request, PortletInvocation invocation) throws
PortletInvokerException
+ protected Response performRequest(Request request, PortletInvocation invocation)
throws Exception
{
int retryCount = 0;
- Object response = null;
+ Response response = null;
// as long as we don't get a non-null response and we're allowed to try
again, try to perform the request
while (response == null && retryCount++ <= MAXIMUM_RETRY_NUMBER)
@@ -140,14 +161,14 @@
sessionHandler.updateCookiesIfNeeded(invocation);
}
- catch (Exception e)
+ /*catch (Exception e)
{
ErrorResponse errorResponse = dealWithError(e, invocation, runtimeContext);
if (errorResponse != null)
{
return errorResponse;
}
- }
+ }*/
finally
{
// we're done: reset currently held information
@@ -157,9 +178,12 @@
if (retryCount >= MAXIMUM_RETRY_NUMBER)
{
- return new ErrorResponse(new RuntimeException("Tried to perform request
" + MAXIMUM_RETRY_NUMBER
+ /*return new ErrorResponse(new RuntimeException("Tried to perform request
" + MAXIMUM_RETRY_NUMBER
+ " times before giving up. This usually happens if an error in the WS
stack prevented the messages to be " +
- "properly transmitted. Look at server.log for clues as to what
happened..."));
+ "properly transmitted. Look at server.log for clues as to what
happened..."));*/
+ throw new RuntimeException("Tried to perform request " +
MAXIMUM_RETRY_NUMBER
+ + " times before giving up. This usually happens if an error in the WS
stack prevented the messages to be " +
+ "properly transmitted. Look at server.log for clues as to what
happened...");
}
if (debug)
@@ -189,7 +213,7 @@
* @return an ErrorResponse if the error couldn't be dealt with or
<code>null</code> if the error was correctly
* handled
*/
- private ErrorResponse dealWithError(Exception error, PortletInvocation invocation,
RuntimeContext runtimeContext)
+ private ErrorResponse dealWithError(Exception error, Invocation invocation,
RuntimeContext runtimeContext)
throws PortletInvokerException
{
log.error("The portlet threw an exception", error);
@@ -255,17 +279,17 @@
}
}
- protected abstract void updateUserContext(Object request, UserContext userContext);
+ protected abstract void updateUserContext(Request request, UserContext userContext);
- protected abstract void updateRegistrationContext(Object request) throws
PortletInvokerException;
+ protected abstract void updateRegistrationContext(Request request) throws
PortletInvokerException;
- protected abstract RuntimeContext getRuntimeContextFrom(Object request);
+ protected abstract RuntimeContext getRuntimeContextFrom(Request request);
- protected abstract Object performRequest(Object request) throws Exception;
+ protected abstract Response performRequest(Request request) throws Exception;
- protected abstract Object prepareRequest(RequestPrecursor requestPrecursor,
PortletInvocation invocation);
+ protected abstract Request prepareRequest(RequestPrecursor<Invocation>
requestPrecursor, Invocation invocation);
- protected abstract PortletInvocationResponse processResponse(Object response,
PortletInvocation invocation, RequestPrecursor requestPrecursor) throws
PortletInvokerException;
+ protected abstract PortletInvocationResponse processResponse(Response response,
Invocation invocation, RequestPrecursor<Invocation> requestPrecursor) throws
PortletInvokerException;
/**
* Extracts basic required elements for all invocation requests.
@@ -274,7 +298,7 @@
* @version $Revision: 13121 $
* @since 2.4
*/
- protected static class RequestPrecursor
+ protected static class RequestPrecursor<Invocation extends PortletInvocation>
{
private final static Logger log = LoggerFactory.getLogger(RequestPrecursor.class);
@@ -288,7 +312,7 @@
private static final String STREAM_INFO = "stream info in invocation
context";
private static final String USER_AGENT = "User-Agent";
- public RequestPrecursor(WSRPConsumerImpl wsrpConsumer, PortletInvocation
invocation) throws PortletInvokerException
+ public RequestPrecursor(WSRPConsumerImpl wsrpConsumer, Invocation invocation)
throws PortletInvokerException
{
// retrieve handle
portletContext =
WSRPUtils.convertToWSRPPortletContext(WSRPConsumerImpl.getPortletContext(invocation));
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-08-10
13:22:57 UTC (rev 3792)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/WSRPConsumerImpl.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -30,12 +30,7 @@
import org.gatein.pc.api.PortletContext;
import org.gatein.pc.api.PortletInvokerException;
import org.gatein.pc.api.PortletStateType;
-import org.gatein.pc.api.invocation.ActionInvocation;
-import org.gatein.pc.api.invocation.EventInvocation;
-import org.gatein.pc.api.invocation.InvocationException;
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.PortletInvocationResponse;
import org.gatein.pc.api.spi.UserContext;
import org.gatein.pc.api.state.DestroyCloneFailure;
@@ -49,10 +44,7 @@
import org.gatein.wsrp.WSRPTypeFactory;
import org.gatein.wsrp.WSRPUtils;
import org.gatein.wsrp.api.SessionEvent;
-import org.gatein.wsrp.consumer.handlers.ActionHandler;
-import org.gatein.wsrp.consumer.handlers.EventHandler;
-import org.gatein.wsrp.consumer.handlers.RenderHandler;
-import org.gatein.wsrp.consumer.handlers.ResourceHandler;
+import org.gatein.wsrp.consumer.handlers.InvocationDispatcher;
import org.gatein.wsrp.consumer.portlet.WSRPPortlet;
import org.gatein.wsrp.consumer.portlet.info.WSRPPortletInfo;
import org.gatein.wsrp.services.MarkupService;
@@ -93,12 +85,10 @@
*/
public class WSRPConsumerImpl implements WSRPConsumer
{
- private final ActionHandler actionHandler;
- private final RenderHandler renderHandler;
- private final ResourceHandler resourceHandler;
private final SessionHandler sessionHandler;
- private final EventHandler eventHandler;
+ private final InvocationDispatcher dispatcher;
+
private ProducerInfo producerInfo;
/** A registration data element used to indicate when no registration was required by
the producer */
@@ -135,11 +125,8 @@
ParameterValidation.throwIllegalArgExceptionIfNull(info,
"ProducerInfo");
producerInfo = info;
- actionHandler = new ActionHandler(this);
- renderHandler = new RenderHandler(this);
sessionHandler = new SessionHandler(this);
- resourceHandler = new ResourceHandler(this);
- eventHandler = new EventHandler(this);
+ dispatcher = new InvocationDispatcher(this);
}
public ProducerInfo getProducerInfo()
@@ -180,30 +167,7 @@
public PortletInvocationResponse invoke(PortletInvocation invocation) throws
PortletInvokerException
{
- InvocationHandler handler;
-
- if (invocation instanceof RenderInvocation)
- {
- handler = renderHandler;
- }
- else if (invocation instanceof ActionInvocation)
- {
- handler = actionHandler;
- }
- else if (invocation instanceof ResourceInvocation)
- {
- handler = resourceHandler;
- }
- else if (invocation instanceof EventInvocation)
- {
- handler = eventHandler;
- }
- else
- {
- throw new InvocationException("Unknown invocation type: " +
invocation);
- }
-
- return handler.handle(invocation);
+ return dispatcher.dispatchAndHandle(invocation);
}
public PortletContext createClone(PortletStateType stateType, PortletContext
portletContext) throws IllegalArgumentException, PortletInvokerException,
UnsupportedOperationException
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ActionHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ActionHandler.java 2010-08-10
13:22:57 UTC (rev 3792)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ActionHandler.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -31,7 +31,6 @@
import org.gatein.pc.api.PortletInvokerException;
import org.gatein.pc.api.StateString;
import org.gatein.pc.api.invocation.ActionInvocation;
-import org.gatein.pc.api.invocation.PortletInvocation;
import org.gatein.pc.api.invocation.response.ErrorResponse;
import org.gatein.pc.api.invocation.response.HTTPRedirectionResponse;
import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
@@ -67,16 +66,60 @@
* @version $Revision: 13121 $
* @since 2.4 (May 31, 2006)
*/
-public class ActionHandler extends NavigationalStateUpdatingHandler
+public class ActionHandler extends NavigationalStateUpdatingHandler<ActionInvocation,
PerformBlockingInteraction, BlockingInteractionResponse>
{
public ActionHandler(WSRPConsumerImpl consumer)
{
super(consumer);
}
- @SuppressWarnings({"CastToConcreteClass"})
- protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
+ @Override
+ protected void updateUserContext(PerformBlockingInteraction
performBlockingInteraction, UserContext userContext)
{
+ performBlockingInteraction.setUserContext(userContext);
+ }
+
+ @Override
+ protected void updateRegistrationContext(PerformBlockingInteraction
performBlockingInteraction) throws PortletInvokerException
+ {
+
performBlockingInteraction.setRegistrationContext(consumer.getRegistrationContext());
+ }
+
+ @Override
+ protected RuntimeContext getRuntimeContextFrom(PerformBlockingInteraction
performBlockingInteraction)
+ {
+ return performBlockingInteraction.getRuntimeContext();
+ }
+
+ @Override
+ protected BlockingInteractionResponse performRequest(PerformBlockingInteraction
interaction) throws Exception
+ {
+ Holder<UpdateResponse> updateResponseHolder = new
Holder<UpdateResponse>();
+ Holder<String> redirectURL = new Holder<String>();
+
+ // invocation
+ if (debug)
+ {
+ log.debug("performBlockingInteraction on '" +
interaction.getPortletContext().getPortletHandle() + "'");
+ }
+
consumer.getMarkupService().performBlockingInteraction(interaction.getRegistrationContext(),
+ interaction.getPortletContext(), interaction.getRuntimeContext(),
interaction.getUserContext(),
+ interaction.getMarkupParams(), interaction.getInteractionParams(),
updateResponseHolder, redirectURL,
+ new Holder<List<Extension>>());
+
+ // construct response
+ if (redirectURL.value != null)
+ {
+ return WSRPTypeFactory.createBlockingInteractionResponse(redirectURL.value);
+ }
+ else
+ {
+ return
WSRPTypeFactory.createBlockingInteractionResponse(updateResponseHolder.value);
+ }
+ }
+
+ protected PerformBlockingInteraction
prepareRequest(RequestPrecursor<ActionInvocation> requestPrecursor, ActionInvocation
invocation)
+ {
if (!(invocation instanceof ActionInvocation))
{
throw new IllegalArgumentException("ActionHandler can only handle
ActionInvocations!");
@@ -219,7 +262,7 @@
requestPrecursor.getMarkupParams(), interactionParams);
}
- protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, RequestPrecursor requestPrecursor) throws PortletInvokerException
+ protected PortletInvocationResponse processResponse(BlockingInteractionResponse
response, ActionInvocation invocation, RequestPrecursor<ActionInvocation>
requestPrecursor) throws PortletInvokerException
{
BlockingInteractionResponse blockingInteractionResponse =
(BlockingInteractionResponse)response;
@@ -247,58 +290,6 @@
}
}
- protected void updateUserContext(Object request, UserContext userContext)
- {
- getActionRequest(request).setUserContext(userContext);
- }
-
- protected void updateRegistrationContext(Object request) throws
PortletInvokerException
- {
-
getActionRequest(request).setRegistrationContext(consumer.getRegistrationContext());
- }
-
- protected RuntimeContext getRuntimeContextFrom(Object request)
- {
- return getActionRequest(request).getRuntimeContext();
- }
-
- protected Object performRequest(Object request) throws Exception
- {
- PerformBlockingInteraction interaction = getActionRequest(request);
- Holder<UpdateResponse> updateResponseHolder = new
Holder<UpdateResponse>();
- Holder<String> redirectURL = new Holder<String>();
-
- // invocation
- if (debug)
- {
- log.debug("performBlockingInteraction on '" +
interaction.getPortletContext().getPortletHandle() + "'");
- }
-
consumer.getMarkupService().performBlockingInteraction(interaction.getRegistrationContext(),
- interaction.getPortletContext(), interaction.getRuntimeContext(),
interaction.getUserContext(),
- interaction.getMarkupParams(), interaction.getInteractionParams(),
updateResponseHolder, redirectURL,
- new Holder<List<Extension>>());
-
- // construct response
- if (redirectURL.value != null)
- {
- return WSRPTypeFactory.createBlockingInteractionResponse(redirectURL.value);
- }
- else
- {
- return
WSRPTypeFactory.createBlockingInteractionResponse(updateResponseHolder.value);
- }
- }
-
- private PerformBlockingInteraction getActionRequest(Object request)
- {
- if (request instanceof PerformBlockingInteraction)
- {
- return (PerformBlockingInteraction)request;
- }
-
- throw new IllegalArgumentException("ActionHandler: request is not a
PerformBlockingInteraction request!");
- }
-
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision: 10198 $
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java 2010-08-10
13:22:57 UTC (rev 3792)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -26,7 +26,6 @@
import org.gatein.common.util.ParameterValidation;
import org.gatein.pc.api.PortletInvokerException;
import org.gatein.pc.api.invocation.EventInvocation;
-import org.gatein.pc.api.invocation.PortletInvocation;
import org.gatein.pc.api.invocation.response.ErrorResponse;
import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
import org.gatein.pc.api.spi.InstanceContext;
@@ -56,7 +55,7 @@
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision$
*/
-public class EventHandler extends NavigationalStateUpdatingHandler
+public class EventHandler extends NavigationalStateUpdatingHandler<EventInvocation,
HandleEvents, HandleEventsResponse>
{
public EventHandler(WSRPConsumerImpl consumer)
{
@@ -64,38 +63,36 @@
}
@Override
- protected void updateUserContext(Object request, UserContext userContext)
+ protected void updateUserContext(HandleEvents request, UserContext userContext)
{
- getHandleEvents(request).setUserContext(userContext);
+ request.setUserContext(userContext);
}
@Override
- protected void updateRegistrationContext(Object request) throws
PortletInvokerException
+ protected void updateRegistrationContext(HandleEvents request) throws
PortletInvokerException
{
-
getHandleEvents(request).setRegistrationContext(consumer.getRegistrationContext());
+ request.setRegistrationContext(consumer.getRegistrationContext());
}
@Override
- protected RuntimeContext getRuntimeContextFrom(Object request)
+ protected RuntimeContext getRuntimeContextFrom(HandleEvents request)
{
- return getHandleEvents(request).getRuntimeContext();
+ return request.getRuntimeContext();
}
@Override
- protected Object performRequest(Object request) throws Exception
+ protected HandleEventsResponse performRequest(HandleEvents request) throws Exception
{
- HandleEvents eventRequest = getHandleEvents(request);
-
if (InvocationHandler.debug)
{
- InvocationHandler.log.debug("handleEvents on '" +
eventRequest.getPortletContext().getPortletHandle() + "'");
+ InvocationHandler.log.debug("handleEvents on '" +
request.getPortletContext().getPortletHandle() + "'");
}
Holder<List<HandleEventsFailed>> failedEvents = new
Holder<List<HandleEventsFailed>>();
Holder<UpdateResponse> updateResponse = new Holder<UpdateResponse>();
- consumer.getMarkupService().handleEvents(eventRequest.getRegistrationContext(),
eventRequest.getPortletContext(),
- eventRequest.getRuntimeContext(), eventRequest.getUserContext(),
eventRequest.getMarkupParams(),
- eventRequest.getEventParams(), updateResponse, failedEvents,
+ consumer.getMarkupService().handleEvents(request.getRegistrationContext(),
request.getPortletContext(),
+ request.getRuntimeContext(), request.getUserContext(),
request.getMarkupParams(),
+ request.getEventParams(), updateResponse, failedEvents,
new Holder<List<Extension>>());
HandleEventsResponse response = WSRPTypeFactory.createHandleEventsReponse();
@@ -108,15 +105,9 @@
}
@Override
- protected Object prepareRequest(InvocationHandler.RequestPrecursor requestPrecursor,
PortletInvocation invocation)
+ protected HandleEvents prepareRequest(RequestPrecursor<EventInvocation>
requestPrecursor, EventInvocation invocation)
{
- if (!(invocation instanceof EventInvocation))
- {
- throw new IllegalArgumentException("EventHandler can only handle
EventInvocations!");
- }
- EventInvocation eventInvocation = (EventInvocation)invocation;
-
PortletContext portletContext = requestPrecursor.getPortletContext();
if (InvocationHandler.debug)
{
@@ -134,8 +125,8 @@
}
// events
- QName name = eventInvocation.getName();
- Serializable payload = eventInvocation.getPayload();
+ QName name = invocation.getName();
+ Serializable payload = invocation.getPayload();
Event event = WSRPTypeFactory.createEvent(name, payload);
EventParams eventParams =
WSRPTypeFactory.createEventParams(Collections.singletonList(event),
WSRPUtils.getStateChangeFromAccessMode(accessMode));
@@ -144,26 +135,14 @@
}
@Override
- protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, InvocationHandler.RequestPrecursor requestPrecursor) throws
PortletInvokerException
+ protected PortletInvocationResponse processResponse(HandleEventsResponse response,
EventInvocation invocation, RequestPrecursor<EventInvocation> requestPrecursor)
throws PortletInvokerException
{
- HandleEventsResponse handleEventsResponse = (HandleEventsResponse)response;
-
- List<HandleEventsFailed> failed = handleEventsResponse.getFailedEvents();
+ List<HandleEventsFailed> failed = response.getFailedEvents();
if (ParameterValidation.existsAndIsNotEmpty(failed))
{
return new ErrorResponse("Couldn't process events: " + failed);
}
- return processUpdateResponse(invocation, requestPrecursor,
handleEventsResponse.getUpdateResponse());
+ return processUpdateResponse(invocation, requestPrecursor,
response.getUpdateResponse());
}
-
- private HandleEvents getHandleEvents(Object request)
- {
- if (request instanceof HandleEvents)
- {
- return (HandleEvents)request;
- }
-
- throw new IllegalArgumentException("EventHandler: request is not a
HandleEvents request!");
- }
}
Added:
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
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -0,0 +1,201 @@
+/*
+ * 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.TypeDef;
+import org.gatein.common.util.MultiValuedPropertyMap;
+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;
+import org.gatein.pc.api.invocation.InvocationException;
+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.InvocationHandler;
+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;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class InvocationDispatcher
+{
+ private final ActionHandler actionHandler;
+ private final RenderHandler renderHandler;
+ private final ResourceHandler resourceHandler;
+ private final EventHandler eventHandler;
+ private final WSRPConsumerImpl consumer;
+
+ public InvocationDispatcher(WSRPConsumerImpl consumer)
+ {
+ this.consumer = consumer;
+ actionHandler = new ActionHandler(consumer);
+ renderHandler = new RenderHandler(consumer);
+ resourceHandler = new ResourceHandler(consumer);
+ eventHandler = new EventHandler(consumer);
+ }
+
+ public PortletInvocationResponse dispatchAndHandle(PortletInvocation invocation)
throws PortletInvokerException
+ {
+ InvocationHandler handler;
+
+ if (invocation instanceof RenderInvocation)
+ {
+ handler = renderHandler;
+ }
+ else if (invocation instanceof ActionInvocation)
+ {
+ handler = actionHandler;
+ }
+ else if (invocation instanceof ResourceInvocation)
+ {
+ String resourceInvocationId = ((ResourceInvocation)invocation).getResourceId();
+
+ Map<String, String> resourceMap =
WSRPResourceURL.decodeResource(resourceInvocationId);
+
+ String resourceId = resourceMap.get(WSRP2RewritingConstants.RESOURCE_ID);
+ String resourceURL = resourceMap.get(WSRPRewritingConstants.RESOURCE_URL);
+ String preferOperationAsString =
resourceMap.get(WSRP2RewritingConstants.RESOURCE_PREFER_OPERATION);
+ boolean preferOperation = (preferOperationAsString != null &&
Boolean.parseBoolean(preferOperationAsString));
+
+ int version = consumer.getMarkupService().getVersion();
+ if (version == 2 && (preferOperation || resourceURL == null ||
(resourceId != null && resourceId.length() > 0)))
+ {
+ handler = resourceHandler;
+ }
+ else
+ {
+ return performDirectURLRequest(resourceURL);
+ }
+ }
+ else if (invocation instanceof EventInvocation)
+ {
+ handler = eventHandler;
+ }
+ else
+ {
+ throw new InvocationException("Unknown invocation type: " +
invocation);
+ }
+
+ 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/MimeResponseHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java 2010-08-10
13:22:57 UTC (rev 3792)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -52,7 +52,7 @@
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision$
*/
-public abstract class MimeResponseHandler<Response, LocalMimeResponse extends
MimeResponse> extends InvocationHandler
+public abstract class MimeResponseHandler<Invocation extends PortletInvocation,
Request, Response, LocalMimeResponse extends MimeResponse> extends
InvocationHandler<Invocation, Request, Response>
{
private static final org.gatein.pc.api.cache.CacheControl DEFAULT_CACHE_CONTROL = new
org.gatein.pc.api.cache.CacheControl(0, CacheScope.PRIVATE, null);
@@ -66,14 +66,12 @@
protected abstract LocalMimeResponse getMimeResponseFrom(Response response);
@Override
- protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, RequestPrecursor requestPrecursor) throws PortletInvokerException
+ protected PortletInvocationResponse processResponse(Response response, Invocation
invocation, RequestPrecursor<Invocation> requestPrecursor) throws
PortletInvokerException
{
- Response localResponse = (Response)response;
-
-
consumer.getSessionHandler().updateSessionIfNeeded(getSessionContextFrom(localResponse),
invocation,
+ consumer.getSessionHandler().updateSessionIfNeeded(getSessionContextFrom(response),
invocation,
requestPrecursor.getPortletHandle());
- LocalMimeResponse mimeResponse = getMimeResponseFrom(localResponse);
+ LocalMimeResponse mimeResponse = getMimeResponseFrom(response);
String markup = mimeResponse.getItemString();
byte[] binary = mimeResponse.getItemBinary();
if (markup != null && binary != null)
@@ -119,7 +117,7 @@
return createContentResponse(mimeResponse, invocation, null, null, mimeType,
binary, markup, createCacheControl(mimeResponse));
}
- protected PortletInvocationResponse createContentResponse(LocalMimeResponse
mimeResponse, PortletInvocation invocation,
+ protected PortletInvocationResponse createContentResponse(LocalMimeResponse
mimeResponse, Invocation invocation,
ResponseProperties
properties, Map<String, Object> attributes,
String mimeType, byte[]
bytes, String markup,
org.gatein.pc.api.cache.CacheControl cacheControl)
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java 2010-08-10
13:22:57 UTC (rev 3792)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -48,14 +48,14 @@
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision$
*/
-public abstract class NavigationalStateUpdatingHandler extends InvocationHandler
+public abstract class NavigationalStateUpdatingHandler<Invocation extends
PortletInvocation, Request, Response> extends InvocationHandler<Invocation, Request,
Response>
{
public NavigationalStateUpdatingHandler(WSRPConsumerImpl consumer)
{
super(consumer);
}
- protected UpdateNavigationalStateResponse processUpdateResponse(PortletInvocation
invocation, RequestPrecursor requestPrecursor, UpdateResponse updateResponse)
+ protected UpdateNavigationalStateResponse processUpdateResponse(Invocation invocation,
RequestPrecursor<Invocation> requestPrecursor, UpdateResponse updateResponse)
{
UpdateNavigationalStateResponse result = new UpdateNavigationalStateResponse();
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/RenderHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/RenderHandler.java 2010-08-10
13:22:57 UTC (rev 3792)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/RenderHandler.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -24,7 +24,6 @@
package org.gatein.wsrp.consumer.handlers;
import org.gatein.pc.api.PortletInvokerException;
-import org.gatein.pc.api.invocation.PortletInvocation;
import org.gatein.pc.api.invocation.RenderInvocation;
import org.gatein.pc.api.invocation.response.FragmentResponse;
import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
@@ -49,7 +48,7 @@
* @version $Revision: 12082 $
* @since 2.4 (May 31, 2006)
*/
-public class RenderHandler extends MimeResponseHandler<MarkupResponse,
MarkupContext>
+public class RenderHandler extends MimeResponseHandler<RenderInvocation, GetMarkup,
MarkupResponse, MarkupContext>
{
public RenderHandler(WSRPConsumerImpl consumer)
@@ -70,7 +69,7 @@
}
@Override
- protected PortletInvocationResponse createContentResponse(MarkupContext markupContext,
PortletInvocation invocation,
+ protected PortletInvocationResponse createContentResponse(MarkupContext markupContext,
RenderInvocation invocation,
ResponseProperties
properties, Map<String, Object> attributes,
String mimeType, byte[]
bytes, String markup,
org.gatein.pc.api.cache.CacheControl cacheControl)
@@ -79,13 +78,8 @@
cacheControl, invocation.getPortalContext().getModes());
}
- protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
+ protected GetMarkup prepareRequest(RequestPrecursor<RenderInvocation>
requestPrecursor, RenderInvocation invocation)
{
- if (!(invocation instanceof RenderInvocation))
- {
- throw new IllegalArgumentException("RenderHandler can only handle
RenderInvocations!");
- }
-
// Create the markup request
PortletContext portletContext = requestPrecursor.getPortletContext();
if (debug)
@@ -95,49 +89,37 @@
return WSRPTypeFactory.createMarkupRequest(portletContext,
requestPrecursor.getRuntimeContext(), requestPrecursor.getMarkupParams());
}
- protected void updateUserContext(Object request, UserContext userContext)
+ protected void updateUserContext(GetMarkup request, UserContext userContext)
{
- getRenderRequest(request).setUserContext(userContext);
+ request.setUserContext(userContext);
}
- protected void updateRegistrationContext(Object request) throws
PortletInvokerException
+ protected void updateRegistrationContext(GetMarkup request) throws
PortletInvokerException
{
-
getRenderRequest(request).setRegistrationContext(consumer.getRegistrationContext());
+ request.setRegistrationContext(consumer.getRegistrationContext());
}
- protected RuntimeContext getRuntimeContextFrom(Object request)
+ protected RuntimeContext getRuntimeContextFrom(GetMarkup request)
{
- return getRenderRequest(request).getRuntimeContext();
+ return request.getRuntimeContext();
}
- protected Object performRequest(Object request) throws Exception
+ protected MarkupResponse performRequest(GetMarkup request) throws Exception
{
- GetMarkup renderRequest = getRenderRequest(request);
if (debug)
{
- log.debug("getMarkup on '" +
renderRequest.getPortletContext().getPortletHandle() + "'");
+ log.debug("getMarkup on '" +
request.getPortletContext().getPortletHandle() + "'");
}
// invocation
Holder<SessionContext> sessionContextHolder = new
Holder<SessionContext>();
Holder<MarkupContext> markupContextHolder = new
Holder<MarkupContext>();
- consumer.getMarkupService().getMarkup(renderRequest.getRegistrationContext(),
renderRequest.getPortletContext(),
- renderRequest.getRuntimeContext(), renderRequest.getUserContext(),
renderRequest.getMarkupParams(),
+ consumer.getMarkupService().getMarkup(request.getRegistrationContext(),
request.getPortletContext(),
+ request.getRuntimeContext(), request.getUserContext(),
request.getMarkupParams(),
markupContextHolder, sessionContextHolder, new
Holder<List<Extension>>());
MarkupResponse markupResponse = new MarkupResponse();
markupResponse.setMarkupContext(markupContextHolder.value);
markupResponse.setSessionContext(sessionContextHolder.value);
return markupResponse;
}
-
- private GetMarkup getRenderRequest(Object request)
- {
- if (request instanceof GetMarkup)
- {
- return (GetMarkup)request;
- }
-
- throw new IllegalArgumentException("RenderHandler: Request is not a GetMarkup
request!");
- }
-
}
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-08-10
13:22:57 UTC (rev 3792)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ResourceHandler.java 2010-08-10
15:35:03 UTC (rev 3793)
@@ -23,28 +23,16 @@
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.StateString;
-import org.gatein.pc.api.invocation.PortletInvocation;
import org.gatein.pc.api.invocation.ResourceInvocation;
-import org.gatein.pc.api.invocation.response.ContentResponse;
-import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
-import org.gatein.pc.api.invocation.response.ResponseProperties;
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;
-import org.gatein.wsrp.handler.CookieUtil;
import org.gatein.wsrp.spec.v2.WSRP2RewritingConstants;
import org.oasis.wsrp.v2.Extension;
import org.oasis.wsrp.v2.GetResource;
@@ -58,9 +46,6 @@
import org.oasis.wsrp.v2.UserContext;
import javax.xml.ws.Holder;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -68,7 +53,7 @@
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision$
*/
-public class ResourceHandler extends MimeResponseHandler<ResourceResponse,
ResourceContext>
+public class ResourceHandler extends MimeResponseHandler<ResourceInvocation,
GetResource, ResourceResponse, ResourceContext>
{
public ResourceHandler(WSRPConsumerImpl consumer)
@@ -89,78 +74,30 @@
}
@Override
- protected void updateUserContext(Object request, UserContext userContext)
+ protected void updateUserContext(GetResource request, UserContext userContext)
{
- if (request instanceof GetResource)
- {
- getResourceRequest(request).setUserContext(userContext);
- }
+ request.setUserContext(userContext);
}
@Override
- protected void updateRegistrationContext(Object request) throws
PortletInvokerException
+ protected void updateRegistrationContext(GetResource request) throws
PortletInvokerException
{
- if (request instanceof GetResource)
- {
-
getResourceRequest(request).setRegistrationContext(consumer.getRegistrationContext());
- }
+ request.setRegistrationContext(consumer.getRegistrationContext());
}
@Override
- protected RuntimeContext getRuntimeContextFrom(Object request)
+ protected RuntimeContext getRuntimeContextFrom(GetResource request)
{
- if (request instanceof GetResource)
- {
- return getResourceRequest(request).getRuntimeContext();
- }
- else
- {
- return null;
- }
+ return request.getRuntimeContext();
}
@Override
- protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
+ protected GetResource prepareRequest(RequestPrecursor<ResourceInvocation>
requestPrecursor, ResourceInvocation invocation)
{
- if (!(invocation instanceof ResourceInvocation))
- {
- throw new IllegalArgumentException("ResourceHandler can only handle
ResourceInvocations!");
- }
-
- ResourceInvocation resourceInvocation = (ResourceInvocation)invocation;
-
- String resourceInvocationId = resourceInvocation.getResourceId();
-
+ String resourceInvocationId = invocation.getResourceId();
Map<String, String> resourceMap =
WSRPResourceURL.decodeResource(resourceInvocationId);
-
String resourceId = resourceMap.get(WSRP2RewritingConstants.RESOURCE_ID);
- String resourceURL = resourceMap.get(WSRPRewritingConstants.RESOURCE_URL);
- String preferOperationAsString =
resourceMap.get(WSRP2RewritingConstants.RESOURCE_PREFER_OPERATION);
- boolean preferOperation = (preferOperationAsString != null &&
Boolean.parseBoolean(preferOperationAsString));
- int version = 1;
- try
- {
- version = consumer.getMarkupService().getVersion();
- }
- catch (PortletInvokerException portletInvokerException)
- {
- log.warn("Encountered an exception when trying to get the consumer's
markup service's version, assuming WSRP 1.0 compliant.",
portletInvokerException);
- }
-
- if (version == 2 && (preferOperation || resourceURL == null || (resourceId
!= null && resourceId.length() > 0)))
- {
- return prepareGetResourceRequest(requestPrecursor, resourceInvocation,
resourceId);
- }
- else
- {
- return resourceURL;
- }
-
- }
-
- private GetResource prepareGetResourceRequest(RequestPrecursor requestPrecursor,
ResourceInvocation invocation, String resourceId)
- {
PortletContext portletContext = requestPrecursor.getPortletContext();
// since we actually extracted the data into MarkupParams in the RequestPrecursor,
use that! :)
@@ -205,31 +142,14 @@
}
@Override
- protected Object performRequest(Object request) throws Exception
+ protected ResourceResponse performRequest(GetResource request) throws Exception
{
- if (request instanceof GetResource)
- {
- return performGetResourceRequest((GetResource)request);
- }
- else if (request instanceof String)
- {
- return performURLRequest((String)request);
- }
- else
- {
- throw new IllegalArgumentException("ResourceHandler performRequest can only
be called with a GetResource or String object. Received : " + request);
- }
-
- }
-
- private ResourceResponse performGetResourceRequest(GetResource getResource) throws
Exception
- {
Holder<SessionContext> sessionContextHolder = new
Holder<SessionContext>();
Holder<ResourceContext> resourceContextHolder = new
Holder<ResourceContext>();
- Holder<PortletContext> portletContextHolder = new
Holder<PortletContext>(getResource.getPortletContext());
+ Holder<PortletContext> portletContextHolder = new
Holder<PortletContext>(request.getPortletContext());
- consumer.getMarkupService().getResource(getResource.getRegistrationContext(),
portletContextHolder, getResource.getRuntimeContext(),
- getResource.getUserContext(), getResource.getResourceParams(),
resourceContextHolder, sessionContextHolder, new Holder<List<Extension>>());
+ consumer.getMarkupService().getResource(request.getRegistrationContext(),
portletContextHolder, request.getRuntimeContext(),
+ request.getUserContext(), request.getResourceParams(), resourceContextHolder,
sessionContextHolder, new Holder<List<Extension>>());
ResourceResponse resourceResponse =
WSRPTypeFactory.createResourceResponse(resourceContextHolder.value);
resourceResponse.setPortletContext(portletContextHolder.value);
@@ -237,102 +157,4 @@
return resourceResponse;
}
- private ContentResponse performURLRequest(String resourceURL) throws Exception
- {
- 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;
- }
-
- @Override
- protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, RequestPrecursor requestPrecursor) throws PortletInvokerException
- {
- if (response instanceof ResourceResponse)
- {
- return super.processResponse(response, invocation, requestPrecursor);
- }
- else if (response instanceof ContentResponse)
- {
- return (ContentResponse)response;
- }
- else
- {
- throw new PortletInvokerException("Invalid response object: " +
response + ". Expected either a " + ContentResponse.class + " or a " +
ResourceResponse.class);
- }
- }
-
- private GetResource getResourceRequest(Object request)
- {
- if (request instanceof GetResource)
- {
- return (GetResource)request;
- }
-
- throw new IllegalArgumentException("ResourceHandler: Request is not a
GetResource request!");
- }
}