1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.pluto.core;
21
22 import javax.portlet.*;
23
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import java.io.*;
28 import java.util.*;
29
30 import org.apache.pluto.factory.PortletObjectAccess;
31 import org.apache.pluto.om.*;
32 import org.apache.pluto.om.portlet.*;
33 import org.apache.pluto.services.information.InformationProviderAccess;
34
35 public class PortletServlet extends HttpServlet
36 {
37
38 private boolean portletInitialized = false;
39 private Portlet portletClass = null;
40
41 private PortletContext portletContext;
42 private PortletConfig portletConfig;
43
44
45 public void init(ServletConfig config) throws ServletException
46 {
47 super.init(config);
48 portletInitialized = false;
49
50 String classString = config.getInitParameter("portlet-class");
51 try
52 {
53 portletClass = (Portlet)Thread.currentThread().getContextClassLoader().loadClass(classString).newInstance();
54 }
55 catch (ClassNotFoundException e)
56 {
57 throw new ServletException(e);
58 }
59 catch (IllegalAccessException e)
60 {
61 throw new ServletException(e);
62 }
63 catch (InstantiationException e)
64 {
65 throw new ServletException(e);
66 }
67
68
69
70
71
72 String portletGUID = config.getInitParameter("portlet-guid");
73 PortletDefinition portletDefinition =
74 InformationProviderAccess.getStaticProvider().getPortletDefinition(org.apache.pluto.util.ObjectIDAccess.createObjectID(portletGUID));
75 if (portletDefinition==null)
76 {
77 throw new ServletException("portlet definition not found from GUID: " + portletGUID);
78 }
79 else
80 {
81 PortletDefinitionCtrl portletDefCtrl = (PortletDefinitionCtrl)ControllerObjectAccess.get(portletDefinition);
82 portletDefCtrl.setPortletClassLoader(Thread.currentThread().getContextClassLoader());
83 }
84
85 portletContext = PortletObjectAccess.getPortletContext(config.getServletContext(),
86 portletDefinition.getPortletApplicationDefinition());
87 portletConfig = PortletObjectAccess.getPortletConfig(config,
88 portletContext,
89 portletDefinition);
90
91 try
92 {
93 portletClass.init(portletConfig);
94 }
95 catch (PortletException e)
96 {
97 throw new ServletException(e);
98 }
99
100 portletInitialized = true;
101
102 }
103
104 public void init() throws ServletException
105 {
106 }
107
108 public final ServletConfig getServletConfig ()
109 {
110 return super.getServletConfig();
111 }
112
113 public final String getInitParameter(String name)
114 {
115 return getServletConfig().getInitParameter(name);
116 }
117
118 public final Enumeration getInitParameterNames()
119 {
120 return getServletConfig().getInitParameterNames();
121 }
122
123 public ServletContext getServletContext()
124 {
125 return getServletConfig().getServletContext();
126 }
127
128 protected long getLastModified(HttpServletRequest req)
129 {
130 return -1;
131 }
132
133 public String getServletInfo()
134 {
135 return "";
136 }
137
138 public final void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
139 {
140 super.service(request,response);
141 }
142
143 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
144 {
145 dispatch(req,resp);
146 }
147
148 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
149 {
150 dispatch(req,resp);
151 }
152
153 protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
154 {
155 dispatch(req,resp);
156 }
157
158 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
159 {
160 super.doDelete(req,resp);
161 }
162
163 protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
164 {
165 super.doOptions(req,resp);
166 }
167
168 protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
169 {
170 super.doTrace(req,resp);
171 }
172
173 public void destroy()
174 {
175 if (portletClass != null)
176 {
177 portletClass.destroy();
178 }
179 super.destroy();
180 }
181
182
183
184 private void dispatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
185 {
186 if (!portletInitialized)
187 {
188 throw new ServletException("this portlet uses the <load-on-startup> flag. You have to turn it off");
189 }
190 try
191 {
192
193 request.setAttribute(org.apache.pluto.Constants.PORTLET_CONFIG, portletConfig);
194
195 Integer method_id = (Integer)request.getAttribute(org.apache.pluto.Constants.METHOD_ID);
196 if (method_id == org.apache.pluto.Constants.METHOD_RENDER)
197 {
198 RenderRequest renderRequest = (RenderRequest)request.getAttribute(org.apache.pluto.Constants.PORTLET_REQUEST);
199 RenderResponse renderResponse = (RenderResponse)request.getAttribute(org.apache.pluto.Constants.PORTLET_RESPONSE);
200
201
202 prepareRenderRequest(renderRequest, request);
203 prepareRenderResponse(renderResponse, request, response);
204
205 portletClass.render(renderRequest,renderResponse);
206 }
207 else if (method_id==org.apache.pluto.Constants.METHOD_ACTION)
208 {
209 ActionRequest actionRequest = (ActionRequest)request.getAttribute(org.apache.pluto.Constants.PORTLET_REQUEST);
210 ActionResponse actionResponse = (ActionResponse)request.getAttribute(org.apache.pluto.Constants.PORTLET_RESPONSE);
211
212
213 prepareActionRequest(actionRequest, request);
214 prepareActionResponse(actionResponse, request, response);
215
216 portletClass.processAction(actionRequest,actionResponse);
217 }
218 else if (method_id == org.apache.pluto.Constants.METHOD_NOOP)
219 {
220
221 }
222
223 }
224 catch (javax.portlet.UnavailableException e)
225 {
226
227
228
229
230
231
232
233 try
234 {
235 portletClass.destroy();
236 }
237 catch (Throwable t)
238 {
239
240 }
241
242
243 throw new javax.servlet.UnavailableException(e.getMessage());
244 }
245 catch (PortletException e)
246 {
247 throw new ServletException(e);
248 }
249 finally
250 {
251 request.removeAttribute(org.apache.pluto.Constants.PORTLET_CONFIG);
252 }
253
254 }
255
256 private void prepareActionRequest(ActionRequest portletRequest,
257 HttpServletRequest servletRequest)
258 {
259 InternalPortletRequest internalPortletRequest =
260 CoreUtils.getInternalRequest(portletRequest);
261
262 internalPortletRequest.lateInit(servletRequest);
263 }
264
265 private void prepareRenderRequest(RenderRequest portletRequest,
266 HttpServletRequest servletRequest)
267 {
268 InternalPortletRequest internalPortletRequest =
269 CoreUtils.getInternalRequest(portletRequest);
270
271 internalPortletRequest.lateInit(servletRequest);
272 }
273
274 private void prepareRenderResponse(RenderResponse portletResponse,
275 HttpServletRequest servletRequest,
276 HttpServletResponse servletResponse)
277 {
278 InternalPortletResponse internalPortletResponse =
279 CoreUtils.getInternalResponse(portletResponse);
280
281 internalPortletResponse.lateInit(servletRequest, servletResponse);
282 }
283
284 private void prepareActionResponse(ActionResponse portletResponse,
285 HttpServletRequest servletRequest,
286 HttpServletResponse servletResponse)
287 {
288 InternalPortletResponse internalPortletResponse =
289 CoreUtils.getInternalResponse(portletResponse);
290
291 internalPortletResponse.lateInit(servletRequest, servletResponse);
292 }
293
294 }