001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.portlet.aspects.portlet;
023:
024: import org.apache.log4j.Logger;
025: import org.jboss.portal.common.invocation.InvocationException;
026: import org.jboss.portal.portlet.invocation.PortletInterceptor;
027: import org.jboss.portal.portlet.invocation.PortletInvocation;
028: import org.jboss.portal.portlet.session.SubSession;
029: import org.jboss.portal.portlet.spi.PortletInvocationContext;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpSession;
033: import java.lang.reflect.Method;
034: import java.util.List;
035:
036: /**
037: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
038: * @version $Revision: 8784 $
039: */
040: public class PortalSessionSynchronizationInterceptor extends
041: PortletInterceptor {
042:
043: /** . */
044: private final Logger log = Logger
045: .getLogger(PortalSessionSynchronizationInterceptor.class);
046:
047: protected Object invoke(PortletInvocation invocation)
048: throws Exception, InvocationException {
049: HttpServletRequest req = null;
050:
051: // We try to access the client request whenever this is possible
052: // This should be better implemented and the task of using the portal session
053: // Should be left up to the caller whenever it is possible
054: try {
055: PortletInvocationContext ctx = invocation
056: .getPortletContext();
057: Method getClientRequestMethod = ctx.getClass().getMethod(
058: "getClientRequest", new Class[0]);
059: req = (HttpServletRequest) getClientRequestMethod.invoke(
060: ctx, new Object[0]);
061: } catch (Exception ignore) {
062: if (log.isDebugEnabled()) {
063: log
064: .debug("Was not able to access the client request from request context");
065: }
066: }
067:
068: if (req != null) {
069: String id = invocation.getInstanceContext().getId();
070: String key = "jboss.portlet.session." + id;
071:
072: // Try to get a sub session from the portal session
073: SubSession ss = null;
074: HttpSession session = req.getSession(false);
075: if (session != null) {
076: ss = (SubSession) session.getAttribute(key);
077: }
078:
079: //
080: try {
081: // Set the sub session for the portlet synchronization
082: if (ss != null && ss.isActivated()) {
083: invocation.setAttribute(
084: PortletInvocation.INVOCATION_SCOPE,
085: "subsession", ss);
086: }
087:
088: //
089: return invocation.invokeNext();
090: } finally {
091: List modifications = (List) invocation.getAttribute(
092: PortletInvocation.INVOCATION_SCOPE,
093: "subsession");
094:
095: // If we have any modifications propagate them
096: if (modifications != null) {
097: //
098: invocation.removeAttribute(
099: PortletInvocation.INVOCATION_SCOPE,
100: "subsession");
101:
102: //
103: if (ss == null) {
104: ss = new SubSession(key);
105: }
106:
107: //
108: ss.synchronizeWithPortalSession(req, modifications,
109: key);
110: }
111: }
112: } else {
113: return invocation.invokeNext();
114: }
115: }
116: }
|