1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.portlet;
20
21 import java.io.IOException;
22
23 import javax.faces.FacesException;
24 import javax.faces.application.Application;
25 import javax.faces.application.ViewHandler;
26 import javax.faces.application.ViewHandlerWrapper;
27 import javax.faces.component.UIViewRoot;
28 import javax.faces.context.FacesContext;
29 import javax.portlet.PortletRequest;
30 import javax.portlet.PortletURL;
31 import javax.portlet.RenderResponse;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
37
38
39
40
41
42 public class PortletViewHandler extends ViewHandlerWrapper
43 {
44 private final ViewHandler _viewHandler;
45
46 private static final Log log = LogFactory.getLog(PortletViewHandler.class);
47
48 public PortletViewHandler(ViewHandler viewHandler)
49 {
50 _viewHandler = viewHandler;
51 }
52
53 @Override
54 protected ViewHandler getWrapped()
55 {
56 return _viewHandler;
57 }
58
59 @Override
60 public UIViewRoot restoreView(FacesContext context, String viewId)
61 {
62 if (PortletUtil.isPortletRequest(context))
63 {
64 PortletRequest request = (PortletRequest) context.getExternalContext().getRequest();
65 String portletViewId = request.getParameter(MyFacesGenericPortlet.VIEW_ID);
66 Application application = context.getApplication();
67 ViewHandler applicationViewHandler = application.getViewHandler();
68 String renderKitId = applicationViewHandler.calculateRenderKitId(context);
69 UIViewRoot viewRoot = application.getStateManager().restoreView(context, portletViewId, renderKitId);
70 return viewRoot;
71 }
72 return super.restoreView(context, viewId);
73 }
74
75 @Override
76 public UIViewRoot createView(FacesContext context, String viewId)
77 {
78 UIViewRoot viewRoot = super.createView(context, viewId);
79 if (PortletUtil.isPortletRequest(context))
80 {
81 PortletRequest request = (PortletRequest) context.getExternalContext().getRequest();
82 viewRoot.setViewId(request.getParameter(MyFacesGenericPortlet.VIEW_ID));
83 }
84 return viewRoot;
85 }
86
87 @Override
88 public String getActionURL(FacesContext context, String viewId)
89 {
90 if (PortletUtil.isRenderResponse(context))
91 {
92 RenderResponse response = (RenderResponse) context.getExternalContext().getResponse();
93 PortletURL url = response.createActionURL();
94 url.setParameter(MyFacesGenericPortlet.VIEW_ID, viewId);
95 return url.toString();
96 }
97 return super.getActionURL(context, viewId);
98 }
99
100 @Override
101 public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException
102 {
103 if (PortletUtil.isPortletRequest(context))
104 {
105 if (viewToRender.isRendered())
106 {
107 if (log.isTraceEnabled())
108 log.trace("It is a portlet request. Dispatching to view");
109 context.getExternalContext().dispatch(viewToRender.getViewId());
110 }
111 }
112 else
113 {
114 super.renderView(context, viewToRender);
115 }
116 }
117 }