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.kernel.portlet.bridges.mvc;
016    
017    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.JavaConstants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.util.WebKeys;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.model.LayoutTypePortlet;
030    import com.liferay.portal.model.Portlet;
031    import com.liferay.portal.model.PortletConstants;
032    import com.liferay.portal.security.auth.PrincipalException;
033    import com.liferay.portal.service.PortletLocalServiceUtil;
034    import com.liferay.portal.theme.ThemeDisplay;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portlet.PortletConfigFactoryUtil;
037    
038    import java.io.IOException;
039    
040    import javax.portlet.ActionRequest;
041    import javax.portlet.ActionResponse;
042    import javax.portlet.PortletConfig;
043    import javax.portlet.PortletException;
044    import javax.portlet.PortletRequest;
045    
046    import javax.servlet.http.HttpServletRequest;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @see    com.liferay.portal.struts.PortletAction
051     */
052    public abstract class BaseMVCActionCommand implements MVCActionCommand {
053    
054            @Override
055            public boolean processAction(
056                            ActionRequest actionRequest, ActionResponse actionResponse)
057                    throws PortletException {
058    
059                    try {
060                            doProcessAction(actionRequest, actionResponse);
061    
062                            return SessionErrors.isEmpty(actionRequest);
063                    }
064                    catch (PortletException pe) {
065                            throw pe;
066                    }
067                    catch (Exception e) {
068                            throw new PortletException(e);
069                    }
070            }
071    
072            protected void addSuccessMessage(
073                    ActionRequest actionRequest, ActionResponse actionResponse) {
074    
075                    PortletConfig portletConfig = (PortletConfig)actionRequest.getAttribute(
076                            JavaConstants.JAVAX_PORTLET_CONFIG);
077    
078                    boolean addProcessActionSuccessMessage = GetterUtil.getBoolean(
079                            portletConfig.getInitParameter("add-process-action-success-action"),
080                            true);
081    
082                    if (!addProcessActionSuccessMessage) {
083                            return;
084                    }
085    
086                    String successMessage = ParamUtil.getString(
087                            actionRequest, "successMessage");
088    
089                    SessionMessages.add(actionRequest, "requestProcessed", successMessage);
090            }
091    
092            protected abstract void doProcessAction(
093                            ActionRequest actionRequest, ActionResponse actionResponse)
094                    throws Exception;
095    
096            protected PortletConfig getPortletConfig(PortletRequest portletRequest) {
097                    String portletId = PortalUtil.getPortletId(portletRequest);
098    
099                    return PortletConfigFactoryUtil.get(
100                            PortletConstants.getRootPortletId(portletId));
101            }
102    
103            protected void hideDefaultErrorMessage(PortletRequest portletRequest) {
104                    SessionMessages.add(
105                            portletRequest,
106                            PortalUtil.getPortletId(portletRequest) +
107                                    SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
108            }
109    
110            protected void hideDefaultSuccessMessage(PortletRequest portletRequest) {
111                    SessionMessages.add(
112                            portletRequest,
113                            PortalUtil.getPortletId(portletRequest) +
114                                    SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
115            }
116    
117            protected boolean isDisplaySuccessMessage(PortletRequest portletRequest) {
118                    if (!SessionErrors.isEmpty(portletRequest)) {
119                            return false;
120                    }
121    
122                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
123                            WebKeys.THEME_DISPLAY);
124    
125                    Layout layout = themeDisplay.getLayout();
126    
127                    if (layout.isTypeControlPanel()) {
128                            return true;
129                    }
130    
131                    String portletId = (String)portletRequest.getAttribute(
132                            WebKeys.PORTLET_ID);
133    
134                    LayoutTypePortlet layoutTypePortlet =
135                            themeDisplay.getLayoutTypePortlet();
136    
137                    if (layoutTypePortlet.hasPortletId(portletId)) {
138                            return true;
139                    }
140    
141                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
142                            themeDisplay.getCompanyId(), portletId);
143    
144                    if (portlet.isAddDefaultResource()) {
145                            return true;
146                    }
147    
148                    return false;
149            }
150    
151            protected boolean redirectToLogin(
152                            ActionRequest actionRequest, ActionResponse actionResponse)
153                    throws IOException {
154    
155                    if (actionRequest.getRemoteUser() == null) {
156                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
157                                    actionRequest);
158    
159                            SessionErrors.add(request, PrincipalException.class.getName());
160    
161                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
162                                    WebKeys.THEME_DISPLAY);
163    
164                            sendRedirect(
165                                    actionRequest, actionResponse, themeDisplay.getURLSignIn());
166    
167                            return true;
168                    }
169                    else {
170                            return false;
171                    }
172            }
173    
174            protected void sendRedirect(
175                            ActionRequest actionRequest, ActionResponse actionResponse)
176                    throws IOException {
177    
178                    sendRedirect(actionRequest, actionResponse, null);
179            }
180    
181            protected void sendRedirect(
182                            ActionRequest actionRequest, ActionResponse actionResponse,
183                            String redirect)
184                    throws IOException {
185    
186                    sendRedirect(null, actionRequest, actionResponse, redirect, null);
187            }
188    
189            protected void sendRedirect(
190                            PortletConfig portletConfig, ActionRequest actionRequest,
191                            ActionResponse actionResponse, String redirect,
192                            String closeRedirect)
193                    throws IOException {
194    
195                    if (isDisplaySuccessMessage(actionRequest)) {
196                            addSuccessMessage(actionRequest, actionResponse);
197                    }
198    
199                    if (Validator.isNull(redirect)) {
200                            redirect = (String)actionRequest.getAttribute(WebKeys.REDIRECT);
201                    }
202    
203                    if (Validator.isNull(redirect)) {
204                            redirect = ParamUtil.getString(actionRequest, "redirect");
205                    }
206    
207                    if ((portletConfig != null) && Validator.isNotNull(redirect) &&
208                            Validator.isNotNull(closeRedirect)) {
209    
210                            redirect = HttpUtil.setParameter(
211                                    redirect, "closeRedirect", closeRedirect);
212    
213                            SessionMessages.add(
214                                    actionRequest,
215                                    PortalUtil.getPortletId(actionRequest) +
216                                            SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
217                                    closeRedirect);
218                    }
219    
220                    if (Validator.isNull(redirect)) {
221                            return;
222                    }
223    
224                    // LPS-1928
225    
226                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
227                            actionRequest);
228    
229                    if (BrowserSnifferUtil.isIe(request) &&
230                            (BrowserSnifferUtil.getMajorVersion(request) == 6.0) &&
231                            redirect.contains(StringPool.POUND)) {
232    
233                            String redirectToken = "&#";
234    
235                            if (!redirect.contains(StringPool.QUESTION)) {
236                                    redirectToken = StringPool.QUESTION + redirectToken;
237                            }
238    
239                            redirect = StringUtil.replace(
240                                    redirect, StringPool.POUND, redirectToken);
241                    }
242    
243                    redirect = PortalUtil.escapeRedirect(redirect);
244    
245                    if (Validator.isNotNull(redirect)) {
246                            actionResponse.sendRedirect(redirect);
247                    }
248            }
249    
250    }