Package org.jboss.portal.portlet.aspects.portlet

Source Code of org.jboss.portal.portlet.aspects.portlet.ContextDispatcherInterceptor

/******************************************************************************
* 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.                   *
******************************************************************************/
package org.jboss.portal.portlet.aspects.portlet;

import org.jboss.portal.portlet.container.PortletApplication;
import org.jboss.portal.portlet.container.PortletContainer;
import org.jboss.portal.portlet.container.ContainerPortletInvoker;
import org.jboss.portal.web.RequestDispatchCallback;
import org.jboss.portal.web.ServletContainer;
import org.jboss.portal.web.ServletContainerFactory;
import org.jboss.portal.portlet.invocation.PortletInvocation;
import org.jboss.portal.portlet.PortletInvokerInterceptor;
import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
import org.jboss.portal.portlet.spi.ServerContext;
import org.jboss.portal.portlet.PortletInvokerException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.IOException;

/**
* This interceptor dispatch the call to the target web application.
*
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7226 $
*/
public class ContextDispatcherInterceptor extends PortletInvokerInterceptor
{

   /** . */
   public static final String REQ_ATT_COMPONENT_INVOCATION = "org.jboss.portal.attribute.component_invocation";

   /** . */
   private ServletContainerFactory servletContainerFactory;

   public ServletContainerFactory getServletContainerFactory()
   {
      return servletContainerFactory;
   }

   public void setServletContainerFactory(ServletContainerFactory servletContainerFactory)
   {
      this.servletContainerFactory = servletContainerFactory;
   }

   public PortletInvocationResponse invoke(PortletInvocation invocation) throws IllegalArgumentException, PortletInvokerException
   {
      PortletContainer container = (PortletContainer)invocation.getAttribute(ContainerPortletInvoker.PORTLET_CONTAINER);
      PortletApplication portletApplication = container.getPortletApplication();
      ServerContext reqCtx = invocation.getServerContext();
      ServletContext targetCtx = portletApplication.getContext().getServletContext();
      ServletContainer servletContainer = servletContainerFactory.getServletContainer();
      try
      {
         return (PortletInvocationResponse)reqCtx.dispatch(servletContainer, targetCtx, callback, invocation);
      }
      catch (Exception e)
      {
         if (e instanceof PortletInvokerException)
         {
            throw (PortletInvokerException)e;
         }
         else if (e instanceof RuntimeException)
         {
            throw (RuntimeException)e;
         }
         else if (e instanceof ServletException)
         {
            ServletException se = (ServletException)e;

            //
            if (se.getRootCause() != null && se.getRootCause() instanceof Exception)
            {
               e = (Exception)se.getRootCause();
            }
         }

         //
         throw new PortletInvokerException(e);
      }
   }

   private final RequestDispatchCallback callback = new RequestDispatchCallback()
   {
      public Object doCallback(ServletContext dispatchedServletContext, HttpServletRequest req, HttpServletResponse resp, Object handback) throws ServletException, IOException
      {
         PortletInvocation invocation = (PortletInvocation)handback;

         //
         try
         {

            // Set dispatched request and response and invocation
            invocation.setDispatchedRequest(req);
            invocation.setDispatchedResponse(resp);

            //
            req.setAttribute(REQ_ATT_COMPONENT_INVOCATION, invocation);

            //
            return ContextDispatcherInterceptor.super.invoke(invocation);
         }
         catch (Exception e)
         {
            throw new ServletException(e);
         }
         finally
         {
            // Clear dispatched request and response
            req.setAttribute(REQ_ATT_COMPONENT_INVOCATION, null);

            //
            invocation.setDispatchedRequest(null);
            invocation.setDispatchedResponse(null);
         }
      }
   };
}
TOP

Related Classes of org.jboss.portal.portlet.aspects.portlet.ContextDispatcherInterceptor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.