FileDocCategorySizeDatePackage
PortletResult.javaAPI DocExample9248Mon Jul 23 13:26:40 BST 2007org.apache.struts2.portlet.result

PortletResult

public class PortletResult extends org.apache.struts2.dispatcher.StrutsResultSupport
Result type that includes a JSP to render.

Fields Summary
private static final long
serialVersionUID
private static final Log
LOG
Logger instance.
private String
contentType
private String
title
Constructors Summary
public PortletResult()


      
        super();
    
public PortletResult(String location)

        super(location);
    
Methods Summary
protected static voidconvertQueryParamsToRenderParams(javax.portlet.ActionResponse response, java.lang.String queryParams)
Converts the query params to render params.

param
response
param
queryParams

        StringTokenizer tok = new StringTokenizer(queryParams, "&");
        while (tok.hasMoreTokens()) {
            String token = tok.nextToken();
            String key = token.substring(0, token.indexOf('="));
            String value = token.substring(token.indexOf('=") + 1);
            response.setRenderParameter(key, value);
        }
    
public voiddoExecute(java.lang.String finalLocation, com.opensymphony.xwork2.ActionInvocation actionInvocation)
Execute the result. Obtains the {@link javax.portlet.PortletRequestDispatcher}from the {@link PortletActionContext}and includes the JSP.

see
com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)


        if (PortletActionContext.isRender()) {
            executeRenderResult(finalLocation);
        } else if (PortletActionContext.isEvent()) {
            executeActionResult(finalLocation, actionInvocation);
        } else {
            executeRegularServletResult(finalLocation, actionInvocation);
        }
    
protected voidexecuteActionResult(java.lang.String finalLocation, com.opensymphony.xwork2.ActionInvocation invocation)
Executes the action result.

param
finalLocation
param
invocation

        LOG.debug("Executing result in Event phase");
        ActionResponse res = PortletActionContext.getActionResponse();
        LOG.debug("Setting event render parameter: " + finalLocation);
        if (finalLocation.indexOf('?") != -1) {
            convertQueryParamsToRenderParams(res, finalLocation
                    .substring(finalLocation.indexOf('?") + 1));
            finalLocation = finalLocation.substring(0, finalLocation
                    .indexOf('?"));
        }
        if (finalLocation.endsWith(".action")) {
            // View is rendered with a view action...luckily...
            finalLocation = finalLocation.substring(0, finalLocation
                    .lastIndexOf("."));
            res.setRenderParameter(PortletActionConstants.ACTION_PARAM, finalLocation);
        } else {
            // View is rendered outside an action...uh oh...
            res.setRenderParameter(PortletActionConstants.ACTION_PARAM, "renderDirect");
            res.setRenderParameter("location", finalLocation);
        }
        res.setRenderParameter(PortletActionConstants.MODE_PARAM, PortletActionContext
                .getRequest().getPortletMode().toString());
    
private voidexecuteRegularServletResult(java.lang.String finalLocation, com.opensymphony.xwork2.ActionInvocation actionInvocation)
Executes the regular servlet result.

param
finalLocation
param
actionInvocation

        ServletContext ctx = ServletActionContext.getServletContext();
        HttpServletRequest req = ServletActionContext.getRequest();
        HttpServletResponse res = ServletActionContext.getResponse();
        try {
            ctx.getRequestDispatcher(finalLocation).include(req, res);
        } catch (ServletException e) {
            LOG.error("ServletException including " + finalLocation, e);
            throw e;
        } catch (IOException e) {
            LOG.error("IOException while including result '" + finalLocation + "'", e);
            throw e;
        }
    
protected voidexecuteRenderResult(java.lang.String finalLocation)
Executes the render result.

param
finalLocation
throws
PortletException
throws
IOException

        LOG.debug("Executing result in Render phase");
        PortletConfig cfg = PortletActionContext.getPortletConfig();
        RenderRequest req = PortletActionContext.getRenderRequest();
        RenderResponse res = PortletActionContext.getRenderResponse();
        LOG.debug("PortletConfig: " + cfg);
        LOG.debug("RenderRequest: " + req);
        LOG.debug("RenderResponse: " + res);
        res.setContentType(contentType);
        if (TextUtils.stringSet(title)) {
            res.setTitle(title);
        }
        LOG.debug("Location: " + finalLocation);
        PortletRequestDispatcher preparator = cfg.getPortletContext()
                .getNamedDispatcher("preparator");
        if(preparator == null) {
            throw new PortletException("Cannot look up 'preparator' servlet. Make sure that you" +
                    "have configured it correctly in the web.xml file.");
        }
        new IncludeTemplate() {
            protected void when(PortletException e) {
                LOG.error("PortletException while dispatching to 'preparator' servlet", e);
            }
            protected void when(IOException e) {
                LOG.error("IOException while dispatching to 'preparator' servlet", e);
            }
        }.include(preparator, req, res);
        PortletRequestDispatcher dispatcher = cfg.getPortletContext().getRequestDispatcher(finalLocation);
        if(dispatcher == null) {
            throw new PortletException("Could not locate dispatcher for '" + finalLocation + "'");
        }
        new IncludeTemplate() {
            protected void when(PortletException e) {
                LOG.error("PortletException while dispatching to '" + finalLocation + "'");
            }
            protected void when(IOException e) {
                LOG.error("IOException while dispatching to '" + finalLocation + "'");
            }
        }.include(dispatcher, req, res);
    
public voidsetContentType(java.lang.String contentType)
Sets the content type.

param
contentType The content type to set.

        this.contentType = contentType;
    
public voidsetTitle(java.lang.String title)
Sets the title.

param
title The title to set.

        this.title = title;