001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.request;
018:
019: import java.security.Principal;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Locale;
024: import java.util.Map;
025:
026: import javax.security.auth.Subject;
027: import javax.servlet.ServletConfig;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletRequestWrapper;
030: import javax.servlet.http.HttpServletResponse;
031: import javax.servlet.http.HttpSession;
032:
033: import org.apache.jetspeed.Jetspeed;
034: import org.apache.jetspeed.PortalReservedParameters;
035: import org.apache.jetspeed.aggregator.ContentDispatcher;
036: import org.apache.jetspeed.aggregator.ContentDispatcherCtrl;
037: import org.apache.jetspeed.capabilities.CapabilityMap;
038: import org.apache.jetspeed.container.url.PortalURL;
039: import org.apache.jetspeed.engine.servlet.ServletRequestFactory;
040: import org.apache.jetspeed.engine.servlet.ServletResponseFactory;
041: import org.apache.jetspeed.om.common.MutableLanguage;
042: import org.apache.jetspeed.om.impl.LanguageImpl;
043: import org.apache.jetspeed.om.page.ContentPage;
044: import org.apache.jetspeed.om.page.ContentPageImpl;
045: import org.apache.jetspeed.pipeline.Pipeline;
046: import org.apache.jetspeed.portalsite.PortalSiteRequestContext;
047: import org.apache.jetspeed.portalsite.PortalSiteSessionContext;
048: import org.apache.jetspeed.profiler.ProfileLocator;
049: import org.apache.jetspeed.profiler.Profiler;
050: import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
051: import org.apache.jetspeed.security.SecurityHelper;
052: import org.apache.jetspeed.security.UserPrincipal;
053: import org.apache.jetspeed.userinfo.UserInfoManager;
054: import org.apache.pluto.om.common.Language;
055: import org.apache.pluto.om.common.LanguageSet;
056: import org.apache.pluto.om.common.ObjectID;
057: import org.apache.pluto.om.portlet.PortletDefinition;
058: import org.apache.pluto.om.window.PortletWindow;
059:
060: /**
061: * Jetspeed Request Context is associated with each portal request. The request
062: * holds the contextual information shared amongst components in the portal,
063: * accessed through a common valve pipeline.
064: *
065: * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor </a>
066: * @version $Id: JetspeedRequestContext.java,v 1.19 2004/05/25 01:37:12 taylor
067: * Exp $
068: */
069: public class JetspeedRequestContext implements RequestContext {
070: private static final String ACTION_ERROR_ATTR = "org.apache.jetspeed.action.error:";
071: private HttpServletRequest request;
072: private HttpServletResponse response;
073: private ServletConfig config;
074: private HttpSession session;
075: private Map locators;
076: private ContentPage page;
077: private PortletDefinition portletDefinition;
078: private Subject subject;
079: private Locale locale;
080: private ContentDispatcher dispatcher;
081: private Pipeline pipeline;
082:
083: private CapabilityMap capabilityMap;
084: private String mimeType;
085: private String mediaType;
086: private PortalURL url;
087: private PortletWindow actionWindow;
088: private String encoding;
089: private String requestPath = null;
090: /** The user info manager. */
091: private UserInfoManager userInfoMgr;
092: private Map requestsForWindows;
093: private Map responsesForWindows;
094: private final Map objects;
095:
096: /**
097: * Create a new Request Context
098: *
099: * @param pc
100: * @param request
101: * @param response
102: * @param config
103: */
104: public JetspeedRequestContext(HttpServletRequest request,
105: HttpServletResponse response, ServletConfig config,
106: UserInfoManager userInfoMgr) {
107: this (request, response, config, userInfoMgr, new HashMap());
108: }
109:
110: public JetspeedRequestContext(HttpServletRequest request,
111: HttpServletResponse response, ServletConfig config,
112: UserInfoManager userInfoMgr, Map objects) {
113: this .request = request;
114: this .response = response;
115: this .config = config;
116: this .session = request.getSession();
117: this .userInfoMgr = userInfoMgr;
118: this .requestsForWindows = new HashMap();
119: this .responsesForWindows = new HashMap();
120: this .objects = objects;
121:
122: // set context in Request for later use
123: if (null != this .request) {
124: this .request.setAttribute(
125: PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE,
126: this );
127: PortalRequestFactory prf = null;
128: try {
129: prf = (PortalRequestFactory) Jetspeed
130: .getComponentManager().getComponent(
131: PortalRequestFactory.class);
132: } catch (Throwable t) {
133: // allow undefined
134: }
135: if (prf != null) {
136: this .request = prf.createPortalRequest(this .request);
137: } else {
138: // Simply wrap the current request so we maintain the same
139: // level of wrapping.
140: // This is needed in the ServletPortletInvoker to get back
141: // to the original request.
142: this .request = new HttpServletRequestWrapper(
143: this .request);
144: }
145: }
146: }
147:
148: public HttpServletRequest getRequest() {
149: return request;
150: }
151:
152: public HttpServletResponse getResponse() {
153: return response;
154: }
155:
156: public ServletConfig getConfig() {
157: return config;
158: }
159:
160: public Map getProfileLocators() {
161: return locators;
162: }
163:
164: public void setProfileLocators(Map locators) {
165: this .locators = locators;
166: }
167:
168: public ContentPage getPage() {
169: return this .page;
170: }
171:
172: public void setPage(ContentPage page) {
173: this .page = page;
174: }
175:
176: public PortletDefinition getPortletDefinition() {
177: return portletDefinition;
178: }
179:
180: public void setPortletDefinition(PortletDefinition portletDefinition) {
181: this .portletDefinition = portletDefinition;
182: }
183:
184: public ContentDispatcher getContentDispatcher() {
185: return dispatcher;
186: }
187:
188: public void setContentDispatcher(ContentDispatcher dispatcher) {
189: this .dispatcher = dispatcher;
190: }
191:
192: /**
193: * get the Capability Map
194: *
195: */
196: public CapabilityMap getCapabilityMap() {
197: return this .capabilityMap;
198: }
199:
200: /**
201: * Set the Mimetype. Used by the CapabilityValve
202: *
203: * @param mimeType
204: */
205: public void setMimeType(String mimeType) {
206: this .mimeType = mimeType;
207: }
208:
209: /**
210: * get the mimeType for the request
211: *
212: */
213: public String getMimeType() {
214: return this .mimeType;
215: }
216:
217: /**
218: * Set the mediaType. Used by the CapabilityValve
219: *
220: * @param mediaType
221: */
222: public void setMediaType(String mediaType) {
223: this .mediaType = mediaType;
224: }
225:
226: /**
227: * get the Media Type
228: *
229: */
230: public String getMediaType() {
231: return this .mediaType;
232: }
233:
234: /**
235: * Get the target Portlet Action Window
236: *
237: * @return PortletWindow The target portlet window
238: */
239: public PortletWindow getActionWindow() {
240: return actionWindow;
241: }
242:
243: /**
244: * Sets the target Portlet Action Window
245: *
246: * @param window
247: */
248: public void setActionWindow(PortletWindow portletWindow) {
249: this .actionWindow = portletWindow;
250: }
251:
252: /**
253: * Set the capabilityMap. Used by the CapabilityValve
254: *
255: * @param capabilityMap
256: */
257: public void setCapabilityMap(CapabilityMap map) {
258: this .capabilityMap = map;
259: }
260:
261: /**
262: * get the character encoding
263: *
264: *
265: */
266: public String getCharacterEncoding() {
267: return this .encoding;
268: }
269:
270: /**
271: * set character encoding
272: *
273: * @param enc
274: */
275: public void setCharacterEncoding(String enc) {
276: String preferedEnc = (String) session
277: .getAttribute(PortalReservedParameters.PREFERED_CHARACTERENCODING_ATTRIBUTE);
278:
279: if (preferedEnc == null || !enc.equals(preferedEnc)) {
280: request
281: .setAttribute(
282: PortalReservedParameters.PREFERED_CHARACTERENCODING_ATTRIBUTE,
283: enc);
284: }
285:
286: this .encoding = enc;
287: }
288:
289: /**
290: * <p>
291: * getRequestForWindow
292: * </p>
293: *
294: * @see org.apache.jetspeed.request.RequestContext#getRequestForWindow(org.apache.pluto.om.window.PortletWindow)
295: * @param window
296: * @return
297: */
298: public HttpServletRequest getRequestForWindow(PortletWindow window) {
299: if (!requestsForWindows.containsKey(window.getId())) {
300: ServletRequestFactory reqFac = (ServletRequestFactory) Jetspeed
301: .getEngine()
302: .getFactory(
303: javax.servlet.http.HttpServletRequest.class);
304: HttpServletRequest requestWrapper = reqFac
305: .getServletRequest(request, window);
306: requestsForWindows.put(window.getId(), requestWrapper);
307: return requestWrapper;
308: } else {
309: return (HttpServletRequest) requestsForWindows.get(window
310: .getId());
311: }
312:
313: }
314:
315: /**
316: * <p>
317: * getResponseForWindow
318: * </p>
319: *
320: * @see org.apache.jetspeed.request.RequestContext#getResponseForWindow(org.apache.pluto.om.window.PortletWindow)
321: * @param window
322: * @return
323: */
324: public HttpServletResponse getResponseForWindow(PortletWindow window) {
325: HttpServletResponse wrappedResponse = null;
326:
327: if (!responsesForWindows.containsKey(window.getId())) {
328: if (getContentDispatcher() != null) {
329: wrappedResponse = ((ContentDispatcherCtrl) getContentDispatcher())
330: .getResponseForWindow(window, this );
331: } else {
332: ServletResponseFactory rspFac = (ServletResponseFactory) Jetspeed
333: .getEngine().getFactory(
334: HttpServletResponse.class);
335: wrappedResponse = rspFac
336: .getServletResponse(this .response);
337:
338: }
339:
340: responsesForWindows.put(window.getId(), wrappedResponse);
341: return wrappedResponse;
342:
343: } else {
344: return (HttpServletResponse) responsesForWindows.get(window
345: .getId());
346: }
347: }
348:
349: /**
350: * @see org.apache.jetspeed.request.RequestContext#getSubject()
351: */
352: public Subject getSubject() {
353: return this .subject;
354: }
355:
356: public Principal getUserPrincipal() {
357: return SecurityHelper.getBestPrincipal(getSubject(),
358: UserPrincipal.class);
359: }
360:
361: /**
362: * @see org.apache.jetspeed.request.RequestContext#setSubject(javax.security.auth.Subject)
363: */
364: public void setSubject(Subject subject) {
365: this .subject = subject;
366: }
367:
368: /**
369: * @see org.apache.jetspeed.request.RequestContext#getLocale()
370: */
371: public Locale getLocale() {
372: return this .locale;
373: }
374:
375: /**
376: * @see org.apache.jetspeed.request.RequestContext#setLocale(java.util.Locale)
377: */
378: public void setLocale(Locale locale) {
379: Locale preferedLocale = (Locale) session
380: .getAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
381:
382: if (preferedLocale == null || !locale.equals(preferedLocale)) {
383: // PREFERED_LANGUAGE_ATTRIBUTE doesn't seem to be used anywhere anymore, and as a WeakHashMap isn't
384: // Serializable, "fixing" that problem (JS2-174) by simply not putting it in the session anymore
385: // request.getSession().setAttribute(PortalReservedParameters.PREFERED_LANGUAGE_ATTRIBUTE, new WeakHashMap());
386: session.setAttribute(
387: PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE,
388: locale);
389: request.setAttribute(
390: PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE,
391: locale);
392: }
393:
394: this .locale = locale;
395: }
396:
397: /**
398: * @see org.apache.jetspeed.request.RequestContext#getRequestParameter(java.lang.String)
399: */
400: public String getRequestParameter(String key) {
401: return request.getParameter(key);
402: }
403:
404: public void setRequestParameter(String key, String value) {
405: request.getParameterMap().put(key, value);
406: }
407:
408: /**
409: * @see org.apache.jetspeed.request.RequestContext#getParameterMap()
410: */
411: public Map getParameterMap() {
412: return request.getParameterMap();
413: }
414:
415: /**
416: * @see org.apache.jetspeed.request.RequestContext#getRequestAttribute(java.lang.String)
417: */
418: public Object getRequestAttribute(String key) {
419: return request.getAttribute(key);
420: }
421:
422: /**
423: * @see org.apache.jetspeed.request.RequestContext#getSessionAttribute(java.lang.String)
424: */
425: public Object getSessionAttribute(String key) {
426: return session.getAttribute(key);
427: }
428:
429: /**
430: * @see org.apache.jetspeed.request.RequestContext#setSessionAttribute(java.lang.String,
431: * java.lang.Object)
432: */
433: public void setSessionAttribute(String key, Object value) {
434: session.setAttribute(key, value);
435: }
436:
437: /**
438: * @see org.apache.jetspeed.request.RequestContext#setAttribute(java.lang.String,
439: * java.lang.Object)
440: */
441: public void setAttribute(String key, Object value) {
442: request.setAttribute(key, value);
443: }
444:
445: /**
446: * @see org.apache.jetspeed.request.RequestContext#getAttribute(java.lang.String)
447: */
448: public Object getAttribute(String key) {
449: return request.getAttribute(key);
450: }
451:
452: /**
453: * @see org.apache.jetspeed.request.RequestContext#getPath()
454: */
455: public String getPath() {
456: if (this .requestPath == null) {
457: this .requestPath = getPortalURL().getPath();
458: }
459: return this .requestPath;
460: }
461:
462: public void setPortalURL(PortalURL url) {
463: if (this .url != null)
464: throw new IllegalStateException("PortalURL already set");
465: if (url == null)
466: throw new IllegalArgumentException(
467: "PortalURL may not be nullified");
468: this .url = url;
469: }
470:
471: public PortalURL getPortalURL() {
472: return url;
473: }
474:
475: /**
476: * @see org.apache.jetspeed.request.RequestContext#getUserInfoMap(org.apache.pluto.om.common.ObjectID)
477: */
478: public Map getUserInfoMap(ObjectID oid) {
479: return userInfoMgr.getUserInfoMap(oid, this );
480: }
481:
482: /**
483: *
484: * <p>
485: * getPreferedLanguage
486: * </p>
487: *
488: * @see org.apache.jetspeed.request.RequestContext#getPreferedLanguage(org.apache.pluto.om.portlet.PortletDefinition)
489: * @param portlet
490: * @return
491: */
492: public Language getPreferedLanguage(PortletDefinition portlet) {
493: // TODO cannot get a proper Language when changing a locale by Locale
494: // Selector
495: // HttpSession session = request.getSession();
496: // Map languageMap = (Map)
497: // session.getAttribute(PREFERED_LANGUAGE_SESSION_KEY);
498: // Language language = (Language) languageMap.get(portlet);
499: // if(language != null)
500: // {
501: // return language;
502: // }
503: LanguageSet languageSet = portlet.getLanguageSet();
504: Language language = languageSet.get(locale);
505:
506: Enumeration locales = request.getLocales();
507: while (locales.hasMoreElements() && language == null) {
508: Locale aLocale = (Locale) locales.nextElement();
509: language = languageSet.get(aLocale);
510: }
511:
512: Iterator langItr = languageSet.iterator();
513: if (langItr.hasNext() && language == null) {
514: language = (Language) langItr.next();
515: }
516:
517: if (language == null) {
518: language = languageSet.get(languageSet.getDefaultLocale());
519: }
520:
521: if (language == null) {
522: MutableLanguage languageCtl = new LanguageImpl();
523: languageCtl.setLocale(locale);
524: languageCtl.setShortTitle(portlet.getName());
525: languageCtl.setTitle(portlet.getName());
526: language = languageCtl;
527: }
528:
529: // languageMap.put(portlet, language);
530: return language;
531: }
532:
533: /**
534: * <p>
535: * setPath
536: * </p>
537: *
538: * @see org.apache.jetspeed.request.RequestContext#setPath(java.lang.String)
539: * @param path
540: */
541: public void setPath(String path) {
542: this .requestPath = path;
543: }
544:
545: /* (non-Javadoc)
546: * @see org.apache.jetspeed.request.RequestContext#getActionFailure()
547: */
548: public Throwable popActionFailure(PortletWindow window) {
549:
550: String key = ACTION_ERROR_ATTR + window.getId();
551: Throwable t = (Throwable) session.getAttribute(key);
552: session.removeAttribute(key);
553: return t;
554:
555: }
556:
557: /*
558: * (non-Javadoc)
559: *
560: * @see org.apache.jetspeed.request.RequestContext#setActionFailed(java.lang.Throwable)
561: */
562: public void setActionFailure(PortletWindow window,
563: Throwable actionFailure) {
564: setSessionAttribute(ACTION_ERROR_ATTR + window.getId(),
565: actionFailure);
566: }
567:
568: /**
569: * Get the current executing pipeline
570: *
571: * @return Pipeline
572: */
573: public Pipeline getPipeline() {
574: return pipeline;
575: }
576:
577: /**
578: * Set the current pipeline
579: * @param pipeline
580: */
581: public void setPipeline(Pipeline pipeline) {
582: this .pipeline = pipeline;
583: }
584:
585: /**
586: * @param request The request to set.
587: */
588: public void setRequest(HttpServletRequest request) {
589: this .request = request;
590: }
591:
592: /**
593: * @param response The request to set.
594: */
595: public void setResponse(HttpServletResponse response) {
596: this .response = response;
597: }
598:
599: public ContentPage locatePage(Profiler profiler,
600: String nonProfiledPath) {
601: try {
602: String pathSave = this .getPath();
603: this .setPath(nonProfiledPath);
604: ContentPage realPage = this .getPage();
605: this .setPage(null);
606: Map locators = null;
607: ProfileLocator locator = profiler.getProfile(this ,
608: ProfileLocator.PAGE_LOCATOR);
609: if (locator != null) {
610: locators = new HashMap();
611: locators.put(ProfileLocator.PAGE_LOCATOR, locator);
612: }
613: PortalSiteSessionContext sessionContext = (PortalSiteSessionContext) getSessionAttribute(ProfilerValveImpl.PORTAL_SITE_SESSION_CONTEXT_ATTR_KEY);
614: PortalSiteRequestContext requestContext = sessionContext
615: .newRequestContext(locators, true, true);
616: ContentPage cpage = new ContentPageImpl(requestContext
617: .getManagedPage());
618: //System.out.println("page is " + cpage.getPath());
619: this .setPage(realPage);
620: this .setPath(pathSave);
621: return cpage;
622: } catch (Throwable t) {
623: t.printStackTrace();
624: }
625: return null;
626: }
627:
628: public Map getObjects() {
629: return objects;
630: }
631: }
|