1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.portlet;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.servlet.SessionMessages;
27  import com.liferay.portal.kernel.util.MethodCache;
28  import com.liferay.portal.kernel.util.MethodKey;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  
32  import java.io.IOException;
33  
34  import java.lang.reflect.InvocationTargetException;
35  import java.lang.reflect.Method;
36  
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.GenericPortlet;
43  import javax.portlet.PortletException;
44  import javax.portlet.PortletMode;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  import javax.portlet.WindowState;
48  
49  /**
50   * <a href="LiferayPortlet.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class LiferayPortlet extends GenericPortlet {
56  
57      public void processAction(
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws IOException, PortletException {
60  
61          if (!callActionMethod(actionRequest, actionResponse)) {
62              return;
63          }
64  
65          if (SessionErrors.isEmpty(actionRequest)) {
66              SessionMessages.add(actionRequest, "request_processed");
67          }
68  
69          String redirect = ParamUtil.getString(actionRequest, "redirect");
70  
71          if (Validator.isNotNull(redirect)) {
72              actionResponse.sendRedirect(redirect);
73          }
74      }
75  
76      protected boolean callActionMethod(
77              ActionRequest actionRequest, ActionResponse actionResponse)
78          throws IOException, PortletException {
79  
80          String actionName = ParamUtil.getString(
81              actionRequest, ActionRequest.ACTION_NAME);
82  
83          if (Validator.isNull(actionName)) {
84              return false;
85          }
86  
87          try {
88              Method method = MethodCache.get(
89                  _classesMap, _methodsMap, getClass().getName(), actionName,
90                  new Class[] {ActionRequest.class, ActionResponse.class});
91  
92              method.invoke(this, actionRequest, actionResponse);
93  
94              return true;
95          }
96          catch (InvocationTargetException ite) {
97              Throwable cause = ite.getCause();
98  
99              if (cause != null) {
100                 throw new PortletException(cause);
101             }
102             else {
103                 throw new PortletException(ite);
104             }
105         }
106         catch (Exception e) {
107             throw new PortletException(e);
108         }
109     }
110 
111     protected void doDispatch(
112             RenderRequest renderRequest, RenderResponse renderResponse)
113         throws IOException, PortletException {
114 
115         WindowState state = renderRequest.getWindowState();
116 
117         if (!state.equals(WindowState.MINIMIZED)) {
118             PortletMode mode = renderRequest.getPortletMode();
119 
120             if (mode.equals(PortletMode.VIEW)) {
121                 doView(renderRequest, renderResponse);
122             }
123             else if (mode.equals(LiferayPortletMode.ABOUT)) {
124                 doAbout(renderRequest, renderResponse);
125             }
126             else if (mode.equals(LiferayPortletMode.CONFIG)) {
127                 doConfig(renderRequest, renderResponse);
128             }
129             else if (mode.equals(PortletMode.EDIT)) {
130                 doEdit(renderRequest, renderResponse);
131             }
132             else if (mode.equals(LiferayPortletMode.EDIT_DEFAULTS)) {
133                 doEditDefaults(renderRequest, renderResponse);
134             }
135             else if (mode.equals(LiferayPortletMode.EDIT_GUEST)) {
136                 doEditGuest(renderRequest, renderResponse);
137             }
138             else if (mode.equals(PortletMode.HELP)) {
139                 doHelp(renderRequest, renderResponse);
140             }
141             else if (mode.equals(LiferayPortletMode.PREVIEW)) {
142                 doPreview(renderRequest, renderResponse);
143             }
144             else if (mode.equals(LiferayPortletMode.PRINT)) {
145                 doPrint(renderRequest, renderResponse);
146             }
147             else {
148                 throw new PortletException(mode.toString());
149             }
150         }
151     }
152 
153     protected void doAbout(
154             RenderRequest renderRequest, RenderResponse renderResponse)
155         throws IOException, PortletException {
156 
157         throw new PortletException("doAbout method not implemented");
158     }
159 
160     protected void doConfig(
161             RenderRequest renderRequest, RenderResponse renderResponse)
162         throws IOException, PortletException {
163 
164         throw new PortletException("doConfig method not implemented");
165     }
166 
167     protected void doEditDefaults(
168             RenderRequest renderRequest, RenderResponse renderResponse)
169         throws IOException, PortletException {
170 
171         throw new PortletException("doEditDefaults method not implemented");
172     }
173 
174     protected void doEditGuest(
175             RenderRequest renderRequest, RenderResponse renderResponse)
176         throws IOException, PortletException {
177 
178         throw new PortletException("doEditGuest method not implemented");
179     }
180 
181     protected void doPreview(
182             RenderRequest renderRequest, RenderResponse renderResponse)
183         throws IOException, PortletException {
184 
185         throw new PortletException("doPreview method not implemented");
186     }
187 
188     protected void doPrint(
189             RenderRequest renderRequest, RenderResponse renderResponse)
190         throws IOException, PortletException {
191 
192         throw new PortletException("doPrint method not implemented");
193     }
194 
195     private Map<String, Class<?>> _classesMap = new HashMap<String, Class<?>>();
196     private Map<MethodKey, Method> _methodsMap =
197         new HashMap<MethodKey, Method>();
198 
199 }