001    /**
002     * Copyright (c) 2000-present 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.portlet.LiferayPortletConfig;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.struts.PortletRequestProcessor;
024    import com.liferay.portal.struts.StrutsUtil;
025    import com.liferay.portal.util.PortalUtil;
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.EventRequest;
034    import javax.portlet.EventResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.PortletContext;
037    import javax.portlet.PortletException;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    import javax.portlet.ResourceRequest;
041    import javax.portlet.ResourceResponse;
042    
043    import javax.servlet.ServletException;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Raymond Aug??
048     */
049    public class StrutsPortlet extends LiferayPortlet {
050    
051            @Override
052            public void doAbout(
053                            RenderRequest renderRequest, RenderResponse renderResponse)
054                    throws IOException, PortletException {
055    
056                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, aboutAction);
057    
058                    include(renderRequest, renderResponse);
059            }
060    
061            @Override
062            public void doConfig(
063                            RenderRequest renderRequest, RenderResponse renderResponse)
064                    throws IOException, PortletException {
065    
066                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, configAction);
067    
068                    include(renderRequest, renderResponse);
069            }
070    
071            @Override
072            public void doEdit(
073                            RenderRequest renderRequest, RenderResponse renderResponse)
074                    throws IOException, PortletException {
075    
076                    if (renderRequest.getPreferences() == null) {
077                            super.doEdit(renderRequest, renderResponse);
078                    }
079                    else {
080                            renderRequest.setAttribute(
081                                    WebKeys.PORTLET_STRUTS_ACTION, editAction);
082    
083                            include(renderRequest, renderResponse);
084                    }
085            }
086    
087            @Override
088            public void doEditDefaults(
089                            RenderRequest renderRequest, RenderResponse renderResponse)
090                    throws IOException, PortletException {
091    
092                    if (renderRequest.getPreferences() == null) {
093                            super.doEdit(renderRequest, renderResponse);
094                    }
095                    else {
096                            renderRequest.setAttribute(
097                                    WebKeys.PORTLET_STRUTS_ACTION, editDefaultsAction);
098    
099                            include(renderRequest, renderResponse);
100                    }
101            }
102    
103            @Override
104            public void doEditGuest(
105                            RenderRequest renderRequest, RenderResponse renderResponse)
106                    throws IOException, PortletException {
107    
108                    if (renderRequest.getPreferences() == null) {
109                            super.doEdit(renderRequest, renderResponse);
110                    }
111                    else {
112                            renderRequest.setAttribute(
113                                    WebKeys.PORTLET_STRUTS_ACTION, editGuestAction);
114    
115                            include(renderRequest, renderResponse);
116                    }
117            }
118    
119            @Override
120            public void doHelp(
121                            RenderRequest renderRequest, RenderResponse renderResponse)
122                    throws IOException, PortletException {
123    
124                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, helpAction);
125    
126                    include(renderRequest, renderResponse);
127            }
128    
129            @Override
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            @Override
141            public void doPrint(
142                            RenderRequest renderRequest, RenderResponse renderResponse)
143                    throws IOException, PortletException {
144    
145                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, printAction);
146    
147                    include(renderRequest, renderResponse);
148            }
149    
150            @Override
151            public void doView(
152                            RenderRequest renderRequest, RenderResponse renderResponse)
153                    throws IOException, PortletException {
154    
155                    renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
156    
157                    include(renderRequest, renderResponse);
158            }
159    
160            @Override
161            public void init(PortletConfig portletConfig) throws PortletException {
162                    super.init(portletConfig);
163    
164                    templatePath = getInitParameter("template-path");
165    
166                    if (Validator.isNull(templatePath)) {
167                            templatePath = StringPool.SLASH;
168                    }
169                    else if (templatePath.contains(StringPool.BACK_SLASH) ||
170                                     templatePath.contains(StringPool.DOUBLE_SLASH) ||
171                                     templatePath.contains(StringPool.PERIOD) ||
172                                     templatePath.contains(StringPool.SPACE)) {
173    
174                            throw new PortletException(
175                                    "template-path " + templatePath + " has invalid characters");
176                    }
177                    else if (!templatePath.startsWith(StringPool.SLASH) ||
178                                     !templatePath.endsWith(StringPool.SLASH)) {
179    
180                            throw new PortletException(
181                                    "template-path " + templatePath +
182                                            " must start and end with a /");
183                    }
184    
185                    aboutAction = getInitParameter("about-action");
186                    configAction = getInitParameter("config-action");
187                    editAction = getInitParameter("edit-action");
188                    editDefaultsAction = getInitParameter("edit-defaults-action");
189                    editGuestAction = getInitParameter("edit-guest-action");
190                    helpAction = getInitParameter("help-action");
191                    previewAction = getInitParameter("preview-action");
192                    printAction = getInitParameter("print-action");
193                    viewAction = getInitParameter("view-action");
194    
195                    copyRequestParameters = GetterUtil.getBoolean(
196                            getInitParameter("copy-request-parameters"), true);
197    
198                    _liferayPortletConfig = (LiferayPortletConfig)portletConfig;
199            }
200    
201            @Override
202            public void processAction(
203                            ActionRequest actionRequest, ActionResponse actionResponse)
204                    throws IOException, PortletException {
205    
206                    String path = actionRequest.getParameter("struts_action");
207    
208                    if (Validator.isNotNull(path)) {
209    
210                            // Call processAction of com.liferay.portal.struts.PortletAction
211    
212                            try {
213                                    PortletRequestProcessor processor =
214                                            _getPortletRequestProcessor();
215    
216                                    processor.process(actionRequest, actionResponse, path);
217                            }
218                            catch (ServletException se) {
219                                    throw new PortletException(se);
220                            }
221                    }
222    
223                    if (copyRequestParameters) {
224                            PortalUtil.copyRequestParameters(actionRequest, actionResponse);
225                    }
226            }
227    
228            @Override
229            public void processEvent(EventRequest request, EventResponse response)
230                    throws IOException, PortletException {
231    
232                    request.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
233    
234                    // Call processEvent of com.liferay.portal.struts.PortletAction
235    
236                    try {
237                            PortletRequestProcessor processor = _getPortletRequestProcessor();
238    
239                            processor.process(request, response);
240                    }
241                    catch (ServletException se) {
242                            throw new PortletException(se);
243                    }
244            }
245    
246            @Override
247            public void serveResource(
248                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
249                    throws IOException, PortletException {
250    
251                    String resourceID = resourceRequest.getResourceID();
252    
253                    checkPath(resourceID);
254    
255                    resourceRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
256    
257                    // Call serveResource of com.liferay.portal.struts.PortletAction
258    
259                    try {
260                            PortletRequestProcessor processor = _getPortletRequestProcessor();
261    
262                            processor.process(resourceRequest, resourceResponse);
263                    }
264                    catch (ServletException se) {
265                            throw new PortletException(se);
266                    }
267            }
268    
269            protected void checkPath(String path) throws PortletException {
270                    if (Validator.isNotNull(path) &&
271                            (!path.startsWith(templatePath) ||
272                             !PortalUtil.isValidResourceId(path) ||
273                             !Validator.isFilePath(path, false))) {
274    
275                            throw new PortletException(
276                                    "Path " + path + " is not accessible by this portlet");
277                    }
278            }
279    
280            protected void include(
281                            RenderRequest renderRequest, RenderResponse renderResponse)
282                    throws IOException, PortletException {
283    
284                    // Call render of com.liferay.portal.struts.PortletAction
285    
286                    Map<String, Object> strutsAttributes = null;
287    
288                    if (_liferayPortletConfig.isWARFile()) {
289                            strutsAttributes = StrutsUtil.removeStrutsAttributes(
290                                    getPortletContext(), renderRequest);
291                    }
292    
293                    try {
294                            PortletRequestProcessor processor = _getPortletRequestProcessor();
295    
296                            processor.process(renderRequest, renderResponse);
297                    }
298                    catch (ServletException se) {
299                            throw new PortletException(se);
300                    }
301                    finally {
302                            if (_liferayPortletConfig.isWARFile()) {
303                                    StrutsUtil.setStrutsAttributes(renderRequest, strutsAttributes);
304                            }
305                    }
306    
307                    if (copyRequestParameters) {
308                            PortalUtil.clearRequestParameters(renderRequest);
309                    }
310            }
311    
312            protected String aboutAction;
313            protected String configAction;
314            protected boolean copyRequestParameters;
315            protected String editAction;
316            protected String editDefaultsAction;
317            protected String editGuestAction;
318            protected String helpAction;
319            protected String previewAction;
320            protected String printAction;
321            protected String templatePath;
322            protected String viewAction;
323    
324            private PortletRequestProcessor _getPortletRequestProcessor() {
325                    PortletContext portletContext = getPortletContext();
326    
327                    return (PortletRequestProcessor)portletContext.getAttribute(
328                            WebKeys.PORTLET_STRUTS_PROCESSOR);
329            }
330    
331            private LiferayPortletConfig _liferayPortletConfig;
332    
333    }