001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.wsrp.producer.context;
014:
015: import com.iplanet.sso.SSOException;
016: import com.iplanet.sso.SSOToken;
017: import com.sun.portal.desktop.context.*;
018: import com.sun.portal.util.SSOUtil;
019: import com.sun.portal.wsrp.producer.filter.ProducerThreadLocalizer;
020:
021: import javax.servlet.ServletRequest;
022: import javax.servlet.http.Cookie;
023: import javax.servlet.http.HttpServletRequest;
024:
025: public class WSRPSessionContext implements SessionContext,
026: SessionAppContext {
027: public static final String WSRP_UID_KEY = "desktop.wsrp.uid";
028: public static final String WSRP_SSOTOKEN = "desktop.wsrp.ssotoken";
029:
030: private static DesktopAppContext dac = null;
031: // Use getSSOToken() method to access this
032: private SSOToken _token = null;
033:
034: static {
035: dac = DesktopAppContextThreadLocalizer.get();
036: }
037:
038: public String getSessionID(HttpServletRequest req) {
039: return getUserID();
040: }
041:
042: public String getUserID(HttpServletRequest req) {
043: return (String) req.getAttribute(WSRP_UID_KEY);
044: }
045:
046: public String getSessionID() {
047: ServletRequest sreq = ProducerThreadLocalizer.getRequest();
048: HttpServletRequest req = (HttpServletRequest) sreq;
049:
050: return getSessionID(req);
051: }
052:
053: public String getUserID() {
054: ServletRequest sreq = ProducerThreadLocalizer.getRequest();
055: HttpServletRequest req = (HttpServletRequest) sreq;
056:
057: return getUserID(req);
058: }
059:
060: public boolean validateSession(HttpServletRequest req) {
061: boolean valid = false;
062:
063: String wsrpUID = getUserID(req);
064: if (wsrpUID != null) {
065: valid = true;
066: }
067:
068: return valid;
069: }
070:
071: public void init(HttpServletRequest req, String portalId) {
072: Object obj = req.getAttribute(WSRPSessionContext.WSRP_SSOTOKEN);
073: if (obj != null) {
074: _token = (SSOToken) obj;
075: return;
076: }
077: _token = getSSOToken(req);
078:
079: }
080:
081: public String getStringProperty(String name) {
082: try {
083: return getSSOToken().getProperty(name);
084: } catch (SSOException ssoe) {
085: throw new ContextError(ssoe, ContextError.SESSION_TYPE);
086: }
087:
088: }
089:
090: public void setStringProperty(String name, String val) {
091:
092: try {
093: getSSOToken().setProperty(name, val);
094: } catch (SSOException ssoe) {
095: throw new ContextError(ssoe, ContextError.SESSION_TYPE);
096: }
097:
098: }
099:
100: private synchronized SSOToken getSSOToken() {
101: if (_token == null) {
102: throw new ContextError(
103: "WSRPSessionContext.getSSOToken(): not initialized");
104: }
105:
106: return _token;
107: }
108:
109: private synchronized SSOToken getSSOToken(HttpServletRequest req) {
110: SSOToken token;
111: String uid = getUserID(req);
112: try {
113: token = SSOUtil.createSSOToken(uid, uid);
114:
115: } catch (SSOException se) {
116: // This means that SSOToken is invalid
117: throw new ContextError(se, ContextError.SESSION_TYPE);
118: }
119:
120: return token;
121: }
122:
123: public void addSessionListener(SessionListener sl) {
124: // not implemented
125: }
126:
127: public void addUserListener(UserListener sl) {
128: // not implemented
129: }
130:
131: public String encodeURL(String url) {
132: // not implemented do we need this?
133: return url;
134: }
135:
136: public String getAuthenticationType() {
137: return "desktop.wsrp";
138: }
139:
140: /* Properties that are stored as Cookie on client browser.
141: * AuthlessAnonymous user state is managed via such cookie.
142: * Authenticated users use AM/DSAME session to manage user state, returns null in such case.
143: */
144: public Cookie getClientProperties() {
145: return null;
146: }
147:
148: public static String getStaticPortalId() {
149: return dac.getPortalId();
150: }
151:
152: }
|