Author: chris.laprun(a)jboss.com
Date: 2010-08-16 13:09:43 -0400 (Mon, 16 Aug 2010)
New Revision: 3835
Added:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ProducerSessionInformation.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/SessionHandler.java
Removed:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/InvocationHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ProducerSessionInformation.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/SessionHandler.java
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/WSRPConsumer.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/WSRPConsumerImpl.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/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/handler/RequestHeaderClientHandler.java
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/ProducerSessionInformationTestCase.java
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/handler/RequestHeaderClientHandlerTestCase.java
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v1/MarkupTestCase.java
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/test/support/MockWSRPConsumer.java
Log:
- GTNWSRP-57: Moved InvocationHandler, SessionHandler and ProducerSessionInformation to
handlers package.
Modified: components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/WSRPConsumer.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/WSRPConsumer.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/WSRPConsumer.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -1,6 +1,6 @@
/*
* JBoss, a division of Red Hat
- * Copyright 2009, Red Hat Middleware, LLC, and individual
+ * 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.
@@ -28,8 +28,8 @@
import org.gatein.pc.api.invocation.PortletInvocation;
import org.gatein.wsrp.api.SessionEventListener;
import org.gatein.wsrp.consumer.ProducerInfo;
-import org.gatein.wsrp.consumer.ProducerSessionInformation;
import org.gatein.wsrp.consumer.RefreshResult;
+import org.gatein.wsrp.consumer.handlers.ProducerSessionInformation;
import javax.servlet.http.HttpSession;
@@ -53,7 +53,7 @@
*
* @param invocation a portlet invocation from which the session information should be
extracted.
* @return the session information for the producer associated with this consumer.
- * @see ProducerSessionInformation
+ * @see org.gatein.wsrp.consumer.handlers.ProducerSessionInformation
*/
ProducerSessionInformation getProducerSessionInformationFrom(PortletInvocation
invocation);
@@ -62,7 +62,7 @@
*
* @param session the session from the information should be extracted.
* @return the session information for the producer associated with this consumer.
- * @see ProducerSessionInformation
+ * @see org.gatein.wsrp.consumer.handlers.ProducerSessionInformation
*/
ProducerSessionInformation getProducerSessionInformationFrom(HttpSession session);
Deleted:
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-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/InvocationHandler.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -1,415 +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.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>
- * @version $Revision: 13121 $
- * @since 2.4 (May 31, 2006)
- */
-public abstract class InvocationHandler<Invocation extends PortletInvocation, Request,
Response>
-{
- protected final WSRPConsumerImpl consumer;
-
- protected static Logger log = LoggerFactory.getLogger(InvocationHandler.class);
- protected static boolean debug = log.isDebugEnabled();
- protected static boolean trace = log.isTraceEnabled();
-
- /**
- * Value indicating that we should not try further (unrecoverable error) for getMarkup
and
- * processBlockingInteraction
- */
- private static final int DO_NOT_RETRY = -1;
-
- /** Maximum number of tries before giving up. */
- private static final int MAXIMUM_RETRY_NUMBER = 3;
-
- protected InvocationHandler(WSRPConsumerImpl consumer)
- {
- this.consumer = consumer;
- }
-
- public PortletInvocationResponse handle(Invocation invocation) throws
PortletInvokerException
- {
- // Extracts basic required information from invocation
- RequestPrecursor<Invocation> requestPrecursor = new
RequestPrecursor<Invocation>(consumer, invocation);
-
- // create the specific request
- Request request = prepareRequest(requestPrecursor, invocation);
-
- // Perform the request
- try
- {
- Response response = performRequest(request, invocation);
- return processResponse(response, invocation, requestPrecursor);
- }
- catch (Exception e)
- {
- if (!(e instanceof PortletInvokerException))
- {
- ErrorResponse errorResponse = dealWithError(e, invocation,
getRuntimeContextFrom(request));
- if (errorResponse != null)
- {
- return unwrapWSRPError(errorResponse);
- }
- else
- {
- return new ErrorResponse(e);
- }
- }
- else
- {
- throw (PortletInvokerException)e;
- }
- }
- /*if (response instanceof ErrorResponse)
- {
- return unwrapWSRPError((ErrorResponse)response);
- }*/
- }
-
- protected Response performRequest(Request request, PortletInvocation invocation)
throws Exception
- {
- int retryCount = 0;
- Response response = null;
-
- // as long as we don't get a non-null response and we're allowed to try
again, try to perform the request
- while (response == null && retryCount++ <= MAXIMUM_RETRY_NUMBER)
- {
- if (debug)
- {
- log.debug("performRequest: " + retryCount + " attempt(s) out
of " + MAXIMUM_RETRY_NUMBER + " possible");
- }
- SessionHandler sessionHandler = consumer.getSessionHandler();
-
- // prepare everything for the request
- updateRegistrationContext(request);
- RuntimeContext runtimeContext = getRuntimeContextFrom(request);
-
- if (runtimeContext != null)
- {
- WindowContext windowContext = invocation.getWindowContext();
- runtimeContext.setNamespacePrefix(getNamespaceFrom(windowContext));
-
- InstanceContext instanceContext = invocation.getInstanceContext();
- runtimeContext.setPortletInstanceKey(instanceContext == null ? null :
instanceContext.getId());
-
- updateUserContext(request, consumer.getUserContextFrom(invocation,
runtimeContext));
- consumer.setTemplatesIfNeeded(invocation, runtimeContext);
- }
-
- try
- {
- sessionHandler.initCookieIfNeeded(invocation);
-
- // if we need cookies, set the current group id
- sessionHandler.initProducerSessionInformation(invocation);
-
- response = performRequest(request);
-
- sessionHandler.updateCookiesIfNeeded(invocation);
- }
- /*catch (Exception e)
- {
- ErrorResponse errorResponse = dealWithError(e, invocation, runtimeContext);
- if (errorResponse != null)
- {
- return errorResponse;
- }
- }*/
- finally
- {
- // we're done: reset currently held information
- sessionHandler.resetCurrentlyHeldInformation();
- }
- }
-
- if (retryCount >= MAXIMUM_RETRY_NUMBER)
- {
- /*return new ErrorResponse(new RuntimeException("Tried to perform request
" + MAXIMUM_RETRY_NUMBER
- + " times before giving up. This usually happens if an error in the WS
stack prevented the messages to be " +
- "properly transmitted. Look at server.log for clues as to what
happened..."));*/
- throw new RuntimeException("Tried to perform request " +
MAXIMUM_RETRY_NUMBER
- + " times before giving up. This usually happens if an error in the WS
stack prevented the messages to be " +
- "properly transmitted. Look at server.log for clues as to what
happened...");
- }
-
- if (debug)
- {
- log.debug("performRequest finished. Response is " + (response != null
? response.getClass().getName() : null));
- }
- return response;
- }
-
- protected static String getNamespaceFrom(WindowContext windowContext)
- {
- if (windowContext != null)
- {
- // MUST match namespace generation used in PortletResponseImpl.getNamespace in
portlet module...
- return PortletUtils.generateNamespaceFrom(windowContext.getId());
- }
-
- return null;
- }
-
- /**
- * Deals with common error conditions.
- *
- * @param error the error that is to be dealt with
- * @param invocation the invocation that caused the error to occur
- * @param runtimeContext the current WSRP RuntimeContext
- * @return an ErrorResponse if the error couldn't be dealt with or
<code>null</code> if the error was correctly
- * handled
- */
- private ErrorResponse dealWithError(Exception error, Invocation invocation,
RuntimeContext runtimeContext)
- throws PortletInvokerException
- {
- log.error("The portlet threw an exception", error);
-
- SessionHandler sessionHandler = consumer.getSessionHandler();
-
- // recoverable errors
- if (error instanceof InvalidCookie)
- {
- // we need to re-init the cookies
- log.debug("Re-initializing cookies after InvalidCookieFault.");
- // force a producer info refresh because the invalid cookie might be due to a
change of cookie policy on the producer
- consumer.refreshProducerInfo();
- try
- {
- sessionHandler.initCookieIfNeeded(invocation);
- }
- catch (Exception e)
- {
- log.debug("Couldn't init cookie: " + e.getLocalizedMessage());
- return new ErrorResponse(e);
- }
- }
- else if (error instanceof InvalidSession)
- {
- log.debug("Session invalidated after InvalidSessionFault, will re-send
session-stored information.");
- sessionHandler.handleInvalidSessionFault(invocation, runtimeContext);
- }
- else if (error instanceof InvalidRegistration)
- {
- log.debug("Invalid registration");
- consumer.handleInvalidRegistrationFault();
- }
- else
- {
- // other errors cannot be dealt with: we have an error condition
- return new ErrorResponse(error);
- }
- return null;
- }
-
- protected ErrorResponse unwrapWSRPError(ErrorResponse errorResponse)
- {
- Throwable cause = errorResponse.getCause();
- if (cause != null)
- {
- // unwrap original exception...
- if (cause instanceof OperationFailed && cause.getCause() != null)
- {
- cause = cause.getCause();
- }
- else if (cause instanceof RemoteException)
- {
- cause = ((RemoteException)cause).detail;
- }
- log.debug("Invocation of action failed: " + cause.getMessage(),
cause); // fix-me?
- return new ErrorResponse(cause);
- }
- else
- {
- log.debug("Invocation of action failed: " +
errorResponse.getMessage());
- return errorResponse;
- }
- }
-
- protected abstract void updateUserContext(Request request, UserContext userContext);
-
- protected abstract void updateRegistrationContext(Request request) throws
PortletInvokerException;
-
- protected abstract RuntimeContext getRuntimeContextFrom(Request request);
-
- protected abstract Response performRequest(Request request) throws Exception;
-
- protected abstract Request prepareRequest(RequestPrecursor<Invocation>
requestPrecursor, Invocation invocation);
-
- protected abstract PortletInvocationResponse processResponse(Response response,
Invocation invocation, RequestPrecursor<Invocation> 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<Invocation extends PortletInvocation>
- {
- 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, Invocation 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/ProducerSessionInformation.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ProducerSessionInformation.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ProducerSessionInformation.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -1,531 +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.util.ParameterValidation;
-import org.gatein.common.util.Tools;
-import org.gatein.wsrp.WSRPConstants;
-import org.oasis.wsrp.v2.SessionContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Records session and cookie information for a producer.
- *
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision: 12736 $
- * @since 2.4 (May 30, 2006)
- */
-public class ProducerSessionInformation implements Serializable
-{
- private static Logger log =
LoggerFactory.getLogger(ProducerSessionInformation.class);
-
- private boolean initCookieDone = false;
- private boolean perGroupCookies = false;
-
- /** group id -> Cookie[] */
- private Map<String, Cookie[]> groupCookies;
-
- /** portlet handle -> SessionInfo */
- private Map<String, SessionInfo> portletSessions;
-
- /** session id -> portlet handle */
- private Map<String, String> sessionId2PortletHandle;
-
- /** Cookies sent by the remote producer */
- private Cookie[] userCookie;
-
- /** Parent SessionHandler so that session mappings can be updated */
- private transient SessionHandler parent;
-
- /** The identifier of the Session containing this ProducerSessionInformation */
- private String parentSessionId;
-
- /**
- * public only for tests
- *
- * @return
- * @since 2.6
- */
- public String getParentSessionId()
- {
- return parentSessionId;
- }
-
- /**
- * public only for tests
- *
- * @param parentSessionId
- * @throws IllegalStateException if an attempt is made to set the parent session id to
a different one when it has
- * already been set.
- * @since 2.6
- */
- public void setParentSessionId(String parentSessionId)
- {
- if (this.parentSessionId != null &&
!this.parentSessionId.equals(parentSessionId))
- {
- throw new IllegalStateException("Cannot modify Parent Session id once it
has been set!");
- }
-
- this.parentSessionId = parentSessionId;
- }
-
- public String getUserCookie()
- {
- if (userCookie == null)
- {
- return null;
- }
-
- userCookie = purgeExpiredCookies(userCookie);
- if (userCookie.length == 0)
- {
- setInitCookieDone(false);
- }
- return outputToExternalForm(userCookie);
- }
-
- public void setUserCookie(Cookie[] userCookie)
- {
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(userCookie,
"cookies");
-
- this.userCookie = userCookie;
- }
-
- public boolean isInitCookieDone()
- {
- return initCookieDone;
- }
-
- public void setInitCookieDone(boolean initCookieDone)
- {
- this.initCookieDone = initCookieDone;
- }
-
- public boolean isPerGroupCookies()
- {
- return perGroupCookies;
- }
-
- public void setPerGroupCookies(boolean perGroupCookies)
- {
- this.perGroupCookies = perGroupCookies;
- }
-
- public void setGroupCookieFor(String groupId, Cookie[] cookies)
- {
- if (!isPerGroupCookies())
- {
- throw new IllegalStateException("Cannot add group cookie when cookie
protocol is perUser.");
- }
-
- if (groupId == null)
- {
- throw new IllegalArgumentException("Cannot set cookie for a null portlet
group id!");
- }
-
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(cookies,
"cookies");
-
- if (groupCookies == null)
- {
- groupCookies = new HashMap<String, Cookie[]>();
- }
-
- if (groupCookies.containsKey(groupId))
- {
- log.debug("Trying to set a cookie for an existing group: " +
groupId);
- }
-
- groupCookies.put(groupId, cookies);
- }
-
- public String getGroupCookieFor(String groupId)
- {
- if (groupCookies == null)
- {
- return null;
- }
-
- // purge expired cookies
- Cookie[] cookies = groupCookies.get(groupId);
- if (cookies != null)
- {
- cookies = purgeExpiredCookies(cookies);
-
- // if there are no non-expired cookies left, we will need to re-init them
- if (cookies.length == 0)
- {
- setInitCookieDone(false);
- }
-
- // update cookies for the considered group id
- groupCookies.put(groupId, cookies);
-
- return outputToExternalForm(cookies);
- }
- else
- {
- return null;
- }
- }
-
- public void clearGroupCookies()
- {
- groupCookies = null;
- }
-
- public void addSessionForPortlet(String portletHandle, SessionContext sessionContext)
- {
- // sessionContext is validated in SessionInfo constructor
- SessionInfo info = new SessionInfo(sessionContext, portletHandle);
-
- if (portletSessions == null)
- {
- portletSessions = new HashMap<String, SessionInfo>();
- sessionId2PortletHandle = new HashMap<String, String>();
- }
-
- portletSessions.put(portletHandle, info);
- sessionId2PortletHandle.put(sessionContext.getSessionID(), portletHandle);
-
- if (parent != null)
- {
- parent.addSessionMapping(sessionContext.getSessionID(), this);
- }
- }
-
- /**
- * Retrieves the session id for the portlet with the specified handle. Note that this
will "touch" the session, hence
- * resetting the time since the last use of the session.
- *
- * @param portletHandle the handle of the portlet for which the session id is to be
retrieved
- * @return the session id for the specified portlet, <code>null</code> if
there is no session associated with the
- * portlet or if the session has expired.
- */
- public String getSessionIdForPortlet(String portletHandle)
- {
- ProducerSessionInformation.SessionIdResult idResult =
internalGetSessionIdForPortlet(portletHandle);
- if (idResult.expired)
- {
- return null;
- }
-
- return idResult.id;
- }
-
- public int getNumberOfSessions()
- {
- if (portletSessions != null)
- {
- return portletSessions.size();
- }
- else
- {
- return 0;
- }
- }
-
- /**
- * @param sessionId
- * @return the id of the removed session or <code>null</code> if the
session had already expired
- */
- public String removeSession(String sessionId)
- {
- ParameterValidation.throwIllegalArgExceptionIfNull(sessionId, "session
id");
-
- String portletHandle = sessionId2PortletHandle.get(sessionId);
- if (portletHandle == null)
- {
- throw new IllegalArgumentException("No such session id: '" +
sessionId + "'");
- }
-
- return removeSessionForPortlet(portletHandle);
- }
-
- /**
- * @return a list containing the session ids that were still valid when they were
removed and would need to be
- * released
- */
- public List<String> removeSessions()
- {
- List<String> idsToRelease = new
ArrayList<String>(getNumberOfSessions());
-
- // copy to avoid ConcurrentModificationException
- List<String> handlesCopy = new
ArrayList<String>(portletSessions.keySet());
-
- for (String handle : handlesCopy)
- {
- SessionIdResult result = removeSessionIdForPortlet(handle);
-
- // only release sessions that are still valid
- if (!result.expired)
- {
- idsToRelease.add(result.id);
- }
- }
-
- return idsToRelease;
- }
-
- /**
- * @param portletHandle
- * @return the id of the removed session or <code>null</code> if the
session had already expired
- */
- public String removeSessionForPortlet(String portletHandle)
- {
- SessionIdResult result = removeSessionIdForPortlet(portletHandle);
-
- return result.expired ? null : result.id;
- }
-
- private SessionIdResult removeSessionIdForPortlet(String portletHandle)
- {
- ProducerSessionInformation.SessionIdResult result =
internalGetSessionIdForPortlet(portletHandle);
- final String id = result.id;
-
- if (id == null)
- {
- throw new IllegalArgumentException("There is no Session associated with
portlet '" + portletHandle + "'");
- }
-
- // if the session is still valid, release it and remove the associated mappings
- if (!result.expired)
- {
- portletSessions.remove(portletHandle);
- sessionId2PortletHandle.remove(id);
- if (parent != null)
- {
- parent.removeSessionId(id);
- }
- }
-
- return result;
- }
-
- public void replaceUserCookiesWith(ProducerSessionInformation currentSessionInfo)
- {
- if (currentSessionInfo != null && currentSessionInfo.userCookie != null
&& currentSessionInfo.userCookie.length > 0)
- {
- this.userCookie = currentSessionInfo.userCookie;
- }
- }
-
- /**
- * Create a String representation of the specified array of Cookies.
- *
- * @param cookies the cookies to be output to external form
- * @return a String representation of the cookies, ready to be sent over the wire.
- */
- private String outputToExternalForm(Cookie[] cookies)
- {
- if (cookies != null && cookies.length != 0)
- {
- int cookieNumber = cookies.length;
- StringBuffer sb = new StringBuffer(128 * cookieNumber);
- for (int i = 0; i < cookieNumber; i++)
- {
- sb.append(cookies[i].toExternalForm());
- if (i != cookieNumber - 1)
- {
- sb.append(","); // multiple cookies are separated by commas:
http://www.ietf.org/rfc/rfc2109.txt, 4.2.2
- }
- }
- return sb.toString();
- }
- return null;
- }
-
- /**
- * Purges the expired cookies in the specified array.
- *
- * @param cookies the cookies to be purged
- * @return an array of Cookies containing only still valid cookies
- */
- private Cookie[] purgeExpiredCookies(Cookie[] cookies)
- {
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(cookies,
"cookies");
-
- List<Cookie> cleanCookies = Tools.toList(cookies);
-
- for (Cookie cookie : cookies)
- {
- if (cookie.isExpired())
- {
- cleanCookies.remove(cookie);
- }
- }
- return cleanCookies.toArray(new Cookie[cleanCookies.size()]);
- }
-
- private SessionIdResult internalGetSessionIdForPortlet(String portletHandle)
- {
- SessionInfo session = getSessionInfoFor(portletHandle);
- if (session != null)
- {
- String id = session.getSessionId();
- if (!session.isStillValid())
- {
- portletSessions.remove(session.getPortletHandle());
- sessionId2PortletHandle.remove(session.getSessionId());
- if (parent != null)
- {
- parent.removeSessionId(session.getSessionId());
- }
- return new SessionIdResult(id, true);
- }
- else
- {
- return new SessionIdResult(id, false);
- }
- }
- return new SessionIdResult(null, false);
- }
-
- private SessionInfo getSessionInfoFor(String portletHandle)
- {
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletHandle,
"portlet handle", null);
-
- if (portletSessions == null)
- {
- return null;
- }
-
- return portletSessions.get(portletHandle);
- }
-
- /**
- * @return the known session id
- * @since 2.6
- */
- Collection<String> getSessionIds()
- {
- return sessionId2PortletHandle.keySet();
- }
-
- /**
- * @param sessionHandler
- * @since 2.6
- */
- void setParent(SessionHandler sessionHandler)
- {
- parent = sessionHandler;
- }
-
- /**
- * Update the mappings that were associated with the specified original portlet handle
after it has been modified as
- * a result of a clone operation to the specified new handle.
- *
- * @param originalHandle
- * @param newHandle
- * @since 2.6
- */
- public void updateHandleAssociatedInfo(String originalHandle, String newHandle)
- {
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(originalHandle,
"original handle",
- "Updating information associated with a portlet handle");
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(newHandle, "new
handle",
- "Updating information associated with a portlet handle");
-
- String sessionId = getSessionIdForPortlet(originalHandle);
- ProducerSessionInformation.SessionInfo info = getSessionInfoFor(originalHandle);
- if (sessionId != null && info != null)
- {
- portletSessions.put(newHandle, info);
- portletSessions.remove(originalHandle);
- sessionId2PortletHandle.put(sessionId, newHandle);
- log.debug("Updated mapping information for '" + originalHandle +
"' to reference '" + newHandle + "' instead.");
- }
- }
-
- private class SessionInfo implements Serializable
- {
- private SessionContext sessionContext;
- private long lastInvocationTime;
- private String portletHandle;
-
- public SessionInfo(SessionContext sessionContext, String portletHandle)
- {
- ParameterValidation.throwIllegalArgExceptionIfNull(sessionContext,
"SessionContext");
-
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(sessionContext.getSessionID(),
"session id", "SessionContext");
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletHandle,
"portlet handle", "SessionInfo");
-
- this.sessionContext = sessionContext;
- this.portletHandle = portletHandle;
- lastInvocationTime = System.currentTimeMillis();
- }
-
- /**
- * Checks that the session associated with the session context hasn't expired
and update the last invocation time
- *
- * @return
- */
- private boolean isStillValid()
- {
- int expires = sessionContext.getExpires();
- if (expires == WSRPConstants.SESSION_NEVER_EXPIRES)
- {
- return true;
- }
-
- long now = System.currentTimeMillis();
- long secondsSinceLastInvocation = (now - lastInvocationTime) / 1000;
- lastInvocationTime = now;
-
- long diff = expires - secondsSinceLastInvocation;
- log.debug("Session ID '" + sessionContext.getSessionID() +
"' is " + ((diff > 0) ? "" : "not")
- + " valid (time since last invocation: " + diff + ")");
- return diff > 0;
- }
-
- private String getSessionId()
- {
- return sessionContext.getSessionID();
- }
-
- private String getPortletHandle()
- {
- return portletHandle;
- }
- }
-
- private class SessionIdResult
- {
- private String id;
- private boolean expired;
-
- public SessionIdResult(String id, boolean expired)
- {
- this.id = id;
- this.expired = expired;
- }
- }
-}
Deleted:
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-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/SessionHandler.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -1,447 +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.InvokerUnavailableException;
-import org.gatein.pc.api.Portlet;
-import org.gatein.pc.api.PortletInvokerException;
-import org.gatein.pc.api.invocation.PortletInvocation;
-import org.gatein.wsrp.api.SessionEvent;
-import org.gatein.wsrp.api.SessionEventListener;
-import org.gatein.wsrp.consumer.portlet.info.WSRPPortletInfo;
-import org.gatein.wsrp.handler.RequestHeaderClientHandler;
-import org.gatein.wsrp.servlet.UserAccess;
-import org.oasis.wsrp.v2.CookieProtocol;
-import org.oasis.wsrp.v2.InvalidRegistration;
-import org.oasis.wsrp.v2.RuntimeContext;
-import org.oasis.wsrp.v2.SessionContext;
-import org.oasis.wsrp.v2.SessionParams;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.servlet.http.HttpSession;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * Manages session informations on behalf of a consumer.
- *
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision: 9360 $
- * @since 2.4 (May 31, 2006)
- */
-public class SessionHandler implements SessionEventListener
-{
- protected WSRPConsumerImpl consumer;
- private static Logger log = LoggerFactory.getLogger(SessionHandler.class);
-
- /** Cookie protocol required by the producer with the consumer */
- private CookieProtocol requiresInitCookie;
-
- /** The prefix used to isolate WSRP-related session information in the actual session
object. */
- private static final String SESSION_ID_PREFIX = "org.gatein.wsrp.session.";
-
- /** session id -> ProducerSessionInformation */
- private Map<String, ProducerSessionInformation> sessionInfos = new
ConcurrentHashMap<String, ProducerSessionInformation>(); // todo: thread-safe?
-
- /**
- * Constructs a new SessionHandler.
- *
- * @param consumer the consumer this SessionHandler is associated with.
- */
- public SessionHandler(WSRPConsumerImpl consumer)
- {
- this.consumer = consumer;
- }
-
- public boolean isPerUserCookieInit()
- {
- return CookieProtocol.PER_USER.equals(requiresInitCookie);
- }
-
- public boolean requiresInitCookie()
- {
- return requiresInitCookie != null &&
!CookieProtocol.NONE.equals(requiresInitCookie);
- }
-
- public void setRequiresInitCookie(CookieProtocol requiresInitCookie)
- {
- this.requiresInitCookie = requiresInitCookie;
- }
-
- void initProducerSessionInformation(PortletInvocation invocation) throws
PortletInvokerException
- {
- // if we need cookies, set the current group id
- String groupId;
- if (requiresGroupInitCookie())
- {
- WSRPPortletInfo info = consumer.getPortletInfo(invocation);
- groupId = info.getGroupId();
- }
- else
- {
- groupId = null;
- }
-
- RequestHeaderClientHandler.setCurrentInfo(groupId,
getProducerSessionInformation(invocation, true));
- }
-
- private boolean requiresGroupInitCookie()
- {
- return requiresInitCookie() &&
CookieProtocol.PER_GROUP.equals(requiresInitCookie);
- }
-
- /** Resets the information held by RequestHeaderClientHandler for the current
interaction. */
- public void resetCurrentlyHeldInformation()
- {
- RequestHeaderClientHandler.resetCurrentInfo();
- }
-
- public void initCookieIfNeeded(PortletInvocation invocation) throws
PortletInvokerException
- {
- initCookieIfNeeded(invocation, true);
- }
-
- private void initCookieIfNeeded(PortletInvocation invocation, boolean retryIfFails)
throws PortletInvokerException
- {
- // check if the cookie protocol requires cookie initialization
- if (!requiresInitCookie())
- {
- return;
- }
-
- // check if we have already initialized cookies for this user
- ProducerSessionInformation sessionInformation =
getProducerSessionInformation(invocation);
- if (sessionInformation.isInitCookieDone())
- {
- return;
- }
- RequestHeaderClientHandler.setCurrentInfo(null, sessionInformation);
-
- if (isPerUserCookieInit())
- {
- log.debug("Cookie initialization per user requested.");
- sessionInformation.setPerGroupCookies(false);
- initCookie(invocation, retryIfFails);
- }
- else
- {
- log.debug("Cookie initialization per group requested.");
- sessionInformation.setPerGroupCookies(true);
- Map<String, Set<Portlet>> groups = consumer.getPortletGroupMap();
-
- try
- {
- for (String groupId : groups.keySet())
- {
- RequestHeaderClientHandler.setCurrentGroupId(groupId);
- log.debug("Initializing cookie for group '" + groupId +
"'.");
- initCookie(invocation, retryIfFails);
- }
- }
- finally
- {
- resetCurrentlyHeldInformation();
- }
- }
-
- sessionInformation.setInitCookieDone(true);
- }
-
- private void initCookie(PortletInvocation invocation, boolean retryIfFails) throws
PortletInvokerException
- {
- try
- {
- consumer.getMarkupService().initCookie(consumer.getRegistrationContext(),
UserAccess.getUserContext());
- }
- catch (InvalidRegistration invalidRegistration)
- {
- consumer.handleInvalidRegistrationFault();
- if (retryIfFails)
- {
- initCookieIfNeeded(invocation, false);
- }
- }
- catch (Exception e)
- {
- throw new InvokerUnavailableException("Couldn't init cookies!",
e);
- }
- }
-
- void setSessionIdIfNeeded(PortletInvocation invocation, RuntimeContext runtimeContext,
String portletHandle)
- {
- ProducerSessionInformation producerSessionInfo =
getProducerSessionInformation(invocation, false);
- if (producerSessionInfo != null)
- {
- String sessionId = producerSessionInfo.getSessionIdForPortlet(portletHandle);
- SessionParams sessionParams = runtimeContext.getSessionParams();
- if (sessionParams == null)
- {
- sessionParams = new SessionParams();
- runtimeContext.setSessionParams(sessionParams);
- }
- sessionParams.setSessionID(sessionId);
- }
- }
-
- public void updateSessionIfNeeded(SessionContext sessionContext, PortletInvocation
invocation, String portletHandle)
- {
- if (sessionContext != null)
- {
- log.debug("Portlet '" + portletHandle + "' created
session with id '" + sessionContext.getSessionID() + "'");
- ProducerSessionInformation sessionInfo =
getProducerSessionInformation(invocation);
- sessionInfo.addSessionForPortlet(portletHandle, sessionContext);
- }
- }
-
- void updateCookiesIfNeeded(PortletInvocation invocation)
- {
- ProducerSessionInformation sessionInfo = getProducerSessionInformation(invocation,
true);
- ProducerSessionInformation currentSessionInfo =
RequestHeaderClientHandler.getCurrentProducerSessionInformation();
- if (sessionInfo != currentSessionInfo)
- {
- sessionInfo.replaceUserCookiesWith(currentSessionInfo);
- }
- }
-
- ProducerSessionInformation getProducerSessionInformation(PortletInvocation
invocation)
- {
- return getProducerSessionInformation(invocation, true);
- }
-
- private ProducerSessionInformation getProducerSessionInformation(PortletInvocation
invocation, boolean create)
- {
- HttpSession session = WSRPConsumerImpl.getHttpSession(invocation);
- return getProducerSessionInformation(session, create);
- }
-
- ProducerSessionInformation getProducerSessionInformation(HttpSession session)
- {
- return getProducerSessionInformation(session, false);
- }
-
- private ProducerSessionInformation getProducerSessionInformation(HttpSession session,
boolean create)
- {
- ParameterValidation.throwIllegalArgExceptionIfNull(session, "Session");
- String producerSessionKey = getProducerSessionInformationKey();
- ProducerSessionInformation sessionInformation =
(ProducerSessionInformation)session.getAttribute(producerSessionKey);
-
- if (sessionInformation != null)
- {
- sessionInformation.setParent(this);
- sessionInformation.setParentSessionId(session.getId());
- }
- else
- {
- if (create)
- {
- sessionInformation = new ProducerSessionInformation();
- sessionInformation.setParentSessionId(session.getId());
- session.setAttribute(producerSessionKey, sessionInformation);
- sessionInformation.setParent(this);
- }
- }
-
- return sessionInformation;
- }
-
- private String getProducerSessionInformationKey()
- {
- return SESSION_ID_PREFIX + consumer.getProducerId();
- }
-
- void handleInvalidSessionFault(PortletInvocation invocation, RuntimeContext
runtimeContext)
- {
- log.info("Resending information after InvalidSessionFault");
- // invalidate current session
- invalidateSession(invocation);
-
- // set the session id to null
- if (runtimeContext != null)
- {
- runtimeContext.setSessionParams(null);
- }
- }
-
- private void invalidateSession(PortletInvocation invocation)
- {
- HttpSession session = WSRPConsumerImpl.getHttpSession(invocation);
-
- // remove the associated info from the known producer session informations
-
- ProducerSessionInformation info = getProducerSessionInformation(session, false);
- if (info != null)
- {
- try
- {
- internalReleaseSessions(info.removeSessions());
- }
- catch (PortletInvokerException e)
- {
- // ignore since it's logged by internalReleaseSessions already
- }
- }
-
-
- session.removeAttribute(getProducerSessionInformationKey());
- RequestHeaderClientHandler.resetCurrentInfo();
- }
-
- /** @since 2.6 */
- void releaseSessions() throws PortletInvokerException
- {
- List<String> idsToRelease = new ArrayList<String>();
-
- Set<ProducerSessionInformation> uniqueInfos = new
HashSet<ProducerSessionInformation>(sessionInfos.values());
-
- for (ProducerSessionInformation info : uniqueInfos)
- {
- idsToRelease.addAll(info.removeSessions());
- }
-
- internalReleaseSessions(idsToRelease);
- }
-
- /**
- * @param sessionIds
- * @throws PortletInvokerException
- * @since 2.6
- */
- void releaseSessions(String[] sessionIds) throws PortletInvokerException
- {
- if (sessionIds != null)
- {
- List<String> idsToRelease = new ArrayList<String>();
-
- for (String sessionId : sessionIds)
- {
- ProducerSessionInformation info = sessionInfos.get(sessionId);
- sessionId = info.removeSession(sessionId);
- if (sessionId != null)
- {
- idsToRelease.add(sessionId);
- }
- }
-
- internalReleaseSessions(idsToRelease);
- }
- }
-
- private void internalReleaseSessions(List<String> idsToRelease) throws
PortletInvokerException
- {
- if (idsToRelease != null && !idsToRelease.isEmpty())
- {
- try
- {
-
consumer.getMarkupService().releaseSessions(consumer.getRegistrationContext(),
idsToRelease, UserAccess.getUserContext());
- }
- catch (InvalidRegistration invalidRegistration)
- {
- log.debug("Invalid Registration");
- consumer.handleInvalidRegistrationFault();
- }
- catch (Exception e)
- {
- String message = "Couldn't release sessions " + idsToRelease;
- log.debug(message, e);
- throw new PortletInvokerException(message, e);
- }
- }
- }
-
- // ProducerSessionInformation callbacks
-
- /**
- * Update session mappings when a session has expired
- *
- * @param id
- * @since 2.6
- */
- void removeSessionId(String id)
- {
- sessionInfos.remove(id);
- }
-
- /**
- * Update session mappings when a new session id is added to the specified
ProducerSessionInformation
- *
- * @param sessionID
- * @param producerSessionInformation
- * @since 2.6
- */
- void addSessionMapping(String sessionID, ProducerSessionInformation
producerSessionInformation)
- {
- sessionInfos.put(sessionID, producerSessionInformation);
- }
-
- // End ProducerSessionInformation callbacks
-
- // SessionEventListener implementation
-
- public void onSessionEvent(SessionEvent event)
- {
- if (SessionEvent.SessionEventType.SESSION_DESTROYED.equals(event.getType()))
- {
- String id = event.getSession().getId();
-
- // check if the session being destroyed is the one associated with this thread
- ProducerSessionInformation info =
RequestHeaderClientHandler.getCurrentProducerSessionInformation();
- if (info != null)
- {
- if (id != null && id.equals(info.getParentSessionId()))
- {
- try
- {
- internalReleaseSessions(info.removeSessions());
- }
- catch (PortletInvokerException e)
- {
- // already logged...
- }
- log.debug("Released session '" + id + "' after
session was destroyed by Portal.");
- }
- }
- }
- }
-
- /**
- * @param originalHandle
- * @param newHandle
- * @param invocation
- * @since 2.6
- */
- public void updateSessionInfoFor(String originalHandle, String newHandle,
PortletInvocation invocation)
- {
- ProducerSessionInformation info = getProducerSessionInformation(invocation,
false);
- if (info != null)
- {
- info.updateHandleAssociatedInfo(originalHandle, newHandle);
- }
- }
-}
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-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/WSRPConsumerImpl.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -46,6 +46,8 @@
import org.gatein.wsrp.WSRPUtils;
import org.gatein.wsrp.api.SessionEvent;
import org.gatein.wsrp.consumer.handlers.InvocationDispatcher;
+import org.gatein.wsrp.consumer.handlers.ProducerSessionInformation;
+import org.gatein.wsrp.consumer.handlers.SessionHandler;
import org.gatein.wsrp.consumer.portlet.WSRPPortlet;
import org.gatein.wsrp.consumer.portlet.info.WSRPPortletInfo;
import org.gatein.wsrp.services.MarkupService;
@@ -392,19 +394,19 @@
}
public PortletContext exportPortlet(PortletStateType stateType, PortletContext
originalPortletContext)
- throws PortletInvokerException
+ throws PortletInvokerException
{
throw new NotYetImplemented();
}
-
+
public PortletContext importPortlet(PortletStateType stateType, PortletContext
originalPortletContext)
- throws PortletInvokerException
+ throws PortletInvokerException
{
throw new NotYetImplemented();
}
// Accessors
********************************************************************************************************
-
+
public String getProducerId()
{
return producerInfo.getId();
@@ -427,12 +429,12 @@
* @return
* @since 2.6
*/
- static PortletContext getPortletContext(PortletInvocation invocation)
+ public static PortletContext getPortletContext(PortletInvocation invocation)
{
return invocation.getTarget();
}
- WSRPPortletInfo getPortletInfo(PortletInvocation invocation) throws
PortletInvokerException
+ public WSRPPortletInfo getPortletInfo(PortletInvocation invocation) throws
PortletInvokerException
{
return (WSRPPortletInfo)getWSRPPortlet(getPortletContext(invocation)).getInfo();
}
@@ -460,7 +462,7 @@
// Registration
*****************************************************************************************************
- void handleInvalidRegistrationFault() throws PortletInvokerException
+ public void handleInvalidRegistrationFault() throws PortletInvokerException
{
// reset registration data and try again
producerInfo.resetRegistration();
@@ -625,7 +627,7 @@
// fix-me!
- org.oasis.wsrp.v2.UserContext getUserContextFrom(PortletInvocation invocation,
RuntimeContext runtimeContext) throws PortletInvokerException
+ public org.oasis.wsrp.v2.UserContext getUserContextFrom(PortletInvocation invocation,
RuntimeContext runtimeContext) throws PortletInvokerException
{
// first decide if we need to pass the user context...
WSRPPortletInfo info = getPortletInfo(invocation);
@@ -651,7 +653,7 @@
return null;
}
- void setTemplatesIfNeeded(PortletInvocation invocation, RuntimeContext runtimeContext)
throws PortletInvokerException
+ public void setTemplatesIfNeeded(PortletInvocation invocation, RuntimeContext
runtimeContext) throws PortletInvokerException
{
// todo: could store templates in producer session info to avoid to re-generate
them all the time?
WSRPPortletInfo info = getPortletInfo(invocation);
@@ -666,13 +668,13 @@
}
}
- static HttpServletRequest getHttpRequest(PortletInvocation invocation)
+ public static HttpServletRequest getHttpRequest(PortletInvocation invocation)
{
AbstractPortletInvocationContext invocationContext =
(AbstractPortletInvocationContext)invocation.getContext();
return invocationContext.getClientRequest();
}
- static HttpSession getHttpSession(PortletInvocation invocation)
+ public static HttpSession getHttpSession(PortletInvocation invocation)
{
return getHttpRequest(invocation).getSession();
}
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/EventHandler.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -32,7 +32,6 @@
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;
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationDispatcher.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -42,7 +42,6 @@
import org.gatein.pc.api.invocation.response.ResponseProperties;
import org.gatein.wsrp.WSRPResourceURL;
import org.gatein.wsrp.WSRPRewritingConstants;
-import org.gatein.wsrp.consumer.InvocationHandler;
import org.gatein.wsrp.consumer.WSRPConsumerImpl;
import org.gatein.wsrp.handler.CookieUtil;
import org.gatein.wsrp.spec.v2.WSRP2RewritingConstants;
Copied:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationHandler.java
(from rev 3834,
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/InvocationHandler.java)
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationHandler.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/InvocationHandler.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -0,0 +1,416 @@
+/*
+ * 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.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.gatein.wsrp.consumer.WSRPConsumerImpl;
+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>
+ * @version $Revision: 13121 $
+ * @since 2.4 (May 31, 2006)
+ */
+public abstract class InvocationHandler<Invocation extends PortletInvocation, Request,
Response>
+{
+ protected final WSRPConsumerImpl consumer;
+
+ protected static Logger log = LoggerFactory.getLogger(InvocationHandler.class);
+ protected static boolean debug = log.isDebugEnabled();
+ protected static boolean trace = log.isTraceEnabled();
+
+ /**
+ * Value indicating that we should not try further (unrecoverable error) for getMarkup
and
+ * processBlockingInteraction
+ */
+ private static final int DO_NOT_RETRY = -1;
+
+ /** Maximum number of tries before giving up. */
+ private static final int MAXIMUM_RETRY_NUMBER = 3;
+
+ protected InvocationHandler(WSRPConsumerImpl consumer)
+ {
+ this.consumer = consumer;
+ }
+
+ public PortletInvocationResponse handle(Invocation invocation) throws
PortletInvokerException
+ {
+ // Extracts basic required information from invocation
+ RequestPrecursor<Invocation> requestPrecursor = new
RequestPrecursor<Invocation>(consumer, invocation);
+
+ // create the specific request
+ Request request = prepareRequest(requestPrecursor, invocation);
+
+ // Perform the request
+ try
+ {
+ Response response = performRequest(request, invocation);
+ return processResponse(response, invocation, requestPrecursor);
+ }
+ catch (Exception e)
+ {
+ if (!(e instanceof PortletInvokerException))
+ {
+ ErrorResponse errorResponse = dealWithError(e, invocation,
getRuntimeContextFrom(request));
+ if (errorResponse != null)
+ {
+ return unwrapWSRPError(errorResponse);
+ }
+ else
+ {
+ return new ErrorResponse(e);
+ }
+ }
+ else
+ {
+ throw (PortletInvokerException)e;
+ }
+ }
+ /*if (response instanceof ErrorResponse)
+ {
+ return unwrapWSRPError((ErrorResponse)response);
+ }*/
+ }
+
+ protected Response performRequest(Request request, PortletInvocation invocation)
throws Exception
+ {
+ int retryCount = 0;
+ Response response = null;
+
+ // as long as we don't get a non-null response and we're allowed to try
again, try to perform the request
+ while (response == null && retryCount++ <= MAXIMUM_RETRY_NUMBER)
+ {
+ if (debug)
+ {
+ log.debug("performRequest: " + retryCount + " attempt(s) out
of " + MAXIMUM_RETRY_NUMBER + " possible");
+ }
+ SessionHandler sessionHandler = consumer.getSessionHandler();
+
+ // prepare everything for the request
+ updateRegistrationContext(request);
+ RuntimeContext runtimeContext = getRuntimeContextFrom(request);
+
+ if (runtimeContext != null)
+ {
+ WindowContext windowContext = invocation.getWindowContext();
+ runtimeContext.setNamespacePrefix(getNamespaceFrom(windowContext));
+
+ InstanceContext instanceContext = invocation.getInstanceContext();
+ runtimeContext.setPortletInstanceKey(instanceContext == null ? null :
instanceContext.getId());
+
+ updateUserContext(request, consumer.getUserContextFrom(invocation,
runtimeContext));
+ consumer.setTemplatesIfNeeded(invocation, runtimeContext);
+ }
+
+ try
+ {
+ sessionHandler.initCookieIfNeeded(invocation);
+
+ // if we need cookies, set the current group id
+ sessionHandler.initProducerSessionInformation(invocation);
+
+ response = performRequest(request);
+
+ sessionHandler.updateCookiesIfNeeded(invocation);
+ }
+ /*catch (Exception e)
+ {
+ ErrorResponse errorResponse = dealWithError(e, invocation, runtimeContext);
+ if (errorResponse != null)
+ {
+ return errorResponse;
+ }
+ }*/
+ finally
+ {
+ // we're done: reset currently held information
+ sessionHandler.resetCurrentlyHeldInformation();
+ }
+ }
+
+ if (retryCount >= MAXIMUM_RETRY_NUMBER)
+ {
+ /*return new ErrorResponse(new RuntimeException("Tried to perform request
" + MAXIMUM_RETRY_NUMBER
+ + " times before giving up. This usually happens if an error in the WS
stack prevented the messages to be " +
+ "properly transmitted. Look at server.log for clues as to what
happened..."));*/
+ throw new RuntimeException("Tried to perform request " +
MAXIMUM_RETRY_NUMBER
+ + " times before giving up. This usually happens if an error in the WS
stack prevented the messages to be " +
+ "properly transmitted. Look at server.log for clues as to what
happened...");
+ }
+
+ if (debug)
+ {
+ log.debug("performRequest finished. Response is " + (response != null
? response.getClass().getName() : null));
+ }
+ return response;
+ }
+
+ protected static String getNamespaceFrom(WindowContext windowContext)
+ {
+ if (windowContext != null)
+ {
+ // MUST match namespace generation used in PortletResponseImpl.getNamespace in
portlet module...
+ return PortletUtils.generateNamespaceFrom(windowContext.getId());
+ }
+
+ return null;
+ }
+
+ /**
+ * Deals with common error conditions.
+ *
+ * @param error the error that is to be dealt with
+ * @param invocation the invocation that caused the error to occur
+ * @param runtimeContext the current WSRP RuntimeContext
+ * @return an ErrorResponse if the error couldn't be dealt with or
<code>null</code> if the error was correctly
+ * handled
+ */
+ private ErrorResponse dealWithError(Exception error, Invocation invocation,
RuntimeContext runtimeContext)
+ throws PortletInvokerException
+ {
+ log.error("The portlet threw an exception", error);
+
+ SessionHandler sessionHandler = consumer.getSessionHandler();
+
+ // recoverable errors
+ if (error instanceof InvalidCookie)
+ {
+ // we need to re-init the cookies
+ log.debug("Re-initializing cookies after InvalidCookieFault.");
+ // force a producer info refresh because the invalid cookie might be due to a
change of cookie policy on the producer
+ consumer.refreshProducerInfo();
+ try
+ {
+ sessionHandler.initCookieIfNeeded(invocation);
+ }
+ catch (Exception e)
+ {
+ log.debug("Couldn't init cookie: " + e.getLocalizedMessage());
+ return new ErrorResponse(e);
+ }
+ }
+ else if (error instanceof InvalidSession)
+ {
+ log.debug("Session invalidated after InvalidSessionFault, will re-send
session-stored information.");
+ sessionHandler.handleInvalidSessionFault(invocation, runtimeContext);
+ }
+ else if (error instanceof InvalidRegistration)
+ {
+ log.debug("Invalid registration");
+ consumer.handleInvalidRegistrationFault();
+ }
+ else
+ {
+ // other errors cannot be dealt with: we have an error condition
+ return new ErrorResponse(error);
+ }
+ return null;
+ }
+
+ protected ErrorResponse unwrapWSRPError(ErrorResponse errorResponse)
+ {
+ Throwable cause = errorResponse.getCause();
+ if (cause != null)
+ {
+ // unwrap original exception...
+ if (cause instanceof OperationFailed && cause.getCause() != null)
+ {
+ cause = cause.getCause();
+ }
+ else if (cause instanceof RemoteException)
+ {
+ cause = ((RemoteException)cause).detail;
+ }
+ log.debug("Invocation of action failed: " + cause.getMessage(),
cause); // fix-me?
+ return new ErrorResponse(cause);
+ }
+ else
+ {
+ log.debug("Invocation of action failed: " +
errorResponse.getMessage());
+ return errorResponse;
+ }
+ }
+
+ protected abstract void updateUserContext(Request request, UserContext userContext);
+
+ protected abstract void updateRegistrationContext(Request request) throws
PortletInvokerException;
+
+ protected abstract RuntimeContext getRuntimeContextFrom(Request request);
+
+ protected abstract Response performRequest(Request request) throws Exception;
+
+ protected abstract Request prepareRequest(RequestPrecursor<Invocation>
requestPrecursor, Invocation invocation);
+
+ protected abstract PortletInvocationResponse processResponse(Response response,
Invocation invocation, RequestPrecursor<Invocation> 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<Invocation extends PortletInvocation>
+ {
+ 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, Invocation 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;
+ }
+ }
+}
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/MimeResponseHandler.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -38,7 +38,6 @@
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;
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/NavigationalStateUpdatingHandler.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -30,7 +30,6 @@
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;
Copied:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ProducerSessionInformation.java
(from rev 3834,
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/ProducerSessionInformation.java)
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ProducerSessionInformation.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/ProducerSessionInformation.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -0,0 +1,531 @@
+/*
+ * 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.util.ParameterValidation;
+import org.gatein.common.util.Tools;
+import org.gatein.wsrp.WSRPConstants;
+import org.oasis.wsrp.v2.SessionContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Records session and cookie information for a producer.
+ *
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 12736 $
+ * @since 2.4 (May 30, 2006)
+ */
+public class ProducerSessionInformation implements Serializable
+{
+ private static Logger log =
LoggerFactory.getLogger(ProducerSessionInformation.class);
+
+ private boolean initCookieDone = false;
+ private boolean perGroupCookies = false;
+
+ /** group id -> Cookie[] */
+ private Map<String, Cookie[]> groupCookies;
+
+ /** portlet handle -> SessionInfo */
+ private Map<String, SessionInfo> portletSessions;
+
+ /** session id -> portlet handle */
+ private Map<String, String> sessionId2PortletHandle;
+
+ /** Cookies sent by the remote producer */
+ private Cookie[] userCookie;
+
+ /** Parent SessionHandler so that session mappings can be updated */
+ private transient SessionHandler parent;
+
+ /** The identifier of the Session containing this ProducerSessionInformation */
+ private String parentSessionId;
+
+ /**
+ * public only for tests
+ *
+ * @return
+ * @since 2.6
+ */
+ public String getParentSessionId()
+ {
+ return parentSessionId;
+ }
+
+ /**
+ * public only for tests
+ *
+ * @param parentSessionId
+ * @throws IllegalStateException if an attempt is made to set the parent session id to
a different one when it has
+ * already been set.
+ * @since 2.6
+ */
+ public void setParentSessionId(String parentSessionId)
+ {
+ if (this.parentSessionId != null &&
!this.parentSessionId.equals(parentSessionId))
+ {
+ throw new IllegalStateException("Cannot modify Parent Session id once it
has been set!");
+ }
+
+ this.parentSessionId = parentSessionId;
+ }
+
+ public String getUserCookie()
+ {
+ if (userCookie == null)
+ {
+ return null;
+ }
+
+ userCookie = purgeExpiredCookies(userCookie);
+ if (userCookie.length == 0)
+ {
+ setInitCookieDone(false);
+ }
+ return outputToExternalForm(userCookie);
+ }
+
+ public void setUserCookie(Cookie[] userCookie)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(userCookie,
"cookies");
+
+ this.userCookie = userCookie;
+ }
+
+ public boolean isInitCookieDone()
+ {
+ return initCookieDone;
+ }
+
+ public void setInitCookieDone(boolean initCookieDone)
+ {
+ this.initCookieDone = initCookieDone;
+ }
+
+ public boolean isPerGroupCookies()
+ {
+ return perGroupCookies;
+ }
+
+ public void setPerGroupCookies(boolean perGroupCookies)
+ {
+ this.perGroupCookies = perGroupCookies;
+ }
+
+ public void setGroupCookieFor(String groupId, Cookie[] cookies)
+ {
+ if (!isPerGroupCookies())
+ {
+ throw new IllegalStateException("Cannot add group cookie when cookie
protocol is perUser.");
+ }
+
+ if (groupId == null)
+ {
+ throw new IllegalArgumentException("Cannot set cookie for a null portlet
group id!");
+ }
+
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(cookies,
"cookies");
+
+ if (groupCookies == null)
+ {
+ groupCookies = new HashMap<String, Cookie[]>();
+ }
+
+ if (groupCookies.containsKey(groupId))
+ {
+ log.debug("Trying to set a cookie for an existing group: " +
groupId);
+ }
+
+ groupCookies.put(groupId, cookies);
+ }
+
+ public String getGroupCookieFor(String groupId)
+ {
+ if (groupCookies == null)
+ {
+ return null;
+ }
+
+ // purge expired cookies
+ Cookie[] cookies = groupCookies.get(groupId);
+ if (cookies != null)
+ {
+ cookies = purgeExpiredCookies(cookies);
+
+ // if there are no non-expired cookies left, we will need to re-init them
+ if (cookies.length == 0)
+ {
+ setInitCookieDone(false);
+ }
+
+ // update cookies for the considered group id
+ groupCookies.put(groupId, cookies);
+
+ return outputToExternalForm(cookies);
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ public void clearGroupCookies()
+ {
+ groupCookies = null;
+ }
+
+ public void addSessionForPortlet(String portletHandle, SessionContext sessionContext)
+ {
+ // sessionContext is validated in SessionInfo constructor
+ SessionInfo info = new SessionInfo(sessionContext, portletHandle);
+
+ if (portletSessions == null)
+ {
+ portletSessions = new HashMap<String, SessionInfo>();
+ sessionId2PortletHandle = new HashMap<String, String>();
+ }
+
+ portletSessions.put(portletHandle, info);
+ sessionId2PortletHandle.put(sessionContext.getSessionID(), portletHandle);
+
+ if (parent != null)
+ {
+ parent.addSessionMapping(sessionContext.getSessionID(), this);
+ }
+ }
+
+ /**
+ * Retrieves the session id for the portlet with the specified handle. Note that this
will "touch" the session, hence
+ * resetting the time since the last use of the session.
+ *
+ * @param portletHandle the handle of the portlet for which the session id is to be
retrieved
+ * @return the session id for the specified portlet, <code>null</code> if
there is no session associated with the
+ * portlet or if the session has expired.
+ */
+ public String getSessionIdForPortlet(String portletHandle)
+ {
+ ProducerSessionInformation.SessionIdResult idResult =
internalGetSessionIdForPortlet(portletHandle);
+ if (idResult.expired)
+ {
+ return null;
+ }
+
+ return idResult.id;
+ }
+
+ public int getNumberOfSessions()
+ {
+ if (portletSessions != null)
+ {
+ return portletSessions.size();
+ }
+ else
+ {
+ return 0;
+ }
+ }
+
+ /**
+ * @param sessionId
+ * @return the id of the removed session or <code>null</code> if the
session had already expired
+ */
+ public String removeSession(String sessionId)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(sessionId, "session
id");
+
+ String portletHandle = sessionId2PortletHandle.get(sessionId);
+ if (portletHandle == null)
+ {
+ throw new IllegalArgumentException("No such session id: '" +
sessionId + "'");
+ }
+
+ return removeSessionForPortlet(portletHandle);
+ }
+
+ /**
+ * @return a list containing the session ids that were still valid when they were
removed and would need to be
+ * released
+ */
+ public List<String> removeSessions()
+ {
+ List<String> idsToRelease = new
ArrayList<String>(getNumberOfSessions());
+
+ // copy to avoid ConcurrentModificationException
+ List<String> handlesCopy = new
ArrayList<String>(portletSessions.keySet());
+
+ for (String handle : handlesCopy)
+ {
+ SessionIdResult result = removeSessionIdForPortlet(handle);
+
+ // only release sessions that are still valid
+ if (!result.expired)
+ {
+ idsToRelease.add(result.id);
+ }
+ }
+
+ return idsToRelease;
+ }
+
+ /**
+ * @param portletHandle
+ * @return the id of the removed session or <code>null</code> if the
session had already expired
+ */
+ public String removeSessionForPortlet(String portletHandle)
+ {
+ SessionIdResult result = removeSessionIdForPortlet(portletHandle);
+
+ return result.expired ? null : result.id;
+ }
+
+ private SessionIdResult removeSessionIdForPortlet(String portletHandle)
+ {
+ ProducerSessionInformation.SessionIdResult result =
internalGetSessionIdForPortlet(portletHandle);
+ final String id = result.id;
+
+ if (id == null)
+ {
+ throw new IllegalArgumentException("There is no Session associated with
portlet '" + portletHandle + "'");
+ }
+
+ // if the session is still valid, release it and remove the associated mappings
+ if (!result.expired)
+ {
+ portletSessions.remove(portletHandle);
+ sessionId2PortletHandle.remove(id);
+ if (parent != null)
+ {
+ parent.removeSessionId(id);
+ }
+ }
+
+ return result;
+ }
+
+ public void replaceUserCookiesWith(ProducerSessionInformation currentSessionInfo)
+ {
+ if (currentSessionInfo != null && currentSessionInfo.userCookie != null
&& currentSessionInfo.userCookie.length > 0)
+ {
+ this.userCookie = currentSessionInfo.userCookie;
+ }
+ }
+
+ /**
+ * Create a String representation of the specified array of Cookies.
+ *
+ * @param cookies the cookies to be output to external form
+ * @return a String representation of the cookies, ready to be sent over the wire.
+ */
+ private String outputToExternalForm(Cookie[] cookies)
+ {
+ if (cookies != null && cookies.length != 0)
+ {
+ int cookieNumber = cookies.length;
+ StringBuffer sb = new StringBuffer(128 * cookieNumber);
+ for (int i = 0; i < cookieNumber; i++)
+ {
+ sb.append(cookies[i].toExternalForm());
+ if (i != cookieNumber - 1)
+ {
+ sb.append(","); // multiple cookies are separated by commas:
http://www.ietf.org/rfc/rfc2109.txt, 4.2.2
+ }
+ }
+ return sb.toString();
+ }
+ return null;
+ }
+
+ /**
+ * Purges the expired cookies in the specified array.
+ *
+ * @param cookies the cookies to be purged
+ * @return an array of Cookies containing only still valid cookies
+ */
+ private Cookie[] purgeExpiredCookies(Cookie[] cookies)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(cookies,
"cookies");
+
+ List<Cookie> cleanCookies = Tools.toList(cookies);
+
+ for (Cookie cookie : cookies)
+ {
+ if (cookie.isExpired())
+ {
+ cleanCookies.remove(cookie);
+ }
+ }
+ return cleanCookies.toArray(new Cookie[cleanCookies.size()]);
+ }
+
+ private SessionIdResult internalGetSessionIdForPortlet(String portletHandle)
+ {
+ SessionInfo session = getSessionInfoFor(portletHandle);
+ if (session != null)
+ {
+ String id = session.getSessionId();
+ if (!session.isStillValid())
+ {
+ portletSessions.remove(session.getPortletHandle());
+ sessionId2PortletHandle.remove(session.getSessionId());
+ if (parent != null)
+ {
+ parent.removeSessionId(session.getSessionId());
+ }
+ return new SessionIdResult(id, true);
+ }
+ else
+ {
+ return new SessionIdResult(id, false);
+ }
+ }
+ return new SessionIdResult(null, false);
+ }
+
+ private SessionInfo getSessionInfoFor(String portletHandle)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletHandle,
"portlet handle", null);
+
+ if (portletSessions == null)
+ {
+ return null;
+ }
+
+ return portletSessions.get(portletHandle);
+ }
+
+ /**
+ * @return the known session id
+ * @since 2.6
+ */
+ Collection<String> getSessionIds()
+ {
+ return sessionId2PortletHandle.keySet();
+ }
+
+ /**
+ * @param sessionHandler
+ * @since 2.6
+ */
+ void setParent(SessionHandler sessionHandler)
+ {
+ parent = sessionHandler;
+ }
+
+ /**
+ * Update the mappings that were associated with the specified original portlet handle
after it has been modified as
+ * a result of a clone operation to the specified new handle.
+ *
+ * @param originalHandle
+ * @param newHandle
+ * @since 2.6
+ */
+ public void updateHandleAssociatedInfo(String originalHandle, String newHandle)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(originalHandle,
"original handle",
+ "Updating information associated with a portlet handle");
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(newHandle, "new
handle",
+ "Updating information associated with a portlet handle");
+
+ String sessionId = getSessionIdForPortlet(originalHandle);
+ ProducerSessionInformation.SessionInfo info = getSessionInfoFor(originalHandle);
+ if (sessionId != null && info != null)
+ {
+ portletSessions.put(newHandle, info);
+ portletSessions.remove(originalHandle);
+ sessionId2PortletHandle.put(sessionId, newHandle);
+ log.debug("Updated mapping information for '" + originalHandle +
"' to reference '" + newHandle + "' instead.");
+ }
+ }
+
+ private class SessionInfo implements Serializable
+ {
+ private SessionContext sessionContext;
+ private long lastInvocationTime;
+ private String portletHandle;
+
+ public SessionInfo(SessionContext sessionContext, String portletHandle)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(sessionContext,
"SessionContext");
+
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(sessionContext.getSessionID(),
"session id", "SessionContext");
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletHandle,
"portlet handle", "SessionInfo");
+
+ this.sessionContext = sessionContext;
+ this.portletHandle = portletHandle;
+ lastInvocationTime = System.currentTimeMillis();
+ }
+
+ /**
+ * Checks that the session associated with the session context hasn't expired
and update the last invocation time
+ *
+ * @return
+ */
+ private boolean isStillValid()
+ {
+ int expires = sessionContext.getExpires();
+ if (expires == WSRPConstants.SESSION_NEVER_EXPIRES)
+ {
+ return true;
+ }
+
+ long now = System.currentTimeMillis();
+ long secondsSinceLastInvocation = (now - lastInvocationTime) / 1000;
+ lastInvocationTime = now;
+
+ long diff = expires - secondsSinceLastInvocation;
+ log.debug("Session ID '" + sessionContext.getSessionID() +
"' is " + ((diff > 0) ? "" : "not")
+ + " valid (time since last invocation: " + diff + ")");
+ return diff > 0;
+ }
+
+ private String getSessionId()
+ {
+ return sessionContext.getSessionID();
+ }
+
+ private String getPortletHandle()
+ {
+ return portletHandle;
+ }
+ }
+
+ private class SessionIdResult
+ {
+ private String id;
+ private boolean expired;
+
+ public SessionIdResult(String id, boolean expired)
+ {
+ this.id = id;
+ this.expired = expired;
+ }
+ }
+}
Copied:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/SessionHandler.java
(from rev 3834,
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/SessionHandler.java)
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/SessionHandler.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/handlers/SessionHandler.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -0,0 +1,448 @@
+/*
+ * 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.InvokerUnavailableException;
+import org.gatein.pc.api.Portlet;
+import org.gatein.pc.api.PortletInvokerException;
+import org.gatein.pc.api.invocation.PortletInvocation;
+import org.gatein.wsrp.api.SessionEvent;
+import org.gatein.wsrp.api.SessionEventListener;
+import org.gatein.wsrp.consumer.WSRPConsumerImpl;
+import org.gatein.wsrp.consumer.portlet.info.WSRPPortletInfo;
+import org.gatein.wsrp.handler.RequestHeaderClientHandler;
+import org.gatein.wsrp.servlet.UserAccess;
+import org.oasis.wsrp.v2.CookieProtocol;
+import org.oasis.wsrp.v2.InvalidRegistration;
+import org.oasis.wsrp.v2.RuntimeContext;
+import org.oasis.wsrp.v2.SessionContext;
+import org.oasis.wsrp.v2.SessionParams;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.http.HttpSession;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Manages session informations on behalf of a consumer.
+ *
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 9360 $
+ * @since 2.4 (May 31, 2006)
+ */
+public class SessionHandler implements SessionEventListener
+{
+ protected WSRPConsumerImpl consumer;
+ private static Logger log = LoggerFactory.getLogger(SessionHandler.class);
+
+ /** Cookie protocol required by the producer with the consumer */
+ private CookieProtocol requiresInitCookie;
+
+ /** The prefix used to isolate WSRP-related session information in the actual session
object. */
+ private static final String SESSION_ID_PREFIX = "org.gatein.wsrp.session.";
+
+ /** session id -> ProducerSessionInformation */
+ private Map<String, ProducerSessionInformation> sessionInfos = new
ConcurrentHashMap<String, ProducerSessionInformation>(); // todo: thread-safe?
+
+ /**
+ * Constructs a new SessionHandler.
+ *
+ * @param consumer the consumer this SessionHandler is associated with.
+ */
+ public SessionHandler(WSRPConsumerImpl consumer)
+ {
+ this.consumer = consumer;
+ }
+
+ public boolean isPerUserCookieInit()
+ {
+ return CookieProtocol.PER_USER.equals(requiresInitCookie);
+ }
+
+ public boolean requiresInitCookie()
+ {
+ return requiresInitCookie != null &&
!CookieProtocol.NONE.equals(requiresInitCookie);
+ }
+
+ public void setRequiresInitCookie(CookieProtocol requiresInitCookie)
+ {
+ this.requiresInitCookie = requiresInitCookie;
+ }
+
+ void initProducerSessionInformation(PortletInvocation invocation) throws
PortletInvokerException
+ {
+ // if we need cookies, set the current group id
+ String groupId;
+ if (requiresGroupInitCookie())
+ {
+ WSRPPortletInfo info = consumer.getPortletInfo(invocation);
+ groupId = info.getGroupId();
+ }
+ else
+ {
+ groupId = null;
+ }
+
+ RequestHeaderClientHandler.setCurrentInfo(groupId,
getProducerSessionInformation(invocation, true));
+ }
+
+ private boolean requiresGroupInitCookie()
+ {
+ return requiresInitCookie() &&
CookieProtocol.PER_GROUP.equals(requiresInitCookie);
+ }
+
+ /** Resets the information held by RequestHeaderClientHandler for the current
interaction. */
+ public void resetCurrentlyHeldInformation()
+ {
+ RequestHeaderClientHandler.resetCurrentInfo();
+ }
+
+ public void initCookieIfNeeded(PortletInvocation invocation) throws
PortletInvokerException
+ {
+ initCookieIfNeeded(invocation, true);
+ }
+
+ private void initCookieIfNeeded(PortletInvocation invocation, boolean retryIfFails)
throws PortletInvokerException
+ {
+ // check if the cookie protocol requires cookie initialization
+ if (!requiresInitCookie())
+ {
+ return;
+ }
+
+ // check if we have already initialized cookies for this user
+ ProducerSessionInformation sessionInformation =
getProducerSessionInformation(invocation);
+ if (sessionInformation.isInitCookieDone())
+ {
+ return;
+ }
+ RequestHeaderClientHandler.setCurrentInfo(null, sessionInformation);
+
+ if (isPerUserCookieInit())
+ {
+ log.debug("Cookie initialization per user requested.");
+ sessionInformation.setPerGroupCookies(false);
+ initCookie(invocation, retryIfFails);
+ }
+ else
+ {
+ log.debug("Cookie initialization per group requested.");
+ sessionInformation.setPerGroupCookies(true);
+ Map<String, Set<Portlet>> groups = consumer.getPortletGroupMap();
+
+ try
+ {
+ for (String groupId : groups.keySet())
+ {
+ RequestHeaderClientHandler.setCurrentGroupId(groupId);
+ log.debug("Initializing cookie for group '" + groupId +
"'.");
+ initCookie(invocation, retryIfFails);
+ }
+ }
+ finally
+ {
+ resetCurrentlyHeldInformation();
+ }
+ }
+
+ sessionInformation.setInitCookieDone(true);
+ }
+
+ private void initCookie(PortletInvocation invocation, boolean retryIfFails) throws
PortletInvokerException
+ {
+ try
+ {
+ consumer.getMarkupService().initCookie(consumer.getRegistrationContext(),
UserAccess.getUserContext());
+ }
+ catch (InvalidRegistration invalidRegistration)
+ {
+ consumer.handleInvalidRegistrationFault();
+ if (retryIfFails)
+ {
+ initCookieIfNeeded(invocation, false);
+ }
+ }
+ catch (Exception e)
+ {
+ throw new InvokerUnavailableException("Couldn't init cookies!",
e);
+ }
+ }
+
+ void setSessionIdIfNeeded(PortletInvocation invocation, RuntimeContext runtimeContext,
String portletHandle)
+ {
+ ProducerSessionInformation producerSessionInfo =
getProducerSessionInformation(invocation, false);
+ if (producerSessionInfo != null)
+ {
+ String sessionId = producerSessionInfo.getSessionIdForPortlet(portletHandle);
+ SessionParams sessionParams = runtimeContext.getSessionParams();
+ if (sessionParams == null)
+ {
+ sessionParams = new SessionParams();
+ runtimeContext.setSessionParams(sessionParams);
+ }
+ sessionParams.setSessionID(sessionId);
+ }
+ }
+
+ public void updateSessionIfNeeded(SessionContext sessionContext, PortletInvocation
invocation, String portletHandle)
+ {
+ if (sessionContext != null)
+ {
+ log.debug("Portlet '" + portletHandle + "' created
session with id '" + sessionContext.getSessionID() + "'");
+ ProducerSessionInformation sessionInfo =
getProducerSessionInformation(invocation);
+ sessionInfo.addSessionForPortlet(portletHandle, sessionContext);
+ }
+ }
+
+ void updateCookiesIfNeeded(PortletInvocation invocation)
+ {
+ ProducerSessionInformation sessionInfo = getProducerSessionInformation(invocation,
true);
+ ProducerSessionInformation currentSessionInfo =
RequestHeaderClientHandler.getCurrentProducerSessionInformation();
+ if (sessionInfo != currentSessionInfo)
+ {
+ sessionInfo.replaceUserCookiesWith(currentSessionInfo);
+ }
+ }
+
+ public ProducerSessionInformation getProducerSessionInformation(PortletInvocation
invocation)
+ {
+ return getProducerSessionInformation(invocation, true);
+ }
+
+ private ProducerSessionInformation getProducerSessionInformation(PortletInvocation
invocation, boolean create)
+ {
+ HttpSession session = WSRPConsumerImpl.getHttpSession(invocation);
+ return getProducerSessionInformation(session, create);
+ }
+
+ public ProducerSessionInformation getProducerSessionInformation(HttpSession session)
+ {
+ return getProducerSessionInformation(session, false);
+ }
+
+ private ProducerSessionInformation getProducerSessionInformation(HttpSession session,
boolean create)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(session, "Session");
+ String producerSessionKey = getProducerSessionInformationKey();
+ ProducerSessionInformation sessionInformation =
(ProducerSessionInformation)session.getAttribute(producerSessionKey);
+
+ if (sessionInformation != null)
+ {
+ sessionInformation.setParent(this);
+ sessionInformation.setParentSessionId(session.getId());
+ }
+ else
+ {
+ if (create)
+ {
+ sessionInformation = new ProducerSessionInformation();
+ sessionInformation.setParentSessionId(session.getId());
+ session.setAttribute(producerSessionKey, sessionInformation);
+ sessionInformation.setParent(this);
+ }
+ }
+
+ return sessionInformation;
+ }
+
+ private String getProducerSessionInformationKey()
+ {
+ return SESSION_ID_PREFIX + consumer.getProducerId();
+ }
+
+ void handleInvalidSessionFault(PortletInvocation invocation, RuntimeContext
runtimeContext)
+ {
+ log.info("Resending information after InvalidSessionFault");
+ // invalidate current session
+ invalidateSession(invocation);
+
+ // set the session id to null
+ if (runtimeContext != null)
+ {
+ runtimeContext.setSessionParams(null);
+ }
+ }
+
+ private void invalidateSession(PortletInvocation invocation)
+ {
+ HttpSession session = WSRPConsumerImpl.getHttpSession(invocation);
+
+ // remove the associated info from the known producer session informations
+
+ ProducerSessionInformation info = getProducerSessionInformation(session, false);
+ if (info != null)
+ {
+ try
+ {
+ internalReleaseSessions(info.removeSessions());
+ }
+ catch (PortletInvokerException e)
+ {
+ // ignore since it's logged by internalReleaseSessions already
+ }
+ }
+
+
+ session.removeAttribute(getProducerSessionInformationKey());
+ RequestHeaderClientHandler.resetCurrentInfo();
+ }
+
+ /** @since 2.6 */
+ public void releaseSessions() throws PortletInvokerException
+ {
+ List<String> idsToRelease = new ArrayList<String>();
+
+ Set<ProducerSessionInformation> uniqueInfos = new
HashSet<ProducerSessionInformation>(sessionInfos.values());
+
+ for (ProducerSessionInformation info : uniqueInfos)
+ {
+ idsToRelease.addAll(info.removeSessions());
+ }
+
+ internalReleaseSessions(idsToRelease);
+ }
+
+ /**
+ * @param sessionIds
+ * @throws PortletInvokerException
+ * @since 2.6
+ */
+ void releaseSessions(String[] sessionIds) throws PortletInvokerException
+ {
+ if (sessionIds != null)
+ {
+ List<String> idsToRelease = new ArrayList<String>();
+
+ for (String sessionId : sessionIds)
+ {
+ ProducerSessionInformation info = sessionInfos.get(sessionId);
+ sessionId = info.removeSession(sessionId);
+ if (sessionId != null)
+ {
+ idsToRelease.add(sessionId);
+ }
+ }
+
+ internalReleaseSessions(idsToRelease);
+ }
+ }
+
+ private void internalReleaseSessions(List<String> idsToRelease) throws
PortletInvokerException
+ {
+ if (idsToRelease != null && !idsToRelease.isEmpty())
+ {
+ try
+ {
+
consumer.getMarkupService().releaseSessions(consumer.getRegistrationContext(),
idsToRelease, UserAccess.getUserContext());
+ }
+ catch (InvalidRegistration invalidRegistration)
+ {
+ log.debug("Invalid Registration");
+ consumer.handleInvalidRegistrationFault();
+ }
+ catch (Exception e)
+ {
+ String message = "Couldn't release sessions " + idsToRelease;
+ log.debug(message, e);
+ throw new PortletInvokerException(message, e);
+ }
+ }
+ }
+
+ // ProducerSessionInformation callbacks
+
+ /**
+ * Update session mappings when a session has expired
+ *
+ * @param id
+ * @since 2.6
+ */
+ void removeSessionId(String id)
+ {
+ sessionInfos.remove(id);
+ }
+
+ /**
+ * Update session mappings when a new session id is added to the specified
ProducerSessionInformation
+ *
+ * @param sessionID
+ * @param producerSessionInformation
+ * @since 2.6
+ */
+ void addSessionMapping(String sessionID, ProducerSessionInformation
producerSessionInformation)
+ {
+ sessionInfos.put(sessionID, producerSessionInformation);
+ }
+
+ // End ProducerSessionInformation callbacks
+
+ // SessionEventListener implementation
+
+ public void onSessionEvent(SessionEvent event)
+ {
+ if (SessionEvent.SessionEventType.SESSION_DESTROYED.equals(event.getType()))
+ {
+ String id = event.getSession().getId();
+
+ // check if the session being destroyed is the one associated with this thread
+ ProducerSessionInformation info =
RequestHeaderClientHandler.getCurrentProducerSessionInformation();
+ if (info != null)
+ {
+ if (id != null && id.equals(info.getParentSessionId()))
+ {
+ try
+ {
+ internalReleaseSessions(info.removeSessions());
+ }
+ catch (PortletInvokerException e)
+ {
+ // already logged...
+ }
+ log.debug("Released session '" + id + "' after
session was destroyed by Portal.");
+ }
+ }
+ }
+ }
+
+ /**
+ * @param originalHandle
+ * @param newHandle
+ * @param invocation
+ * @since 2.6
+ */
+ public void updateSessionInfoFor(String originalHandle, String newHandle,
PortletInvocation invocation)
+ {
+ ProducerSessionInformation info = getProducerSessionInformation(invocation,
false);
+ if (info != null)
+ {
+ info.updateHandleAssociatedInfo(originalHandle, newHandle);
+ }
+ }
+}
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/handler/RequestHeaderClientHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/handler/RequestHeaderClientHandler.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/handler/RequestHeaderClientHandler.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -24,7 +24,7 @@
package org.gatein.wsrp.handler;
import org.apache.commons.httpclient.Cookie;
-import org.gatein.wsrp.consumer.ProducerSessionInformation;
+import org.gatein.wsrp.consumer.handlers.ProducerSessionInformation;
import javax.xml.namespace.QName;
import javax.xml.soap.MimeHeaders;
Modified:
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/ProducerSessionInformationTestCase.java
===================================================================
---
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/ProducerSessionInformationTestCase.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/ProducerSessionInformationTestCase.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -27,6 +27,7 @@
import org.apache.commons.httpclient.Cookie;
import org.gatein.wsrp.WSRPConstants;
import org.gatein.wsrp.WSRPTypeFactory;
+import org.gatein.wsrp.consumer.handlers.ProducerSessionInformation;
/**
* @author <a
href="mailto:chris.laprun@jboss.com?subject=org.gatein.wsrp.consumer.ProducerSessionInformationTestCase">Chris
Modified:
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/handler/RequestHeaderClientHandlerTestCase.java
===================================================================
---
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/handler/RequestHeaderClientHandlerTestCase.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/handler/RequestHeaderClientHandlerTestCase.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -1,31 +1,31 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, 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. *
- ******************************************************************************/
+/*
+ * 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.handler;
import junit.framework.TestCase;
import org.apache.commons.httpclient.Cookie;
-import org.gatein.wsrp.consumer.ProducerSessionInformation;
+import org.gatein.wsrp.consumer.handlers.ProducerSessionInformation;
import org.gatein.wsrp.test.handler.MockSOAPMessage;
import org.gatein.wsrp.test.handler.MockSOAPMessageContext;
Modified:
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v1/MarkupTestCase.java
===================================================================
---
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v1/MarkupTestCase.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/protocol/v1/MarkupTestCase.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -40,7 +40,7 @@
import org.gatein.pc.portlet.impl.spi.AbstractUserContext;
import org.gatein.pc.portlet.impl.spi.AbstractWindowContext;
import org.gatein.wsrp.WSRPResourceURL;
-import org.gatein.wsrp.consumer.ProducerSessionInformation;
+import org.gatein.wsrp.consumer.handlers.ProducerSessionInformation;
import org.gatein.wsrp.test.ExtendedAssert;
import org.gatein.wsrp.test.protocol.v1.BehaviorRegistry;
import org.gatein.wsrp.test.protocol.v1.behaviors.BasicMarkupBehavior;
@@ -67,7 +67,6 @@
import javax.servlet.http.HttpSession;
import javax.xml.ws.Holder;
-
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
@@ -182,7 +181,7 @@
ExtendedAssert.assertFalse(sessionInfo.isPerGroupCookies());
ExtendedAssert.assertTrue(sessionInfo.isInitCookieDone());
-
+
ExtendedAssert.assertNotNull(sessionInfo.getUserCookie());
ExtendedAssert.assertEquals(1, behavior.getInitCookieCallCount());
@@ -219,7 +218,7 @@
String resourceID = WSRPResourceURL.encodeResource(null, new
URL("http://localhost:8080/test-resource-portlet/gif/logo.gif"), false);
String expectedResult = "<img src='Resource id=" + resourceID +
" ns=null ws=null m=null'/>";
-
+
//NOTE: the value we get back is from the TestPortletInvocationContext, not what we
would normally receive
checkRenderResult(response, expectedResult);
}
Modified:
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/test/support/MockWSRPConsumer.java
===================================================================
---
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/test/support/MockWSRPConsumer.java 2010-08-16
10:06:08 UTC (rev 3834)
+++
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/test/support/MockWSRPConsumer.java 2010-08-16
17:09:43 UTC (rev 3835)
@@ -30,14 +30,13 @@
import org.gatein.pc.api.PortletStateType;
import org.gatein.pc.api.invocation.PortletInvocation;
import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
-import org.gatein.pc.api.state.DestroyCloneFailure;
import org.gatein.pc.api.state.PropertyChange;
import org.gatein.pc.api.state.PropertyMap;
import org.gatein.wsrp.WSRPConsumer;
import org.gatein.wsrp.api.SessionEvent;
import org.gatein.wsrp.consumer.ProducerInfo;
-import org.gatein.wsrp.consumer.ProducerSessionInformation;
import org.gatein.wsrp.consumer.RefreshResult;
+import org.gatein.wsrp.consumer.handlers.ProducerSessionInformation;
import javax.servlet.http.HttpSession;
import java.util.List;
@@ -171,13 +170,13 @@
}
public PortletContext exportPortlet(PortletStateType stateType, PortletContext
originalPortletContext)
- throws PortletInvokerException
+ throws PortletInvokerException
{
throw new NotYetImplemented();
}
-
+
public PortletContext importPortlet(PortletStateType stateType, PortletContext
originalPortletContext)
- throws PortletInvokerException
+ throws PortletInvokerException
{
throw new NotYetImplemented();
}