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 iPlanet
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.app.calendarcommon.common;
013:
014: import com.iplanet.sso.SSOToken;
015: import com.sun.portal.app.calendarcommon.calendar.provisioning.SharedCalendarPortletProvisionListener;
016: import com.sun.portal.community.CommunityFactory;
017: import com.sun.portal.community.Community;
018: import com.sun.portal.log.common.PortalLogger;
019: import java.util.Set;
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022: import javax.faces.context.FacesContext;
023: import javax.portlet.PortletPreferences;
024: import javax.portlet.PortletRequest;
025: import javax.servlet.ServletContext;
026:
027: /**
028: * Dummy implementation of SharedServicesUtils for testing.
029: *
030: * @author Nigel Jacobs
031: * @created May 19, 2005
032: *
033: */
034: public class SharedServicesUtilsImpl implements SharedServicesUtils {
035:
036: ServletContext _context;
037:
038: private static Logger logger = PortalLogger
039: .getLogger(SharedCalendarPortletProvisionListener.class);
040:
041: protected SharedServicesUtilsImpl(ServletContext context) {
042: _context = context;
043: }
044:
045: /**
046: * Get the community id.
047: *
048: * @param request
049: * @throws com.sun.portal.app.SharedServices.common.SharedServicesException
050: * @return community id.
051: */
052: public String getCommunityID(PortletRequest request)
053: throws SharedServicesException {
054: //TESTING: REPLACE
055: //return getStringPreference(request, "bookmarkHelpPage");
056: return getStringPreference(request,
057: CommunityFactory.COMMUNITY_ID_PREF);
058:
059: }
060:
061: /**
062: * Get the community id.
063: *
064: * @param context
065: * @throws com.sun.portal.app.SharedServices.common.SharedServicesException
066: * @return community id.
067: */
068: public String getCommunityID(FacesContext context)
069: throws SharedServicesException {
070: Object request = context.getExternalContext().getRequest();
071: if (request instanceof PortletRequest) {
072: return getCommunityID((PortletRequest) request);
073: } else {
074: throw new SharedServicesException(
075: "Can't getCommunityID: only supporting Portlet environment.");
076: }
077: }
078:
079: /**
080: * Get the ids of all the members of the community, as a set of Strings.
081: *
082: * @param request
083: * @return member id Strings.
084: */
085: public synchronized Set getMemberIDs(PortletRequest request)
086: throws SharedServicesException {
087:
088: try {
089: CommunityFactory cf = CommunityFactory.getInstance();
090: Community c = cf.getCommunity(request);
091: // TODO: restrict to only member role here?
092: Set members = c.getUsers();
093:
094: if (logger.isLoggable(Level.FINER)) {
095: logger.finer("members = " + members);
096: }
097:
098: return members;
099: } catch (Exception ex) {
100: logger.log(Level.WARNING,
101: "Cant get member IDs for community: ", ex);
102: throw new SharedServicesException(
103: "Cant get member IDs for community: ", ex);
104: }
105:
106: }
107:
108: /**
109: * Get the ids of all the members of the community, as a set of Strings.
110: *
111: * @param context
112: * @return member id Strings.
113: */
114: public synchronized Set getMemberIDs(FacesContext context)
115: throws SharedServicesException {
116: Object request = context.getExternalContext().getRequest();
117: if (request instanceof PortletRequest) {
118: return getMemberIDs((PortletRequest) request);
119: } else {
120: throw new SharedServicesException(
121: "Can't getMemberIDs: only supporting Portlet environment.");
122: }
123: }
124:
125: /**
126: * Get the current users community member id.
127: *
128: * @param request
129: * @throws com.sun.portal.app.SharedServices.common.SharedServicesException
130: * @return member id.
131: */
132: public String getCurrentMemberID(PortletRequest request)
133: throws SharedServicesException {
134:
135: try {
136: SSOToken token = (SSOToken) request
137: .getAttribute(SSO_TOKEN_CONTEXT_ATTR);
138: if (token == null)
139: return null;
140: return token.getPrincipal().getName();
141: } catch (Exception e) {
142: throw new SharedServicesException(
143: "Can't get SSOToken from portlet request", e);
144: }
145:
146: }
147:
148: /**
149: * Get the current users community member id.
150: *
151: * @param request
152: * @throws com.sun.portal.app.SharedServices.common.SharedServicesException
153: * @return member id.
154: */
155: public String getCurrentMemberID(FacesContext context)
156: throws SharedServicesException {
157: Object request = context.getExternalContext().getRequest();
158: if (request instanceof PortletRequest) {
159: return getCurrentMemberID((PortletRequest) request);
160: } else {
161: throw new SharedServicesException(
162: "Can't getCurrentMemberID: only supporting Portlet environment.");
163: }
164: }
165:
166: /**
167: * Clean up the resources.
168: */
169: public void destroy() {
170:
171: }
172:
173: private String getStringPreference(PortletRequest request,
174: String preference) {
175: if (request == null) {
176: logger.warning("Request is null");
177: return null;
178: }
179: if (preference == null) {
180: logger.warning("Preference is null");
181: return null;
182: }
183: PortletPreferences prefs = request.getPreferences();
184: if (prefs == null) {
185: logger.warning("Preferences is null");
186: return null;
187: }
188: return prefs.getValue(preference, null);
189: }
190:
191: /**
192: * Get the URL of the community search server.
193: *
194: * @param request
195: * @return url String
196: * @throws com.sun.portal.app.SharedServices.common.SharedServicesException
197: */
198: public String getSearchServerURL(PortletRequest request)
199: throws SharedServicesException {
200: return getStringPreference(request, SEARCH_SERVER_URL_ATTR);
201: }
202:
203: /**
204: * Get the URL of the community search server.
205: *
206: * @param context
207: * @return url String
208: * @throws com.sun.portal.app.SharedServices.common.SharedServicesException
209: */
210: public String getSearchServerURL(FacesContext context)
211: throws SharedServicesException {
212: Object request = context.getExternalContext().getRequest();
213: if (request instanceof PortletRequest) {
214: return getSearchServerURL((PortletRequest) request);
215: } else {
216: throw new SharedServicesException(
217: "Can't getCommunityID: only supporting Portlet environment.");
218: }
219: }
220:
221: /**
222: * Get the community search database.
223: *
224: * @param request
225: * @return database name
226: * @throws com.sun.portal.app.SharedServices.common.SharedServicesException
227: */
228: public String getSearchDatabase(PortletRequest request)
229: throws SharedServicesException {
230: return getStringPreference(request, SEARCH_DB_ATTR);
231: }
232:
233: /**
234: * Get the community search database.
235: *
236: * @param context
237: * @return database name
238: * @throws com.sun.portal.app.SharedServices.common.SharedServicesException
239: */
240: public String getSearchDatabase(FacesContext context)
241: throws SharedServicesException {
242: Object request = context.getExternalContext().getRequest();
243: if (request instanceof PortletRequest) {
244: return getSearchDatabase((PortletRequest) request);
245: } else {
246: throw new SharedServicesException(
247: "Can't getCommunityID: only supporting Portlet environment.");
248: }
249: }
250:
251: }
|