1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.portlet;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.servlet.SessionMessages;
19  import com.liferay.portal.kernel.util.MethodCache;
20  import com.liferay.portal.kernel.util.MethodKey;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.kernel.util.WebKeys;
24  
25  import java.io.IOException;
26  
27  import java.lang.reflect.InvocationTargetException;
28  import java.lang.reflect.Method;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.GenericPortlet;
36  import javax.portlet.PortletException;
37  import javax.portlet.PortletMode;
38  import javax.portlet.PortletRequest;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  import javax.portlet.ResourceRequest;
42  import javax.portlet.ResourceResponse;
43  import javax.portlet.WindowState;
44  
45  /**
46   * <a href="LiferayPortlet.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   */
50  public class LiferayPortlet extends GenericPortlet {
51  
52      public void processAction(
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws IOException, PortletException {
55  
56          if (!isProcessActionRequest(actionRequest)) {
57              return;
58          }
59  
60          if (!callActionMethod(actionRequest, actionResponse)) {
61              return;
62          }
63  
64          if (SessionErrors.isEmpty(actionRequest)) {
65              SessionMessages.add(actionRequest, "request_processed");
66          }
67          else {
68              return;
69          }
70  
71          String redirect = ParamUtil.getString(actionRequest, "redirect");
72  
73          if (Validator.isNotNull(redirect)) {
74              actionResponse.sendRedirect(redirect);
75          }
76      }
77  
78      public void serveResource(
79              ResourceRequest resourceRequest, ResourceResponse resourceResponse)
80          throws IOException, PortletException {
81  
82          if (!isProcessResourceRequest(resourceRequest)) {
83              return;
84          }
85  
86          super.serveResource(resourceRequest, resourceResponse);
87      }
88  
89      protected boolean callActionMethod(
90              ActionRequest actionRequest, ActionResponse actionResponse)
91          throws PortletException {
92  
93          String actionName = ParamUtil.getString(
94              actionRequest, ActionRequest.ACTION_NAME);
95  
96          if (Validator.isNull(actionName)) {
97              return false;
98          }
99  
100         try {
101             Method method = MethodCache.get(
102                 _classesMap, _methodsMap, getClass().getName(), actionName,
103                 new Class[] {ActionRequest.class, ActionResponse.class});
104 
105             method.invoke(this, actionRequest, actionResponse);
106 
107             return true;
108         }
109         catch (InvocationTargetException ite) {
110             Throwable cause = ite.getCause();
111 
112             if (cause != null) {
113                 throw new PortletException(cause);
114             }
115             else {
116                 throw new PortletException(ite);
117             }
118         }
119         catch (Exception e) {
120             throw new PortletException(e);
121         }
122     }
123 
124     protected void doDispatch(
125             RenderRequest renderRequest, RenderResponse renderResponse)
126         throws IOException, PortletException {
127 
128         if (!isProcessRenderRequest(renderRequest)) {
129             renderRequest.setAttribute(WebKeys.PORTLET_DECORATE, Boolean.FALSE);
130 
131             return;
132         }
133 
134         WindowState state = renderRequest.getWindowState();
135 
136         if (state.equals(WindowState.MINIMIZED)) {
137             return;
138         }
139 
140         PortletMode mode = renderRequest.getPortletMode();
141 
142         if (mode.equals(PortletMode.VIEW)) {
143             doView(renderRequest, renderResponse);
144         }
145         else if (mode.equals(LiferayPortletMode.ABOUT)) {
146             doAbout(renderRequest, renderResponse);
147         }
148         else if (mode.equals(LiferayPortletMode.CONFIG)) {
149             doConfig(renderRequest, renderResponse);
150         }
151         else if (mode.equals(PortletMode.EDIT)) {
152             doEdit(renderRequest, renderResponse);
153         }
154         else if (mode.equals(LiferayPortletMode.EDIT_DEFAULTS)) {
155             doEditDefaults(renderRequest, renderResponse);
156         }
157         else if (mode.equals(LiferayPortletMode.EDIT_GUEST)) {
158             doEditGuest(renderRequest, renderResponse);
159         }
160         else if (mode.equals(PortletMode.HELP)) {
161             doHelp(renderRequest, renderResponse);
162         }
163         else if (mode.equals(LiferayPortletMode.PREVIEW)) {
164             doPreview(renderRequest, renderResponse);
165         }
166         else if (mode.equals(LiferayPortletMode.PRINT)) {
167             doPrint(renderRequest, renderResponse);
168         }
169         else {
170             throw new PortletException(mode.toString());
171         }
172     }
173 
174     @SuppressWarnings("unused")
175     protected void doAbout(
176             RenderRequest renderRequest, RenderResponse renderResponse)
177         throws IOException, PortletException {
178 
179         throw new PortletException("doAbout method not implemented");
180     }
181 
182     @SuppressWarnings("unused")
183     protected void doConfig(
184             RenderRequest renderRequest, RenderResponse renderResponse)
185         throws IOException, PortletException {
186 
187         throw new PortletException("doConfig method not implemented");
188     }
189 
190     @SuppressWarnings("unused")
191     protected void doEditDefaults(
192             RenderRequest renderRequest, RenderResponse renderResponse)
193         throws IOException, PortletException {
194 
195         throw new PortletException("doEditDefaults method not implemented");
196     }
197 
198     @SuppressWarnings("unused")
199     protected void doEditGuest(
200             RenderRequest renderRequest, RenderResponse renderResponse)
201         throws IOException, PortletException {
202 
203         throw new PortletException("doEditGuest method not implemented");
204     }
205 
206     @SuppressWarnings("unused")
207     protected void doPreview(
208             RenderRequest renderRequest, RenderResponse renderResponse)
209         throws IOException, PortletException {
210 
211         throw new PortletException("doPreview method not implemented");
212     }
213 
214     @SuppressWarnings("unused")
215     protected void doPrint(
216             RenderRequest renderRequest, RenderResponse renderResponse)
217         throws IOException, PortletException {
218 
219         throw new PortletException("doPrint method not implemented");
220     }
221 
222     protected boolean isProcessActionRequest(ActionRequest actionRequest) {
223         return isProcessPortletRequest(actionRequest);
224     }
225 
226     protected boolean isProcessPortletRequest(PortletRequest portletRequest) {
227         return _PROCESS_PORTLET_REQUEST;
228     }
229 
230     protected boolean isProcessRenderRequest(RenderRequest renderRequest) {
231         return isProcessPortletRequest(renderRequest);
232     }
233 
234     protected boolean isProcessResourceRequest(
235         ResourceRequest resourceRequest) {
236 
237         return isProcessPortletRequest(resourceRequest);
238     }
239 
240     private static final boolean _PROCESS_PORTLET_REQUEST = true;
241 
242     private Map<String, Class<?>> _classesMap = new HashMap<String, Class<?>>();
243     private Map<MethodKey, Method> _methodsMap =
244         new HashMap<MethodKey, Method>();
245 
246 }