001
014
015 package com.liferay.util.bridges.mvc;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.portlet.LiferayPortlet;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.NullWrapper;
022 import com.liferay.portal.kernel.util.ParamUtil;
023 import com.liferay.portal.kernel.util.PortalClassInvoker;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.kernel.util.WebKeys;
027 import com.liferay.portal.util.PortalUtil;
028
029 import java.io.IOException;
030
031 import java.util.List;
032
033 import javax.portlet.ActionRequest;
034 import javax.portlet.ActionResponse;
035 import javax.portlet.EventRequest;
036 import javax.portlet.EventResponse;
037 import javax.portlet.PortletException;
038 import javax.portlet.PortletRequest;
039 import javax.portlet.PortletRequestDispatcher;
040 import javax.portlet.PortletResponse;
041 import javax.portlet.RenderRequest;
042 import javax.portlet.RenderResponse;
043 import javax.portlet.ResourceRequest;
044 import javax.portlet.ResourceResponse;
045 import javax.portlet.WindowState;
046
047
050 public class MVCPortlet extends LiferayPortlet {
051
052 public void doAbout(
053 RenderRequest renderRequest, RenderResponse renderResponse)
054 throws IOException, PortletException {
055
056 include(aboutJSP, renderRequest, renderResponse);
057 }
058
059 public void doConfig(
060 RenderRequest renderRequest, RenderResponse renderResponse)
061 throws IOException, PortletException {
062
063 include(configJSP, renderRequest, renderResponse);
064 }
065
066 public void doEdit(
067 RenderRequest renderRequest, RenderResponse renderResponse)
068 throws IOException, PortletException {
069
070 if (renderRequest.getPreferences() == null) {
071 super.doEdit(renderRequest, renderResponse);
072 }
073 else {
074 include(editJSP, renderRequest, renderResponse);
075 }
076 }
077
078 public void doEditDefaults(
079 RenderRequest renderRequest, RenderResponse renderResponse)
080 throws IOException, PortletException {
081
082 if (renderRequest.getPreferences() == null) {
083 super.doEdit(renderRequest, renderResponse);
084 }
085 else {
086 include(editDefaultsJSP, renderRequest, renderResponse);
087 }
088 }
089
090 public void doEditGuest(
091 RenderRequest renderRequest, RenderResponse renderResponse)
092 throws IOException, PortletException {
093
094 if (renderRequest.getPreferences() == null) {
095 super.doEdit(renderRequest, renderResponse);
096 }
097 else {
098 include(editGuestJSP, renderRequest, renderResponse);
099 }
100 }
101
102 public void doHelp(
103 RenderRequest renderRequest, RenderResponse renderResponse)
104 throws IOException, PortletException {
105
106 include(helpJSP, renderRequest, renderResponse);
107 }
108
109 public void doPreview(
110 RenderRequest renderRequest, RenderResponse renderResponse)
111 throws IOException, PortletException {
112
113 include(previewJSP, renderRequest, renderResponse);
114 }
115
116 public void doPrint(
117 RenderRequest renderRequest, RenderResponse renderResponse)
118 throws IOException, PortletException {
119
120 include(printJSP, renderRequest, renderResponse);
121 }
122
123 public void doView(
124 RenderRequest renderRequest, RenderResponse renderResponse)
125 throws IOException, PortletException {
126
127 include(viewJSP, renderRequest, renderResponse);
128 }
129
130 public void init() throws PortletException {
131 super.init();
132
133 jspPath = getInitParameter("jsp-path");
134
135 if (Validator.isNull(jspPath)) {
136 jspPath = StringPool.SLASH;
137 }
138 else if (jspPath.contains(StringPool.BACK_SLASH) ||
139 jspPath.contains(StringPool.DOUBLE_SLASH) ||
140 jspPath.contains(StringPool.PERIOD) ||
141 jspPath.contains(StringPool.SPACE)) {
142
143 throw new PortletException(
144 "jsp-path " + jspPath + " has invalid characters");
145 }
146 else if (!jspPath.startsWith(StringPool.SLASH) ||
147 !jspPath.endsWith(StringPool.SLASH)) {
148
149 throw new PortletException(
150 "jsp-path " + jspPath + " must start and end with a /");
151 }
152
153 aboutJSP = getInitParameter("about-jsp");
154 configJSP = getInitParameter("config-jsp");
155 editJSP = getInitParameter("edit-jsp");
156 editDefaultsJSP = getInitParameter("edit-defaults-jsp");
157 editGuestJSP = getInitParameter("edit-guest-jsp");
158 helpJSP = getInitParameter("help-jsp");
159 previewJSP = getInitParameter("preview-jsp");
160 printJSP = getInitParameter("print-jsp");
161 viewJSP = getInitParameter("view-jsp");
162
163 clearRequestParameters = GetterUtil.getBoolean(
164 getInitParameter("clear-request-parameters"));
165 copyRequestParameters = GetterUtil.getBoolean(
166 getInitParameter("copy-request-parameters"));
167
168 String packagePrefix = getInitParameter(
169 ActionCommandCache.ACTION_PACKAGE_NAME);
170
171 if (Validator.isNotNull(packagePrefix)) {
172 _actionCommandCache = new ActionCommandCache(packagePrefix);
173 }
174 }
175
176 public void invokeTaglibDiscussion(
177 ActionRequest actionRequest, ActionResponse actionResponse)
178 throws Exception {
179
180 Object[] arguments = new Object[] {
181 new NullWrapper("org.apache.struts.action.ActionMapping"),
182 new NullWrapper("org.apache.struts.action.ActionForm"),
183 getPortletConfig(), actionRequest, actionResponse
184 };
185
186 PortalClassInvoker.invoke(
187 "com.liferay.portlet.messageboards.action.EditDiscussionAction",
188 "processAction", arguments);
189 }
190
191 public void processAction(
192 ActionRequest actionRequest, ActionResponse actionResponse)
193 throws IOException, PortletException {
194
195 super.processAction(actionRequest, actionResponse);
196
197 if (copyRequestParameters) {
198 PortalUtil.copyRequestParameters(actionRequest, actionResponse);
199 }
200 }
201
202 public void serveResource(
203 ResourceRequest resourceRequest, ResourceResponse resourceResponse)
204 throws IOException, PortletException {
205
206 String jspPage = resourceRequest.getParameter("jspPage");
207
208 if (jspPage != null) {
209 include(
210 jspPage, resourceRequest, resourceResponse,
211 PortletRequest.RESOURCE_PHASE);
212 }
213 else {
214 super.serveResource(resourceRequest, resourceResponse);
215 }
216 }
217
218 protected boolean callActionMethod(
219 ActionRequest request, ActionResponse response)
220 throws PortletException {
221
222 if ((_actionCommandCache == null) || _actionCommandCache.isEmpty()) {
223 return super.callActionMethod(request, response);
224 }
225
226 String actionName = ParamUtil.getString(
227 request, ActionRequest.ACTION_NAME);
228
229 if (!actionName.contains(StringPool.COMMA)) {
230 ActionCommand actionCommand = _actionCommandCache.getActionCommand(
231 actionName);
232
233 if (actionCommand != ActionCommandCache.EMPTY) {
234 return actionCommand.processCommand(request, response);
235 }
236 }
237 else {
238 List<ActionCommand> actionCommands =
239 _actionCommandCache.getActionCommandChain(actionName);
240
241 if (actionCommands.isEmpty()) {
242 return false;
243 }
244
245 for (ActionCommand actionCommand : actionCommands) {
246 if (!actionCommand.processCommand(request, response)) {
247 return false;
248 }
249 }
250
251 return true;
252 }
253
254 return false;
255 }
256
257 protected void checkJSPPath(String path) throws PortletException {
258 if (!path.startsWith(jspPath) ||
259 path.contains(StringPool.DOUBLE_PERIOD) ||
260 !PortalUtil.isValidResourceId(path)) {
261
262 throw new PortletException(
263 "Path " + path + " is not accessible by this portlet");
264 }
265 }
266
267 protected void doDispatch(
268 RenderRequest renderRequest, RenderResponse renderResponse)
269 throws IOException, PortletException {
270
271 String jspPage = renderRequest.getParameter("jspPage");
272
273 if (jspPage != null) {
274 if (!isProcessRenderRequest(renderRequest)) {
275 renderRequest.setAttribute(
276 WebKeys.PORTLET_DECORATE, Boolean.FALSE);
277
278 return;
279 }
280
281 WindowState windowState = renderRequest.getWindowState();
282
283 if (windowState.equals(WindowState.MINIMIZED)) {
284 return;
285 }
286
287 include(jspPage, renderRequest, renderResponse);
288 }
289 else {
290 super.doDispatch(renderRequest, renderResponse);
291 }
292 }
293
294 protected void include(
295 String path, ActionRequest actionRequest,
296 ActionResponse actionResponse)
297 throws IOException, PortletException {
298
299 include(
300 path, actionRequest, actionResponse, PortletRequest.ACTION_PHASE);
301 }
302
303 protected void include(
304 String path, EventRequest eventRequest, EventResponse eventResponse)
305 throws IOException, PortletException {
306
307 include(path, eventRequest, eventResponse, PortletRequest.EVENT_PHASE);
308 }
309
310 protected void include(
311 String path, PortletRequest portletRequest,
312 PortletResponse portletResponse, String lifecycle)
313 throws IOException, PortletException {
314
315 PortletRequestDispatcher portletRequestDispatcher =
316 getPortletContext().getRequestDispatcher(path);
317
318 if (portletRequestDispatcher == null) {
319 _log.error(path + " is not a valid include");
320 }
321 else {
322 checkJSPPath(path);
323
324 portletRequestDispatcher.include(portletRequest, portletResponse);
325 }
326
327 if (clearRequestParameters) {
328 if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
329 portletResponse.setProperty("clear-request-parameters", "true");
330 }
331 }
332 }
333
334 protected void include(
335 String path, RenderRequest renderRequest,
336 RenderResponse renderResponse)
337 throws IOException, PortletException {
338
339 include(
340 path, renderRequest, renderResponse, PortletRequest.RENDER_PHASE);
341 }
342
343 protected void include(
344 String path, ResourceRequest resourceRequest,
345 ResourceResponse resourceResponse)
346 throws IOException, PortletException {
347
348 include(
349 path, resourceRequest, resourceResponse,
350 PortletRequest.RESOURCE_PHASE);
351 }
352
353 private static Log _log = LogFactoryUtil.getLog(MVCPortlet.class);
354
355 protected ActionCommandCache _actionCommandCache;
356
357 protected String aboutJSP;
358 protected boolean clearRequestParameters;
359 protected String configJSP;
360 protected boolean copyRequestParameters;
361 protected String editDefaultsJSP;
362 protected String editGuestJSP;
363 protected String editJSP;
364 protected String helpJSP;
365 protected String jspPath;
366 protected String previewJSP;
367 protected String printJSP;
368 protected String viewJSP;
369
370 }