001    /**
002     * Copyright (c) 2000-2013 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.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.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                    initValidPaths(templatePath, ".jsp");
201            }
202    
203            @Override
204            public void processAction(
205                            ActionRequest actionRequest, ActionResponse actionResponse)
206                    throws IOException, PortletException {
207    
208                    String path = actionRequest.getParameter("struts_action");
209    
210                    if (Validator.isNotNull(path)) {
211    
212                            // Call processAction of com.liferay.portal.struts.PortletAction
213    
214                            try {
215                                    PortletRequestProcessor processor =
216                                            _getPortletRequestProcessor();
217    
218                                    processor.process(actionRequest, actionResponse, path);
219                            }
220                            catch (ServletException se) {
221                                    throw new PortletException(se);
222                            }
223                    }
224    
225                    if (copyRequestParameters) {
226                            PortalUtil.copyRequestParameters(actionRequest, actionResponse);
227                    }
228            }
229    
230            @Override
231            public void processEvent(EventRequest request, EventResponse response)
232                    throws IOException, PortletException {
233    
234                    request.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
235    
236                    // Call processEvent of com.liferay.portal.struts.PortletAction
237    
238                    try {
239                            PortletRequestProcessor processor = _getPortletRequestProcessor();
240    
241                            processor.process(request, response);
242                    }
243                    catch (ServletException se) {
244                            throw new PortletException(se);
245                    }
246            }
247    
248            @Override
249            public void serveResource(
250                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
251                    throws IOException, PortletException {
252    
253                    String resourceId = resourceRequest.getResourceID();
254    
255                    checkPath(resourceId);
256    
257                    resourceRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
258    
259                    // Call serveResource of com.liferay.portal.struts.PortletAction
260    
261                    try {
262                            PortletRequestProcessor processor = _getPortletRequestProcessor();
263    
264                            processor.process(resourceRequest, resourceResponse);
265                    }
266                    catch (ServletException se) {
267                            throw new PortletException(se);
268                    }
269            }
270    
271            protected void include(
272                            RenderRequest renderRequest, RenderResponse renderResponse)
273                    throws IOException, PortletException {
274    
275                    // Call render of com.liferay.portal.struts.PortletAction
276    
277                    Map<String, Object> strutsAttributes = null;
278    
279                    if (_liferayPortletConfig.isWARFile()) {
280                            strutsAttributes = StrutsUtil.removeStrutsAttributes(
281                                    getPortletContext(), renderRequest);
282                    }
283    
284                    try {
285                            PortletRequestProcessor processor = _getPortletRequestProcessor();
286    
287                            processor.process(renderRequest, renderResponse);
288                    }
289                    catch (ServletException se) {
290                            throw new PortletException(se);
291                    }
292                    finally {
293                            if (_liferayPortletConfig.isWARFile()) {
294                                    StrutsUtil.setStrutsAttributes(renderRequest, strutsAttributes);
295                            }
296                    }
297    
298                    if (copyRequestParameters) {
299                            PortalUtil.clearRequestParameters(renderRequest);
300                    }
301            }
302    
303            protected String aboutAction;
304            protected String configAction;
305            protected boolean copyRequestParameters;
306            protected String editAction;
307            protected String editDefaultsAction;
308            protected String editGuestAction;
309            protected String helpAction;
310            protected String previewAction;
311            protected String printAction;
312            protected String templatePath;
313            protected String viewAction;
314    
315            private PortletRequestProcessor _getPortletRequestProcessor() {
316                    PortletContext portletContext = getPortletContext();
317    
318                    return (PortletRequestProcessor)portletContext.getAttribute(
319                            WebKeys.PORTLET_STRUTS_PROCESSOR);
320            }
321    
322            private LiferayPortletConfig _liferayPortletConfig;
323    
324    }