1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.request;
18
19 import java.security.Principal;
20 import java.util.Enumeration;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Locale;
24 import java.util.Map;
25
26 import javax.security.auth.Subject;
27 import javax.servlet.ServletConfig;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletRequestWrapper;
30 import javax.servlet.http.HttpServletResponse;
31 import javax.servlet.http.HttpSession;
32
33 import org.apache.jetspeed.Jetspeed;
34 import org.apache.jetspeed.PortalReservedParameters;
35 import org.apache.jetspeed.aggregator.ContentDispatcher;
36 import org.apache.jetspeed.aggregator.ContentDispatcherCtrl;
37 import org.apache.jetspeed.capabilities.CapabilityMap;
38 import org.apache.jetspeed.container.url.PortalURL;
39 import org.apache.jetspeed.engine.servlet.ServletRequestFactory;
40 import org.apache.jetspeed.engine.servlet.ServletResponseFactory;
41 import org.apache.jetspeed.om.common.MutableLanguage;
42 import org.apache.jetspeed.om.impl.LanguageImpl;
43 import org.apache.jetspeed.om.page.ContentPage;
44 import org.apache.jetspeed.om.page.ContentPageImpl;
45 import org.apache.jetspeed.pipeline.Pipeline;
46 import org.apache.jetspeed.portalsite.PortalSiteRequestContext;
47 import org.apache.jetspeed.portalsite.PortalSiteSessionContext;
48 import org.apache.jetspeed.profiler.ProfileLocator;
49 import org.apache.jetspeed.profiler.Profiler;
50 import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
51 import org.apache.jetspeed.security.SecurityHelper;
52 import org.apache.jetspeed.security.UserPrincipal;
53 import org.apache.jetspeed.userinfo.UserInfoManager;
54 import org.apache.pluto.om.common.Language;
55 import org.apache.pluto.om.common.LanguageSet;
56 import org.apache.pluto.om.common.ObjectID;
57 import org.apache.pluto.om.portlet.PortletDefinition;
58 import org.apache.pluto.om.window.PortletWindow;
59
60 /***
61 * Jetspeed Request Context is associated with each portal request. The request
62 * holds the contextual information shared amongst components in the portal,
63 * accessed through a common valve pipeline.
64 *
65 * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor </a>
66 * @version $Id: JetspeedRequestContext.java,v 1.19 2004/05/25 01:37:12 taylor
67 * Exp $
68 */
69 public class JetspeedRequestContext implements RequestContext
70 {
71 private static final String ACTION_ERROR_ATTR = "org.apache.jetspeed.action.error:";
72 private HttpServletRequest request;
73 private HttpServletResponse response;
74 private ServletConfig config;
75 private HttpSession session;
76 private Map locators;
77 private ContentPage page;
78 private PortletDefinition portletDefinition;
79 private Subject subject;
80 private Locale locale;
81 private ContentDispatcher dispatcher;
82 private Pipeline pipeline;
83
84 private CapabilityMap capabilityMap;
85 private String mimeType;
86 private String mediaType;
87 private PortalURL url;
88 private PortletWindow actionWindow;
89 private String encoding;
90 private String requestPath = null;
91 /*** The user info manager. */
92 private UserInfoManager userInfoMgr;
93 private Map requestsForWindows;
94 private Map responsesForWindows;
95 private final Map objects;
96
97 /***
98 * Create a new Request Context
99 *
100 * @param pc
101 * @param request
102 * @param response
103 * @param config
104 */
105 public JetspeedRequestContext( HttpServletRequest request, HttpServletResponse response, ServletConfig config,
106 UserInfoManager userInfoMgr )
107 {
108 this(request, response, config, userInfoMgr, new HashMap());
109 }
110
111 public JetspeedRequestContext( HttpServletRequest request, HttpServletResponse response, ServletConfig config,
112 UserInfoManager userInfoMgr, Map objects)
113 {
114 this.request = request;
115 this.response = response;
116 this.config = config;
117 this.session = request.getSession();
118 this.userInfoMgr = userInfoMgr;
119 this.requestsForWindows = new HashMap();
120 this.responsesForWindows = new HashMap();
121 this.objects = objects;
122
123
124 if (null != this.request)
125 {
126 this.request.setAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE, this);
127 PortalRequestFactory prf = null;
128 try
129 {
130 prf = (PortalRequestFactory)Jetspeed.getComponentManager().getComponent(PortalRequestFactory.class);
131 }
132 catch (Throwable t)
133 {
134
135 }
136 if ( prf != null )
137 {
138 this.request = prf.createPortalRequest(this.request);
139 }
140 else
141 {
142
143
144
145
146 this.request = new HttpServletRequestWrapper(this.request);
147 }
148 }
149 }
150
151 public HttpServletRequest getRequest()
152 {
153 return request;
154 }
155
156 public HttpServletResponse getResponse()
157 {
158 return response;
159 }
160
161 public ServletConfig getConfig()
162 {
163 return config;
164 }
165
166 public Map getProfileLocators()
167 {
168 return locators;
169 }
170
171 public void setProfileLocators( Map locators )
172 {
173 this.locators = locators;
174 }
175
176 public ContentPage getPage()
177 {
178 return this.page;
179 }
180
181 public void setPage( ContentPage page )
182 {
183 this.page = page;
184 }
185
186 public PortletDefinition getPortletDefinition()
187 {
188 return portletDefinition;
189 }
190
191 public void setPortletDefinition( PortletDefinition portletDefinition )
192 {
193 this.portletDefinition = portletDefinition;
194 }
195
196 public ContentDispatcher getContentDispatcher()
197 {
198 return dispatcher;
199 }
200
201 public void setContentDispatcher( ContentDispatcher dispatcher )
202 {
203 this.dispatcher = dispatcher;
204 }
205
206 /***
207 * get the Capability Map
208 *
209 */
210 public CapabilityMap getCapabilityMap()
211 {
212 return this.capabilityMap;
213 }
214
215 /***
216 * Set the Mimetype. Used by the CapabilityValve
217 *
218 * @param mimeType
219 */
220 public void setMimeType( String mimeType )
221 {
222 this.mimeType = mimeType;
223 }
224
225 /***
226 * get the mimeType for the request
227 *
228 */
229 public String getMimeType()
230 {
231 return this.mimeType;
232 }
233
234 /***
235 * Set the mediaType. Used by the CapabilityValve
236 *
237 * @param mediaType
238 */
239 public void setMediaType( String mediaType )
240 {
241 this.mediaType = mediaType;
242 }
243
244 /***
245 * get the Media Type
246 *
247 */
248 public String getMediaType()
249 {
250 return this.mediaType;
251 }
252
253 /***
254 * Get the target Portlet Action Window
255 *
256 * @return PortletWindow The target portlet window
257 */
258 public PortletWindow getActionWindow()
259 {
260 return actionWindow;
261 }
262
263 /***
264 * Sets the target Portlet Action Window
265 *
266 * @param window
267 */
268 public void setActionWindow( PortletWindow portletWindow )
269 {
270 this.actionWindow = portletWindow;
271 }
272
273
274 /***
275 * Set the capabilityMap. Used by the CapabilityValve
276 *
277 * @param capabilityMap
278 */
279 public void setCapabilityMap( CapabilityMap map )
280 {
281 this.capabilityMap = map;
282 }
283
284 /***
285 * get the character encoding
286 *
287 *
288 */
289 public String getCharacterEncoding()
290 {
291 return this.encoding;
292 }
293
294 /***
295 * set character encoding
296 *
297 * @param enc
298 */
299 public void setCharacterEncoding( String enc )
300 {
301 String preferedEnc = (String) session.getAttribute(PortalReservedParameters.PREFERED_CHARACTERENCODING_ATTRIBUTE);
302
303 if (preferedEnc == null || !enc.equals(preferedEnc))
304 {
305 request.setAttribute(PortalReservedParameters.PREFERED_CHARACTERENCODING_ATTRIBUTE, enc);
306 }
307
308 this.encoding = enc;
309 }
310
311 /***
312 * <p>
313 * getRequestForWindow
314 * </p>
315 *
316 * @see org.apache.jetspeed.request.RequestContext#getRequestForWindow(org.apache.pluto.om.window.PortletWindow)
317 * @param window
318 * @return
319 */
320 public HttpServletRequest getRequestForWindow( PortletWindow window )
321 {
322 if (!requestsForWindows.containsKey(window.getId()))
323 {
324 ServletRequestFactory reqFac = (ServletRequestFactory) Jetspeed.getEngine().getFactory(
325 javax.servlet.http.HttpServletRequest.class);
326 HttpServletRequest requestWrapper = reqFac.getServletRequest(request, window);
327 requestsForWindows.put(window.getId(), requestWrapper);
328 return requestWrapper;
329 }
330 else
331 {
332 return (HttpServletRequest) requestsForWindows.get(window.getId());
333 }
334
335 }
336
337 /***
338 * <p>
339 * getResponseForWindow
340 * </p>
341 *
342 * @see org.apache.jetspeed.request.RequestContext#getResponseForWindow(org.apache.pluto.om.window.PortletWindow)
343 * @param window
344 * @return
345 */
346 public HttpServletResponse getResponseForWindow( PortletWindow window )
347 {
348 HttpServletResponse wrappedResponse = null;
349
350 if (!responsesForWindows.containsKey(window.getId()))
351 {
352 if (getContentDispatcher() != null)
353 {
354 wrappedResponse = ((ContentDispatcherCtrl) getContentDispatcher()).getResponseForWindow(window, this);
355 }
356 else
357 {
358 ServletResponseFactory rspFac = (ServletResponseFactory) Jetspeed.getEngine().getFactory(
359 HttpServletResponse.class);
360 wrappedResponse = rspFac.getServletResponse(this.response);
361
362 }
363
364 responsesForWindows.put(window.getId(), wrappedResponse);
365 return wrappedResponse;
366
367 }
368 else
369 {
370 return (HttpServletResponse) responsesForWindows.get(window.getId());
371 }
372 }
373
374 /***
375 * @see org.apache.jetspeed.request.RequestContext#getSubject()
376 */
377 public Subject getSubject()
378 {
379 return this.subject;
380 }
381
382 public Principal getUserPrincipal()
383 {
384 return SecurityHelper.getBestPrincipal(getSubject(), UserPrincipal.class);
385 }
386
387 /***
388 * @see org.apache.jetspeed.request.RequestContext#setSubject(javax.security.auth.Subject)
389 */
390 public void setSubject( Subject subject )
391 {
392 this.subject = subject;
393 }
394
395 /***
396 * @see org.apache.jetspeed.request.RequestContext#getLocale()
397 */
398 public Locale getLocale()
399 {
400 return this.locale;
401 }
402
403 /***
404 * @see org.apache.jetspeed.request.RequestContext#setLocale(java.util.Locale)
405 */
406 public void setLocale( Locale locale )
407 {
408 Locale preferedLocale = (Locale) session.getAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
409
410 if (preferedLocale == null || !locale.equals(preferedLocale))
411 {
412
413
414
415 session.setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
416 request.setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
417 }
418
419 this.locale = locale;
420 }
421
422 /***
423 * @see org.apache.jetspeed.request.RequestContext#getRequestParameter(java.lang.String)
424 */
425 public String getRequestParameter( String key )
426 {
427 return request.getParameter(key);
428 }
429
430 public void setRequestParameter(String key, String value)
431 {
432 request.getParameterMap().put(key, value);
433 }
434
435 /***
436 * @see org.apache.jetspeed.request.RequestContext#getParameterMap()
437 */
438 public Map getParameterMap()
439 {
440 return request.getParameterMap();
441 }
442
443 /***
444 * @see org.apache.jetspeed.request.RequestContext#getRequestAttribute(java.lang.String)
445 */
446 public Object getRequestAttribute( String key )
447 {
448 return request.getAttribute(key);
449 }
450
451 /***
452 * @see org.apache.jetspeed.request.RequestContext#getSessionAttribute(java.lang.String)
453 */
454 public Object getSessionAttribute( String key )
455 {
456 return session.getAttribute(key);
457 }
458
459 /***
460 * @see org.apache.jetspeed.request.RequestContext#setSessionAttribute(java.lang.String,
461 * java.lang.Object)
462 */
463 public void setSessionAttribute( String key, Object value )
464 {
465 session.setAttribute(key, value);
466 }
467
468 /***
469 * @see org.apache.jetspeed.request.RequestContext#setAttribute(java.lang.String,
470 * java.lang.Object)
471 */
472 public void setAttribute( String key, Object value )
473 {
474 request.setAttribute(key, value);
475 }
476
477 /***
478 * @see org.apache.jetspeed.request.RequestContext#getAttribute(java.lang.String)
479 */
480 public Object getAttribute( String key )
481 {
482 return request.getAttribute(key);
483 }
484
485 /***
486 * @see org.apache.jetspeed.request.RequestContext#getPath()
487 */
488 public String getPath()
489 {
490 if (this.requestPath == null)
491 {
492 this.requestPath = getPortalURL().getPath();
493 }
494 return this.requestPath;
495 }
496
497 public void setPortalURL(PortalURL url)
498 {
499 if ( this.url != null )
500 throw new IllegalStateException("PortalURL already set");
501 if ( url == null )
502 throw new IllegalArgumentException("PortalURL may not be nullified");
503 this.url = url;
504 }
505
506 public PortalURL getPortalURL()
507 {
508 return url;
509 }
510
511 /***
512 * @see org.apache.jetspeed.request.RequestContext#getUserInfoMap(org.apache.pluto.om.common.ObjectID)
513 */
514 public Map getUserInfoMap( ObjectID oid )
515 {
516 return userInfoMgr.getUserInfoMap(oid, this);
517 }
518
519 /***
520 *
521 * <p>
522 * getPreferedLanguage
523 * </p>
524 *
525 * @see org.apache.jetspeed.request.RequestContext#getPreferedLanguage(org.apache.pluto.om.portlet.PortletDefinition)
526 * @param portlet
527 * @return
528 */
529 public Language getPreferedLanguage( PortletDefinition portlet )
530 {
531
532
533
534
535
536
537
538
539
540
541 LanguageSet languageSet = portlet.getLanguageSet();
542 Language language = languageSet.get(locale);
543
544 Enumeration locales = request.getLocales();
545 while (locales.hasMoreElements() && language == null)
546 {
547 Locale aLocale = (Locale) locales.nextElement();
548 language = languageSet.get(aLocale);
549 }
550
551 Iterator langItr = languageSet.iterator();
552 if (langItr.hasNext() && language == null)
553 {
554 language = (Language) langItr.next();
555 }
556
557 if (language == null)
558 {
559 language = languageSet.get(languageSet.getDefaultLocale());
560 }
561
562 if (language == null)
563 {
564 MutableLanguage languageCtl = new LanguageImpl();
565 languageCtl.setLocale(locale);
566 languageCtl.setShortTitle(portlet.getName());
567 languageCtl.setTitle(portlet.getName());
568 language = languageCtl;
569 }
570
571
572 return language;
573 }
574
575 /***
576 * <p>
577 * setPath
578 * </p>
579 *
580 * @see org.apache.jetspeed.request.RequestContext#setPath(java.lang.String)
581 * @param path
582 */
583 public void setPath( String path )
584 {
585 this.requestPath = path;
586 }
587
588
589
590
591
592 public Throwable popActionFailure(PortletWindow window)
593 {
594
595 String key = ACTION_ERROR_ATTR + window.getId();
596 Throwable t = (Throwable) session.getAttribute(key);
597 session.removeAttribute(key);
598 return t;
599
600 }
601
602
603
604
605
606
607
608 public void setActionFailure(PortletWindow window, Throwable actionFailure)
609 {
610 setSessionAttribute(ACTION_ERROR_ATTR + window.getId(),
611 actionFailure);
612 }
613
614 /***
615 * Get the current executing pipeline
616 *
617 * @return Pipeline
618 */
619 public Pipeline getPipeline()
620 {
621 return pipeline;
622 }
623
624
625 /***
626 * Set the current pipeline
627 * @param pipeline
628 */
629 public void setPipeline(Pipeline pipeline)
630 {
631 this.pipeline = pipeline;
632 }
633
634
635 /***
636 * @param request The request to set.
637 */
638 public void setRequest(HttpServletRequest request)
639 {
640 this.request = request;
641 }
642
643 /***
644 * @param response The request to set.
645 */
646 public void setResponse(HttpServletResponse response)
647 {
648 this.response = response;
649 }
650
651 public ContentPage locatePage(Profiler profiler, String nonProfiledPath)
652 {
653 try
654 {
655 String pathSave = this.getPath();
656 this.setPath(nonProfiledPath);
657 ContentPage realPage = this.getPage();
658 this.setPage(null);
659 Map locators = null;
660 ProfileLocator locator = profiler.getProfile(this, ProfileLocator.PAGE_LOCATOR);
661 if ( locator != null )
662 {
663 locators = new HashMap();
664 locators.put(ProfileLocator.PAGE_LOCATOR, locator);
665 }
666 PortalSiteSessionContext sessionContext = (PortalSiteSessionContext)getSessionAttribute(ProfilerValveImpl.PORTAL_SITE_SESSION_CONTEXT_ATTR_KEY);
667 PortalSiteRequestContext requestContext = sessionContext.newRequestContext(locators, true, true);
668 ContentPage cpage = new ContentPageImpl(requestContext.getManagedPage());
669
670 this.setPage(realPage);
671 this.setPath(pathSave);
672 return cpage;
673 }
674 catch (Throwable t)
675 {
676 t.printStackTrace();
677 }
678 return null;
679 }
680
681 public Map getObjects()
682 {
683 return objects;
684 }
685 }