001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortlet;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    import com.liferay.portal.security.permission.PermissionThreadLocal;
022    import com.liferay.portal.struts.PortletRequestProcessor;
023    import com.liferay.portal.struts.StrutsUtil;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.WebKeys;
026    
027    import java.io.IOException;
028    
029    import java.util.Map;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.PortletException;
035    import javax.portlet.PortletRequest;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    import javax.portlet.ResourceRequest;
039    import javax.portlet.ResourceResponse;
040    
041    import javax.servlet.ServletException;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     */
046    public class StrutsPortlet extends LiferayPortlet {
047    
048            public void doAbout(
049                            RenderRequest renderRequest, RenderResponse renderResponse)
050                    throws IOException, PortletException {
051    
052                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, aboutAction);
053    
054                    include(renderRequest, renderResponse);
055            }
056    
057            public void doConfig(
058                            RenderRequest renderRequest, RenderResponse renderResponse)
059                    throws IOException, PortletException {
060    
061                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, configAction);
062    
063                    include(renderRequest, renderResponse);
064            }
065    
066            public void doEdit(
067                            RenderRequest renderRequest, RenderResponse renderResponse)
068                    throws IOException, PortletException {
069    
070                    if (renderRequest.getPreferences() == null) {
071                            super.doEdit(renderRequest, renderResponse);
072                    }
073                    else {
074                            renderRequest.setAttribute(
075                                    WebKeys.PORTLET_STRUTS_ACTION, editAction);
076    
077                            include(renderRequest, renderResponse);
078                    }
079            }
080    
081            public void doEditDefaults(
082                            RenderRequest renderRequest, RenderResponse renderResponse)
083                    throws IOException, PortletException {
084    
085                    if (renderRequest.getPreferences() == null) {
086                            super.doEdit(renderRequest, renderResponse);
087                    }
088                    else {
089                            renderRequest.setAttribute(
090                                    WebKeys.PORTLET_STRUTS_ACTION, editDefaultsAction);
091    
092                            include(renderRequest, renderResponse);
093                    }
094            }
095    
096            public void doEditGuest(
097                            RenderRequest renderRequest, RenderResponse renderResponse)
098                    throws IOException, PortletException {
099    
100                    if (renderRequest.getPreferences() == null) {
101                            super.doEdit(renderRequest, renderResponse);
102                    }
103                    else {
104                            renderRequest.setAttribute(
105                                    WebKeys.PORTLET_STRUTS_ACTION, editGuestAction);
106    
107                            include(renderRequest, renderResponse);
108                    }
109            }
110    
111            public void doHelp(
112                            RenderRequest renderRequest, RenderResponse renderResponse)
113                    throws IOException, PortletException {
114    
115                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, helpAction);
116    
117                    include(renderRequest, renderResponse);
118            }
119    
120            public void doPreview(
121                            RenderRequest renderRequest, RenderResponse renderResponse)
122                    throws IOException, PortletException {
123    
124                    renderRequest.setAttribute(
125                            WebKeys.PORTLET_STRUTS_ACTION, previewAction);
126    
127                    include(renderRequest, renderResponse);
128            }
129    
130            public void doPrint(
131                            RenderRequest renderRequest, RenderResponse renderResponse)
132                    throws IOException, PortletException {
133    
134                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, printAction);
135    
136                    include(renderRequest, renderResponse);
137            }
138    
139            public void doView(
140                            RenderRequest renderRequest, RenderResponse renderResponse)
141                    throws IOException, PortletException {
142    
143                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
144    
145                    include(renderRequest, renderResponse);
146            }
147    
148            public void init(PortletConfig portletConfig) throws PortletException {
149                    super.init(portletConfig);
150    
151                    aboutAction = getInitParameter("about-action");
152                    configAction = getInitParameter("config-action");
153                    editAction = getInitParameter("edit-action");
154                    editDefaultsAction = getInitParameter("edit-defaults-action");
155                    editGuestAction = getInitParameter("edit-guest-action");
156                    helpAction = getInitParameter("help-action");
157                    previewAction = getInitParameter("preview-action");
158                    printAction = getInitParameter("print-action");
159                    viewAction = getInitParameter("view-action");
160    
161                    copyRequestParameters = GetterUtil.getBoolean(
162                            getInitParameter("copy-request-parameters"), true);
163    
164                    _portletConfig = (PortletConfigImpl)portletConfig;
165            }
166    
167            public void processAction(
168                            ActionRequest actionRequest, ActionResponse actionResponse)
169                    throws IOException, PortletException {
170    
171                    String path = actionRequest.getParameter("struts_action");
172    
173                    if (Validator.isNotNull(path)) {
174    
175                            // Call processAction of com.liferay.portal.struts.PortletAction
176    
177                            PermissionChecker permissionChecker =
178                                    PermissionThreadLocal.getPermissionChecker();
179    
180                            try {
181                                    permissionChecker.setValues(actionRequest);
182    
183                                    PortletRequestProcessor processor =
184                                            _getPortletRequestProcessor(actionRequest);
185    
186                                    processor.process(actionRequest, actionResponse, path);
187                            }
188                            catch (ServletException se) {
189                                    throw new PortletException(se);
190                            }
191                            finally {
192                                    permissionChecker.resetValues();
193                            }
194                    }
195    
196                    if (copyRequestParameters) {
197                            PortalUtil.copyRequestParameters(actionRequest, actionResponse);
198                    }
199            }
200    
201            public void serveResource(
202                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
203                    throws IOException, PortletException {
204    
205                    resourceRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
206    
207                    // Call serveResource of com.liferay.portal.struts.PortletAction
208    
209                    PermissionChecker permissionChecker =
210                            PermissionThreadLocal.getPermissionChecker();
211    
212                    try {
213                            permissionChecker.setValues(resourceRequest);
214    
215                            PortletRequestProcessor processor =
216                                    _getPortletRequestProcessor(resourceRequest);
217    
218                            processor.process(resourceRequest, resourceResponse);
219                    }
220                    catch (ServletException se) {
221                            throw new PortletException(se);
222                    }
223                    finally {
224                            permissionChecker.resetValues();
225                    }
226            }
227    
228            protected void include(
229                            RenderRequest renderRequest, RenderResponse renderResponse)
230                    throws IOException, PortletException {
231    
232                    // Call render of com.liferay.portal.struts.PortletAction
233    
234                    Map<String, Object> strutsAttributes = null;
235    
236                    if (_portletConfig.isWARFile()) {
237                            strutsAttributes = StrutsUtil.removeStrutsAttributes(
238                                    getPortletContext(), renderRequest);
239                    }
240    
241                    PermissionChecker permissionChecker =
242                            PermissionThreadLocal.getPermissionChecker();
243    
244                    try {
245                            permissionChecker.setValues(renderRequest);
246    
247                            PortletRequestProcessor processor =
248                                    _getPortletRequestProcessor(renderRequest);
249    
250                            processor.process(renderRequest, renderResponse);
251                    }
252                    catch (ServletException se) {
253                            throw new PortletException(se);
254                    }
255                    finally {
256                            permissionChecker.resetValues();
257    
258                            if (_portletConfig.isWARFile()) {
259                                    StrutsUtil.setStrutsAttributes(renderRequest, strutsAttributes);
260                            }
261                    }
262    
263                    if (copyRequestParameters) {
264                            PortalUtil.clearRequestParameters(renderRequest);
265                    }
266            }
267    
268            private PortletRequestProcessor _getPortletRequestProcessor(
269                    PortletRequest portletRequest) {
270    
271                    return (PortletRequestProcessor)getPortletContext().getAttribute(
272                            WebKeys.PORTLET_STRUTS_PROCESSOR);
273            }
274    
275            protected String aboutAction;
276            protected String configAction;
277            protected String editAction;
278            protected String editDefaultsAction;
279            protected String editGuestAction;
280            protected String helpAction;
281            protected String previewAction;
282            protected String printAction;
283            protected String viewAction;
284            protected boolean copyRequestParameters;
285    
286            private PortletConfigImpl _portletConfig;
287    
288    }