1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.engine; |
18 |
|
|
19 |
|
import java.io.IOException; |
20 |
|
import java.security.Principal; |
21 |
|
|
22 |
|
import javax.security.auth.Subject; |
23 |
|
import javax.servlet.ServletConfig; |
24 |
|
import javax.servlet.ServletContext; |
25 |
|
import javax.servlet.ServletException; |
26 |
|
import javax.servlet.http.HttpServlet; |
27 |
|
import javax.servlet.http.HttpServletRequest; |
28 |
|
import javax.servlet.http.HttpServletResponse; |
29 |
|
import javax.servlet.http.HttpSessionEvent; |
30 |
|
import javax.servlet.http.HttpSessionListener; |
31 |
|
|
32 |
|
import org.apache.commons.configuration.Configuration; |
33 |
|
import org.apache.commons.configuration.PropertiesConfiguration; |
34 |
|
import org.apache.commons.lang.exception.ExceptionUtils; |
35 |
|
import org.apache.commons.logging.Log; |
36 |
|
import org.apache.commons.logging.LogFactory; |
37 |
|
import org.apache.jetspeed.Jetspeed; |
38 |
|
import org.apache.jetspeed.PortalReservedParameters; |
39 |
|
import org.apache.jetspeed.cache.ContentCacheKeyGenerator; |
40 |
|
import org.apache.jetspeed.cache.JetspeedCache; |
41 |
|
import org.apache.jetspeed.components.ComponentManager; |
42 |
|
import org.apache.jetspeed.components.SpringComponentManager; |
43 |
|
import org.apache.jetspeed.components.factorybeans.ServletConfigFactoryBean; |
44 |
|
import org.apache.jetspeed.container.session.PortalSessionsManager; |
45 |
|
import org.apache.jetspeed.engine.servlet.ServletHelper; |
46 |
|
import org.apache.jetspeed.exception.JetspeedException; |
47 |
|
import org.apache.jetspeed.pipeline.valve.SecurityValve; |
48 |
|
import org.apache.jetspeed.request.RequestContext; |
49 |
|
import org.apache.jetspeed.request.RequestContextComponent; |
50 |
|
import org.apache.jetspeed.security.SecurityHelper; |
51 |
|
import org.apache.jetspeed.security.UserPrincipal; |
52 |
|
import org.apache.jetspeed.services.JetspeedPortletServices; |
53 |
|
import org.apache.jetspeed.services.PortletServices; |
54 |
|
import org.apache.jetspeed.statistics.PortalStatistics; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
0 |
public class JetspeedServlet |
63 |
|
extends HttpServlet |
64 |
|
implements JetspeedEngineConstants, HttpSessionListener |
65 |
|
{ |
66 |
|
private static Log log; |
67 |
|
private static Log console; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
0 |
private static boolean firstInit = true; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
0 |
private static Throwable initFailure = null; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
0 |
private static boolean firstDoGet = true; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
private static Engine engine; |
90 |
|
private static RequestContextComponent contextComponent; |
91 |
|
|
92 |
|
private static String webappRoot; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
private static final String INIT_START_MSG = "Jetspeed Starting Initialization..."; |
98 |
|
private static final String INIT_DONE_MSG = "Jetspeed Initialization complete, Ready to service requests."; |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
public final void init( ServletConfig config ) throws ServletException |
104 |
|
{ |
105 |
0 |
synchronized (this.getClass()) |
106 |
|
{ |
107 |
0 |
if ( log == null ) |
108 |
|
{ |
109 |
0 |
log = LogFactory.getLog(JetspeedServlet.class); |
110 |
0 |
console = LogFactory.getLog(CONSOLE_LOGGER); |
111 |
|
} |
112 |
|
|
113 |
0 |
console.info(INIT_START_MSG); |
114 |
|
|
115 |
0 |
super.init(config); |
116 |
|
|
117 |
0 |
if (!firstInit) |
118 |
|
{ |
119 |
0 |
log.info("Double initialization of Jetspeed was attempted!"); |
120 |
0 |
console.info("Double initialization of Jetspeed was attempted!"); |
121 |
0 |
return; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
0 |
firstInit = false; |
126 |
|
|
127 |
|
try |
128 |
|
{ |
129 |
|
|
130 |
0 |
ServletContext context = config.getServletContext(); |
131 |
|
|
132 |
0 |
String propertiesFilename = ServletHelper.findInitParameter(context, config, JETSPEED_PROPERTIES_KEY, |
133 |
|
JETSPEED_PROPERTIES_DEFAULT); |
134 |
|
|
135 |
0 |
String applicationRoot = ServletHelper.findInitParameter(context, config, APPLICATION_ROOT_KEY, |
136 |
|
APPLICATION_ROOT_DEFAULT); |
137 |
|
|
138 |
0 |
console.info("JetspeedServlet identifying web application root..."); |
139 |
0 |
webappRoot = config.getServletContext().getRealPath("/"); |
140 |
0 |
console.info("JetspeedServlet identifed web application root as " + webappRoot); |
141 |
|
|
142 |
0 |
if (applicationRoot == null || applicationRoot.equals(WEB_CONTEXT)) |
143 |
|
{ |
144 |
0 |
applicationRoot = webappRoot; |
145 |
|
} |
146 |
|
|
147 |
0 |
Configuration properties = new PropertiesConfiguration(ServletHelper.getRealPath( |
148 |
|
config, propertiesFilename)); |
149 |
|
|
150 |
0 |
properties.setProperty(APPLICATION_ROOT_KEY, applicationRoot); |
151 |
0 |
properties.setProperty(WEBAPP_ROOT_KEY, webappRoot); |
152 |
|
|
153 |
0 |
console.info("JetspeedServlet attempting to create the portlet engine..."); |
154 |
|
|
155 |
0 |
engine = new JetspeedEngine(properties, applicationRoot, config, initializeComponentManager(config, applicationRoot, properties)); |
156 |
|
|
157 |
0 |
console.info("JetspeedServlet attempting to start the Jetspeed Portal Engine..."); |
158 |
0 |
Jetspeed.setEngine(engine); |
159 |
0 |
engine.start(); |
160 |
0 |
console.info("JetspeedServlet has successfuly started the Jetspeed Portal Engine...."); |
161 |
0 |
contextComponent = (RequestContextComponent) Jetspeed.getComponentManager().getComponent(RequestContextComponent.class); |
162 |
|
} |
163 |
0 |
catch (Throwable e) |
164 |
|
{ |
165 |
|
|
166 |
0 |
final String msg = "Jetspeed: init() failed: "; |
167 |
0 |
initFailure = e; |
168 |
0 |
log.fatal(msg, e); |
169 |
0 |
console.fatal(msg, e); |
170 |
0 |
} |
171 |
|
|
172 |
0 |
console.info(INIT_DONE_MSG); |
173 |
0 |
log.info(INIT_DONE_MSG); |
174 |
0 |
} |
175 |
0 |
} |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
public final void init( HttpServletRequest request, HttpServletResponse response ) |
185 |
|
{ |
186 |
0 |
synchronized (JetspeedServlet.class) |
187 |
|
{ |
188 |
0 |
if (firstDoGet) |
189 |
|
{ |
190 |
|
|
191 |
0 |
firstDoGet = false; |
192 |
|
} |
193 |
0 |
} |
194 |
0 |
} |
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
public final void doGet( HttpServletRequest req, HttpServletResponse res ) throws IOException, ServletException |
213 |
|
{ |
214 |
|
try |
215 |
|
{ |
216 |
|
|
217 |
0 |
if (initFailure != null) |
218 |
|
{ |
219 |
0 |
throw new ServletException("Failed to initalize jetspeed. "+initFailure.toString(), initFailure); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
0 |
if (firstDoGet) |
225 |
|
{ |
226 |
0 |
init(req, res); |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
|
231 |
0 |
String wasFiltered = (String) req.getAttribute("org.apache.jetspeed.content.filtered"); |
232 |
0 |
if (wasFiltered == null || !wasFiltered.equals("true")) |
233 |
|
{ |
234 |
|
|
235 |
|
|
236 |
0 |
res.setHeader("Cache-Control", "no-cache,no-store,private"); |
237 |
0 |
res.setHeader("Pragma", "no-cache"); |
238 |
0 |
res.setHeader("Expires", "0"); |
239 |
|
|
240 |
|
|
241 |
0 |
RequestContext context = contextComponent.create(req, res, getServletConfig()); |
242 |
0 |
engine.service(context); |
243 |
0 |
contextComponent.release(context); |
244 |
|
} |
245 |
|
|
246 |
|
} |
247 |
0 |
catch (JetspeedException e) |
248 |
|
{ |
249 |
0 |
final String msg = "Fatal error encountered while processing portal request: "+e.toString(); |
250 |
0 |
log.fatal(msg, e); |
251 |
0 |
throw new ServletException(msg, e); |
252 |
0 |
} |
253 |
0 |
} |
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
public final void doPost( HttpServletRequest req, HttpServletResponse res ) throws IOException, ServletException |
268 |
|
{ |
269 |
0 |
doGet(req, res); |
270 |
0 |
} |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
public final void destroy() |
281 |
|
{ |
282 |
|
try |
283 |
|
{ |
284 |
0 |
Jetspeed.shutdown(); |
285 |
|
} |
286 |
0 |
catch (JetspeedException e) |
287 |
|
{ |
288 |
0 |
log.fatal("Jetspeed: shutdown() failed: ", e); |
289 |
0 |
System.err.println(ExceptionUtils.getStackTrace(e)); |
290 |
0 |
} |
291 |
|
|
292 |
|
|
293 |
0 |
firstInit = true; |
294 |
|
|
295 |
0 |
log.info("Done shutting down!"); |
296 |
0 |
} |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
protected ComponentManager initializeComponentManager(ServletConfig servletConfig, String appRoot, Configuration configuration) throws IOException |
311 |
|
{ |
312 |
0 |
ServletConfigFactoryBean.setServletConfig(servletConfig); |
313 |
0 |
final String assemblyDir = configuration.getString("assembly.dir","/WEB-INF/assembly"); |
314 |
0 |
final String assemblyFileExtension = configuration.getString("assembly.extension",".xml"); |
315 |
|
|
316 |
0 |
String[] bootConfigs = new String[] {"/WEB-INF/assembly/boot/*.xml"}; |
317 |
0 |
String[] appConfigs = new String[] {assemblyDir+"/*"+assemblyFileExtension, assemblyDir+"/override/*"+assemblyFileExtension}; |
318 |
0 |
ServletContext servletContext = servletConfig.getServletContext(); |
319 |
0 |
SpringComponentManager cm = new SpringComponentManager(bootConfigs, appConfigs, servletContext, appRoot); |
320 |
|
|
321 |
0 |
return cm; |
322 |
|
} |
323 |
|
|
324 |
|
public void sessionCreated(HttpSessionEvent se) |
325 |
|
{ |
326 |
0 |
PortletServices services = JetspeedPortletServices.getSingleton(); |
327 |
0 |
if (services != null) |
328 |
|
{ |
329 |
0 |
PortalSessionsManager psm = (PortalSessionsManager)services.getService(PortalSessionsManager.SERVICE_NAME); |
330 |
0 |
if (psm != null) |
331 |
|
{ |
332 |
0 |
psm.portalSessionCreated(se.getSession()); |
333 |
|
} |
334 |
|
} |
335 |
0 |
} |
336 |
|
|
337 |
|
public void sessionDestroyed(HttpSessionEvent se) |
338 |
|
{ |
339 |
0 |
Subject subject = (Subject)se.getSession().getAttribute(PortalReservedParameters.SESSION_KEY_SUBJECT); |
340 |
0 |
if (subject == null) |
341 |
0 |
return; |
342 |
0 |
if (firstInit) |
343 |
|
{ |
344 |
|
|
345 |
|
|
346 |
|
|
347 |
0 |
return; |
348 |
|
} |
349 |
0 |
Principal subjectUserPrincipal = SecurityHelper.getPrincipal(subject, UserPrincipal.class); |
350 |
0 |
PortalStatistics statistics = (PortalStatistics)engine.getComponentManager().getComponent("PortalStatistics"); |
351 |
0 |
long sessionLength = System.currentTimeMillis() - se.getSession().getCreationTime(); |
352 |
0 |
String ipAddress = (String)se.getSession().getAttribute(SecurityValve.IP_ADDRESS); |
353 |
0 |
statistics.logUserLogout(ipAddress, subjectUserPrincipal.getName(), sessionLength); |
354 |
0 |
JetspeedCache portletContentCache = (JetspeedCache)engine.getComponentManager().getComponent("portletContentCache"); |
355 |
0 |
JetspeedCache decorationContentCache = null; |
356 |
|
|
357 |
|
try |
358 |
|
{ |
359 |
0 |
decorationContentCache = (JetspeedCache)engine.getComponentManager().getComponent("decorationContentCache"); |
360 |
|
} |
361 |
0 |
catch (Exception e) |
362 |
|
{ |
363 |
0 |
} |
364 |
|
|
365 |
0 |
ContentCacheKeyGenerator generator = (ContentCacheKeyGenerator)engine.getComponentManager().getComponent("ContentCacheKeyGenerator"); |
366 |
|
|
367 |
0 |
if (generator.isCacheBySessionId()) |
368 |
|
{ |
369 |
0 |
portletContentCache.evictContentForUser(se.getSession().getId()); |
370 |
|
|
371 |
0 |
if (decorationContentCache != null) |
372 |
|
{ |
373 |
0 |
decorationContentCache.evictContentForUser(se.getSession().getId()); |
374 |
|
} |
375 |
|
} |
376 |
|
else |
377 |
|
{ |
378 |
0 |
portletContentCache.evictContentForUser(subjectUserPrincipal.getName()); |
379 |
|
|
380 |
0 |
if (decorationContentCache != null) |
381 |
|
{ |
382 |
0 |
decorationContentCache.evictContentForUser(subjectUserPrincipal.getName()); } |
383 |
|
} |
384 |
0 |
} |
385 |
|
} |