001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.getc
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036:
037: package com.sun.portal.community.impl;
038:
039: import javax.portlet.PortletRequest;
040: import javax.portlet.PortletPreferences;
041: import com.sun.portal.portlet.impl.PortletRequestConstants;
042:
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045: import javax.servlet.ServletContextListener;
046: import javax.servlet.ServletContextEvent;
047:
048: import com.sun.portal.community.CommunityFactory;
049: import com.sun.portal.community.CommunityException;
050: import com.sun.portal.community.CommunityDoesNotExistException;
051: import com.sun.portal.community.CommunityManager;
052: import com.sun.portal.community.Community;
053: import com.sun.portal.community.CommunityUser;
054: import com.sun.portal.community.CommunityContext;
055: import com.sun.portal.community.CommunityId;
056:
057: import com.iplanet.sso.SSOTokenManager;
058: import com.iplanet.sso.SSOToken;
059: import com.iplanet.sso.SSOException;
060:
061: import java.security.Principal;
062:
063: import netscape.ldap.LDAPDN;
064:
065: /**
066: * Factory class used to get references to various community objects, and to
067: * create communities, remove communities, and test for existence of
068: * communities.
069: */
070: public class CommunityFactoryImpl extends CommunityFactory implements
071: ServletContextListener {
072: public CommunityManager getCommunityManager(PortletRequest preq)
073: throws CommunityException {
074: HttpServletRequest req = getRequest(preq);
075: HttpServletResponse res = getResponse(preq);
076:
077: return getCommunityManager(req, res);
078: }
079:
080: public CommunityManager getCommunityManager(
081: HttpServletRequest request, HttpServletResponse response)
082: throws CommunityException {
083:
084: SSOToken ssoToken = null;
085: try {
086: SSOTokenManager ssoTokenMgr = SSOTokenManager.getInstance();
087: ssoToken = ssoTokenMgr.createSSOToken(request);
088: } catch (SSOException se) {
089: throw new CommunityException(
090: "CommunityManagerImpl.init(): failed to create ssoToken.",
091: se);
092: }
093:
094: return getCommunityManager(request, response, ssoToken);
095: }
096:
097: public CommunityManager getCommunityManager(
098: HttpServletRequest request, HttpServletResponse response,
099: SSOToken sso) throws CommunityException {
100: return new CommunityManagerImpl(request, response, sso);
101: }
102:
103: public Community getCommunity(PortletRequest preq)
104: throws CommunityDoesNotExistException, CommunityException {
105: HttpServletRequest req = getRequest(preq);
106: HttpServletResponse res = getResponse(preq);
107:
108: // Get ssotoken
109: SSOToken ssoToken = null;
110: try {
111: SSOTokenManager ssoTokenMgr = SSOTokenManager.getInstance();
112: ssoToken = ssoTokenMgr.createSSOToken(req);
113: } catch (SSOException se) {
114: throw new CommunityException(
115: "CommunityManagerImpl.init(): failed to create ssoToken.",
116: se);
117: }
118:
119: // Get community ID
120: CommunityId cid = getCommunityId(preq);
121:
122: return getCommunity(req, res, ssoToken, cid);
123: }
124:
125: public Community getCommunity(HttpServletRequest req,
126: HttpServletResponse res)
127: throws CommunityDoesNotExistException, CommunityException {
128:
129: // Get ssotoken
130: SSOToken ssoToken = null;
131: try {
132: SSOTokenManager ssoTokenMgr = SSOTokenManager.getInstance();
133: ssoToken = ssoTokenMgr.createSSOToken(req);
134: } catch (SSOException se) {
135: throw new CommunityException(
136: "CommunityManagerImpl.init(): failed to create ssoToken.",
137: se);
138: }
139:
140: // Get community ID
141: CommunityId cid = getCommunityId(req);
142:
143: return getCommunity(req, res, ssoToken, cid);
144: }
145:
146: public Community getCommunity(HttpServletRequest req,
147: HttpServletResponse res, SSOToken ssoToken, CommunityId cid)
148: throws CommunityDoesNotExistException, CommunityException {
149: return new CommunityImpl(req, res, ssoToken, cid);
150: }
151:
152: public CommunityUser getCommunityUser(PortletRequest preq)
153: throws CommunityException {
154: HttpServletRequest req = getRequest(preq);
155: String userId = getUserId(preq);
156:
157: return getCommunityUser(req, LDAPDN.normalize(userId
158: .toLowerCase()));
159: }
160:
161: public CommunityUser getCommunityUser(HttpServletRequest req)
162: throws CommunityException {
163: String userId = getUserId(req);
164:
165: return getCommunityUser(req, LDAPDN.normalize(userId
166: .toLowerCase()));
167: }
168:
169: public CommunityUser getCommunityUser(HttpServletRequest req,
170: String userId) throws CommunityException {
171: return new CommunityUserImpl(req, LDAPDN.normalize(userId
172: .toLowerCase()));
173: }
174:
175: public CommunityContext getCommunityContext(PortletRequest preq)
176: throws CommunityException {
177: HttpServletRequest req = getRequest(preq);
178: CommunityId cid = getCommunityId(preq);
179: String userId = getUserId(preq);
180: return getCommunityContext(req, cid, userId);
181: }
182:
183: public CommunityContext getCommunityContext(HttpServletRequest req)
184: throws CommunityException {
185: CommunityId cid = getCommunityId(req);
186: String userId = getUserId(req);
187: return getCommunityContext(req, cid, userId);
188: }
189:
190: public CommunityContext getCommunityContext(HttpServletRequest req,
191: CommunityId cid) throws CommunityException {
192: String userId = getUserId(req);
193: return getCommunityContext(req, cid, userId);
194: }
195:
196: public CommunityContext getCommunityContext(HttpServletRequest req,
197: CommunityId cid, String userId) throws CommunityException {
198: return new CommunityContextImpl(req, cid, LDAPDN
199: .normalize(userId.toLowerCase()));
200: }
201:
202: private HttpServletRequest getRequest(PortletRequest preq)
203: throws CommunityException {
204: HttpServletRequest req = (HttpServletRequest) preq
205: .getAttribute(PortletRequestConstants.HTTP_SERVLET_REQUEST);
206: if (req == null) {
207: throw new CommunityException(
208: "could not get http request from portlet request");
209: }
210: return req;
211: }
212:
213: private HttpServletResponse getResponse(PortletRequest preq)
214: throws CommunityException {
215: HttpServletResponse res = (HttpServletResponse) preq
216: .getAttribute(PortletRequestConstants.HTTP_SERVLET_RESPONSE);
217: if (res == null) {
218: throw new CommunityException(
219: "could not get http response from portlet request");
220: }
221: return res;
222: }
223:
224: private CommunityId getCommunityId(PortletRequest preq)
225: throws CommunityException {
226: PortletPreferences pp = preq.getPreferences();
227: String cidString = pp.getValue(COMMUNITY_ID_PREF, "");
228: if (cidString == null || cidString.length() == 0) {
229: throw new CommunityException(
230: "could not get community ID from portlet preference");
231: }
232: return new CommunityId(cidString);
233: }
234:
235: private CommunityId getCommunityId(HttpServletRequest req)
236: throws CommunityException {
237: String cidString = req.getParameter(COMMUNITY_ID_PARAM);
238: if (cidString == null || cidString.length() == 0) {
239: throw new CommunityException(
240: "could not get community ID from request parameter");
241: }
242: return new CommunityId(cidString);
243: }
244:
245: private String getUserId(PortletRequest preq)
246: throws CommunityException {
247: HttpServletRequest req = getRequest(preq);
248: return getUserId(req);
249: }
250:
251: private String getUserId(HttpServletRequest req)
252: throws CommunityException {
253: String userId;
254: try {
255: SSOTokenManager ssoTokenMgr = SSOTokenManager.getInstance();
256: SSOToken ssoToken = ssoTokenMgr.createSSOToken(req);
257: // userid is returned lower case, but to be consistent with the rest of code, we are explicitly
258: // making it lowercase.
259: userId = LDAPDN.normalize(ssoToken.getPrincipal().getName()
260: .toLowerCase());
261: } catch (SSOException se) {
262: throw new CommunityException(
263: "could not get user ID from SSO token", se);
264: }
265:
266: return userId;
267: }
268:
269: public void contextInitialized(ServletContextEvent event) {
270: //System.err.println("CommunityFactoryImpl.contextInitialized(): called");
271: setInstance(this );
272: }
273:
274: public void contextDestroyed(ServletContextEvent event) {
275: //System.err.println("CommunityFactoryImpl.contextDestroyed(): called");
276: //setInstance(null);
277: }
278: }
|