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 | 0 | private static final Log log = LogFactory.getLog(PortletViewHandler.class); |
47 | |
|
48 | |
public PortletViewHandler(ViewHandler viewHandler) |
49 | 0 | { |
50 | 0 | _viewHandler = viewHandler; |
51 | 0 | } |
52 | |
|
53 | |
@Override |
54 | |
protected ViewHandler getWrapped() |
55 | |
{ |
56 | 0 | return _viewHandler; |
57 | |
} |
58 | |
|
59 | |
@Override |
60 | |
public UIViewRoot restoreView(FacesContext context, String viewId) |
61 | |
{ |
62 | 0 | if (PortletUtil.isPortletRequest(context)) |
63 | |
{ |
64 | 0 | PortletRequest request = (PortletRequest) context.getExternalContext().getRequest(); |
65 | 0 | String portletViewId = request.getParameter(MyFacesGenericPortlet.VIEW_ID); |
66 | 0 | Application application = context.getApplication(); |
67 | 0 | ViewHandler applicationViewHandler = application.getViewHandler(); |
68 | 0 | String renderKitId = applicationViewHandler.calculateRenderKitId(context); |
69 | 0 | UIViewRoot viewRoot = application.getStateManager().restoreView(context, portletViewId, renderKitId); |
70 | 0 | return viewRoot; |
71 | |
} |
72 | 0 | return super.restoreView(context, viewId); |
73 | |
} |
74 | |
|
75 | |
@Override |
76 | |
public UIViewRoot createView(FacesContext context, String viewId) |
77 | |
{ |
78 | 0 | UIViewRoot viewRoot = super.createView(context, viewId); |
79 | 0 | if (PortletUtil.isPortletRequest(context)) |
80 | |
{ |
81 | 0 | PortletRequest request = (PortletRequest) context.getExternalContext().getRequest(); |
82 | 0 | viewRoot.setViewId(request.getParameter(MyFacesGenericPortlet.VIEW_ID)); |
83 | |
} |
84 | 0 | return viewRoot; |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public String getActionURL(FacesContext context, String viewId) |
89 | |
{ |
90 | 0 | if (PortletUtil.isRenderResponse(context)) |
91 | |
{ |
92 | 0 | RenderResponse response = (RenderResponse) context.getExternalContext().getResponse(); |
93 | 0 | PortletURL url = response.createActionURL(); |
94 | 0 | url.setParameter(MyFacesGenericPortlet.VIEW_ID, viewId); |
95 | 0 | return url.toString(); |
96 | |
} |
97 | 0 | return super.getActionURL(context, viewId); |
98 | |
} |
99 | |
|
100 | |
@Override |
101 | |
public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException |
102 | |
{ |
103 | 0 | if (PortletUtil.isPortletRequest(context)) |
104 | |
{ |
105 | 0 | if (viewToRender.isRendered()) |
106 | |
{ |
107 | 0 | if (log.isTraceEnabled()) |
108 | 0 | log.trace("It is a portlet request. Dispatching to view"); |
109 | 0 | context.getExternalContext().dispatch(viewToRender.getViewId()); |
110 | |
} |
111 | |
} |
112 | |
else |
113 | |
{ |
114 | 0 | super.renderView(context, viewToRender); |
115 | |
} |
116 | 0 | } |
117 | |
} |