Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
RenderService |
|
| 1.375;1.375 |
1 | // Copyright 2005 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | ||
15 | package org.apache.tapestry.portlet; |
|
16 | ||
17 | import java.io.IOException; |
|
18 | ||
19 | import javax.portlet.PortletRequest; |
|
20 | ||
21 | import org.apache.tapestry.IRequestCycle; |
|
22 | import org.apache.tapestry.engine.IEngineService; |
|
23 | import org.apache.tapestry.engine.ILink; |
|
24 | import org.apache.tapestry.services.ServiceConstants; |
|
25 | ||
26 | /** |
|
27 | * Responsible for rendering out a page; a Portlet render URL is built during |
|
28 | * action processing that stores the active page; this is the page that will be |
|
29 | * rendered. The render service is (typically) the only service that operates |
|
30 | * during a portlet RenderRequest. All other services will be an ActionRequest |
|
31 | * that (via {@link org.apache.tapestry.portlet.PortletResponseRenderer}, |
|
32 | * writes query parameters to activate this service during the render request. |
|
33 | * <p> |
|
34 | * Problematic is is anything related to the portlet mode or window state. As |
|
35 | * per the Portlet spec, when the user clicks the "help" or "edit" buttons (or |
|
36 | * the minimize, maximize, etc.), this causes a new RenderRequest, but |
|
37 | * explicitly keeps the render parameters set by the most recent ActionRequest. |
|
38 | * But what Tapestry needs is to detect that the mode or state has changed and |
|
39 | * select a different page to render the response. So we store the mode and |
|
40 | * state in effect when the ActionRequest executed as two more query parameters, |
|
41 | * and detect changes to mode and state that way. If there is a change, then we |
|
42 | * ignore the page query parameter and use the {@link PortletPageResolver} to |
|
43 | * figure out the correct page to display instead. |
|
44 | * |
|
45 | * @author Howard M. Lewis Ship |
|
46 | * @since 4.0 |
|
47 | * @see org.apache.tapestry.services.impl.ResponseRendererImpl |
|
48 | */ |
|
49 | 4 | public class RenderService implements IEngineService |
50 | { |
|
51 | ||
52 | private PortletRequest _request; |
|
53 | ||
54 | private PortletRenderer _portletRenderer; |
|
55 | ||
56 | private PortletPageResolver _pageResolver; |
|
57 | ||
58 | public ILink getLink(boolean post, Object parameter) |
|
59 | { |
|
60 | 1 | throw new UnsupportedOperationException(PortletMessages |
61 | .unsupportedMethod("getLink")); |
|
62 | } |
|
63 | ||
64 | public void service(IRequestCycle cycle) |
|
65 | throws IOException |
|
66 | { |
|
67 | 3 | String pageName = getPageNameToRender(cycle); |
68 | ||
69 | 3 | _portletRenderer.renderPage(cycle, pageName); |
70 | 3 | } |
71 | ||
72 | private String getPageNameToRender(IRequestCycle cycle) |
|
73 | { |
|
74 | 3 | if (isStateChange(cycle)) |
75 | 2 | return _pageResolver.getPageNameForRequest(cycle); |
76 | ||
77 | 1 | return cycle.getParameter(ServiceConstants.PAGE); |
78 | } |
|
79 | ||
80 | /** |
|
81 | * Returns true if the portlet mode or the window state has changed since. |
|
82 | * The values stored previously (during an action request) are compared to |
|
83 | * the current values. |
|
84 | */ |
|
85 | ||
86 | boolean isStateChange(IRequestCycle cycle) |
|
87 | { |
|
88 | 3 | String expectedPortletMode = cycle |
89 | .getParameter(PortletConstants.PORTLET_MODE); |
|
90 | 3 | String expectedWindowState = cycle |
91 | .getParameter(PortletConstants.WINDOW_STATE); |
|
92 | ||
93 | 3 | return !(_request.getPortletMode().toString().equals( |
94 | expectedPortletMode) && _request.getWindowState().toString() |
|
95 | .equals(expectedWindowState)); |
|
96 | ||
97 | } |
|
98 | ||
99 | public String getName() |
|
100 | { |
|
101 | 0 | return PortletConstants.RENDER_SERVICE; |
102 | } |
|
103 | ||
104 | public void setPortletRenderer(PortletRenderer portletRenderer) |
|
105 | { |
|
106 | 3 | _portletRenderer = portletRenderer; |
107 | 3 | } |
108 | ||
109 | public void setRequest(PortletRequest request) |
|
110 | { |
|
111 | 3 | _request = request; |
112 | 3 | } |
113 | ||
114 | public void setPageResolver(PortletPageResolver pageResolver) |
|
115 | { |
|
116 | 2 | _pageResolver = pageResolver; |
117 | 2 | } |
118 | } |