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.portal.struts;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.portlet.PortletResponseUtil;
021    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
022    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
023    import com.liferay.portal.kernel.servlet.SessionErrors;
024    import com.liferay.portal.kernel.servlet.SessionMessages;
025    import com.liferay.portal.kernel.util.ContentTypes;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.HttpUtil;
028    import com.liferay.portal.kernel.util.JavaConstants;
029    import com.liferay.portal.kernel.util.ParamUtil;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.StringUtil;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.model.Layout;
034    import com.liferay.portal.model.LayoutTypePortlet;
035    import com.liferay.portal.model.Portlet;
036    import com.liferay.portal.security.auth.PrincipalException;
037    import com.liferay.portal.service.PortletLocalServiceUtil;
038    import com.liferay.portal.theme.ThemeDisplay;
039    import com.liferay.portal.util.PortalUtil;
040    import com.liferay.portal.util.WebKeys;
041    import com.liferay.portlet.PortletPreferencesFactoryUtil;
042    import com.liferay.portlet.StrictPortletPreferencesImpl;
043    
044    import java.io.IOException;
045    
046    import javax.portlet.ActionRequest;
047    import javax.portlet.ActionResponse;
048    import javax.portlet.EventRequest;
049    import javax.portlet.EventResponse;
050    import javax.portlet.MimeResponse;
051    import javax.portlet.PortletConfig;
052    import javax.portlet.PortletContext;
053    import javax.portlet.PortletPreferences;
054    import javax.portlet.PortletRequest;
055    import javax.portlet.PortletRequestDispatcher;
056    import javax.portlet.PortletResponse;
057    import javax.portlet.RenderRequest;
058    import javax.portlet.RenderResponse;
059    import javax.portlet.ResourceRequest;
060    import javax.portlet.ResourceResponse;
061    
062    import javax.servlet.ServletContext;
063    import javax.servlet.http.HttpServletRequest;
064    import javax.servlet.http.HttpServletResponse;
065    
066    import org.apache.struts.Globals;
067    import org.apache.struts.action.Action;
068    import org.apache.struts.action.ActionForm;
069    import org.apache.struts.action.ActionForward;
070    import org.apache.struts.action.ActionMapping;
071    import org.apache.struts.config.ModuleConfig;
072    import org.apache.struts.util.MessageResources;
073    
074    /**
075     * @author Brian Wing Shun Chan
076     */
077    public class PortletAction extends Action {
078    
079            public static String getForwardKey(HttpServletRequest request) {
080                    String portletId = (String)request.getAttribute(WebKeys.PORTLET_ID);
081    
082                    String portletNamespace = PortalUtil.getPortletNamespace(portletId);
083    
084                    return portletNamespace.concat(WebKeys.PORTLET_STRUTS_FORWARD);
085            }
086    
087            public static String getForwardKey(PortletRequest portletRequest) {
088                    String portletId = (String)portletRequest.getAttribute(
089                            WebKeys.PORTLET_ID);
090    
091                    String portletNamespace = PortalUtil.getPortletNamespace(portletId);
092    
093                    return portletNamespace.concat(WebKeys.PORTLET_STRUTS_FORWARD);
094            }
095    
096            @Override
097            public ActionForward execute(
098                            ActionMapping actionMapping, ActionForm actionForm,
099                            HttpServletRequest request, HttpServletResponse response)
100                    throws Exception {
101    
102                    PortletConfig portletConfig = (PortletConfig)request.getAttribute(
103                            JavaConstants.JAVAX_PORTLET_CONFIG);
104    
105                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
106                            JavaConstants.JAVAX_PORTLET_REQUEST);
107    
108                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
109                            JavaConstants.JAVAX_PORTLET_RESPONSE);
110    
111                    Boolean strutsExecute = (Boolean)request.getAttribute(
112                            WebKeys.PORTLET_STRUTS_EXECUTE);
113    
114                    if ((strutsExecute != null) && strutsExecute.booleanValue()) {
115                            return strutsExecute(actionMapping, actionForm, request, response);
116                    }
117                    else if (portletRequest instanceof RenderRequest) {
118                            return render(
119                                    actionMapping, actionForm, portletConfig,
120                                    (RenderRequest)portletRequest, (RenderResponse)portletResponse);
121                    }
122                    else {
123                            if (portletRequest instanceof EventRequest) {
124                                    processEvent(
125                                            actionMapping, actionForm, portletConfig,
126                                            (EventRequest)portletRequest,
127                                            (EventResponse)portletResponse);
128                            }
129                            else {
130                                    serveResource(
131                                            actionMapping, actionForm, portletConfig,
132                                            (ResourceRequest)portletRequest,
133                                            (ResourceResponse)portletResponse);
134                            }
135    
136                            return actionMapping.findForward(ActionConstants.COMMON_NULL);
137                    }
138            }
139    
140            public void processAction(
141                            ActionMapping actionMapping, ActionForm actionForm,
142                            PortletConfig portletConfig, ActionRequest actionRequest,
143                            ActionResponse actionResponse)
144                    throws Exception {
145            }
146    
147            public void processEvent(
148                            ActionMapping actionMapping, ActionForm actionForm,
149                            PortletConfig portletConfig, EventRequest eventRequest,
150                            EventResponse eventResponse)
151                    throws Exception {
152            }
153    
154            public ActionForward render(
155                            ActionMapping actionMapping, ActionForm actionForm,
156                            PortletConfig portletConfig, RenderRequest renderRequest,
157                            RenderResponse renderResponse)
158                    throws Exception {
159    
160                    if (_log.isDebugEnabled()) {
161                            _log.debug("Forward to " + getForward(renderRequest));
162                    }
163    
164                    return actionMapping.findForward(getForward(renderRequest));
165            }
166    
167            public void serveResource(
168                            ActionMapping actionMapping, ActionForm actionForm,
169                            PortletConfig portletConfig, ResourceRequest resourceRequest,
170                            ResourceResponse resourceResponse)
171                    throws Exception {
172    
173                    String resourceId = resourceRequest.getResourceID();
174    
175                    if (Validator.isNull(resourceId)) {
176                            return;
177                    }
178    
179                    PortletContext portletContext = portletConfig.getPortletContext();
180    
181                    PortletRequestDispatcher portletRequestDispatcher =
182                            portletContext.getRequestDispatcher(resourceId);
183    
184                    if (portletRequestDispatcher == null) {
185                            return;
186                    }
187    
188                    portletRequestDispatcher.forward(resourceRequest, resourceResponse);
189            }
190    
191            public ActionForward strutsExecute(
192                            ActionMapping actionMapping, ActionForm actionForm,
193                            HttpServletRequest request, HttpServletResponse response)
194                    throws Exception {
195    
196                    return super.execute(actionMapping, actionForm, request, response);
197            }
198    
199            protected void addSuccessMessage(
200                    ActionRequest actionRequest, ActionResponse actionResponse) {
201    
202                    PortletConfig portletConfig = (PortletConfig)actionRequest.getAttribute(
203                            JavaConstants.JAVAX_PORTLET_CONFIG);
204    
205                    boolean addProcessActionSuccessMessage = GetterUtil.getBoolean(
206                            portletConfig.getInitParameter("add-process-action-success-action"),
207                            true);
208    
209                    if (!addProcessActionSuccessMessage) {
210                            return;
211                    }
212    
213                    String successMessage = ParamUtil.getString(
214                            actionRequest, "successMessage");
215    
216                    SessionMessages.add(actionRequest, "requestProcessed", successMessage);
217            }
218    
219            protected String getForward(PortletRequest portletRequest) {
220                    return getForward(portletRequest, null);
221            }
222    
223            protected String getForward(
224                    PortletRequest portletRequest, String defaultValue) {
225    
226                    String forward = (String)portletRequest.getAttribute(
227                            getForwardKey(portletRequest));
228    
229                    if (forward == null) {
230                            return defaultValue;
231                    }
232                    else {
233                            return forward;
234                    }
235            }
236    
237            protected ModuleConfig getModuleConfig(PortletRequest portletRequest) {
238                    return (ModuleConfig)portletRequest.getAttribute(Globals.MODULE_KEY);
239            }
240    
241            protected MessageResources getResources() {
242                    ServletContext servletContext = getServlet().getServletContext();
243    
244                    return (MessageResources)servletContext.getAttribute(
245                            Globals.MESSAGES_KEY);
246            }
247    
248            @Override
249            protected MessageResources getResources(HttpServletRequest request) {
250                    return getResources();
251            }
252    
253            protected MessageResources getResources(PortletRequest portletRequest) {
254                    return getResources();
255            }
256    
257            protected PortletPreferences getStrictPortletSetup(
258                            Layout layout, String portletId)
259                    throws PortalException {
260    
261                    if (Validator.isNull(portletId)) {
262                            return null;
263                    }
264    
265                    PortletPreferences portletPreferences =
266                            PortletPreferencesFactoryUtil.getStrictPortletSetup(
267                                    layout, portletId);
268    
269                    if (portletPreferences instanceof StrictPortletPreferencesImpl) {
270                            throw new PrincipalException();
271                    }
272    
273                    return portletPreferences;
274            }
275    
276            protected PortletPreferences getStrictPortletSetup(
277                            PortletRequest portletRequest)
278                    throws PortalException {
279    
280                    String portletResource = ParamUtil.getString(
281                            portletRequest, "portletResource");
282    
283                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
284                            WebKeys.THEME_DISPLAY);
285    
286                    return getStrictPortletSetup(themeDisplay.getLayout(), portletResource);
287            }
288    
289            protected void hideDefaultErrorMessage(PortletRequest portletRequest) {
290                    SessionMessages.add(
291                            portletRequest,
292                            PortalUtil.getPortletId(portletRequest) +
293                                    SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
294            }
295    
296            /**
297             * @deprecated As of 6.2.0 {@link
298             *             #hideDefaultSuccessMessage(PortletRequest)}
299             */
300            @Deprecated
301            protected void hideDefaultSuccessMessage(
302                    PortletConfig portletConfig, PortletRequest portletRequest) {
303    
304                    hideDefaultSuccessMessage(portletRequest);
305            }
306    
307            protected void hideDefaultSuccessMessage(PortletRequest portletRequest) {
308                    SessionMessages.add(
309                            portletRequest,
310                            PortalUtil.getPortletId(portletRequest) +
311                                    SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
312            }
313    
314            protected boolean isCheckMethodOnProcessAction() {
315                    return _CHECK_METHOD_ON_PROCESS_ACTION;
316            }
317    
318            protected boolean isDisplaySuccessMessage(PortletRequest portletRequest) {
319                    if (!SessionErrors.isEmpty(portletRequest)) {
320                            return false;
321                    }
322    
323                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
324                            WebKeys.THEME_DISPLAY);
325    
326                    Layout layout = themeDisplay.getLayout();
327    
328                    if (layout.isTypeControlPanel()) {
329                            return true;
330                    }
331    
332                    String portletId = (String)portletRequest.getAttribute(
333                            WebKeys.PORTLET_ID);
334    
335                    try {
336                            LayoutTypePortlet layoutTypePortlet =
337                                    themeDisplay.getLayoutTypePortlet();
338    
339                            if (layoutTypePortlet.hasPortletId(portletId)) {
340                                    return true;
341                            }
342                    }
343                    catch (PortalException pe) {
344                            if (_log.isDebugEnabled()) {
345                                    _log.debug(pe, pe);
346                            }
347                    }
348    
349                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
350                            themeDisplay.getCompanyId(), portletId);
351    
352                    if (portlet.isAddDefaultResource()) {
353                            return true;
354                    }
355    
356                    return false;
357            }
358    
359            protected boolean redirectToLogin(
360                            ActionRequest actionRequest, ActionResponse actionResponse)
361                    throws IOException {
362    
363                    if (actionRequest.getRemoteUser() == null) {
364                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
365                                    actionRequest);
366    
367                            SessionErrors.add(request, PrincipalException.class.getName());
368    
369                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
370                                    WebKeys.THEME_DISPLAY);
371    
372                            actionResponse.sendRedirect(themeDisplay.getURLSignIn());
373    
374                            return true;
375                    }
376                    else {
377                            return false;
378                    }
379            }
380    
381            protected void sendRedirect(
382                            ActionRequest actionRequest, ActionResponse actionResponse)
383                    throws IOException {
384    
385                    sendRedirect(actionRequest, actionResponse, null);
386            }
387    
388            protected void sendRedirect(
389                            ActionRequest actionRequest, ActionResponse actionResponse,
390                            String redirect)
391                    throws IOException {
392    
393                    sendRedirect(null, actionRequest, actionResponse, redirect, null);
394            }
395    
396            protected void sendRedirect(
397                            PortletConfig portletConfig, ActionRequest actionRequest,
398                            ActionResponse actionResponse, String redirect,
399                            String closeRedirect)
400                    throws IOException {
401    
402                    if (isDisplaySuccessMessage(actionRequest)) {
403                            addSuccessMessage(actionRequest, actionResponse);
404                    }
405    
406                    if (Validator.isNull(redirect)) {
407                            redirect = (String)actionRequest.getAttribute(WebKeys.REDIRECT);
408                    }
409    
410                    if (Validator.isNull(redirect)) {
411                            redirect = ParamUtil.getString(actionRequest, "redirect");
412                    }
413    
414                    if ((portletConfig != null) && Validator.isNotNull(redirect) &&
415                            Validator.isNotNull(closeRedirect)) {
416    
417                            redirect = HttpUtil.setParameter(
418                                    redirect, "closeRedirect", closeRedirect);
419    
420                            SessionMessages.add(
421                                    actionRequest,
422                                    PortalUtil.getPortletId(actionRequest) +
423                                            SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
424                                    closeRedirect);
425                    }
426    
427                    if (Validator.isNull(redirect)) {
428                            return;
429                    }
430    
431                    // LPS-1928
432    
433                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
434                            actionRequest);
435    
436                    if (BrowserSnifferUtil.isIe(request) &&
437                            (BrowserSnifferUtil.getMajorVersion(request) == 6.0) &&
438                            redirect.contains(StringPool.POUND)) {
439    
440                            String redirectToken = "&#";
441    
442                            if (!redirect.contains(StringPool.QUESTION)) {
443                                    redirectToken = StringPool.QUESTION + redirectToken;
444                            }
445    
446                            redirect = StringUtil.replace(
447                                    redirect, StringPool.POUND, redirectToken);
448                    }
449    
450                    redirect = PortalUtil.escapeRedirect(redirect);
451    
452                    if (Validator.isNotNull(redirect)) {
453                            actionResponse.sendRedirect(redirect);
454                    }
455            }
456    
457            protected void setForward(PortletRequest portletRequest, String forward) {
458                    portletRequest.setAttribute(getForwardKey(portletRequest), forward);
459            }
460    
461            protected void writeJSON(
462                            PortletRequest portletRequest, ActionResponse actionResponse,
463                            Object json)
464                    throws IOException {
465    
466                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
467                            portletRequest);
468    
469                    String contentType = ContentTypes.APPLICATION_JSON;
470    
471                    if (BrowserSnifferUtil.isIe(request)) {
472                            contentType = ContentTypes.TEXT_HTML;
473                    }
474    
475                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
476                            actionResponse);
477    
478                    response.setContentType(contentType);
479    
480                    ServletResponseUtil.write(response, json.toString());
481    
482                    response.flushBuffer();
483    
484                    setForward(portletRequest, ActionConstants.COMMON_NULL);
485            }
486    
487            protected void writeJSON(
488                            PortletRequest portletRequest, MimeResponse mimeResponse,
489                            Object json)
490                    throws IOException {
491    
492                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
493                            portletRequest);
494    
495                    String contentType = ContentTypes.APPLICATION_JSON;
496    
497                    if (BrowserSnifferUtil.isIe(request)) {
498                            contentType = ContentTypes.TEXT_HTML;
499                    }
500    
501                    mimeResponse.setContentType(contentType);
502    
503                    PortletResponseUtil.write(mimeResponse, json.toString());
504    
505                    mimeResponse.flushBuffer();
506            }
507    
508            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = true;
509    
510            private static final Log _log = LogFactoryUtil.getLog(PortletAction.class);
511    
512    }