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