001: /**
002: * Copyright 2003 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: */package com.sun.portal.wsrp.consumer.markup.impl;
013:
014: import com.sun.portal.container.ContainerRequest;
015: import com.sun.portal.container.ContainerException;
016: import com.sun.portal.container.ContentException;
017: import com.sun.portal.wsrp.common.WSRPSpecKeys;
018: import com.sun.portal.wsrp.consumer.markup.UserContextManager;
019: import com.sun.portal.wsrp.consumer.markup.MarkupConfig;
020: import com.sun.portal.wsrp.consumer.common.Compare;
021: import com.sun.portal.wsrp.common.stubs.*;
022: import com.sun.portal.log.common.PortalLogger;
023:
024: import java.util.List;
025: import java.util.Map;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: public class UserContextManagerImpl implements UserContextManager {
030:
031: private static final String USER_CONTEXT_CACHE_KEY = "user_context_cache_key";
032:
033: private static Logger logger = PortalLogger
034: .getLogger(UserContextManagerImpl.class);
035:
036: public boolean isUserContextCacheValid(MarkupConfig markupConfig,
037: ContainerRequest request) throws ContainerException,
038: ContentException {
039:
040: if (request.getUserID() == null) {
041: //
042: // since there is no cache kept for anonymous
043: //
044: return false;
045: } else {
046: Map currentUserInfo = request.getUserInfo();
047: List currentRoles = request.getRoles();
048:
049: //
050: // get the cached userContext
051: //
052: UserContextCache userContextCache = getUserContextCache(request);
053:
054: //
055: // Compare the user and role info to see if the cache is
056: // still valid
057: //
058: if (userContextCache != null
059: && (userContextCache.match(currentUserInfo,
060: currentRoles))) {
061:
062: //
063: // debug
064: //
065:
066: if (logger.isLoggable(Level.FINEST))
067: logger.log(Level.FINEST, "PSWS_CSPWCMI0018",
068: request.getEntityID());
069:
070: return true;
071: } else {
072: //
073: // debug
074: //
075: if (logger.isLoggable(Level.FINEST))
076: logger.log(Level.FINEST, "PSWS_CSPWCMI0019",
077: request.getEntityID());
078: if (userContextCache != null) {
079: invalidateUserContextCache(request);
080: }
081: return false;
082: }
083: }
084: }
085:
086: public UserContext getUserContext(MarkupConfig markupConfig,
087: ContainerRequest request) throws ContainerException,
088: ContentException {
089:
090: UserContext userContext = null;
091:
092: //
093: // send cached UserContext if exists
094: //
095: if (request.getUserID() != null) {
096: UserContextCache userContextCache = getUserContextCache(request);
097: if (userContext != null) {
098: userContext = userContextCache.getUserContext();
099: }
100: }
101:
102: //
103: // generate new if no cache
104: //
105:
106: if (userContext == null) {
107: userContext = getFreshUserContext(markupConfig, request);
108:
109: // If regular user, store it as cache in the session
110: if (request.getUserID() != null) {
111: putUserContextCache(request, new UserContextCache(
112: userContext, request.getUserInfo(), request
113: .getRoles()));
114: }
115: }
116:
117: return userContext;
118:
119: }
120:
121: private UserContext getFreshUserContext(MarkupConfig markupConfig,
122: ContainerRequest request) throws ContainerException,
123: ContentException {
124:
125: UserContext context = new UserContext();
126:
127: //
128: // Set the user id as the key that producer
129: // should use when referencing this user.
130: //
131:
132: if (request.getUserID() == null) {
133: context.setUserContextKey(WSRPSpecKeys.WSRP_GUEST_KEY);
134: } else {
135: context.setUserContextKey(request.getUserID());
136: }
137:
138: //
139: // Sets the roles that this user is in
140: //
141:
142: List roles = request.getRoles();
143: if (roles != null) {
144: String[] userCategories = new String[roles.size()];
145: roles.toArray(userCategories);
146: context.setUserCategories(userCategories);
147: }
148:
149: //
150: // Initialize the UserContext with empty UserProfile
151: //
152:
153: context.setProfile(getUserProfile(markupConfig, request));
154:
155: return context;
156:
157: }
158:
159: private UserProfile getUserProfile(MarkupConfig markupConfig,
160: ContainerRequest request) throws ContainerException,
161: ContentException {
162:
163: String[] keys = markupConfig.getPortletDescription()
164: .getUserProfileItems();
165:
166: //
167: // Get the map
168: //
169:
170: Map userInfo = request.getUserInfo();
171:
172: //
173: // Get out of here if nothing to do
174: //
175: if (keys.length == 0 || userInfo == null
176: || userInfo.size() == 0) {
177: return null;
178: }
179:
180: //
181: // Initialize empty UserProfile
182: //
183: UserProfile userProfile = getEmptyUserProfile();
184:
185: //
186: // Populate only the fields that are declared
187: // by this portlet in PortletDescription and
188: //
189:
190: for (int i = 0; i < keys.length; i++) {
191:
192: //
193: // Sanity checks on the keys
194: //
195:
196: if (keys[i] == null) {
197: throw new ContainerException(
198: "Found an empty user profile key");
199:
200: }
201:
202: //
203: // Populate the UserProfile fields
204: //
205:
206: String value = (String) userInfo.get(keys[i]);
207:
208: if (keys[i].equals("name/prefix")) {
209: userProfile.getName().setPrefix(value);
210: } else if (keys[i].equals("name/given")) {
211: userProfile.getName().setGiven(value);
212: } else if (keys[i].equals("name/family")) {
213: userProfile.getName().setFamily(value);
214: } else if (keys[i].equals("name/middle")) {
215: userProfile.getName().setMiddle(value);
216: } else if (keys[i].equals("name/suffix")) {
217: userProfile.getName().setSuffix(value);
218: } else if (keys[i].equals("name/nickname")) {
219: userProfile.getName().setNickname(value);
220: } else if (keys[i].equals("bdate")) {
221: //userProfile.setBdate("TODO");
222: } else if (keys[i].equals("gender")) {
223: userProfile.setGender(value);
224: } else if (keys[i].equals("employerInfo/employer")) {
225: userProfile.getEmployerInfo().setEmployer(value);
226: } else if (keys[i].equals("employerInfo/department")) {
227: userProfile.getEmployerInfo().setDepartment(value);
228: } else if (keys[i].equals("employerInfo/jobtitle")) {
229: userProfile.getEmployerInfo().setJobtitle(value);
230: } else if (keys[i].equals("homeInfo/postal/name")) {
231: userProfile.getHomeInfo().getPostal().setName(value);
232: } else if (keys[i].equals("homeInfo/postal/street")) {
233: userProfile.getHomeInfo().getPostal().setStreet(value);
234: } else if (keys[i].equals("homeInfo/postal/city")) {
235: userProfile.getHomeInfo().getPostal().setCity(value);
236: } else if (keys[i].equals("homeInfo/postal/stateprov")) {
237: userProfile.getHomeInfo().getPostal().setStateprov(
238: value);
239: } else if (keys[i].equals("homeInfo/postal/postalcode")) {
240: userProfile.getHomeInfo().getPostal().setPostalcode(
241: value);
242: } else if (keys[i].equals("homeInfo/postal/country")) {
243: userProfile.getHomeInfo().getPostal().setCountry(value);
244: } else if (keys[i].equals("homeInfo/postal/organization")) {
245: userProfile.getHomeInfo().getPostal().setOrganization(
246: value);
247: } else if (keys[i]
248: .equals("homeInfo/telecom/telephone/intcode")) {
249: userProfile.getHomeInfo().getTelecom().getTelephone()
250: .setIntcode(value);
251: } else if (keys[i]
252: .equals("homeInfo/telecom/telephone/loccode")) {
253: userProfile.getHomeInfo().getTelecom().getTelephone()
254: .setLoccode(value);
255: } else if (keys[i]
256: .equals("homeInfo/telecom/telephone/number")) {
257: userProfile.getHomeInfo().getTelecom().getTelephone()
258: .setNumber(value);
259: } else if (keys[i].equals("homeInfo/telecom/telephone/ext")) {
260: userProfile.getHomeInfo().getTelecom().getTelephone()
261: .setExt(value);
262: } else if (keys[i]
263: .equals("homeInfo/telecom/telephone/comment")) {
264: userProfile.getHomeInfo().getTelecom().getTelephone()
265: .setComment(value);
266: } else if (keys[i].equals("homeInfo/telecom/fax/intcode")) {
267: userProfile.getHomeInfo().getTelecom().getFax()
268: .setIntcode(value);
269: } else if (keys[i].equals("homeInfo/telecom/fax/loccode")) {
270: userProfile.getHomeInfo().getTelecom().getFax()
271: .setLoccode(value);
272: } else if (keys[i].equals("homeInfo/telecom/fax/number")) {
273: userProfile.getHomeInfo().getTelecom().getFax()
274: .setNumber(value);
275: } else if (keys[i].equals("homeInfo/telecom/fax/ext")) {
276: userProfile.getHomeInfo().getTelecom().getFax().setExt(
277: value);
278: } else if (keys[i].equals("homeInfo/telecom/fax/comment")) {
279: userProfile.getHomeInfo().getTelecom().getFax()
280: .setComment(value);
281: } else if (keys[i]
282: .equals("homeInfo/telecom/mobile/intcode")) {
283: userProfile.getHomeInfo().getTelecom().getMobile()
284: .setIntcode(value);
285: } else if (keys[i]
286: .equals("homeInfo/telecom/mobile/loccode")) {
287: userProfile.getHomeInfo().getTelecom().getMobile()
288: .setLoccode(value);
289: } else if (keys[i].equals("homeInfo/telecom/mobile/number")) {
290: userProfile.getHomeInfo().getTelecom().getMobile()
291: .setNumber(value);
292: } else if (keys[i].equals("homeInfo/telecom/mobile/ext")) {
293: userProfile.getHomeInfo().getTelecom().getMobile()
294: .setExt(value);
295: } else if (keys[i]
296: .equals("homeInfo/telecom/mobile/comment")) {
297: userProfile.getHomeInfo().getTelecom().getMobile()
298: .setComment(value);
299: } else if (keys[i].equals("homeInfo/telecom/pager/intcode")) {
300: userProfile.getHomeInfo().getTelecom().getPager()
301: .setIntcode(value);
302: } else if (keys[i].equals("homeInfo/telecom/pager/loccode")) {
303: userProfile.getHomeInfo().getTelecom().getPager()
304: .setLoccode(value);
305: } else if (keys[i].equals("homeInfo/telecom/pager/number")) {
306: userProfile.getHomeInfo().getTelecom().getPager()
307: .setNumber(value);
308: } else if (keys[i].equals("homeInfo/telecom/pager/ext")) {
309: userProfile.getHomeInfo().getTelecom().getPager()
310: .setExt(value);
311: } else if (keys[i].equals("homeInfo/telecom/pager/comment")) {
312: userProfile.getHomeInfo().getTelecom().getPager()
313: .setComment(value);
314: } else if (keys[i].equals("homeInfo/online/email")) {
315: userProfile.getHomeInfo().getOnline().setEmail(value);
316: } else if (keys[i].equals("homeInfo/online/uri")) {
317: userProfile.getHomeInfo().getOnline().setUri(value);
318: }
319: }
320:
321: return userProfile;
322:
323: }
324:
325: private UserProfile getEmptyUserProfile() {
326:
327: UserProfile userProfile = new UserProfile();
328: userProfile.setEmployerInfo(new EmployerInfo());
329: userProfile.setHomeInfo(getEmptyContact());
330: userProfile.setBusinessInfo(getEmptyContact());
331: userProfile.setName(new PersonName());
332: return userProfile;
333: }
334:
335: private Contact getEmptyContact() {
336:
337: Contact contact = new Contact();
338: contact.setPostal(new Postal());
339: contact.setTelecom(getEmptyTelecom());
340: contact.setOnline(new Online());
341: return contact;
342:
343: }
344:
345: private Telecom getEmptyTelecom() {
346: Telecom telecom = new Telecom();
347: telecom.setTelephone(new TelephoneNum());
348: telecom.setFax(new TelephoneNum());
349: telecom.setMobile(new TelephoneNum());
350: telecom.setPager(new TelephoneNum());
351: return telecom;
352: }
353:
354: //*************************************************************
355: // Internal methods and internal class to
356: // manage UserContext cache
357: //*************************************************************
358: private UserContextCache getUserContextCache(
359: ContainerRequest request) {
360: return (UserContextCache) MarkupUtil
361: .getProviderContext(request).getSessionProperty(
362: USER_CONTEXT_CACHE_KEY + request.getEntityID());
363: }
364:
365: private void putUserContextCache(ContainerRequest request,
366: UserContextCache userContextCache) {
367: MarkupUtil.getProviderContext(request).setSessionProperty(
368: USER_CONTEXT_CACHE_KEY + request.getEntityID(),
369: userContextCache);
370: }
371:
372: private void invalidateUserContextCache(ContainerRequest request) {
373: MarkupUtil.getProviderContext(request).setSessionProperty(
374: USER_CONTEXT_CACHE_KEY + request.getEntityID(), null);
375:
376: }
377:
378: /**
379: * Internal bean class to store the cache information
380: * related to the UserContext. It is used only for the
381: * authenticated user.
382: */
383:
384: private class UserContextCache {
385: private Map _userInfo;
386: private List _roles;
387: private UserContext _userContext;
388:
389: UserContextCache(UserContext userContext, Map userInfo,
390: List roles) {
391: _userContext = userContext;
392: _userInfo = userInfo;
393: _roles = roles;
394: }
395:
396: Map getUserInfo() {
397: return _userInfo;
398: }
399:
400: List getRoles() {
401: return _roles;
402: }
403:
404: UserContext getUserContext() {
405: return _userContext;
406: }
407:
408: boolean match(Map newUserInfo, List newRoles) {
409:
410: if (Compare.isMapSame(newUserInfo, _userInfo)
411: && Compare.isListSame(newRoles, _roles)) {
412: return true;
413: } else {
414: return false;
415: }
416:
417: }
418:
419: }
420: }
|