Author: chris.laprun(a)jboss.com
Date: 2010-08-09 13:50:06 -0400 (Mon, 09 Aug 2010)
New Revision: 3782
Added:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/
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
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/RenderHandlerTestCase.java
Removed:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ActionHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/EventHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MarkupProcessor.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MimeResponseHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/NavigationalStateUpdatingHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RequestContextWrapper.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RequestPrecursor.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ResourceHandler.java
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/RenderHandlerTestCase.java
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/SessionHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/WSRPConsumerImpl.java
Log:
- GTNWSRP-57: Consumer side
+ Moved handlers except InvocationHandler (facade) and SessionHandler to handlers
package.
+ Made MarkupProcessor, RequestContextWrapper and RequestPrecursor inner classes
+ Moved RenderHandlerTestCase to handlers package in test.
Deleted:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ActionHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ActionHandler.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ActionHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,298 +0,0 @@
-/*
- * 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;
-
-import org.apache.commons.fileupload.FileItemIterator;
-import org.apache.commons.fileupload.FileItemStream;
-import org.apache.commons.fileupload.FileUpload;
-import org.apache.commons.fileupload.util.Streams;
-import org.gatein.common.util.ParameterValidation;
-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;
-import org.gatein.pc.api.spi.InstanceContext;
-import org.gatein.pc.api.state.AccessMode;
-import org.gatein.wsrp.WSRPTypeFactory;
-import org.gatein.wsrp.WSRPUtils;
-import org.oasis.wsrp.v2.BlockingInteractionResponse;
-import org.oasis.wsrp.v2.Extension;
-import org.oasis.wsrp.v2.InteractionParams;
-import org.oasis.wsrp.v2.NamedString;
-import org.oasis.wsrp.v2.PerformBlockingInteraction;
-import org.oasis.wsrp.v2.PortletContext;
-import org.oasis.wsrp.v2.RuntimeContext;
-import org.oasis.wsrp.v2.UpdateResponse;
-import org.oasis.wsrp.v2.UploadContext;
-import org.oasis.wsrp.v2.UserContext;
-
-import javax.xml.ws.Holder;
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision: 13121 $
- * @since 2.4 (May 31, 2006)
- */
-public class ActionHandler extends NavigationalStateUpdatingHandler
-{
- protected ActionHandler(WSRPConsumerImpl consumer)
- {
- super(consumer);
- }
-
- @SuppressWarnings({"CastToConcreteClass"})
- protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
- {
- if (!(invocation instanceof ActionInvocation))
- {
- throw new IllegalArgumentException("ActionHandler can only handle
ActionInvocations!");
- }
-
- ActionInvocation actionInvocation = (ActionInvocation)invocation;
-
- PortletContext portletContext = requestPrecursor.getPortletContext();
- if (debug)
- {
- log.debug("Consumer about to attempt action on portlet '" +
portletContext.getPortletHandle() + "'");
- }
-
- // access mode
- InstanceContext instanceContext = invocation.getInstanceContext();
- ParameterValidation.throwIllegalArgExceptionIfNull(instanceContext, "instance
context");
- AccessMode accessMode = instanceContext.getAccessMode();
- ParameterValidation.throwIllegalArgExceptionIfNull(accessMode, "access
mode");
- if (debug)
- {
- log.debug("Portlet is requesting " + accessMode + " access
mode");
- }
- InteractionParams interactionParams =
WSRPTypeFactory.createInteractionParams(WSRPUtils.getStateChangeFromAccessMode(accessMode));
-
- // interaction state
- StateString interactionState = actionInvocation.getInteractionState();
- if (interactionState != null)
- {
- String state = interactionState.getStringValue();
- if (!StateString.JBPNS_PREFIX.equals(state)) // fix-me: see JBPORTAL-900
- {
- interactionParams.setInteractionState(state);
- }
- }
-
- // check for multi-part
- RequestContextWrapper requestContext = new
RequestContextWrapper(actionInvocation.getRequestContext());
- try
- {
- if (FileUpload.isMultipartContent(requestContext))
- {
- // content is multipart, we need to parse it (that includes form parameters)
- FileUpload upload = new FileUpload();
- FileItemIterator iter = upload.getItemIterator(requestContext);
- List<UploadContext> uploadContexts = new
ArrayList<UploadContext>(7);
- List<NamedString> formParameters = new
ArrayList<NamedString>(7);
- while (iter.hasNext())
- {
- FileItemStream item = iter.next();
- InputStream stream = item.openStream();
- if (!item.isFormField())
- {
- String contentType = item.getContentType();
- if (debug)
- {
- log.debug("File field " + item.getFieldName() + "
with file name " + item.getName() + " and content type "
- + contentType + " detected.");
- }
-
- BufferedInputStream bufIn = new BufferedInputStream(stream);
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- BufferedOutputStream bos = new BufferedOutputStream(baos);
-
- int c = bufIn.read();
- while (c != -1)
- {
- bos.write(c);
- c = bufIn.read();
- }
-
- bos.flush();
- baos.flush();
- bufIn.close();
- bos.close();
-
- UploadContext uploadContext =
WSRPTypeFactory.createUploadContext(contentType, baos.toByteArray());
-
- List<NamedString> mimeAttributes = new
ArrayList<NamedString>(2);
-
- String value = FileUpload.FORM_DATA + ";"
- + " name=\"" + item.getFieldName() +
"\";"
- + " filename=\"" + item.getName() +
"\"";
- NamedString mimeAttribute =
WSRPTypeFactory.createNamedString(FileUpload.CONTENT_DISPOSITION, value);
- mimeAttributes.add(mimeAttribute);
-
- mimeAttribute =
WSRPTypeFactory.createNamedString(FileUpload.CONTENT_TYPE, item.getContentType());
- mimeAttributes.add(mimeAttribute);
-
- uploadContext.getMimeAttributes().addAll(mimeAttributes);
-
- uploadContexts.add(uploadContext);
- }
- else
- {
- NamedString formParameter =
WSRPTypeFactory.createNamedString(item.getFieldName(), Streams.asString(stream));
- formParameters.add(formParameter);
- }
- }
- interactionParams.getUploadContexts().addAll(uploadContexts);
- interactionParams.getFormParameters().addAll(formParameters);
- }
- else
- {
- // if the content is not multipart, then check for form parameters
- Map<String, String[]> params = actionInvocation.getForm();
- if (params != null && !params.isEmpty())
- {
- int capacity = params.size();
- List<NamedString> formParameters = new
ArrayList<NamedString>(capacity);
- for (Map.Entry param : params.entrySet())
- {
- String name = (String)param.getKey();
- String[] values = (String[])param.getValue();
- NamedString formParameter;
- for (String value : values)
- {
- formParameter = WSRPTypeFactory.createNamedString(name, value);
- formParameters.add(formParameter);
- }
- }
- interactionParams.getFormParameters().addAll(formParameters);
- }
- }
- }
- catch (Exception e)
- {
- log.debug("Couldn't create UploadContext", e);
- }
-
- // todo: need to deal with GET method in forms
-
- if (trace)
- {
- log.trace(WSRPUtils.toString(interactionParams));
- }
-
- // Create the blocking action request
- return WSRPTypeFactory.createPerformBlockingInteraction(portletContext,
requestPrecursor.runtimeContext,
- requestPrecursor.markupParams, interactionParams);
- }
-
- protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, RequestPrecursor requestPrecursor) throws PortletInvokerException
- {
- BlockingInteractionResponse blockingInteractionResponse =
(BlockingInteractionResponse)response;
-
- String redirectURL = blockingInteractionResponse.getRedirectURL();
- UpdateResponse updateResponse = blockingInteractionResponse.getUpdateResponse();
- if (redirectURL != null && updateResponse != null)
- {
- return new ErrorResponse(new IllegalArgumentException("Producer error:
response cannot both redirect and update state."));
- }
-
- if (redirectURL == null && updateResponse == null)
- {
- return new ErrorResponse(new IllegalArgumentException("Producer error:
response must redirect or update state."));
- }
-
- if (redirectURL != null)
- {
- return new HTTPRedirectionResponse(redirectURL); // do we need to process URLs?
- }
- else
- {
- // updateResponse.getMarkupContext(); // ignore bundled markup for now.
-
- return processUpdateResponse(invocation, requestPrecursor, updateResponse);
- }
- }
-
- 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!");
- }
-}
Deleted:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/EventHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/EventHandler.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/EventHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,167 +0,0 @@
-/*
- * 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;
-
-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;
-import org.gatein.pc.api.state.AccessMode;
-import org.gatein.wsrp.WSRPTypeFactory;
-import org.gatein.wsrp.WSRPUtils;
-import org.oasis.wsrp.v2.Event;
-import org.oasis.wsrp.v2.EventParams;
-import org.oasis.wsrp.v2.Extension;
-import org.oasis.wsrp.v2.HandleEvents;
-import org.oasis.wsrp.v2.HandleEventsFailed;
-import org.oasis.wsrp.v2.HandleEventsResponse;
-import org.oasis.wsrp.v2.PortletContext;
-import org.oasis.wsrp.v2.RuntimeContext;
-import org.oasis.wsrp.v2.UpdateResponse;
-import org.oasis.wsrp.v2.UserContext;
-
-import javax.xml.namespace.QName;
-import javax.xml.ws.Holder;
-import java.io.Serializable;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision$
- */
-public class EventHandler extends NavigationalStateUpdatingHandler
-{
- public EventHandler(WSRPConsumerImpl consumer)
- {
- super(consumer);
- }
-
- @Override
- protected void updateUserContext(Object request, UserContext userContext)
- {
- getHandleEvents(request).setUserContext(userContext);
- }
-
- @Override
- protected void updateRegistrationContext(Object request) throws
PortletInvokerException
- {
-
getHandleEvents(request).setRegistrationContext(consumer.getRegistrationContext());
- }
-
- @Override
- protected RuntimeContext getRuntimeContextFrom(Object request)
- {
- return getHandleEvents(request).getRuntimeContext();
- }
-
- @Override
- protected Object performRequest(Object request) throws Exception
- {
- HandleEvents eventRequest = getHandleEvents(request);
-
- if (debug)
- {
- log.debug("handleEvents on '" +
eventRequest.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,
- new Holder<List<Extension>>());
-
- HandleEventsResponse response = WSRPTypeFactory.createHandleEventsReponse();
- response.setUpdateResponse(updateResponse.value);
- if (ParameterValidation.existsAndIsNotEmpty(failedEvents.value))
- {
- response.getFailedEvents().addAll(failedEvents.value);
- }
- return response;
- }
-
- @Override
- protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
- {
- if (!(invocation instanceof EventInvocation))
- {
- throw new IllegalArgumentException("EventHandler can only handle
EventInvocations!");
- }
-
- EventInvocation eventInvocation = (EventInvocation)invocation;
-
- PortletContext portletContext = requestPrecursor.getPortletContext();
- if (debug)
- {
- log.debug("Consumer about to attempt action on portlet '" +
portletContext.getPortletHandle() + "'");
- }
-
- // access mode
- InstanceContext instanceContext = invocation.getInstanceContext();
- ParameterValidation.throwIllegalArgExceptionIfNull(instanceContext, "instance
context");
- AccessMode accessMode = instanceContext.getAccessMode();
- ParameterValidation.throwIllegalArgExceptionIfNull(accessMode, "access
mode");
- if (debug)
- {
- log.debug("Portlet is requesting " + accessMode + " access
mode");
- }
-
- // events
- QName name = eventInvocation.getName();
- Serializable payload = eventInvocation.getPayload();
- Event event = WSRPTypeFactory.createEvent(name, payload);
- EventParams eventParams =
WSRPTypeFactory.createEventParams(Collections.singletonList(event),
WSRPUtils.getStateChangeFromAccessMode(accessMode));
-
- return WSRPTypeFactory.createHandleEvents(portletContext,
requestPrecursor.runtimeContext,
- requestPrecursor.markupParams, eventParams);
- }
-
- @Override
- protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, RequestPrecursor requestPrecursor) throws PortletInvokerException
- {
- HandleEventsResponse handleEventsResponse = (HandleEventsResponse)response;
-
- List<HandleEventsFailed> failed = handleEventsResponse.getFailedEvents();
- if (ParameterValidation.existsAndIsNotEmpty(failed))
- {
- return new ErrorResponse("Couldn't process events: " + failed);
- }
-
- return processUpdateResponse(invocation, requestPrecursor,
handleEventsResponse.getUpdateResponse());
- }
-
- private HandleEvents getHandleEvents(Object request)
- {
- if (request instanceof HandleEvents)
- {
- return (HandleEvents)request;
- }
-
- throw new IllegalArgumentException("EventHandler: request is not a
HandleEvents request!");
- }
-}
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-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/InvocationHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -23,23 +23,36 @@
package org.gatein.wsrp.consumer;
+import org.gatein.common.util.ContentInfo;
+import org.gatein.common.util.ParameterValidation;
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.response.ErrorResponse;
import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
import org.gatein.pc.api.spi.InstanceContext;
+import org.gatein.pc.api.spi.PortletInvocationContext;
+import org.gatein.pc.api.spi.SecurityContext;
import org.gatein.pc.api.spi.WindowContext;
import org.gatein.pc.portlet.impl.jsr168.PortletUtils;
+import org.gatein.wsrp.WSRPConstants;
+import org.gatein.wsrp.WSRPTypeFactory;
+import org.gatein.wsrp.WSRPUtils;
import org.oasis.wsrp.v2.InvalidCookie;
import org.oasis.wsrp.v2.InvalidRegistration;
import org.oasis.wsrp.v2.InvalidSession;
+import org.oasis.wsrp.v2.MarkupParams;
+import org.oasis.wsrp.v2.NavigationalContext;
import org.oasis.wsrp.v2.OperationFailed;
+import org.oasis.wsrp.v2.PortletContext;
import org.oasis.wsrp.v2.RuntimeContext;
import org.oasis.wsrp.v2.UserContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.rmi.RemoteException;
+import java.util.Collections;
+import java.util.Map;
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
@@ -48,7 +61,7 @@
*/
public abstract class InvocationHandler
{
- protected WSRPConsumerImpl consumer;
+ protected final WSRPConsumerImpl consumer;
protected static Logger log = LoggerFactory.getLogger(InvocationHandler.class);
protected static boolean debug = log.isDebugEnabled();
@@ -63,7 +76,6 @@
/** Maximum number of tries before giving up. */
private static final int MAXIMUM_RETRY_NUMBER = 3;
-
protected InvocationHandler(WSRPConsumerImpl consumer)
{
this.consumer = consumer;
@@ -157,7 +169,7 @@
return response;
}
- static String getNamespaceFrom(WindowContext windowContext)
+ protected static String getNamespaceFrom(WindowContext windowContext)
{
if (windowContext != null)
{
@@ -255,4 +267,125 @@
protected abstract PortletInvocationResponse processResponse(Object response,
PortletInvocation invocation, RequestPrecursor requestPrecursor) throws
PortletInvokerException;
+ /**
+ * Extracts basic required elements for all invocation requests.
+ *
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 13121 $
+ * @since 2.4
+ */
+ protected static class RequestPrecursor
+ {
+ private final static Logger log = LoggerFactory.getLogger(RequestPrecursor.class);
+
+ private PortletContext portletContext;
+ private RuntimeContext runtimeContext;
+ private MarkupParams markupParams;
+ private static final String PORTLET_HANDLE = "portlet handle";
+ private static final String SECURITY_CONTEXT = "security context";
+ private static final String USER_CONTEXT = "user context";
+ private static final String INVOCATION_CONTEXT = "invocation context";
+ 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
+ {
+ // retrieve handle
+ portletContext =
WSRPUtils.convertToWSRPPortletContext(WSRPConsumerImpl.getPortletContext(invocation));
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(getPortletHandle(),
PORTLET_HANDLE, null);
+ if (log.isDebugEnabled())
+ {
+ log.debug("About to invoke on portlet: " + getPortletHandle());
+ }
+
+ // create runtime context
+ SecurityContext securityContext = invocation.getSecurityContext();
+ ParameterValidation.throwIllegalArgExceptionIfNull(securityContext,
SECURITY_CONTEXT);
+ String authType =
WSRPUtils.convertRequestAuthTypeToWSRPAuthType(securityContext.getAuthType());
+ setRuntimeContext(WSRPTypeFactory.createRuntimeContext(authType));
+
+ // set the session id if needed
+ wsrpConsumer.getSessionHandler().setSessionIdIfNeeded(invocation,
getRuntimeContext(), getPortletHandle());
+
+ wsrpConsumer.setTemplatesIfNeeded(invocation, getRuntimeContext());
+
+ // create markup params
+ org.gatein.pc.api.spi.UserContext userContext = invocation.getUserContext();
+ ParameterValidation.throwIllegalArgExceptionIfNull(userContext, USER_CONTEXT);
+ PortletInvocationContext context = invocation.getContext();
+ ParameterValidation.throwIllegalArgExceptionIfNull(context,
INVOCATION_CONTEXT);
+ ContentInfo streamInfo = context.getMarkupInfo();
+ ParameterValidation.throwIllegalArgExceptionIfNull(streamInfo, STREAM_INFO);
+
+ String mode;
+ try
+ {
+ mode = WSRPUtils.getWSRPNameFromJSR168PortletMode(invocation.getMode());
+ }
+ catch (Exception e)
+ {
+ log.debug("Mode was null in context.");
+ mode = WSRPConstants.VIEW_MODE;
+ }
+
+ String windowState;
+ try
+ {
+ windowState =
WSRPUtils.getWSRPNameFromJSR168WindowState(invocation.getWindowState());
+ }
+ catch (Exception e)
+ {
+ log.debug("WindowState was null in context.");
+ windowState = WSRPConstants.NORMAL_WINDOW_STATE;
+ }
+
+ setMarkupParams(WSRPTypeFactory.createMarkupParams(securityContext.isSecure(),
+ WSRPUtils.convertLocalesToRFC3066LanguageTags(userContext.getLocales()),
+ Collections.singletonList(streamInfo.getMediaType().getValue()), mode,
windowState));
+ String userAgent =
WSRPConsumerImpl.getHttpRequest(invocation).getHeader(USER_AGENT);
+ getMarkupParams().setClientData(WSRPTypeFactory.createClientData(userAgent));
+
+ // navigational state
+ StateString navigationalState = invocation.getNavigationalState();
+ Map<String, String[]> publicNavigationalState =
invocation.getPublicNavigationalState();
+ NavigationalContext navigationalContext =
WSRPTypeFactory.createNavigationalContextOrNull(navigationalState,
publicNavigationalState);
+ getMarkupParams().setNavigationalContext(navigationalContext);
+
+ if (log.isDebugEnabled())
+ {
+ log.debug(WSRPUtils.toString(getMarkupParams()));
+ }
+ }
+
+ public String getPortletHandle()
+ {
+ return portletContext.getPortletHandle();
+ }
+
+
+ public PortletContext getPortletContext()
+ {
+ return portletContext;
+ }
+
+ public RuntimeContext getRuntimeContext()
+ {
+ return runtimeContext;
+ }
+
+ private void setRuntimeContext(RuntimeContext runtimeContext)
+ {
+ this.runtimeContext = runtimeContext;
+ }
+
+ public MarkupParams getMarkupParams()
+ {
+ return markupParams;
+ }
+
+ private void setMarkupParams(MarkupParams markupParams)
+ {
+ this.markupParams = markupParams;
+ }
+ }
}
Deleted:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MarkupProcessor.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MarkupProcessor.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MarkupProcessor.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,90 +0,0 @@
-/*
- * 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;
-
-import org.gatein.common.text.TextTools;
-import org.gatein.pc.api.URLFormat;
-import org.gatein.pc.api.spi.PortletInvocationContext;
-import org.gatein.wsrp.WSRPPortletURL;
-import org.gatein.wsrp.WSRPResourceURL;
-import org.gatein.wsrp.WSRPRewritingConstants;
-
-import java.util.Set;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision$
- */
-public class MarkupProcessor implements TextTools.StringReplacementGenerator
-{
- private final PortletInvocationContext context;
- private final URLFormat format;
- private final Set<String> supportedCustomModes;
- private final Set<String> supportedCustomWindowStates;
- private final String namespace;
-
- protected MarkupProcessor(String namespace, PortletInvocationContext context,
org.gatein.pc.api.PortletContext target, URLFormat format, ProducerInfo info)
- {
- this.namespace = namespace;
- this.context = context;
- this.format = format;
- supportedCustomModes = info.getSupportedCustomModes();
- supportedCustomWindowStates = info.getSupportedCustomWindowStates();
- }
-
- public String getReplacementFor(String match, String prefix, String suffix)
- {
- if (prefix.equals(match))
- {
- return namespace;
- }
- else if (match.startsWith(WSRPRewritingConstants.BEGIN_WSRP_REWRITE_END))
- {
- // remove end of rewrite token
- match =
match.substring(WSRPRewritingConstants.BEGIN_WSRP_REWRITE_END.length());
-
- WSRPPortletURL portletURL = WSRPPortletURL.create(match, supportedCustomModes,
supportedCustomWindowStates, true);
- return context.renderURL(portletURL, format);
- }
- else
- {
- // match is not something we know how to process
- return match;
- }
- }
-
-
- static String getResourceURL(String urlAsString, WSRPResourceURL resource)
- {
- String resourceURL = resource.getResourceURL().toExternalForm();
- if (InvocationHandler.log.isDebugEnabled())
- {
- InvocationHandler.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;
- }
-}
Deleted:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MimeResponseHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MimeResponseHandler.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MimeResponseHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,182 +0,0 @@
-/*
- * 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;
-
-import org.gatein.common.text.TextTools;
-import org.gatein.pc.api.PortletInvokerException;
-import org.gatein.pc.api.URLFormat;
-import org.gatein.pc.api.cache.CacheScope;
-import org.gatein.pc.api.invocation.PortletInvocation;
-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.pc.api.spi.PortletInvocationContext;
-import org.gatein.wsrp.WSRPConstants;
-import org.gatein.wsrp.WSRPConsumer;
-import org.gatein.wsrp.WSRPRewritingConstants;
-import org.oasis.wsrp.v2.CacheControl;
-import org.oasis.wsrp.v2.MimeResponse;
-import org.oasis.wsrp.v2.SessionContext;
-
-import java.util.Map;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision$
- */
-public abstract class MimeResponseHandler<Response, LocalMimeResponse extends
MimeResponse> extends InvocationHandler
-{
- private static final org.gatein.pc.api.cache.CacheControl DEFAULT_CACHE_CONTROL = new
org.gatein.pc.api.cache.CacheControl(0, CacheScope.PRIVATE, null);
-
- protected MimeResponseHandler(WSRPConsumerImpl consumer)
- {
- super(consumer);
- }
-
- protected abstract SessionContext getSessionContextFrom(Response response);
-
- protected abstract LocalMimeResponse getMimeResponseFrom(Response response);
-
- @Override
- protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, RequestPrecursor requestPrecursor) throws PortletInvokerException
- {
- Response localResponse = (Response)response;
-
-
consumer.getSessionHandler().updateSessionIfNeeded(getSessionContextFrom(localResponse),
invocation,
- requestPrecursor.getPortletHandle());
-
- LocalMimeResponse mimeResponse = getMimeResponseFrom(localResponse);
- String markup = mimeResponse.getItemString();
- byte[] binary = mimeResponse.getItemBinary();
- if (markup != null && binary != null)
- {
- return new ErrorResponse(new IllegalArgumentException("Markup response
cannot contain both string and binary " +
- "markup. Per Section 6.1.10 of the WSRP specification, this is a
Producer error."));
- }
-
- if (markup == null && binary == null)
- {
- if (mimeResponse.isUseCachedItem())
- {
- //todo: deal with cache GTNWSRP-40
- }
- else
- {
- return new ErrorResponse(new IllegalArgumentException("Markup response
must contain at least string or binary" +
- " markup. Per Section 6.1.10 of the WSRP specification, this is a
Producer error."));
- }
- }
-
- if (markup != null && markup.length() > 0)
- {
- if (Boolean.TRUE.equals(mimeResponse.isRequiresRewriting()))
- {
- markup = processMarkup(
- markup,
- getNamespaceFrom(invocation.getWindowContext()),
- invocation.getContext(),
- invocation.getTarget(),
- new URLFormat(invocation.getSecurityContext().isSecure(),
invocation.getSecurityContext().isAuthenticated(), true, true),
- consumer
- );
- }
- }
-
- String mimeType = mimeResponse.getMimeType();
- if (mimeType == null || mimeType.length() == 0)
- {
- return new ErrorResponse(new IllegalArgumentException("No MIME type was
provided for portlet content."));
- }
-
- return createContentResponse(mimeResponse, invocation, null, null, mimeType,
binary, markup, createCacheControl(mimeResponse));
- }
-
- protected PortletInvocationResponse createContentResponse(LocalMimeResponse
mimeResponse, PortletInvocation invocation,
- ResponseProperties
properties, Map<String, Object> attributes,
- String mimeType, byte[]
bytes, String markup,
-
org.gatein.pc.api.cache.CacheControl cacheControl)
- {
- return new ContentResponse(properties, attributes, mimeType, bytes, markup,
cacheControl);
- }
-
- static String processMarkup(String markup, String namespace, PortletInvocationContext
context, org.gatein.pc.api.PortletContext target, URLFormat format, WSRPConsumer
consumer)
- {
- // fix-me: how to deal with fragment header? => interceptor?
-
- // todo: remove, this is a work-around for GTNWSRP-12
- markup = markup.replaceFirst("%3ftimeout%3d.*%2f", "%2f");
-
- markup = TextTools.replaceBoundedString(
- markup,
- WSRPRewritingConstants.WSRP_REWRITE,
- WSRPRewritingConstants.END_WSRP_REWRITE,
- new MarkupProcessor(namespace, context, target, format,
consumer.getProducerInfo()),
- true,
- false,
- true
- );
-
- return markup;
- }
-
- protected org.gatein.pc.api.cache.CacheControl createCacheControl(LocalMimeResponse
mimeResponse)
- {
- CacheControl cacheControl = mimeResponse.getCacheControl();
- org.gatein.pc.api.cache.CacheControl result = DEFAULT_CACHE_CONTROL;
-
- int expires;
- if (cacheControl != null)
- {
- expires = cacheControl.getExpires();
- String userScope = cacheControl.getUserScope();
-
- // check that we support the user scope...
- if (consumer.supportsUserScope(userScope))
- {
- if (debug)
- {
- log.debug("Trying to cache markup " + userScope + " for
" + expires + " seconds.");
- }
- CacheScope scope;
- if (WSRPConstants.CACHE_FOR_ALL.equals(userScope))
- {
- scope = CacheScope.PUBLIC;
- }
- else if (WSRPConstants.CACHE_PER_USER.equals(userScope))
- {
- scope = CacheScope.PRIVATE;
- }
- else
- {
- throw new IllegalArgumentException("Unknown CacheControl user scope:
" + userScope); // should not happen
- }
-
- result = new org.gatein.pc.api.cache.CacheControl(expires, scope,
cacheControl.getValidateTag());
- }
- }
-
- return result;
- }
-}
Deleted:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/NavigationalStateUpdatingHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/NavigationalStateUpdatingHandler.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/NavigationalStateUpdatingHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,149 +0,0 @@
-/*
- * 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;
-
-import org.gatein.common.util.ParameterValidation;
-import org.gatein.pc.api.OpaqueStateString;
-import org.gatein.pc.api.StateEvent;
-import org.gatein.pc.api.invocation.PortletInvocation;
-import org.gatein.pc.api.invocation.response.UpdateNavigationalStateResponse;
-import org.gatein.pc.api.spi.InstanceContext;
-import org.gatein.wsrp.WSRPUtils;
-import org.gatein.wsrp.payload.PayloadUtils;
-import org.oasis.wsrp.v2.Event;
-import org.oasis.wsrp.v2.EventPayload;
-import org.oasis.wsrp.v2.NamedString;
-import org.oasis.wsrp.v2.NavigationalContext;
-import org.oasis.wsrp.v2.PortletContext;
-import org.oasis.wsrp.v2.UpdateResponse;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision$
- */
-public abstract class NavigationalStateUpdatingHandler extends InvocationHandler
-{
- public NavigationalStateUpdatingHandler(WSRPConsumerImpl consumer)
- {
- super(consumer);
- }
-
- protected UpdateNavigationalStateResponse processUpdateResponse(PortletInvocation
invocation, RequestPrecursor requestPrecursor, UpdateResponse updateResponse)
- {
- UpdateNavigationalStateResponse result = new UpdateNavigationalStateResponse();
-
- // new mode
- String newMode = updateResponse.getNewMode();
- if (newMode != null)
- {
- result.setMode(WSRPUtils.getJSR168PortletModeFromWSRPName(newMode));
- }
-
- // new window state
- String newWindowState = updateResponse.getNewWindowState();
- if (newWindowState != null)
- {
-
result.setWindowState(WSRPUtils.getJSR168WindowStateFromWSRPName(newWindowState));
- }
-
- // navigational state
- NavigationalContext navigationalContext = updateResponse.getNavigationalContext();
- if (navigationalContext != null)
- {
- String navigationalState = navigationalContext.getOpaqueValue();
- if (navigationalState != null) // todo: check meaning of empty private NS
- {
- result.setNavigationalState(new OpaqueStateString(navigationalState));
- }
-
- List<NamedString> publicParams = navigationalContext.getPublicValues();
- if (ParameterValidation.existsAndIsNotEmpty(publicParams))
- {
- Map<String, String[]> publicNS =
WSRPUtils.createPublicNSFrom(publicParams);
- result.setPublicNavigationalStateUpdates(publicNS);
- }
- }
-
- // events
- List<Event> events = updateResponse.getEvents();
- if (ParameterValidation.existsAndIsNotEmpty(events))
- {
- for (Event event : events)
- {
- EventPayload payload = event.getPayload();
- result.queueEvent(new UpdateNavigationalStateResponse.Event(event.getName(),
PayloadUtils.getPayloadAsSerializable(event.getType(), payload)));
- }
- }
-
- // check if the portlet was cloned
- PortletContext portletContext = updateResponse.getPortletContext();
- if (portletContext != null)
- {
- PortletContext originalContext = requestPrecursor.getPortletContext();
- InstanceContext context = invocation.getInstanceContext();
-
- String handle = portletContext.getPortletHandle();
- if (!originalContext.getPortletHandle().equals(handle))
- {
- // todo: GTNWSRP-36 If the Producer returns a new portletHandle without
returning a new sessionID, the Consumer MUST
- // associate the current sessionID with the new portletHandle rather than the
previous portletHandle.
- if (debug)
- {
- log.debug("Portlet '" + requestPrecursor.getPortletHandle()
+ "' was implicitely cloned. New handle is '"
- + handle + "'");
- }
-
- StateEvent event = new
StateEvent(WSRPUtils.convertToPortalPortletContext(portletContext),
StateEvent.Type.PORTLET_CLONED_EVENT);
- context.onStateEvent(event);
- }
- else
- {
- // check if the state was modified
- byte[] originalState = originalContext.getPortletState();
- byte[] newState = portletContext.getPortletState();
- if (!Arrays.equals(originalState, newState))
- {
- StateEvent event = new
StateEvent(WSRPUtils.convertToPortalPortletContext(portletContext),
StateEvent.Type.PORTLET_MODIFIED_EVENT);
- context.onStateEvent(event);
- }
- }
-
- // update the session information associated with the portlet handle
-
consumer.getSessionHandler().updateSessionInfoFor(originalContext.getPortletHandle(),
handle, invocation);
- }
- else
- {
- portletContext = requestPrecursor.getPortletContext();
- }
-
- // update the session info, using either the original or cloned portlet context, as
appropriate
-
consumer.getSessionHandler().updateSessionIfNeeded(updateResponse.getSessionContext(),
invocation,
- portletContext.getPortletHandle());
- return result;
- }
-}
Deleted:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,142 +0,0 @@
-/*
- * 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;
-
-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;
-import org.gatein.pc.api.invocation.response.ResponseProperties;
-import org.gatein.wsrp.WSRPTypeFactory;
-import org.oasis.wsrp.v2.Extension;
-import org.oasis.wsrp.v2.GetMarkup;
-import org.oasis.wsrp.v2.MarkupContext;
-import org.oasis.wsrp.v2.MarkupResponse;
-import org.oasis.wsrp.v2.PortletContext;
-import org.oasis.wsrp.v2.RuntimeContext;
-import org.oasis.wsrp.v2.SessionContext;
-import org.oasis.wsrp.v2.UserContext;
-
-import javax.xml.ws.Holder;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision: 12082 $
- * @since 2.4 (May 31, 2006)
- */
-public class RenderHandler extends MimeResponseHandler<MarkupResponse,
MarkupContext>
-{
-
- protected RenderHandler(WSRPConsumerImpl consumer)
- {
- super(consumer);
- }
-
- @Override
- protected SessionContext getSessionContextFrom(MarkupResponse response)
- {
- return response.getSessionContext();
- }
-
- @Override
- protected MarkupContext getMimeResponseFrom(MarkupResponse markupResponse)
- {
- return markupResponse.getMarkupContext();
- }
-
- @Override
- protected PortletInvocationResponse createContentResponse(MarkupContext markupContext,
PortletInvocation invocation,
- ResponseProperties
properties, Map<String, Object> attributes,
- String mimeType, byte[]
bytes, String markup,
-
org.gatein.pc.api.cache.CacheControl cacheControl)
- {
- return new FragmentResponse(properties, attributes, mimeType, bytes, markup,
markupContext.getPreferredTitle(),
- cacheControl, invocation.getPortalContext().getModes());
- }
-
- protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
- {
- if (!(invocation instanceof RenderInvocation))
- {
- throw new IllegalArgumentException("RenderHandler can only handle
RenderInvocations!");
- }
-
- // Create the markup request
- PortletContext portletContext = requestPrecursor.getPortletContext();
- if (debug)
- {
- log.debug("Consumer about to attempt rendering portlet '" +
portletContext.getPortletHandle() + "'");
- }
- return WSRPTypeFactory.createMarkupRequest(portletContext,
requestPrecursor.runtimeContext, requestPrecursor.markupParams);
- }
-
- protected void updateUserContext(Object request, UserContext userContext)
- {
- getRenderRequest(request).setUserContext(userContext);
- }
-
- protected void updateRegistrationContext(Object request) throws
PortletInvokerException
- {
-
getRenderRequest(request).setRegistrationContext(consumer.getRegistrationContext());
- }
-
- protected RuntimeContext getRuntimeContextFrom(Object request)
- {
- return getRenderRequest(request).getRuntimeContext();
- }
-
- protected Object performRequest(Object request) throws Exception
- {
- GetMarkup renderRequest = getRenderRequest(request);
- if (debug)
- {
- log.debug("getMarkup on '" +
renderRequest.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(),
- 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!");
- }
-
-}
Deleted:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RequestContextWrapper.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RequestContextWrapper.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RequestContextWrapper.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,64 +0,0 @@
-/*
- * JBoss, a division of Red Hat
- * Copyright 2009, 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;
-
-import org.gatein.pc.api.spi.RequestContext;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision: 10198 $
- * @since 2.6
- */
-public class RequestContextWrapper implements
org.apache.commons.fileupload.RequestContext
-{
- private RequestContext requestContext;
-
- public RequestContextWrapper(RequestContext requestContext)
- {
- this.requestContext = requestContext;
- }
-
- public String getCharacterEncoding()
- {
- return requestContext.getCharacterEncoding();
- }
-
- public String getContentType()
- {
- return requestContext.getContentType();
- }
-
- public int getContentLength()
- {
- return requestContext.getContentLength();
- }
-
- public InputStream getInputStream() throws IOException
- {
- return requestContext.getInputStream();
- }
-}
Deleted:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RequestPrecursor.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RequestPrecursor.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RequestPrecursor.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,147 +0,0 @@
-/*
- * 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;
-
-import org.gatein.common.util.ContentInfo;
-import org.gatein.common.util.ParameterValidation;
-import org.gatein.pc.api.PortletInvokerException;
-import org.gatein.pc.api.StateString;
-import org.gatein.pc.api.invocation.PortletInvocation;
-import org.gatein.pc.api.spi.PortletInvocationContext;
-import org.gatein.pc.api.spi.SecurityContext;
-import org.gatein.pc.api.spi.UserContext;
-import org.gatein.wsrp.WSRPConstants;
-import org.gatein.wsrp.WSRPTypeFactory;
-import org.gatein.wsrp.WSRPUtils;
-import org.oasis.wsrp.v2.MarkupParams;
-import org.oasis.wsrp.v2.NavigationalContext;
-import org.oasis.wsrp.v2.PortletContext;
-import org.oasis.wsrp.v2.RuntimeContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.Collections;
-import java.util.Map;
-
-/**
- * Extracts basic required elements for all invocation requests.
- *
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision: 13121 $
- * @since 2.4
- */
-class RequestPrecursor
-{
- private final static Logger log = LoggerFactory.getLogger(RequestPrecursor.class);
-
- private PortletContext portletContext;
- RuntimeContext runtimeContext;
- MarkupParams markupParams;
- private static final String PORTLET_HANDLE = "portlet handle";
- private static final String SECURITY_CONTEXT = "security context";
- private static final String USER_CONTEXT = "user context";
- private static final String INVOCATION_CONTEXT = "invocation context";
- 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
- {
- // retrieve handle
- portletContext =
WSRPUtils.convertToWSRPPortletContext(WSRPConsumerImpl.getPortletContext(invocation));
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(getPortletHandle(),
PORTLET_HANDLE, null);
- if (log.isDebugEnabled())
- {
- log.debug("About to invoke on portlet: " + getPortletHandle());
- }
-
- // create runtime context
- SecurityContext securityContext = invocation.getSecurityContext();
- ParameterValidation.throwIllegalArgExceptionIfNull(securityContext,
SECURITY_CONTEXT);
- String authType =
WSRPUtils.convertRequestAuthTypeToWSRPAuthType(securityContext.getAuthType());
- runtimeContext = WSRPTypeFactory.createRuntimeContext(authType);
-
- // set the session id if needed
- wsrpConsumer.getSessionHandler().setSessionIdIfNeeded(invocation, runtimeContext,
getPortletHandle());
-
- wsrpConsumer.setTemplatesIfNeeded(invocation, runtimeContext);
-
- // create markup params
- UserContext userContext = invocation.getUserContext();
- ParameterValidation.throwIllegalArgExceptionIfNull(userContext, USER_CONTEXT);
- PortletInvocationContext context = invocation.getContext();
- ParameterValidation.throwIllegalArgExceptionIfNull(context, INVOCATION_CONTEXT);
- ContentInfo streamInfo = context.getMarkupInfo();
- ParameterValidation.throwIllegalArgExceptionIfNull(streamInfo, STREAM_INFO);
-
- String mode;
- try
- {
- mode = WSRPUtils.getWSRPNameFromJSR168PortletMode(invocation.getMode());
- }
- catch (Exception e)
- {
- log.debug("Mode was null in context.");
- mode = WSRPConstants.VIEW_MODE;
- }
-
- String windowState;
- try
- {
- windowState =
WSRPUtils.getWSRPNameFromJSR168WindowState(invocation.getWindowState());
- }
- catch (Exception e)
- {
- log.debug("WindowState was null in context.");
- windowState = WSRPConstants.NORMAL_WINDOW_STATE;
- }
-
- markupParams = WSRPTypeFactory.createMarkupParams(securityContext.isSecure(),
- WSRPUtils.convertLocalesToRFC3066LanguageTags(userContext.getLocales()),
- Collections.singletonList(streamInfo.getMediaType().getValue()), mode,
windowState);
- String userAgent =
WSRPConsumerImpl.getHttpRequest(invocation).getHeader(USER_AGENT);
- markupParams.setClientData(WSRPTypeFactory.createClientData(userAgent));
-
- // navigational state
- StateString navigationalState = invocation.getNavigationalState();
- Map<String,String[]> publicNavigationalState =
invocation.getPublicNavigationalState();
- NavigationalContext navigationalContext =
WSRPTypeFactory.createNavigationalContextOrNull(navigationalState,
publicNavigationalState);
- markupParams.setNavigationalContext(navigationalContext);
-
- if (log.isDebugEnabled())
- {
- log.debug(WSRPUtils.toString(markupParams));
- }
- }
-
- public String getPortletHandle()
- {
- return portletContext.getPortletHandle();
- }
-
-
- public PortletContext getPortletContext()
- {
- return portletContext;
- }
-}
Deleted:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ResourceHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ResourceHandler.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ResourceHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,337 +0,0 @@
-/*
- * 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;
-
-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.handler.CookieUtil;
-import org.gatein.wsrp.spec.v2.WSRP2RewritingConstants;
-import org.oasis.wsrp.v2.Extension;
-import org.oasis.wsrp.v2.GetResource;
-import org.oasis.wsrp.v2.MarkupParams;
-import org.oasis.wsrp.v2.PortletContext;
-import org.oasis.wsrp.v2.ResourceContext;
-import org.oasis.wsrp.v2.ResourceParams;
-import org.oasis.wsrp.v2.ResourceResponse;
-import org.oasis.wsrp.v2.RuntimeContext;
-import org.oasis.wsrp.v2.SessionContext;
-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;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision$
- */
-public class ResourceHandler extends MimeResponseHandler<ResourceResponse,
ResourceContext>
-{
-
- protected ResourceHandler(WSRPConsumerImpl consumer)
- {
- super(consumer);
- }
-
- @Override
- protected SessionContext getSessionContextFrom(ResourceResponse resourceResponse)
- {
- return resourceResponse.getSessionContext();
- }
-
- @Override
- protected ResourceContext getMimeResponseFrom(ResourceResponse resourceResponse)
- {
- return resourceResponse.getResourceContext();
- }
-
- @Override
- protected void updateUserContext(Object request, UserContext userContext)
- {
- if (request instanceof GetResource)
- {
- getResourceRequest(request).setUserContext(userContext);
- }
- }
-
- @Override
- protected void updateRegistrationContext(Object request) throws
PortletInvokerException
- {
- if (request instanceof GetResource)
- {
-
getResourceRequest(request).setRegistrationContext(consumer.getRegistrationContext());
- }
- }
-
- @Override
- protected RuntimeContext getRuntimeContextFrom(Object request)
- {
- if (request instanceof GetResource)
- {
- return getResourceRequest(request).getRuntimeContext();
- }
- else
- {
- return null;
- }
- }
-
- @Override
- protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
- {
- if (!(invocation instanceof ResourceInvocation))
- {
- throw new IllegalArgumentException("ResourceHandler can only handle
ResourceInvocations!");
- }
-
- ResourceInvocation resourceInvocation = (ResourceInvocation)invocation;
-
- String resourceInvocationId = resourceInvocation.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! :)
- MarkupParams params = requestPrecursor.markupParams;
-
- // access mode
- InstanceContext instanceContext = invocation.getInstanceContext();
- ParameterValidation.throwIllegalArgExceptionIfNull(instanceContext, "instance
context");
- AccessMode accessMode = instanceContext.getAccessMode();
- ParameterValidation.throwIllegalArgExceptionIfNull(accessMode, "access
mode");
- if (debug)
- {
- log.debug("Portlet is requesting " + accessMode + " access
mode");
- }
-
- // if the portlet didn't request a resource id, use the fake one:
- if (resourceId == null || resourceId.length() == 0)
- {
- resourceId = WSRPResourceURL.DEFAULT_RESOURCE_ID;
- }
-
- // Create ResourceParams
- ResourceParams resourceParams =
WSRPTypeFactory.createResourceParams(params.isSecureClientCommunication(),
- params.getLocales(), params.getMimeTypes(), params.getMode(),
params.getWindowState(), resourceId,
- WSRPUtils.getStateChangeFromAccessMode(accessMode));
-
- resourceParams.setNavigationalContext(params.getNavigationalContext());
- resourceParams.setClientData(params.getClientData());
-
resourceParams.setResourceCacheability(WSRPUtils.getResourceCacheabilityFromCacheLevel(invocation.getCacheLevel()));
-
- StateString resourceState = invocation.getResourceState();
- if (resourceState != null)
- {
- String state = resourceState.getStringValue();
- if (!StateString.JBPNS_PREFIX.equals(state)) // fix-me: see JBPORTAL-900
- {
- resourceParams.setResourceState(state);
- }
- }
-
- return WSRPTypeFactory.createResourceRequest(portletContext,
requestPrecursor.runtimeContext, resourceParams);
- }
-
- @Override
- protected Object performRequest(Object 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());
-
- consumer.getMarkupService().getResource(getResource.getRegistrationContext(),
portletContextHolder, getResource.getRuntimeContext(),
- getResource.getUserContext(), getResource.getResourceParams(),
resourceContextHolder, sessionContextHolder, new Holder<List<Extension>>());
-
- ResourceResponse resourceResponse =
WSRPTypeFactory.createResourceResponse(resourceContextHolder.value);
- resourceResponse.setPortletContext(portletContextHolder.value);
- resourceResponse.setSessionContext(sessionContextHolder.value);
- 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!");
- }
-}
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/SessionHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/SessionHandler.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/SessionHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -201,7 +201,7 @@
{
String sessionId = producerSessionInfo.getSessionIdForPortlet(portletHandle);
SessionParams sessionParams = runtimeContext.getSessionParams();
- if(sessionParams == null)
+ if (sessionParams == null)
{
sessionParams = new SessionParams();
runtimeContext.setSessionParams(sessionParams);
@@ -210,7 +210,7 @@
}
}
- void updateSessionIfNeeded(SessionContext sessionContext, PortletInvocation
invocation, String portletHandle)
+ public void updateSessionIfNeeded(SessionContext sessionContext, PortletInvocation
invocation, String portletHandle)
{
if (sessionContext != null)
{
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-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/WSRPConsumerImpl.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -49,6 +49,10 @@
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.portlet.WSRPPortlet;
import org.gatein.wsrp.consumer.portlet.info.WSRPPortletInfo;
import org.gatein.wsrp.services.MarkupService;
@@ -486,7 +490,7 @@
refreshProducerInfo(true);
}
- RegistrationContext getRegistrationContext() throws PortletInvokerException
+ public RegistrationContext getRegistrationContext() throws PortletInvokerException
{
return producerInfo.getRegistrationContext();
}
Copied:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ActionHandler.java
(from rev 3774,
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ActionHandler.java)
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ActionHandler.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ActionHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -0,0 +1,336 @@
+/*
+ * 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.fileupload.FileItemIterator;
+import org.apache.commons.fileupload.FileItemStream;
+import org.apache.commons.fileupload.FileUpload;
+import org.apache.commons.fileupload.util.Streams;
+import org.gatein.common.util.ParameterValidation;
+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;
+import org.gatein.pc.api.spi.InstanceContext;
+import org.gatein.pc.api.spi.RequestContext;
+import org.gatein.pc.api.state.AccessMode;
+import org.gatein.wsrp.WSRPTypeFactory;
+import org.gatein.wsrp.WSRPUtils;
+import org.gatein.wsrp.consumer.WSRPConsumerImpl;
+import org.oasis.wsrp.v2.BlockingInteractionResponse;
+import org.oasis.wsrp.v2.Extension;
+import org.oasis.wsrp.v2.InteractionParams;
+import org.oasis.wsrp.v2.NamedString;
+import org.oasis.wsrp.v2.PerformBlockingInteraction;
+import org.oasis.wsrp.v2.PortletContext;
+import org.oasis.wsrp.v2.RuntimeContext;
+import org.oasis.wsrp.v2.UpdateResponse;
+import org.oasis.wsrp.v2.UploadContext;
+import org.oasis.wsrp.v2.UserContext;
+
+import javax.xml.ws.Holder;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 13121 $
+ * @since 2.4 (May 31, 2006)
+ */
+public class ActionHandler extends NavigationalStateUpdatingHandler
+{
+ public ActionHandler(WSRPConsumerImpl consumer)
+ {
+ super(consumer);
+ }
+
+ @SuppressWarnings({"CastToConcreteClass"})
+ protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
+ {
+ if (!(invocation instanceof ActionInvocation))
+ {
+ throw new IllegalArgumentException("ActionHandler can only handle
ActionInvocations!");
+ }
+
+ ActionInvocation actionInvocation = (ActionInvocation)invocation;
+
+ PortletContext portletContext = requestPrecursor.getPortletContext();
+ if (debug)
+ {
+ log.debug("Consumer about to attempt action on portlet '" +
portletContext.getPortletHandle() + "'");
+ }
+
+ // access mode
+ InstanceContext instanceContext = invocation.getInstanceContext();
+ ParameterValidation.throwIllegalArgExceptionIfNull(instanceContext, "instance
context");
+ AccessMode accessMode = instanceContext.getAccessMode();
+ ParameterValidation.throwIllegalArgExceptionIfNull(accessMode, "access
mode");
+ if (debug)
+ {
+ log.debug("Portlet is requesting " + accessMode + " access
mode");
+ }
+ InteractionParams interactionParams =
WSRPTypeFactory.createInteractionParams(WSRPUtils.getStateChangeFromAccessMode(accessMode));
+
+ // interaction state
+ StateString interactionState = actionInvocation.getInteractionState();
+ if (interactionState != null)
+ {
+ String state = interactionState.getStringValue();
+ if (!StateString.JBPNS_PREFIX.equals(state)) // fix-me: see JBPORTAL-900
+ {
+ interactionParams.setInteractionState(state);
+ }
+ }
+
+ // check for multi-part
+ RequestContextWrapper requestContext = new
RequestContextWrapper(actionInvocation.getRequestContext());
+ try
+ {
+ if (FileUpload.isMultipartContent(requestContext))
+ {
+ // content is multipart, we need to parse it (that includes form parameters)
+ FileUpload upload = new FileUpload();
+ FileItemIterator iter = upload.getItemIterator(requestContext);
+ List<UploadContext> uploadContexts = new
ArrayList<UploadContext>(7);
+ List<NamedString> formParameters = new
ArrayList<NamedString>(7);
+ while (iter.hasNext())
+ {
+ FileItemStream item = iter.next();
+ InputStream stream = item.openStream();
+ if (!item.isFormField())
+ {
+ String contentType = item.getContentType();
+ if (debug)
+ {
+ log.debug("File field " + item.getFieldName() + "
with file name " + item.getName() + " and content type "
+ + contentType + " detected.");
+ }
+
+ BufferedInputStream bufIn = new BufferedInputStream(stream);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ BufferedOutputStream bos = new BufferedOutputStream(baos);
+
+ int c = bufIn.read();
+ while (c != -1)
+ {
+ bos.write(c);
+ c = bufIn.read();
+ }
+
+ bos.flush();
+ baos.flush();
+ bufIn.close();
+ bos.close();
+
+ UploadContext uploadContext =
WSRPTypeFactory.createUploadContext(contentType, baos.toByteArray());
+
+ List<NamedString> mimeAttributes = new
ArrayList<NamedString>(2);
+
+ String value = FileUpload.FORM_DATA + ";"
+ + " name=\"" + item.getFieldName() +
"\";"
+ + " filename=\"" + item.getName() +
"\"";
+ NamedString mimeAttribute =
WSRPTypeFactory.createNamedString(FileUpload.CONTENT_DISPOSITION, value);
+ mimeAttributes.add(mimeAttribute);
+
+ mimeAttribute =
WSRPTypeFactory.createNamedString(FileUpload.CONTENT_TYPE, item.getContentType());
+ mimeAttributes.add(mimeAttribute);
+
+ uploadContext.getMimeAttributes().addAll(mimeAttributes);
+
+ uploadContexts.add(uploadContext);
+ }
+ else
+ {
+ NamedString formParameter =
WSRPTypeFactory.createNamedString(item.getFieldName(), Streams.asString(stream));
+ formParameters.add(formParameter);
+ }
+ }
+ interactionParams.getUploadContexts().addAll(uploadContexts);
+ interactionParams.getFormParameters().addAll(formParameters);
+ }
+ else
+ {
+ // if the content is not multipart, then check for form parameters
+ Map<String, String[]> params = actionInvocation.getForm();
+ if (params != null && !params.isEmpty())
+ {
+ int capacity = params.size();
+ List<NamedString> formParameters = new
ArrayList<NamedString>(capacity);
+ for (Map.Entry param : params.entrySet())
+ {
+ String name = (String)param.getKey();
+ String[] values = (String[])param.getValue();
+ NamedString formParameter;
+ for (String value : values)
+ {
+ formParameter = WSRPTypeFactory.createNamedString(name, value);
+ formParameters.add(formParameter);
+ }
+ }
+ interactionParams.getFormParameters().addAll(formParameters);
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ log.debug("Couldn't create UploadContext", e);
+ }
+
+ // todo: need to deal with GET method in forms
+
+ if (trace)
+ {
+ log.trace(WSRPUtils.toString(interactionParams));
+ }
+
+ // Create the blocking action request
+ return WSRPTypeFactory.createPerformBlockingInteraction(portletContext,
requestPrecursor.getRuntimeContext(),
+ requestPrecursor.getMarkupParams(), interactionParams);
+ }
+
+ protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, RequestPrecursor requestPrecursor) throws PortletInvokerException
+ {
+ BlockingInteractionResponse blockingInteractionResponse =
(BlockingInteractionResponse)response;
+
+ String redirectURL = blockingInteractionResponse.getRedirectURL();
+ UpdateResponse updateResponse = blockingInteractionResponse.getUpdateResponse();
+ if (redirectURL != null && updateResponse != null)
+ {
+ return new ErrorResponse(new IllegalArgumentException("Producer error:
response cannot both redirect and update state."));
+ }
+
+ if (redirectURL == null && updateResponse == null)
+ {
+ return new ErrorResponse(new IllegalArgumentException("Producer error:
response must redirect or update state."));
+ }
+
+ if (redirectURL != null)
+ {
+ return new HTTPRedirectionResponse(redirectURL); // do we need to process URLs?
+ }
+ else
+ {
+ // updateResponse.getMarkupContext(); // ignore bundled markup for now.
+
+ return processUpdateResponse(invocation, requestPrecursor, updateResponse);
+ }
+ }
+
+ 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 $
+ * @since 2.6
+ */
+ public static class RequestContextWrapper implements
org.apache.commons.fileupload.RequestContext
+ {
+ private RequestContext requestContext;
+
+ public RequestContextWrapper(RequestContext requestContext)
+ {
+ this.requestContext = requestContext;
+ }
+
+ public String getCharacterEncoding()
+ {
+ return requestContext.getCharacterEncoding();
+ }
+
+ public String getContentType()
+ {
+ return requestContext.getContentType();
+ }
+
+ public int getContentLength()
+ {
+ return requestContext.getContentLength();
+ }
+
+ public InputStream getInputStream() throws IOException
+ {
+ return requestContext.getInputStream();
+ }
+ }
+}
Copied:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java
(from rev 3774,
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/EventHandler.java)
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -0,0 +1,169 @@
+/*
+ * 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.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;
+import org.gatein.pc.api.state.AccessMode;
+import org.gatein.wsrp.WSRPTypeFactory;
+import org.gatein.wsrp.WSRPUtils;
+import org.gatein.wsrp.consumer.InvocationHandler;
+import org.gatein.wsrp.consumer.WSRPConsumerImpl;
+import org.oasis.wsrp.v2.Event;
+import org.oasis.wsrp.v2.EventParams;
+import org.oasis.wsrp.v2.Extension;
+import org.oasis.wsrp.v2.HandleEvents;
+import org.oasis.wsrp.v2.HandleEventsFailed;
+import org.oasis.wsrp.v2.HandleEventsResponse;
+import org.oasis.wsrp.v2.PortletContext;
+import org.oasis.wsrp.v2.RuntimeContext;
+import org.oasis.wsrp.v2.UpdateResponse;
+import org.oasis.wsrp.v2.UserContext;
+
+import javax.xml.namespace.QName;
+import javax.xml.ws.Holder;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class EventHandler extends NavigationalStateUpdatingHandler
+{
+ public EventHandler(WSRPConsumerImpl consumer)
+ {
+ super(consumer);
+ }
+
+ @Override
+ protected void updateUserContext(Object request, UserContext userContext)
+ {
+ getHandleEvents(request).setUserContext(userContext);
+ }
+
+ @Override
+ protected void updateRegistrationContext(Object request) throws
PortletInvokerException
+ {
+
getHandleEvents(request).setRegistrationContext(consumer.getRegistrationContext());
+ }
+
+ @Override
+ protected RuntimeContext getRuntimeContextFrom(Object request)
+ {
+ return getHandleEvents(request).getRuntimeContext();
+ }
+
+ @Override
+ protected Object performRequest(Object request) throws Exception
+ {
+ HandleEvents eventRequest = getHandleEvents(request);
+
+ if (InvocationHandler.debug)
+ {
+ InvocationHandler.log.debug("handleEvents on '" +
eventRequest.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,
+ new Holder<List<Extension>>());
+
+ HandleEventsResponse response = WSRPTypeFactory.createHandleEventsReponse();
+ response.setUpdateResponse(updateResponse.value);
+ if (ParameterValidation.existsAndIsNotEmpty(failedEvents.value))
+ {
+ response.getFailedEvents().addAll(failedEvents.value);
+ }
+ return response;
+ }
+
+ @Override
+ protected Object prepareRequest(InvocationHandler.RequestPrecursor requestPrecursor,
PortletInvocation invocation)
+ {
+ if (!(invocation instanceof EventInvocation))
+ {
+ throw new IllegalArgumentException("EventHandler can only handle
EventInvocations!");
+ }
+
+ EventInvocation eventInvocation = (EventInvocation)invocation;
+
+ PortletContext portletContext = requestPrecursor.getPortletContext();
+ if (InvocationHandler.debug)
+ {
+ InvocationHandler.log.debug("Consumer about to attempt action on portlet
'" + portletContext.getPortletHandle() + "'");
+ }
+
+ // access mode
+ InstanceContext instanceContext = invocation.getInstanceContext();
+ ParameterValidation.throwIllegalArgExceptionIfNull(instanceContext, "instance
context");
+ AccessMode accessMode = instanceContext.getAccessMode();
+ ParameterValidation.throwIllegalArgExceptionIfNull(accessMode, "access
mode");
+ if (InvocationHandler.debug)
+ {
+ InvocationHandler.log.debug("Portlet is requesting " + accessMode +
" access mode");
+ }
+
+ // events
+ QName name = eventInvocation.getName();
+ Serializable payload = eventInvocation.getPayload();
+ Event event = WSRPTypeFactory.createEvent(name, payload);
+ EventParams eventParams =
WSRPTypeFactory.createEventParams(Collections.singletonList(event),
WSRPUtils.getStateChangeFromAccessMode(accessMode));
+
+ return WSRPTypeFactory.createHandleEvents(portletContext,
requestPrecursor.getRuntimeContext(),
+ requestPrecursor.getMarkupParams(), eventParams);
+ }
+
+ @Override
+ protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, InvocationHandler.RequestPrecursor requestPrecursor) throws
PortletInvokerException
+ {
+ HandleEventsResponse handleEventsResponse = (HandleEventsResponse)response;
+
+ List<HandleEventsFailed> failed = handleEventsResponse.getFailedEvents();
+ if (ParameterValidation.existsAndIsNotEmpty(failed))
+ {
+ return new ErrorResponse("Couldn't process events: " + failed);
+ }
+
+ return processUpdateResponse(invocation, requestPrecursor,
handleEventsResponse.getUpdateResponse());
+ }
+
+ private HandleEvents getHandleEvents(Object request)
+ {
+ if (request instanceof HandleEvents)
+ {
+ return (HandleEvents)request;
+ }
+
+ throw new IllegalArgumentException("EventHandler: request is not a
HandleEvents request!");
+ }
+}
Copied:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java
(from rev 3774,
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MimeResponseHandler.java)
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -0,0 +1,245 @@
+/*
+ * 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.gatein.common.text.TextTools;
+import org.gatein.pc.api.PortletInvokerException;
+import org.gatein.pc.api.URLFormat;
+import org.gatein.pc.api.cache.CacheScope;
+import org.gatein.pc.api.invocation.PortletInvocation;
+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.pc.api.spi.PortletInvocationContext;
+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.consumer.InvocationHandler;
+import org.gatein.wsrp.consumer.ProducerInfo;
+import org.gatein.wsrp.consumer.WSRPConsumerImpl;
+import org.oasis.wsrp.v2.CacheControl;
+import org.oasis.wsrp.v2.MimeResponse;
+import org.oasis.wsrp.v2.SessionContext;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public abstract class MimeResponseHandler<Response, LocalMimeResponse extends
MimeResponse> extends InvocationHandler
+{
+ private static final org.gatein.pc.api.cache.CacheControl DEFAULT_CACHE_CONTROL = new
org.gatein.pc.api.cache.CacheControl(0, CacheScope.PRIVATE, null);
+
+ protected MimeResponseHandler(WSRPConsumerImpl consumer)
+ {
+ super(consumer);
+ }
+
+ protected abstract SessionContext getSessionContextFrom(Response response);
+
+ protected abstract LocalMimeResponse getMimeResponseFrom(Response response);
+
+ @Override
+ protected PortletInvocationResponse processResponse(Object response, PortletInvocation
invocation, RequestPrecursor requestPrecursor) throws PortletInvokerException
+ {
+ Response localResponse = (Response)response;
+
+
consumer.getSessionHandler().updateSessionIfNeeded(getSessionContextFrom(localResponse),
invocation,
+ requestPrecursor.getPortletHandle());
+
+ LocalMimeResponse mimeResponse = getMimeResponseFrom(localResponse);
+ String markup = mimeResponse.getItemString();
+ byte[] binary = mimeResponse.getItemBinary();
+ if (markup != null && binary != null)
+ {
+ return new ErrorResponse(new IllegalArgumentException("Markup response
cannot contain both string and binary " +
+ "markup. Per Section 6.1.10 of the WSRP specification, this is a
Producer error."));
+ }
+
+ if (markup == null && binary == null)
+ {
+ if (mimeResponse.isUseCachedItem())
+ {
+ //todo: deal with cache GTNWSRP-40
+ }
+ else
+ {
+ return new ErrorResponse(new IllegalArgumentException("Markup response
must contain at least string or binary" +
+ " markup. Per Section 6.1.10 of the WSRP specification, this is a
Producer error."));
+ }
+ }
+
+ if (markup != null && markup.length() > 0)
+ {
+ if (Boolean.TRUE.equals(mimeResponse.isRequiresRewriting()))
+ {
+ markup = processMarkup(
+ markup,
+ getNamespaceFrom(invocation.getWindowContext()),
+ invocation.getContext(),
+ invocation.getTarget(),
+ new URLFormat(invocation.getSecurityContext().isSecure(),
invocation.getSecurityContext().isAuthenticated(), true, true),
+ consumer
+ );
+ }
+ }
+
+ String mimeType = mimeResponse.getMimeType();
+ if (mimeType == null || mimeType.length() == 0)
+ {
+ return new ErrorResponse(new IllegalArgumentException("No MIME type was
provided for portlet content."));
+ }
+
+ return createContentResponse(mimeResponse, invocation, null, null, mimeType,
binary, markup, createCacheControl(mimeResponse));
+ }
+
+ protected PortletInvocationResponse createContentResponse(LocalMimeResponse
mimeResponse, PortletInvocation invocation,
+ ResponseProperties
properties, Map<String, Object> attributes,
+ String mimeType, byte[]
bytes, String markup,
+
org.gatein.pc.api.cache.CacheControl cacheControl)
+ {
+ return new ContentResponse(properties, attributes, mimeType, bytes, markup,
cacheControl);
+ }
+
+ static String processMarkup(String markup, String namespace, PortletInvocationContext
context, org.gatein.pc.api.PortletContext target, URLFormat format, WSRPConsumer
consumer)
+ {
+ // fix-me: how to deal with fragment header? => interceptor?
+
+ // todo: remove, this is a work-around for GTNWSRP-12
+ markup = markup.replaceFirst("%3ftimeout%3d.*%2f", "%2f");
+
+ markup = TextTools.replaceBoundedString(
+ markup,
+ WSRPRewritingConstants.WSRP_REWRITE,
+ WSRPRewritingConstants.END_WSRP_REWRITE,
+ new MarkupProcessor(namespace, context, target, format,
consumer.getProducerInfo()),
+ true,
+ false,
+ true
+ );
+
+ return markup;
+ }
+
+ protected org.gatein.pc.api.cache.CacheControl createCacheControl(LocalMimeResponse
mimeResponse)
+ {
+ CacheControl cacheControl = mimeResponse.getCacheControl();
+ org.gatein.pc.api.cache.CacheControl result = DEFAULT_CACHE_CONTROL;
+
+ int expires;
+ if (cacheControl != null)
+ {
+ expires = cacheControl.getExpires();
+ String userScope = cacheControl.getUserScope();
+
+ // check that we support the user scope...
+ if (consumer.supportsUserScope(userScope))
+ {
+ if (debug)
+ {
+ log.debug("Trying to cache markup " + userScope + " for
" + expires + " seconds.");
+ }
+ CacheScope scope;
+ if (WSRPConstants.CACHE_FOR_ALL.equals(userScope))
+ {
+ scope = CacheScope.PUBLIC;
+ }
+ else if (WSRPConstants.CACHE_PER_USER.equals(userScope))
+ {
+ scope = CacheScope.PRIVATE;
+ }
+ else
+ {
+ throw new IllegalArgumentException("Unknown CacheControl user scope:
" + userScope); // should not happen
+ }
+
+ result = new org.gatein.pc.api.cache.CacheControl(expires, scope,
cacheControl.getValidateTag());
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+ private static class MarkupProcessor implements TextTools.StringReplacementGenerator
+ {
+ private final PortletInvocationContext context;
+ private final URLFormat format;
+ private final Set<String> supportedCustomModes;
+ private final Set<String> supportedCustomWindowStates;
+ private final String namespace;
+
+ protected MarkupProcessor(String namespace, PortletInvocationContext context,
org.gatein.pc.api.PortletContext target, URLFormat format, ProducerInfo info)
+ {
+ this.namespace = namespace;
+ this.context = context;
+ this.format = format;
+ supportedCustomModes = info.getSupportedCustomModes();
+ supportedCustomWindowStates = info.getSupportedCustomWindowStates();
+ }
+
+ public String getReplacementFor(String match, String prefix, String suffix)
+ {
+ if (prefix.equals(match))
+ {
+ return namespace;
+ }
+ else if (match.startsWith(WSRPRewritingConstants.BEGIN_WSRP_REWRITE_END))
+ {
+ // remove end of rewrite token
+ match =
match.substring(WSRPRewritingConstants.BEGIN_WSRP_REWRITE_END.length());
+
+ WSRPPortletURL portletURL = WSRPPortletURL.create(match,
supportedCustomModes, supportedCustomWindowStates, true);
+ return context.renderURL(portletURL, format);
+ }
+ else
+ {
+ // match is not something we know how to process
+ return match;
+ }
+ }
+
+
+ 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;
+ }
+ }
+}
Copied:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java
(from rev 3774,
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/NavigationalStateUpdatingHandler.java)
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -0,0 +1,151 @@
+/*
+ * 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.gatein.common.util.ParameterValidation;
+import org.gatein.pc.api.OpaqueStateString;
+import org.gatein.pc.api.StateEvent;
+import org.gatein.pc.api.invocation.PortletInvocation;
+import org.gatein.pc.api.invocation.response.UpdateNavigationalStateResponse;
+import org.gatein.pc.api.spi.InstanceContext;
+import org.gatein.wsrp.WSRPUtils;
+import org.gatein.wsrp.consumer.InvocationHandler;
+import org.gatein.wsrp.consumer.WSRPConsumerImpl;
+import org.gatein.wsrp.payload.PayloadUtils;
+import org.oasis.wsrp.v2.Event;
+import org.oasis.wsrp.v2.EventPayload;
+import org.oasis.wsrp.v2.NamedString;
+import org.oasis.wsrp.v2.NavigationalContext;
+import org.oasis.wsrp.v2.PortletContext;
+import org.oasis.wsrp.v2.UpdateResponse;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public abstract class NavigationalStateUpdatingHandler extends InvocationHandler
+{
+ public NavigationalStateUpdatingHandler(WSRPConsumerImpl consumer)
+ {
+ super(consumer);
+ }
+
+ protected UpdateNavigationalStateResponse processUpdateResponse(PortletInvocation
invocation, RequestPrecursor requestPrecursor, UpdateResponse updateResponse)
+ {
+ UpdateNavigationalStateResponse result = new UpdateNavigationalStateResponse();
+
+ // new mode
+ String newMode = updateResponse.getNewMode();
+ if (newMode != null)
+ {
+ result.setMode(WSRPUtils.getJSR168PortletModeFromWSRPName(newMode));
+ }
+
+ // new window state
+ String newWindowState = updateResponse.getNewWindowState();
+ if (newWindowState != null)
+ {
+
result.setWindowState(WSRPUtils.getJSR168WindowStateFromWSRPName(newWindowState));
+ }
+
+ // navigational state
+ NavigationalContext navigationalContext = updateResponse.getNavigationalContext();
+ if (navigationalContext != null)
+ {
+ String navigationalState = navigationalContext.getOpaqueValue();
+ if (navigationalState != null) // todo: check meaning of empty private NS
+ {
+ result.setNavigationalState(new OpaqueStateString(navigationalState));
+ }
+
+ List<NamedString> publicParams = navigationalContext.getPublicValues();
+ if (ParameterValidation.existsAndIsNotEmpty(publicParams))
+ {
+ Map<String, String[]> publicNS =
WSRPUtils.createPublicNSFrom(publicParams);
+ result.setPublicNavigationalStateUpdates(publicNS);
+ }
+ }
+
+ // events
+ List<Event> events = updateResponse.getEvents();
+ if (ParameterValidation.existsAndIsNotEmpty(events))
+ {
+ for (Event event : events)
+ {
+ EventPayload payload = event.getPayload();
+ result.queueEvent(new UpdateNavigationalStateResponse.Event(event.getName(),
PayloadUtils.getPayloadAsSerializable(event.getType(), payload)));
+ }
+ }
+
+ // check if the portlet was cloned
+ PortletContext portletContext = updateResponse.getPortletContext();
+ if (portletContext != null)
+ {
+ PortletContext originalContext = requestPrecursor.getPortletContext();
+ InstanceContext context = invocation.getInstanceContext();
+
+ String handle = portletContext.getPortletHandle();
+ if (!originalContext.getPortletHandle().equals(handle))
+ {
+ // todo: GTNWSRP-36 If the Producer returns a new portletHandle without
returning a new sessionID, the Consumer MUST
+ // associate the current sessionID with the new portletHandle rather than the
previous portletHandle.
+ if (debug)
+ {
+ log.debug("Portlet '" + requestPrecursor.getPortletHandle()
+ "' was implicitely cloned. New handle is '"
+ + handle + "'");
+ }
+
+ StateEvent event = new
StateEvent(WSRPUtils.convertToPortalPortletContext(portletContext),
StateEvent.Type.PORTLET_CLONED_EVENT);
+ context.onStateEvent(event);
+ }
+ else
+ {
+ // check if the state was modified
+ byte[] originalState = originalContext.getPortletState();
+ byte[] newState = portletContext.getPortletState();
+ if (!Arrays.equals(originalState, newState))
+ {
+ StateEvent event = new
StateEvent(WSRPUtils.convertToPortalPortletContext(portletContext),
StateEvent.Type.PORTLET_MODIFIED_EVENT);
+ context.onStateEvent(event);
+ }
+ }
+
+ // update the session information associated with the portlet handle
+
consumer.getSessionHandler().updateSessionInfoFor(originalContext.getPortletHandle(),
handle, invocation);
+ }
+ else
+ {
+ portletContext = requestPrecursor.getPortletContext();
+ }
+
+ // update the session info, using either the original or cloned portlet context, as
appropriate
+
consumer.getSessionHandler().updateSessionIfNeeded(updateResponse.getSessionContext(),
invocation,
+ portletContext.getPortletHandle());
+ return result;
+ }
+}
Copied:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/RenderHandler.java
(from rev 3774,
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java)
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/RenderHandler.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/RenderHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -0,0 +1,143 @@
+/*
+ * 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.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;
+import org.gatein.pc.api.invocation.response.ResponseProperties;
+import org.gatein.wsrp.WSRPTypeFactory;
+import org.gatein.wsrp.consumer.WSRPConsumerImpl;
+import org.oasis.wsrp.v2.Extension;
+import org.oasis.wsrp.v2.GetMarkup;
+import org.oasis.wsrp.v2.MarkupContext;
+import org.oasis.wsrp.v2.MarkupResponse;
+import org.oasis.wsrp.v2.PortletContext;
+import org.oasis.wsrp.v2.RuntimeContext;
+import org.oasis.wsrp.v2.SessionContext;
+import org.oasis.wsrp.v2.UserContext;
+
+import javax.xml.ws.Holder;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 12082 $
+ * @since 2.4 (May 31, 2006)
+ */
+public class RenderHandler extends MimeResponseHandler<MarkupResponse,
MarkupContext>
+{
+
+ public RenderHandler(WSRPConsumerImpl consumer)
+ {
+ super(consumer);
+ }
+
+ @Override
+ protected SessionContext getSessionContextFrom(MarkupResponse response)
+ {
+ return response.getSessionContext();
+ }
+
+ @Override
+ protected MarkupContext getMimeResponseFrom(MarkupResponse markupResponse)
+ {
+ return markupResponse.getMarkupContext();
+ }
+
+ @Override
+ protected PortletInvocationResponse createContentResponse(MarkupContext markupContext,
PortletInvocation invocation,
+ ResponseProperties
properties, Map<String, Object> attributes,
+ String mimeType, byte[]
bytes, String markup,
+
org.gatein.pc.api.cache.CacheControl cacheControl)
+ {
+ return new FragmentResponse(properties, attributes, mimeType, bytes, markup,
markupContext.getPreferredTitle(),
+ cacheControl, invocation.getPortalContext().getModes());
+ }
+
+ protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
+ {
+ if (!(invocation instanceof RenderInvocation))
+ {
+ throw new IllegalArgumentException("RenderHandler can only handle
RenderInvocations!");
+ }
+
+ // Create the markup request
+ PortletContext portletContext = requestPrecursor.getPortletContext();
+ if (debug)
+ {
+ log.debug("Consumer about to attempt rendering portlet '" +
portletContext.getPortletHandle() + "'");
+ }
+ return WSRPTypeFactory.createMarkupRequest(portletContext,
requestPrecursor.getRuntimeContext(), requestPrecursor.getMarkupParams());
+ }
+
+ protected void updateUserContext(Object request, UserContext userContext)
+ {
+ getRenderRequest(request).setUserContext(userContext);
+ }
+
+ protected void updateRegistrationContext(Object request) throws
PortletInvokerException
+ {
+
getRenderRequest(request).setRegistrationContext(consumer.getRegistrationContext());
+ }
+
+ protected RuntimeContext getRuntimeContextFrom(Object request)
+ {
+ return getRenderRequest(request).getRuntimeContext();
+ }
+
+ protected Object performRequest(Object request) throws Exception
+ {
+ GetMarkup renderRequest = getRenderRequest(request);
+ if (debug)
+ {
+ log.debug("getMarkup on '" +
renderRequest.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(),
+ 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!");
+ }
+
+}
Copied:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ResourceHandler.java
(from rev 3774,
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ResourceHandler.java)
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ResourceHandler.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ResourceHandler.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -0,0 +1,338 @@
+/*
+ * 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.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;
+import org.oasis.wsrp.v2.MarkupParams;
+import org.oasis.wsrp.v2.PortletContext;
+import org.oasis.wsrp.v2.ResourceContext;
+import org.oasis.wsrp.v2.ResourceParams;
+import org.oasis.wsrp.v2.ResourceResponse;
+import org.oasis.wsrp.v2.RuntimeContext;
+import org.oasis.wsrp.v2.SessionContext;
+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;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class ResourceHandler extends MimeResponseHandler<ResourceResponse,
ResourceContext>
+{
+
+ public ResourceHandler(WSRPConsumerImpl consumer)
+ {
+ super(consumer);
+ }
+
+ @Override
+ protected SessionContext getSessionContextFrom(ResourceResponse resourceResponse)
+ {
+ return resourceResponse.getSessionContext();
+ }
+
+ @Override
+ protected ResourceContext getMimeResponseFrom(ResourceResponse resourceResponse)
+ {
+ return resourceResponse.getResourceContext();
+ }
+
+ @Override
+ protected void updateUserContext(Object request, UserContext userContext)
+ {
+ if (request instanceof GetResource)
+ {
+ getResourceRequest(request).setUserContext(userContext);
+ }
+ }
+
+ @Override
+ protected void updateRegistrationContext(Object request) throws
PortletInvokerException
+ {
+ if (request instanceof GetResource)
+ {
+
getResourceRequest(request).setRegistrationContext(consumer.getRegistrationContext());
+ }
+ }
+
+ @Override
+ protected RuntimeContext getRuntimeContextFrom(Object request)
+ {
+ if (request instanceof GetResource)
+ {
+ return getResourceRequest(request).getRuntimeContext();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ @Override
+ protected Object prepareRequest(RequestPrecursor requestPrecursor, PortletInvocation
invocation)
+ {
+ if (!(invocation instanceof ResourceInvocation))
+ {
+ throw new IllegalArgumentException("ResourceHandler can only handle
ResourceInvocations!");
+ }
+
+ ResourceInvocation resourceInvocation = (ResourceInvocation)invocation;
+
+ String resourceInvocationId = resourceInvocation.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! :)
+ MarkupParams params = requestPrecursor.getMarkupParams();
+
+ // access mode
+ InstanceContext instanceContext = invocation.getInstanceContext();
+ ParameterValidation.throwIllegalArgExceptionIfNull(instanceContext, "instance
context");
+ AccessMode accessMode = instanceContext.getAccessMode();
+ ParameterValidation.throwIllegalArgExceptionIfNull(accessMode, "access
mode");
+ if (debug)
+ {
+ log.debug("Portlet is requesting " + accessMode + " access
mode");
+ }
+
+ // if the portlet didn't request a resource id, use the fake one:
+ if (resourceId == null || resourceId.length() == 0)
+ {
+ resourceId = WSRPResourceURL.DEFAULT_RESOURCE_ID;
+ }
+
+ // Create ResourceParams
+ ResourceParams resourceParams =
WSRPTypeFactory.createResourceParams(params.isSecureClientCommunication(),
+ params.getLocales(), params.getMimeTypes(), params.getMode(),
params.getWindowState(), resourceId,
+ WSRPUtils.getStateChangeFromAccessMode(accessMode));
+
+ resourceParams.setNavigationalContext(params.getNavigationalContext());
+ resourceParams.setClientData(params.getClientData());
+
resourceParams.setResourceCacheability(WSRPUtils.getResourceCacheabilityFromCacheLevel(invocation.getCacheLevel()));
+
+ StateString resourceState = invocation.getResourceState();
+ if (resourceState != null)
+ {
+ String state = resourceState.getStringValue();
+ if (!StateString.JBPNS_PREFIX.equals(state)) // fix-me: see JBPORTAL-900
+ {
+ resourceParams.setResourceState(state);
+ }
+ }
+
+ return WSRPTypeFactory.createResourceRequest(portletContext,
requestPrecursor.getRuntimeContext(), resourceParams);
+ }
+
+ @Override
+ protected Object performRequest(Object 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());
+
+ consumer.getMarkupService().getResource(getResource.getRegistrationContext(),
portletContextHolder, getResource.getRuntimeContext(),
+ getResource.getUserContext(), getResource.getResourceParams(),
resourceContextHolder, sessionContextHolder, new Holder<List<Extension>>());
+
+ ResourceResponse resourceResponse =
WSRPTypeFactory.createResourceResponse(resourceContextHolder.value);
+ resourceResponse.setPortletContext(portletContextHolder.value);
+ resourceResponse.setSessionContext(sessionContextHolder.value);
+ 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!");
+ }
+}
Deleted:
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/RenderHandlerTestCase.java
===================================================================
---
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/RenderHandlerTestCase.java 2010-08-09
15:54:05 UTC (rev 3781)
+++
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/RenderHandlerTestCase.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -1,179 +0,0 @@
-/*
- * 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;
-
-import junit.framework.TestCase;
-import org.gatein.pc.api.PortletContext;
-import org.gatein.pc.api.URLFormat;
-import org.gatein.wsrp.WSRPRewritingConstants;
-import org.gatein.wsrp.test.support.MockWSRPConsumer;
-import org.gatein.wsrp.test.support.TestPortletInvocationContext;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision: 10507 $
- * @since 2.6
- */
-public class RenderHandlerTestCase extends TestCase
-{
- public static final String NAMESPACE = "NAMESPACE";
- public static final String PORTLETID = "PORTLETID";
- public static final MockWSRPConsumer CONSUMER = new
MockWSRPConsumer("foo");
- public static final PortletContext PORTLET_CONTEXT =
PortletContext.createPortletContext(PORTLETID);
- public static final TestPortletInvocationContext CONTEXT = new
TestPortletInvocationContext();
- public static final URLFormat FORMAT = new URLFormat(false, false, true, true);
-
- public void testProcessMarkup()
- {
- String markup;
- String expected;
- markup =
"khlaksdhjflkjhsadljkwsrp_rewrite?wsrp-urlType=blockingAction&wsrp-interactionState=JBPNS_/wsrp_rewrite"
+
-
"fadsfadswsrp_rewrite?wsrp-urlType=render&wsrp-navigationalState=JBPNS_/wsrp_rewritefajdshfkjdshgfgrept";
- expected = "khlaksdhjflkjhsadljkAction is=JBPNS_ ns=null ws=null m=null"
+
- "fadsfadsRender ns=JBPNS_ ws=null m=nullfajdshfkjdshgfgrept";
- processMarkupAndCheck(markup, expected);
-
- markup = "<form method='post'
action='wsrp_rewrite?wsrp-urlType=blockingAction&wsrp" +
- "-interactionState=JBPNS_/wsrp_rewrite'
id='wsrp_rewrite_portfolioManager'><table><tr><td>Stock
symbol</t" +
- "d><td><input
name='symbol'/></td></tr><tr><td><input
type='submit'
value='Submit'></td></tr></table></form>";
- expected = "<form method='post' action='Action is=JBPNS_
ns=null ws=null m=null' id='" + NAMESPACE
- + "_portfolioManager'><table><tr><td>Stock
symbol</t" +
- "d><td><input
name='symbol'/></td></tr><tr><td><input
type='submit'
value='Submit'></td></tr></table></form>";
- processMarkupAndCheck(markup, expected);
- }
-
- /*public void testResourceURLs()
- {
- String markup;
- String expected;
- markup = "<img
src='wsrp_rewrite?wsrp-urlType=resource&wsrp-url=http%3A%2F%2Flocalhost%3A8080%2Ftest-resource-portlet%2Fgif%2Flogo.gif&wsrp-requiresRewrite=true/wsrp_rewrite'/>";
- expected = "<img
src='http://localhost:8080/test-resource-portlet/gif/logo.gif'/>";
- processMarkupAndCheck(markup, expected);
-
- markup = "<img
src='http://localhost:8080/test-resourcenoencodeurl-portlet/gif/logo.gif'/>";
- processMarkupAndCheck(markup, markup);
-
- markup =
"wsrp_rewrite?wsrp-urlType=resource&wsrp-url=http%3A%2F%2Flocalhost%3A8080%2Fhelloworld&wsrp-requiresRewrite=true/wsrp_rewrite/helloworld.jar";
- processMarkupAndCheck(markup,
"http://localhost:8080/helloworld/helloworld.jar");
-
- markup =
"wsrp_rewrite?wsrp-urlType=resource&wsrp-url=http%3A%2F%2Flocalhost%3A8080%2Fhelloworld&wsrp-requiresRewrite=true/wsrp_rewrite&foo=bar/helloworld.jar";
- processMarkupAndCheck(markup,
"http://localhost:8080/helloworld&foo=bar/helloworld.jar");
- }*/
-
- public void testRegularURLIsNotAffected()
- {
- String markup;
- markup = "<a
href=\"/portal/portal/default/Test/EXAMPLE/EXAMPLE?action=1d&windowstate=&mode="
+
-
"&ns=_next%3D%2Fdk%2Fskat%2Fportal%2Ffront%2Fportlets%2Fexample%2Findex.jsp"
+
-
"&is=_action%3D%252Fdk%252Fskat%252Fportal%252Ffront%252Fportlets%252Fexample%252FprocessLink"
+
-
"%26jbpns_2fdefault_2fTest_2fEXAMPLE_2fEXAMPLEsnpbjname%3DChris\">Press to
use default name.</a>";
- processMarkupAndCheck(markup, markup);
- }
-
- /*public void testProcessMarkupResourceFromTemplate()
- {
- String url =
"http%3a%2f%2fwsrp.netunitysoftware.com%2fWSRPTestService%2fWSRPTestService.asmx%3ftimeout%3d30000%2fgetResource%3fportletHandle%3d781F3EE5-22DF-4ef9-9664-F5FC759065DB%26Function%3dResource%26Name%3dNetUnity%26Type%3dGIF";
- String markup = "<table cellpadding=\"2\"
cellspacing=\"0\" border=\"0\" width=\"100%\">\n" +
- "\t<tr class=\"portlet-table-header\">\n" +
- "\t\t<td>Symbol</td>\n" +
- "\t\t<td>Name</td>\n" +
- "\t\t<td align=\"right\">Price</td>\n" +
- "\t\t<td></td>\n" +
- "\t\t<td align=\"right\">Change</td>\n" +
- "\t\t<td align=\"right\">% Chg</td>\n" +
- "\t</tr>\n" +
- "</table>\n" +
- "<A HREF=\"http://www.netunitysoftware.com\"
TITLE=\"NetUnity WSRP .NET Framework\" >" +
- "<img src=\"" + getResourceURL(url, false) + "\"
border=\"0\" /></A>";
-
- String expected = "<table cellpadding=\"2\"
cellspacing=\"0\" border=\"0\" width=\"100%\">\n" +
- "\t<tr class=\"portlet-table-header\">\n" +
- "\t\t<td>Symbol</td>\n" +
- "\t\t<td>Name</td>\n" +
- "\t\t<td align=\"right\">Price</td>\n" +
- "\t\t<td></td>\n" +
- "\t\t<td align=\"right\">Change</td>\n" +
- "\t\t<td align=\"right\">% Chg</td>\n" +
- "\t</tr>\n" +
- "</table>\n" +
- "<A HREF=\"http://www.netunitysoftware.com\"
TITLE=\"NetUnity WSRP .NET Framework\" >" +
- "<img src=\"" + URLTools.decodeXWWWFormURL(url) +
"\" border=\"0\" /></A>";
- processMarkupAndCheck(markup, expected);
- }*/
-
- public void testGTNWSRP12Workaround()
- {
- String timeout = "%3ftimeout%3d100000";
- String beforeTimeout =
"http%3a%2f%2fwsrp.netunitysoftware.com%2fWSRPTestService%2fWSRPTestService.asmx";
- String afterTimeout =
"%2fgetResource%3fportletHandle%3d781F3EE5-22DF-4ef9-9664-F5FC759065DB%26Function%3dResource%26Name%3dNetUnity%26Type%3dGIF";
- String originalURL = beforeTimeout + timeout + afterTimeout;
- String markup = "<table cellpadding=\"2\"
cellspacing=\"0\" border=\"0\" width=\"100%\">\n" +
- "\t<tr class=\"portlet-table-header\">\n" +
- "\t\t<td>Symbol</td>\n" +
- "\t\t<td>Name</td>\n" +
- "\t\t<td align=\"right\">Price</td>\n" +
- "\t\t<td></td>\n" +
- "\t\t<td align=\"right\">Change</td>\n" +
- "\t\t<td align=\"right\">% Chg</td>\n" +
- "\t</tr>\n" +
- "</table>\n" +
- "<A HREF=\"http://www.netunitysoftware.com\"
TITLE=\"NetUnity WSRP .NET Framework\" >" +
- "<img src=\"" + originalURL + "\"
border=\"0\" /></A>";
-
- String expected = "<table cellpadding=\"2\"
cellspacing=\"0\" border=\"0\" width=\"100%\">\n" +
- "\t<tr class=\"portlet-table-header\">\n" +
- "\t\t<td>Symbol</td>\n" +
- "\t\t<td>Name</td>\n" +
- "\t\t<td align=\"right\">Price</td>\n" +
- "\t\t<td></td>\n" +
- "\t\t<td align=\"right\">Change</td>\n" +
- "\t\t<td align=\"right\">% Chg</td>\n" +
- "\t</tr>\n" +
- "</table>\n" +
- "<A HREF=\"http://www.netunitysoftware.com\"
TITLE=\"NetUnity WSRP .NET Framework\" >" +
- "<img src=\"" + beforeTimeout + afterTimeout + "\"
border=\"0\" /></A>";
-
- processMarkupAndCheck(markup, expected);
- }
-
- private void processMarkupAndCheck(String markup, String expected)
- {
- String result = RenderHandler.processMarkup(
- markup,
- NAMESPACE,
- CONTEXT,
- PORTLET_CONTEXT,
- FORMAT,
- CONSUMER
- );
- assertEquals(expected, result);
- }
-
- private String getResourceURL(String encodedURL, boolean requiresRewrite)
- {
- String result =
WSRPRewritingConstants.FAKE_RESOURCE_URL.replace(WSRPRewritingConstants.WSRP_URL,
encodedURL);
- result = result.replace(WSRPRewritingConstants.WSRP_REQUIRES_REWRITE,
Boolean.toString(requiresRewrite));
- return result;
- }
-}
Copied:
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/RenderHandlerTestCase.java
(from rev 3774,
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/RenderHandlerTestCase.java)
===================================================================
---
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/RenderHandlerTestCase.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/RenderHandlerTestCase.java 2010-08-09
17:50:06 UTC (rev 3782)
@@ -0,0 +1,179 @@
+/*
+ * 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 junit.framework.TestCase;
+import org.gatein.pc.api.PortletContext;
+import org.gatein.pc.api.URLFormat;
+import org.gatein.wsrp.WSRPRewritingConstants;
+import org.gatein.wsrp.test.support.MockWSRPConsumer;
+import org.gatein.wsrp.test.support.TestPortletInvocationContext;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 10507 $
+ * @since 2.6
+ */
+public class RenderHandlerTestCase extends TestCase
+{
+ public static final String NAMESPACE = "NAMESPACE";
+ public static final String PORTLETID = "PORTLETID";
+ public static final MockWSRPConsumer CONSUMER = new
MockWSRPConsumer("foo");
+ public static final PortletContext PORTLET_CONTEXT =
PortletContext.createPortletContext(PORTLETID);
+ public static final TestPortletInvocationContext CONTEXT = new
TestPortletInvocationContext();
+ public static final URLFormat FORMAT = new URLFormat(false, false, true, true);
+
+ public void testProcessMarkup()
+ {
+ String markup;
+ String expected;
+ markup =
"khlaksdhjflkjhsadljkwsrp_rewrite?wsrp-urlType=blockingAction&wsrp-interactionState=JBPNS_/wsrp_rewrite"
+
+
"fadsfadswsrp_rewrite?wsrp-urlType=render&wsrp-navigationalState=JBPNS_/wsrp_rewritefajdshfkjdshgfgrept";
+ expected = "khlaksdhjflkjhsadljkAction is=JBPNS_ ns=null ws=null m=null"
+
+ "fadsfadsRender ns=JBPNS_ ws=null m=nullfajdshfkjdshgfgrept";
+ processMarkupAndCheck(markup, expected);
+
+ markup = "<form method='post'
action='wsrp_rewrite?wsrp-urlType=blockingAction&wsrp" +
+ "-interactionState=JBPNS_/wsrp_rewrite'
id='wsrp_rewrite_portfolioManager'><table><tr><td>Stock
symbol</t" +
+ "d><td><input
name='symbol'/></td></tr><tr><td><input
type='submit'
value='Submit'></td></tr></table></form>";
+ expected = "<form method='post' action='Action is=JBPNS_
ns=null ws=null m=null' id='" + NAMESPACE
+ + "_portfolioManager'><table><tr><td>Stock
symbol</t" +
+ "d><td><input
name='symbol'/></td></tr><tr><td><input
type='submit'
value='Submit'></td></tr></table></form>";
+ processMarkupAndCheck(markup, expected);
+ }
+
+ /*public void testResourceURLs()
+ {
+ String markup;
+ String expected;
+ markup = "<img
src='wsrp_rewrite?wsrp-urlType=resource&wsrp-url=http%3A%2F%2Flocalhost%3A8080%2Ftest-resource-portlet%2Fgif%2Flogo.gif&wsrp-requiresRewrite=true/wsrp_rewrite'/>";
+ expected = "<img
src='http://localhost:8080/test-resource-portlet/gif/logo.gif'/>";
+ processMarkupAndCheck(markup, expected);
+
+ markup = "<img
src='http://localhost:8080/test-resourcenoencodeurl-portlet/gif/logo.gif'/>";
+ processMarkupAndCheck(markup, markup);
+
+ markup =
"wsrp_rewrite?wsrp-urlType=resource&wsrp-url=http%3A%2F%2Flocalhost%3A8080%2Fhelloworld&wsrp-requiresRewrite=true/wsrp_rewrite/helloworld.jar";
+ processMarkupAndCheck(markup,
"http://localhost:8080/helloworld/helloworld.jar");
+
+ markup =
"wsrp_rewrite?wsrp-urlType=resource&wsrp-url=http%3A%2F%2Flocalhost%3A8080%2Fhelloworld&wsrp-requiresRewrite=true/wsrp_rewrite&foo=bar/helloworld.jar";
+ processMarkupAndCheck(markup,
"http://localhost:8080/helloworld&foo=bar/helloworld.jar");
+ }*/
+
+ public void testRegularURLIsNotAffected()
+ {
+ String markup;
+ markup = "<a
href=\"/portal/portal/default/Test/EXAMPLE/EXAMPLE?action=1d&windowstate=&mode="
+
+
"&ns=_next%3D%2Fdk%2Fskat%2Fportal%2Ffront%2Fportlets%2Fexample%2Findex.jsp"
+
+
"&is=_action%3D%252Fdk%252Fskat%252Fportal%252Ffront%252Fportlets%252Fexample%252FprocessLink"
+
+
"%26jbpns_2fdefault_2fTest_2fEXAMPLE_2fEXAMPLEsnpbjname%3DChris\">Press to
use default name.</a>";
+ processMarkupAndCheck(markup, markup);
+ }
+
+ /*public void testProcessMarkupResourceFromTemplate()
+ {
+ String url =
"http%3a%2f%2fwsrp.netunitysoftware.com%2fWSRPTestService%2fWSRPTestService.asmx%3ftimeout%3d30000%2fgetResource%3fportletHandle%3d781F3EE5-22DF-4ef9-9664-F5FC759065DB%26Function%3dResource%26Name%3dNetUnity%26Type%3dGIF";
+ String markup = "<table cellpadding=\"2\"
cellspacing=\"0\" border=\"0\" width=\"100%\">\n" +
+ "\t<tr class=\"portlet-table-header\">\n" +
+ "\t\t<td>Symbol</td>\n" +
+ "\t\t<td>Name</td>\n" +
+ "\t\t<td align=\"right\">Price</td>\n" +
+ "\t\t<td></td>\n" +
+ "\t\t<td align=\"right\">Change</td>\n" +
+ "\t\t<td align=\"right\">% Chg</td>\n" +
+ "\t</tr>\n" +
+ "</table>\n" +
+ "<A HREF=\"http://www.netunitysoftware.com\"
TITLE=\"NetUnity WSRP .NET Framework\" >" +
+ "<img src=\"" + getResourceURL(url, false) + "\"
border=\"0\" /></A>";
+
+ String expected = "<table cellpadding=\"2\"
cellspacing=\"0\" border=\"0\" width=\"100%\">\n" +
+ "\t<tr class=\"portlet-table-header\">\n" +
+ "\t\t<td>Symbol</td>\n" +
+ "\t\t<td>Name</td>\n" +
+ "\t\t<td align=\"right\">Price</td>\n" +
+ "\t\t<td></td>\n" +
+ "\t\t<td align=\"right\">Change</td>\n" +
+ "\t\t<td align=\"right\">% Chg</td>\n" +
+ "\t</tr>\n" +
+ "</table>\n" +
+ "<A HREF=\"http://www.netunitysoftware.com\"
TITLE=\"NetUnity WSRP .NET Framework\" >" +
+ "<img src=\"" + URLTools.decodeXWWWFormURL(url) +
"\" border=\"0\" /></A>";
+ processMarkupAndCheck(markup, expected);
+ }*/
+
+ public void testGTNWSRP12Workaround()
+ {
+ String timeout = "%3ftimeout%3d100000";
+ String beforeTimeout =
"http%3a%2f%2fwsrp.netunitysoftware.com%2fWSRPTestService%2fWSRPTestService.asmx";
+ String afterTimeout =
"%2fgetResource%3fportletHandle%3d781F3EE5-22DF-4ef9-9664-F5FC759065DB%26Function%3dResource%26Name%3dNetUnity%26Type%3dGIF";
+ String originalURL = beforeTimeout + timeout + afterTimeout;
+ String markup = "<table cellpadding=\"2\"
cellspacing=\"0\" border=\"0\" width=\"100%\">\n" +
+ "\t<tr class=\"portlet-table-header\">\n" +
+ "\t\t<td>Symbol</td>\n" +
+ "\t\t<td>Name</td>\n" +
+ "\t\t<td align=\"right\">Price</td>\n" +
+ "\t\t<td></td>\n" +
+ "\t\t<td align=\"right\">Change</td>\n" +
+ "\t\t<td align=\"right\">% Chg</td>\n" +
+ "\t</tr>\n" +
+ "</table>\n" +
+ "<A HREF=\"http://www.netunitysoftware.com\"
TITLE=\"NetUnity WSRP .NET Framework\" >" +
+ "<img src=\"" + originalURL + "\"
border=\"0\" /></A>";
+
+ String expected = "<table cellpadding=\"2\"
cellspacing=\"0\" border=\"0\" width=\"100%\">\n" +
+ "\t<tr class=\"portlet-table-header\">\n" +
+ "\t\t<td>Symbol</td>\n" +
+ "\t\t<td>Name</td>\n" +
+ "\t\t<td align=\"right\">Price</td>\n" +
+ "\t\t<td></td>\n" +
+ "\t\t<td align=\"right\">Change</td>\n" +
+ "\t\t<td align=\"right\">% Chg</td>\n" +
+ "\t</tr>\n" +
+ "</table>\n" +
+ "<A HREF=\"http://www.netunitysoftware.com\"
TITLE=\"NetUnity WSRP .NET Framework\" >" +
+ "<img src=\"" + beforeTimeout + afterTimeout + "\"
border=\"0\" /></A>";
+
+ processMarkupAndCheck(markup, expected);
+ }
+
+ private void processMarkupAndCheck(String markup, String expected)
+ {
+ String result = RenderHandler.processMarkup(
+ markup,
+ NAMESPACE,
+ CONTEXT,
+ PORTLET_CONTEXT,
+ FORMAT,
+ CONSUMER
+ );
+ assertEquals(expected, result);
+ }
+
+ private String getResourceURL(String encodedURL, boolean requiresRewrite)
+ {
+ String result =
WSRPRewritingConstants.FAKE_RESOURCE_URL.replace(WSRPRewritingConstants.WSRP_URL,
encodedURL);
+ result = result.replace(WSRPRewritingConstants.WSRP_REQUIRES_REWRITE,
Boolean.toString(requiresRewrite));
+ return result;
+ }
+}
Property changes on:
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/handlers/RenderHandlerTestCase.java
___________________________________________________________________
Name: svn:executable
+ *