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.SessionErrors;
018    import com.liferay.portal.kernel.servlet.SessionMessages;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.JavaConstants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.model.PortletConstants;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.PortletConfigFactoryUtil;
028    
029    import java.io.IOException;
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    
037    import javax.servlet.http.HttpServletRequest;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public abstract class BaseMVCActionCommand implements MVCActionCommand {
043    
044            @Override
045            public boolean processAction(
046                            ActionRequest actionRequest, ActionResponse actionResponse)
047                    throws PortletException {
048    
049                    try {
050                            doProcessAction(actionRequest, actionResponse);
051    
052                            return SessionErrors.isEmpty(actionRequest);
053                    }
054                    catch (PortletException pe) {
055                            throw pe;
056                    }
057                    catch (Exception e) {
058                            throw new PortletException(e);
059                    }
060            }
061    
062            protected void addSuccessMessage(
063                    ActionRequest actionRequest, ActionResponse actionResponse) {
064    
065                    PortletConfig portletConfig = (PortletConfig)actionRequest.getAttribute(
066                            JavaConstants.JAVAX_PORTLET_CONFIG);
067    
068                    boolean addProcessActionSuccessMessage = GetterUtil.getBoolean(
069                            portletConfig.getInitParameter("add-process-action-success-action"),
070                            true);
071    
072                    if (!addProcessActionSuccessMessage) {
073                            return;
074                    }
075    
076                    String successMessage = ParamUtil.getString(
077                            actionRequest, "successMessage");
078    
079                    SessionMessages.add(actionRequest, "requestProcessed", successMessage);
080            }
081    
082            protected abstract void doProcessAction(
083                            ActionRequest actionRequest, ActionResponse actionResponse)
084                    throws Exception;
085    
086            protected PortletConfig getPortletConfig(PortletRequest portletRequest) {
087                    String portletId = PortalUtil.getPortletId(portletRequest);
088    
089                    return PortletConfigFactoryUtil.get(
090                            PortletConstants.getRootPortletId(portletId));
091            }
092    
093            protected void hideDefaultErrorMessage(PortletRequest portletRequest) {
094                    SessionMessages.add(
095                            portletRequest,
096                            PortalUtil.getPortletId(portletRequest) +
097                                    SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
098            }
099    
100            protected void hideDefaultSuccessMessage(PortletRequest portletRequest) {
101                    SessionMessages.add(
102                            portletRequest,
103                            PortalUtil.getPortletId(portletRequest) +
104                                    SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
105            }
106    
107            protected boolean redirectToLogin(
108                            ActionRequest actionRequest, ActionResponse actionResponse)
109                    throws IOException {
110    
111                    if (actionRequest.getRemoteUser() == null) {
112                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
113                                    actionRequest);
114    
115                            SessionErrors.add(request, PrincipalException.class.getName());
116    
117                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
118                                    WebKeys.THEME_DISPLAY);
119    
120                            sendRedirect(
121                                    actionRequest, actionResponse, themeDisplay.getURLSignIn());
122    
123                            return true;
124                    }
125                    else {
126                            return false;
127                    }
128            }
129    
130            protected void sendRedirect(
131                            ActionRequest actionRequest, ActionResponse actionResponse,
132                            String redirect)
133                    throws IOException {
134    
135                    actionResponse.sendRedirect(redirect);
136    
137                    SessionMessages.add(
138                            actionRequest,
139                            PortalUtil.getPortletId(actionRequest) +
140                                    SessionMessages.KEY_SUFFIX_FORCE_SEND_REDIRECT);
141            }
142    
143    }