1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.portlet.LiferayPortlet;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.security.permission.PermissionChecker;
29  import com.liferay.portal.security.permission.PermissionThreadLocal;
30  import com.liferay.portal.struts.PortletRequestProcessor;
31  import com.liferay.portal.struts.StrutsUtil;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  
35  import java.io.IOException;
36  
37  import java.util.Map;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.PortletException;
43  import javax.portlet.PortletRequest;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  import javax.portlet.ResourceRequest;
47  import javax.portlet.ResourceResponse;
48  
49  import javax.servlet.ServletException;
50  
51  /**
52   * <a href="StrutsPortlet.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   */
56  public class StrutsPortlet extends LiferayPortlet {
57  
58      public void doAbout(
59              RenderRequest renderRequest, RenderResponse renderResponse)
60          throws IOException, PortletException {
61  
62          renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, aboutAction);
63  
64          include(renderRequest, renderResponse);
65      }
66  
67      public void doConfig(
68              RenderRequest renderRequest, RenderResponse renderResponse)
69          throws IOException, PortletException {
70  
71          renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, configAction);
72  
73          include(renderRequest, renderResponse);
74      }
75  
76      public void doEdit(
77              RenderRequest renderRequest, RenderResponse renderResponse)
78          throws IOException, PortletException {
79  
80          if (renderRequest.getPreferences() == null) {
81              super.doEdit(renderRequest, renderResponse);
82          }
83          else {
84              renderRequest.setAttribute(
85                  WebKeys.PORTLET_STRUTS_ACTION, editAction);
86  
87              include(renderRequest, renderResponse);
88          }
89      }
90  
91      public void doEditDefaults(
92              RenderRequest renderRequest, RenderResponse renderResponse)
93          throws IOException, PortletException {
94  
95          if (renderRequest.getPreferences() == null) {
96              super.doEdit(renderRequest, renderResponse);
97          }
98          else {
99              renderRequest.setAttribute(
100                 WebKeys.PORTLET_STRUTS_ACTION, editDefaultsAction);
101 
102             include(renderRequest, renderResponse);
103         }
104     }
105 
106     public void doEditGuest(
107             RenderRequest renderRequest, RenderResponse renderResponse)
108         throws IOException, PortletException {
109 
110         if (renderRequest.getPreferences() == null) {
111             super.doEdit(renderRequest, renderResponse);
112         }
113         else {
114             renderRequest.setAttribute(
115                 WebKeys.PORTLET_STRUTS_ACTION, editGuestAction);
116 
117             include(renderRequest, renderResponse);
118         }
119     }
120 
121     public void doHelp(
122             RenderRequest renderRequest, RenderResponse renderResponse)
123         throws IOException, PortletException {
124 
125         renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, helpAction);
126 
127         include(renderRequest, renderResponse);
128     }
129 
130     public void doPreview(
131             RenderRequest renderRequest, RenderResponse renderResponse)
132         throws IOException, PortletException {
133 
134         renderRequest.setAttribute(
135             WebKeys.PORTLET_STRUTS_ACTION, previewAction);
136 
137         include(renderRequest, renderResponse);
138     }
139 
140     public void doPrint(
141             RenderRequest renderRequest, RenderResponse renderResponse)
142         throws IOException, PortletException {
143 
144         renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, printAction);
145 
146         include(renderRequest, renderResponse);
147     }
148 
149     public void doView(
150             RenderRequest renderRequest, RenderResponse renderResponse)
151         throws IOException, PortletException {
152 
153         renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
154 
155         include(renderRequest, renderResponse);
156     }
157 
158     public void init(PortletConfig portletConfig) throws PortletException {
159         super.init(portletConfig);
160 
161         aboutAction = getInitParameter("about-action");
162         configAction = getInitParameter("config-action");
163         editAction = getInitParameter("edit-action");
164         editDefaultsAction = getInitParameter("edit-defaults-action");
165         editGuestAction = getInitParameter("edit-guest-action");
166         helpAction = getInitParameter("help-action");
167         previewAction = getInitParameter("preview-action");
168         printAction = getInitParameter("print-action");
169         viewAction = getInitParameter("view-action");
170 
171         copyRequestParameters = GetterUtil.getBoolean(
172             getInitParameter("copy-request-parameters"), true);
173 
174         _portletConfig = (PortletConfigImpl)portletConfig;
175     }
176 
177     public void processAction(
178             ActionRequest actionRequest, ActionResponse actionResponse)
179         throws IOException, PortletException {
180 
181         String path = actionRequest.getParameter("struts_action");
182 
183         if (Validator.isNotNull(path)) {
184 
185             // Call processAction of com.liferay.portal.struts.PortletAction
186 
187             PermissionChecker permissionChecker =
188                 PermissionThreadLocal.getPermissionChecker();
189 
190             try {
191                 permissionChecker.setValues(actionRequest);
192 
193                 PortletRequestProcessor processor =
194                     _getPortletRequestProcessor(actionRequest);
195 
196                 processor.process(actionRequest, actionResponse, path);
197             }
198             catch (ServletException se) {
199                 throw new PortletException(se);
200             }
201             finally {
202                 permissionChecker.resetValues();
203             }
204         }
205 
206         if (copyRequestParameters) {
207             PortalUtil.copyRequestParameters(actionRequest, actionResponse);
208         }
209     }
210 
211     public void serveResource(
212             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
213         throws IOException, PortletException {
214 
215         resourceRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
216 
217         // Call serveResource of com.liferay.portal.struts.PortletAction
218 
219         PermissionChecker permissionChecker =
220             PermissionThreadLocal.getPermissionChecker();
221 
222         try {
223             permissionChecker.setValues(resourceRequest);
224 
225             PortletRequestProcessor processor =
226                 _getPortletRequestProcessor(resourceRequest);
227 
228             processor.process(resourceRequest, resourceResponse);
229         }
230         catch (ServletException se) {
231             throw new PortletException(se);
232         }
233         finally {
234             permissionChecker.resetValues();
235         }
236     }
237 
238     protected void include(
239             RenderRequest renderRequest, RenderResponse renderResponse)
240         throws IOException, PortletException {
241 
242         // Call render of com.liferay.portal.struts.PortletAction
243 
244         Map<String, Object> strutsAttributes = null;
245 
246         if (_portletConfig.isWARFile()) {
247             strutsAttributes = StrutsUtil.removeStrutsAttributes(
248                 getPortletContext(), renderRequest);
249         }
250 
251         PermissionChecker permissionChecker =
252             PermissionThreadLocal.getPermissionChecker();
253 
254         try {
255             permissionChecker.setValues(renderRequest);
256 
257             PortletRequestProcessor processor =
258                 _getPortletRequestProcessor(renderRequest);
259 
260             processor.process(renderRequest, renderResponse);
261         }
262         catch (ServletException se) {
263             throw new PortletException(se);
264         }
265         finally {
266             permissionChecker.resetValues();
267 
268             if (_portletConfig.isWARFile()) {
269                 StrutsUtil.setStrutsAttributes(renderRequest, strutsAttributes);
270             }
271         }
272 
273         if (copyRequestParameters) {
274             PortalUtil.clearRequestParameters(renderRequest);
275         }
276     }
277 
278     private PortletRequestProcessor _getPortletRequestProcessor(
279         PortletRequest portletRequest) {
280 
281         return (PortletRequestProcessor)getPortletContext().getAttribute(
282             WebKeys.PORTLET_STRUTS_PROCESSOR);
283     }
284 
285     protected String aboutAction;
286     protected String configAction;
287     protected String editAction;
288     protected String editDefaultsAction;
289     protected String editGuestAction;
290     protected String helpAction;
291     protected String previewAction;
292     protected String printAction;
293     protected String viewAction;
294     protected boolean copyRequestParameters;
295 
296     private PortletConfigImpl _portletConfig;
297 
298 }