001: /**
002: * $Id: SSOAdapterSession.java,v 1.5 2005/08/24 16:57:06 rakeshn Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and iPlanet
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.ssoadapter;
014:
015: import java.util.Locale;
016: import java.util.List;
017: import java.util.Properties;
018: import java.security.Principal;
019: import com.iplanet.sso.SSOToken;
020: import com.iplanet.sso.SSOTokenManager;
021: import com.iplanet.sso.SSOTokenListener;
022: import com.iplanet.sso.SSOTokenEvent;
023: import com.iplanet.sso.SSOException;
024: import com.iplanet.am.util.AMClientDetector;
025:
026: import javax.servlet.http.HttpServletRequest;
027:
028: /**
029: * This class is a wrapper for SSOToken and other inputs to the SSOAdapter.
030: * @version 1.0
031: * @see com.sun.ssoadapter.SSOAdapterFactory
032: * @see com.sun.ssoadapter.SSOAdapterException
033: */
034:
035: public class SSOAdapterSession {
036: /**
037: * The SSOToken reqd for the session .
038: */
039: private SSOToken token;
040: /**
041: * the clientType as String
042: */
043: private String clientType;
044: /**
045: * locale to get localized error message
046: */
047: private Locale locale;
048: /**
049: * Incase of authless ( where SSOToken is not valid ) pass the authlessUId
050: */
051: private String authlessUid;
052: /**
053: * the instanceName with which you want to dealwith
054: */
055: private String instanceName;
056:
057: /**
058: * clientDetector
059: */
060: private static AMClientDetector cd = new AMClientDetector();
061: /**
062: * Token Manger
063: */
064: private static SSOTokenManager tokenMgr = null;
065:
066: /**
067: * the SSOToken is stored in the httpRequest
068: */
069: private static final String SSO_TOKEN = "ps.session";
070:
071: /**
072: * The Constructor
073: * @param tokenParam SSOToken
074: * @param clientTypeParam the clientType
075: * @param localeParam locale
076: * @param authlessUidParam the authlessUID
077: * @param instanceNameParam instanceName
078: */
079: public SSOAdapterSession(SSOToken tokenParam,
080: String clientTypeParam, Locale localeParam,
081: String authlessUidParam, String instanceNameParam) {
082: this .token = tokenParam;
083: this .clientType = clientTypeParam;
084: this .locale = localeParam;
085: this .authlessUid = authlessUidParam;
086: this .instanceName = instanceNameParam;
087: }
088:
089: /**
090: * An Constructor with HttpServletRequest
091: * @param requestParam
092: * @param localeParam
093: * @param authlessUidParam
094: * @param instanceNameParam
095: */
096: public SSOAdapterSession(HttpServletRequest requestParam,
097: Locale localeParam, String authlessUidParam,
098: String instanceNameParam) {
099: this (getSSOToken(requestParam), getClientType(requestParam),
100: localeParam, authlessUidParam, instanceNameParam);
101: }
102:
103: /**
104: * An Constructor with HttpServletRequest
105: * @param requestParam
106: */
107: public SSOAdapterSession(HttpServletRequest requestParam) {
108: this (requestParam, null, null, null);
109: }
110:
111: /**
112: * An Constructor with SSOToken
113: * @param tokenParam
114: */
115: public SSOAdapterSession(SSOToken tokenParam) {
116: this (tokenParam, null, null, null, null);
117: }
118:
119: /**
120: *
121: * @return SSOToken
122: */
123: public SSOToken getSSOToken() {
124: return token;
125: }
126:
127: /**
128: *
129: * @return clientType
130: */
131: public String getClientType() {
132: return clientType;
133: }
134:
135: /**
136: *
137: * @return the authlessUID if provided
138: */
139: public String getAuthlessUID() {
140: return authlessUid;
141: }
142:
143: /**
144: *
145: * @return the instanceName if provided
146: */
147: public String getInstanceName() {
148: return instanceName;
149: }
150:
151: /**
152: *
153: * @return checks if session is Valid
154: */
155: public boolean isSessionValid() {
156: boolean isValid = false;
157: if (token != null && getSSOTokenManager().isValidToken(token)) {
158: String uid = getUserID();
159: if (uid != null) {
160: isValid = true;
161: }
162: }
163: return isValid;
164: }
165:
166: /**
167: *
168: * @return gets theUserId from SSOToken if possible
169: */
170: private String getUserID() {
171: Principal p = null;
172: try {
173: if (token == null) {
174: return null;
175: }
176: p = token.getPrincipal();
177: } catch (SSOException ssoe) {
178: return null;
179: }
180: return p.toString();
181: }
182:
183: /**
184: *
185: * @return gets the SSOToken as String
186: */
187: public String getSessionID() {
188: String tokenID = null;
189: if (token != null) {
190: tokenID = token.getTokenID().toString();
191: }
192: return tokenID;
193: }
194:
195: /**
196: *
197: * @return locale
198: */
199: public Locale getLocale() {
200: return locale;
201: }
202:
203: /**
204: *
205: * @param listner
206: * @throws com.iplanet.sso.SSOException
207: */
208: public void addSSOTokenListener(SSOTokenListener listner)
209: throws SSOException {
210: if (token != null)
211: token.addSSOTokenListener(listner);
212: }
213:
214: /**
215: *
216: * @param request
217: * @return clientType
218: */
219: public static String getClientType(HttpServletRequest request) {
220: return cd.getClientType(request);
221: }
222:
223: /**
224: *
225: * @return the SSOTokenManager
226: */
227: public static SSOTokenManager getSSOTokenManager() {
228: if (tokenMgr != null)
229: return tokenMgr;
230: try {
231: tokenMgr = SSOTokenManager.getInstance();
232: } catch (SSOException ex) {
233: throw new IllegalStateException(
234: "SSOAdapterSession.getSSOTokenManager():Failed to get SSOTokenManager.getInstance(). "
235: + ex);
236:
237: }
238:
239: return tokenMgr;
240: }
241:
242: /**
243: *
244: * @param request
245: * @return SSOToken : gets the SSOToken from the HttpServletRequest
246: */
247: public static SSOToken getSSOToken(HttpServletRequest request) {
248: //
249: // try to get the token from the request. this saves
250: // us creating >1 sso tokens for multiple
251: // per-request calls into this api
252: //
253: SSOToken token = (SSOToken) request.getAttribute(SSO_TOKEN);
254: if (token == null) {
255: try {
256: token = getSSOTokenManager().createSSOToken(request);
257: //
258: // put the token into the request, so multiple calls
259: // into this api per request can use this value
260: //
261: request.setAttribute(SSO_TOKEN, token);
262: } catch (SSOException se) {
263: // This means that SSOToken is invalid
264: //
265: token = null;
266: }
267: }
268:
269: return token;
270: }
271:
272: }
|