Portlet:转发ResourceRequest以显示完整的门户 [英] Portlet: Forward a ResourceRequest to show the full portal

查看:100
本文介绍了Portlet:转发ResourceRequest以显示完整的门户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是处理为资源提供服务的ResourceRequest(以动态方式生成的PDF).如果在生成该文件时出现问题,则应该呈现整个门户网站,并在portlet中显示失败消息.

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

我希望我已经清楚地解释了我的问题.谢谢你.

相关/重复的: 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); } }

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

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

解决方案

以下是尝试使用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

my intention is to process a ResourceRequest that serves a resource (A dinamically generated PDF). If something goes wrong generating this file, the whole portal with a failure message in the portlet should be rendered.

Is it possible to forward a ResourceRequest to a request that renders the complete portal? I am also considering a redirect, but I will like to be able to pass some attributes/parameters along.

I hope I explained my problem clear enough. Thank you.

Related/Duplicate: How to make the ResourceResponse to forward the request to error page in liferay portlet

Example

This is an example that works and does something similar to what I want to achieve. I uses the utility class SessionErrors of Liferay: The serveResource() saves an object in the session and does a redirection to a render URL. The doView() method is called during the subsequent request and can read the contents saved in the session.

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);
    }
}

What I want to achieve is the same, but preventing the browser from doing two requests. For that, I would like to do a forward and I should write the error message as a request attribute.

I uploaded a project with this example to GitHub: https://github.com/adrianrm/poc-forward-portlet/tree/master/src/main/java/arm/requestforward

解决方案

The following is an attempt to do it using a servlet forward, and it seems to work. Still, I think it is a workaround for a feature not present in the portlet specification:

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);
        }
    }
}

On top of that, when the forward occurs, Liferay shows a warning on the console:

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

这篇关于Portlet:转发ResourceRequest以显示完整的门户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆