Package net.sf.jportlet.web.servlet

Source Code of net.sf.jportlet.web.servlet.PortletServlet

/*
* Created on Mar 10, 2003
*/
package net.sf.jportlet.web.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.jportlet.impl.ActionEventImpl;
import net.sf.jportlet.impl.PortletApplicationImpl;
import net.sf.jportlet.impl.PortletRequestImpl;
import net.sf.jportlet.impl.PortletResponseImpl;
import net.sf.jportlet.impl.PortletURIImpl;
import net.sf.jportlet.portlet.PortletURI;
import net.sf.jportlet.portlet.application.PortletProxy;
import net.sf.jportlet.portlet.descriptor.WebflowActionDescriptor;
import net.sf.jportlet.service.PortletServiceFactory;
import net.sf.jportlet.service.page.PortletPageService;
import net.sf.jportlet.util.Constants;


/**
* Portlet Servlet
*
* @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
*/
public class PortletServlet
    extends HttpServlet
{
    //~ Static fields/initializers ---------------------------------------------

    private static final Log __log = LogFactory.getLog( PortletServlet.class );

    //~ Instance fields --------------------------------------------------------

    private PortletApplicationImpl _application = new PortletApplicationImpl(  );

    //~ Methods ----------------------------------------------------------------

    /**
     * @see javax.servlet.Servlet#destroy()
     */
    public void destroy(  )
    {
        if ( __log.isInfoEnabled(  ) )
        {
            __log.info( "Destroying" );
        }

        _application.destroy(  );
        super.destroy(  );
    }

    protected void doAction( PortletURIImpl      uri,
                             HttpServletRequest  request,
                             HttpServletResponse response )
        throws ServletException,
                   IOException
    {
        boolean debug = __log.isDebugEnabled(  );

        if ( debug )
        {
            __log.debug( "doAction(" + uri + ")" );
        }

        /* Perform the action */
        PortletServiceFactory serviceFactory = _application.getPortletServiceFactory(  );
        PortletProxy          proxy = ( PortletProxy ) _application.getPortlet( uri.getPortletName(  ) );
        PortletRequestImpl    req = new PortletRequestImpl( proxy, request, serviceFactory );
        PortletResponseImpl   resp = new PortletResponseImpl( proxy, req, response );
        HttpServletRequest    hreq = req.getHttpRequest(  );
        String                action = uri.getAction(  );
        ActionEventImpl       event = new ActionEventImpl( action, req, resp );
        proxy.actionPerformed( event );

        /* Get the URI where to move*/
        PortletURI nextURI = event.getNextURI(  );
        String     nextUri;
        if ( nextURI != null )
        {
            nextUri = nextURI.toString(  ).substring( request.getContextPath(  ).length(  ) );
        }
        else
        {
            WebflowActionDescriptor webflow = proxy.getDescriptor(  ).getWebflowAction( action );
            nextUri = ( webflow != null )
                      ? webflow.getReturnURI( event.getReturnCode(  ) )
                      : null;
        }

        if ( debug )
        {
            __log.debug( "nextUri=" + nextUri );
        }

        /* Move to the next URI */
        if ( nextUri == null )
        {
            response.sendError( HttpServletResponse.SC_NOT_FOUND );
        }
        else
        {
            /* @return */
            if ( WebflowActionDescriptor.RETURN.equals( nextUri ) )
            {
                nextUri = request.getParameter( Constants.RETURN_URI_KEY );

                if ( debug )
                {
                    __log.debug( WebflowActionDescriptor.RETURN + "=" + nextUri );
                }

                if ( nextUri != null )
                {
                    String contextPath = hreq.getContextPath(  );

                    if ( nextUri.startsWith( contextPath ) )
                    {
                        nextUri = nextUri.substring( contextPath.length(  ) );
                    }
                }
            }

            if ( nextUri == null )
            {
                response.sendError( HttpServletResponse.SC_NOT_FOUND );
            }
            else
            {
                /* Forward the current PortletRequest */
                request.setAttribute( PortletRequestImpl.PORTLET_REQUEST_KEY, req );

                /* Invalidate the current URI */
                hreq.removeAttribute( PortletRequestImpl.PORTLET_URI_KEY );

                /* Move to the next URI */
                if ( debug )
                {
                    __log.debug( "currentRequest=" + request.getAttribute( PortletRequestImpl.PORTLET_REQUEST_KEY ) );
                }

                request.getRequestDispatcher( nextUri ).forward( request, response );
            }
        }
    }

    protected void doRender( PortletURIImpl      uri,
                             HttpServletRequest  request,
                             HttpServletResponse response )
        throws ServletException,
                   IOException
    {
        if ( __log.isDebugEnabled(  ) )
        {
            __log.debug( "doRender(" + uri + ")" );
        }

        PortletServiceFactory serviceFactory = _application.getPortletServiceFactory(  );
        PortletPageService    srv = ( PortletPageService ) serviceFactory.getPortletService( PortletPageService.NAME );
        String                path = srv.getPage( uri.getMode(  ) );

        if ( __log.isDebugEnabled(  ) )
        {
            __log.debug( "Path=" + path );
        }

        request.getRequestDispatcher( path ).forward( request, response );
    }

    /**
     * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
     */
    public void init( ServletConfig config )
        throws ServletException
    {
        if ( __log.isInfoEnabled(  ) )
        {
            __log.info( "Initializing" );
        }

        super.init( config );
        _application.init( config );

        config.getServletContext(  ).setAttribute( Constants.APPLICATION_KEY, _application );
    }

    /**
     * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void service( HttpServletRequest  request,
                            HttpServletResponse response )
        throws ServletException,
                   IOException
    {
        if ( __log.isDebugEnabled(  ) )
        {
            __log.debug( "service()" );
        }

        /* Current URI */
        PortletURIImpl uri = PortletURIImpl.parse( request.getRequestURI(  ) );
        request.setAttribute( PortletRequestImpl.PORTLET_URI_KEY, uri );

        /* Handle the request */
        if ( uri.getAction(  ) == null )
        {
            doRender( uri, request, response );
        }
        else
        {
            doAction( uri, request, response );
        }
    }
}
TOP

Related Classes of net.sf.jportlet.web.servlet.PortletServlet

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.