Search in sources :

Example 1 with PortletServletRequest

use of org.apache.struts2.portlet.servlet.PortletServletRequest in project struts by apache.

the class Jsr168Dispatcher method serviceAction.

/**
 * Loads the action and executes it. This method first creates the action
 * context from the given parameters then loads an <tt>ActionProxy</tt>
 * from the given action name and namespace. After that, the action is
 * executed and output channels throught the response object.
 *
 * @param request          the HttpServletRequest object.
 * @param response         the HttpServletResponse object.
 * @param requestMap       a Map of request attributes.
 * @param parameterMap     a Map of request parameters.
 * @param sessionMap       a Map of all session attributes.
 * @param applicationMap   a Map of all application attributes.
 * @param portletNamespace the namespace or context of the action.
 * @param phase            The portlet phase (render or action, see {@link PortletConstants})
 * @throws PortletException in case of errors
 */
public void serviceAction(PortletRequest request, PortletResponse response, Map<String, Object> requestMap, Map<String, String[]> parameterMap, Map<String, Object> sessionMap, Map<String, Object> applicationMap, String portletNamespace, PortletPhase phase) throws PortletException {
    if (LOG.isDebugEnabled())
        LOG.debug("serviceAction");
    Dispatcher.setInstance(dispatcherUtils);
    String actionName = null;
    String namespace;
    try {
        HttpServletRequest servletRequest = new PortletServletRequest(request, getPortletContext());
        HttpServletResponse servletResponse = createPortletServletResponse(response);
        if (phase.isAction()) {
            servletRequest = dispatcherUtils.wrapRequest(servletRequest);
            if (servletRequest instanceof MultiPartRequestWrapper) {
                // Multipart request. Request parameters are encoded in the multipart data,
                // so we need to manually add them to the parameter map.
                parameterMap.putAll(servletRequest.getParameterMap());
            }
        }
        container.inject(servletRequest);
        ActionMapping mapping = getActionMapping(request, servletRequest);
        actionName = mapping.getName();
        if ("renderDirect".equals(actionName)) {
            namespace = request.getParameter(PortletConstants.RENDER_DIRECT_NAMESPACE);
        } else {
            namespace = mapping.getNamespace();
        }
        Map<String, Object> extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, servletRequest, servletResponse, servletContext, getPortletConfig(), phase);
        extraContext.put(PortletConstants.ACTION_MAPPING, mapping);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Creating action proxy for name = " + actionName + ", namespace = " + namespace);
        }
        ActionProxy proxy = factory.createActionProxy(namespace, actionName, mapping.getMethod(), extraContext);
        request.setAttribute("struts.valueStack", proxy.getInvocation().getStack());
        proxy.execute();
    } catch (ConfigurationException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Could not find action", e);
        }
        throw new PortletException("Could not find action " + actionName, e);
    } catch (Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Could not execute action", e);
        }
        throw new PortletException("Error executing action " + actionName, e);
    } finally {
        Dispatcher.setInstance(null);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) ActionProxy(com.opensymphony.xwork2.ActionProxy) PortletServletRequest(org.apache.struts2.portlet.servlet.PortletServletRequest) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) PortletException(javax.portlet.PortletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) MultiPartRequestWrapper(org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException) PortletException(javax.portlet.PortletException)

Aggregations

ActionProxy (com.opensymphony.xwork2.ActionProxy)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 IOException (java.io.IOException)1 PortletException (javax.portlet.PortletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 StrutsException (org.apache.struts2.StrutsException)1 ActionMapping (org.apache.struts2.dispatcher.mapper.ActionMapping)1 MultiPartRequestWrapper (org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper)1 PortletServletRequest (org.apache.struts2.portlet.servlet.PortletServletRequest)1