Portlet:转发ResourceRequest以显示完整门户

时间:2011-04-30 18:45:20

标签: liferay portlet

我的目的是处理一个为资源提供服务的ResourceRequest(一个动态生成的PDF)。如果生成此文件出错,则应呈现portlet中包含失败消息的整个门户。

是否可以将ResourceRequest转发给呈现完整门户的请求?我也在考虑重定向,但我希望能够传递一些属性/参数。

我希望我能够清楚地解释我的问题。谢谢。

相关/复制:How to make the ResourceResponse to forward the request to error page in liferay portlet

实施例

这是一个有效的例子,它做了类似于我想要实现的事情。我使用Liferay的实用程序类SessionErrorsserveResource()在会话中保存对象并重定向到渲染URL。 doView()方法在后续请求期间被调用,并且可以读取会话中保存的内容。

import com.liferay.portal.kernel.servlet.SessionErrors;
import javax.portlet.GenericPortlet;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;

public class ResourceRequestForwardPortlet extends GenericPortlet {

    public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {

        SessionErrors.add(resourceRequest, "resourcerequest.error", "ERROR " + errorText);

        resourceResponse.setProperty(ResourceResponse.HTTP_STATUS_CODE, "302");
        resourceResponse.addProperty("Location", resourceResponse.createRenderURL().toString());
    }

    public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {

        String error = (String)SessionErrors.get(renderRequest, "resourcerequest.error");
        SessionErrors.clear(renderRequest);

        renderRequest.setAttribute("errorMsg", error);

        PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher("view.jsp");
        include(viewTemplate, renderRequest, renderResponse);
        portletRequestDispatcher.include(renderRequest, renderResponse);
    }
}

我想要实现的是相同的,但阻止浏览器执行两个请求。为此,我想做一个转发,我应该将错误消息写为请求属性。

我使用此示例将项目上传到GitHub: https://github.com/adrianrm/poc-forward-portlet/tree/master/src/main/java/arm/requestforward

1 个答案:

答案 0 :(得分:0)

以下是尝试使用servlet前进,它似乎工作。不过,我认为这是portlet规范中没有的功能的解决方法:

public class ResourceRequestForwardPortlet extends GenericPortlet {

    public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {

        HttpServletRequest request = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(portletRequest));
        PortletURL renderURL = portletResponse.createRenderURL();
        try {
            request.setAttribute("error", errorText);

            String forwardURL = renderURL.toString().replace("http://localhost:8080", "");//The generation of the forwardURL could be done nicer
            RequestDispatcher rd = request.getRequestDispatcher(forwardURL);
            rd.forward(request, PortalUtil.getHttpServletResponse(portletResponse));
        } catch (ServletException e) {
            throw new PortletException(e);
        }
    }
}

最重要的是,当前进发生时,Liferay会在控制台上显示警告:

14:33:51,048 WARN  [http-apr-8080-exec-112][code_jsp:128] {code="404", msg="/poc-forward-portlet/web/guest/home", uri=/web/guest/home}
X-Requested-With null