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.
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.test.provider;
038:
039: import java.io.IOException;
040: import java.io.FileNotFoundException;
041:
042: import java.util.ResourceBundle;
043: import java.util.MissingResourceException;
044: import java.util.Locale;
045: import java.util.Properties;
046: import java.util.logging.Logger;
047: import java.util.logging.Level;
048: import java.util.logging.LogRecord;
049:
050: import javax.servlet.http.HttpServletRequest;
051: import javax.servlet.http.HttpServletResponse;
052:
053: import com.sun.portal.log.common.PortalLogger;
054:
055: import com.sun.portal.portlet.impl.PortletRequestConstants;
056:
057: import com.sun.portal.community.CommunityManager;
058: import com.sun.portal.community.CommunityFactory;
059: import com.sun.portal.community.CommunityException;
060: import com.sun.portal.community.Community;
061: import com.sun.portal.community.CommunityId;
062: import com.sun.portal.community.CommunityUser;
063:
064: import com.sun.portal.util.ResourceLoader;
065:
066: import com.iplanet.sso.SSOTokenManager;
067: import com.iplanet.sso.SSOToken;
068: import com.iplanet.sso.SSOException;
069:
070: /**
071: * This is the base class that any JSF backing bean (i.e. handler) used
072: * in the JSF portlets can extend. This class makes some basic
073: * portlet-ish information available in the backing bean.
074: */
075: public class CommunityBaseHandler implements java.io.Serializable {
076:
077: private static final String SEARCH_SERVER_URL_KEY = "search-server-url";
078: private static final String COMMUNITY_SEARCH_DB_PREFIX_KEY = "community-search-database-prefix";
079: private static final String SEARCH_TAXONOMY_ROOT_KEY = "search-taxonomy-root";
080: private static final String DP_PRIORITY_BASE_KEY = "dp-priority-base";
081:
082: public static final String COMMUNITY_MESSAGES_RESOURCE_BUNDLE_NAME = "com.sun.portal.app.communityportlets.bundle.communityMessages";
083:
084: public static final String CATEGORY_SESSION_ATTRIBUTE_KEY = "com.sun.portal.app.communityportlets.faces.currentCategory";
085: public static final String CATEGORY_ID_SESSION_ATTRIBUTE_KEY = "com.sun.portal.app.communityportlets.faces.currentCategoryId";
086: public static final String COMMUNITY_CREATED_SESSION_ATTRIBUTE_KEY = "com.sun.portal.app.communityportlets.faces.communityCreated";
087:
088: private static Properties configProperties = null;
089:
090: private static Logger logger = PortalLogger
091: .getLogger(CommunityBaseHandler.class);
092:
093: public static final String COMMUNITY_PORTLETS_CONFIG_FILE = "communityportlets.properties";
094:
095: static {
096: ResourceLoader rl = ResourceLoader.getInstance(System
097: .getProperties());
098: try {
099: configProperties = rl
100: .getProperties(COMMUNITY_PORTLETS_CONFIG_FILE);
101: } catch (FileNotFoundException fnfe) {
102: logger.log(Level.SEVERE, "PSCPL_CSPACF00000", fnfe);
103: throw new RuntimeException("CommunityBaseHandler: ", fnfe);
104: } catch (IOException ioe) {
105: logger.log(Level.SEVERE, "PSCPL_CSPACF00000", ioe);
106: throw new RuntimeException("CommunityBaseHandler: ", ioe);
107: }
108:
109: if (logger.isLoggable(Level.INFO)) {
110: logger.log(Level.INFO, "PSCPL_CSPACF00001",
111: configProperties);
112: }
113: }
114:
115: protected Locale userLocale;
116: protected String userName;
117:
118: HttpServletRequest req;
119: HttpServletResponse response;
120:
121: public CommunityBaseHandler(HttpServletRequest req,
122: HttpServletResponse response) {
123: this .req = req;
124: this .response = response;
125: }
126:
127: protected String getConfigParameter(String key) {
128: //
129: // makes an assumption that every property is mandatory
130: //
131: String val = configProperties.getProperty(key);
132: if (val == null || val.length() == 0) {
133: logger.log(Level.SEVERE, "PSCPL_CSPACF00002", key);
134: throw new NullPointerException("config parameter, " + key
135: + " is either undefined or null.");
136: }
137: return val;
138: }
139:
140: protected String getSearchServerUrl() {
141: return getConfigParameter(SEARCH_SERVER_URL_KEY);
142: }
143:
144: private String getCommunitySearchDbPrefix() {
145: return getConfigParameter(COMMUNITY_SEARCH_DB_PREFIX_KEY);
146: }
147:
148: protected String getCommunitiesSearchDb() {
149: String pre = getCommunitySearchDbPrefix();
150: return pre + "_communities_" + getPortalId();
151: }
152:
153: protected String getCommunityContentsSearchDb(
154: CommunityId communityId) {
155: String pre = getCommunitySearchDbPrefix();
156: return pre + "_contents_" + getPortalId() + "." + communityId;
157: }
158:
159: protected String getCommunityDiscussionsSearchDb(
160: CommunityId communityId) {
161: String pre = getCommunitySearchDbPrefix();
162: return pre + "_discussions_" + getPortalId() + "."
163: + communityId;
164:
165: }
166:
167: protected String getSearchTaxonomyRoot() {
168: return getConfigParameter(SEARCH_TAXONOMY_ROOT_KEY);
169: }
170:
171: protected int getDpPriorityBase() {
172: String base = getConfigParameter(DP_PRIORITY_BASE_KEY);
173:
174: return new Integer(base).intValue();
175: }
176:
177: protected String getPortalId() {
178: String id = ResourceLoader.getInstance(System.getProperties())
179: .getPortalId();
180: if (id == null || id.length() == 0) {
181: logger.severe("PSCPL_CSPACF00003");
182: throw new NullPointerException("portal id is null");
183: }
184:
185: if (logger.isLoggable(Level.FINER)) {
186: logger.log(Level.FINER, "PSCPL_CSPACF00004", id);
187: }
188:
189: return id;
190: }
191:
192: protected HttpServletRequest getHttpServletRequest() {
193:
194: return req;
195: }
196:
197: protected HttpServletResponse getHttpServletResponse() {
198:
199: return response;
200: }
201:
202: protected CommunityManager getCommunityManager() {
203:
204: HttpServletRequest req = getHttpServletRequest();
205: HttpServletResponse res = getHttpServletResponse();
206:
207: CommunityManager cm = null;
208: try {
209: cm = CommunityFactory.getInstance().getCommunityManager(
210: req, res);
211: } catch (CommunityException ce) {
212: logger.log(Level.SEVERE, "PSCPL_CSPACF00007", ce);
213: throw new RuntimeException(ce);
214: }
215:
216: return cm;
217: }
218:
219: /*
220: protected CommunityId getCommunityId() {
221: String cidString = getPreference(CommunityFactory.COMMUNITY_ID_PREF, null);
222: if (cidString == null || cidString.length() == 0) {
223: logger.severe("PSCPL_CSPACF00008");
224: throw new NullPointerException("cannot get community ID from preferences");
225: }
226: CommunityId cid = new CommunityId(cidString);
227: return cid;
228: }
229:
230: protected Community getCommunity() throws CommunityException {
231: return getCommunity(getCommunityId());
232: }
233: */
234:
235: protected Community getCommunity(CommunityId cid)
236: throws CommunityException {
237: HttpServletRequest req = getHttpServletRequest();
238: HttpServletResponse res = getHttpServletResponse();
239:
240: Community c = null;
241: c = CommunityFactory.getInstance().getCommunity(req, res,
242: getSSOToken(req), cid);
243:
244: return c;
245: }
246:
247: protected CommunityUser getCommunityUser() {
248: return getCommunityUser(null);
249: }
250:
251: protected CommunityUser getCommunityUser(String userID) {
252: HttpServletRequest req = getHttpServletRequest();
253: HttpServletResponse res = getHttpServletResponse();
254:
255: CommunityUser cu = null;
256: try {
257: if (userID != null) {
258: cu = CommunityFactory.getInstance().getCommunityUser(
259: req, userID);
260: } else {
261: cu = CommunityFactory.getInstance().getCommunityUser(
262: req);
263: }
264: } catch (CommunityException ce) {
265: if (userID != null) {
266: logRecord(logger, Level.SEVERE, "PSCPL_CSPACF00009",
267: ce, userID);
268: } else {
269: logger.log(Level.SEVERE, "PSCPL_CSPACF00010", ce);
270: }
271: throw new RuntimeException(ce);
272: }
273:
274: return cu;
275: }
276:
277: private SSOToken getSSOToken(HttpServletRequest request)
278: throws CommunityException {
279: SSOToken ssoToken = null;
280: try {
281: SSOTokenManager ssoTokenMgr = SSOTokenManager.getInstance();
282: ssoToken = ssoTokenMgr.createSSOToken(request);
283: if (ssoToken == null) {
284: throw new RuntimeException(
285: "Expecting a request with SSOToken");
286: }
287: return ssoToken;
288: } catch (SSOException se) {
289: throw new CommunityException(
290: "CommunityBaseHandler.init(): failed to create ssoToken.",
291: se);
292: }
293: }
294:
295: public void logRecord(Logger logger, Level level, String msgKey,
296: Throwable th, Object parameter) {
297: LogRecord logRecord = new LogRecord(level, msgKey);
298: logRecord.setLoggerName(logger.getName());
299: logRecord.setThrown(th);
300: logRecord.setParameters(new Object[] { parameter });
301: logger.log(logRecord);
302: }
303:
304: public void logRecord(Logger logger, Level level, String msgKey,
305: Throwable th, Object[] parameters) {
306: LogRecord logRecord = new LogRecord(level, msgKey);
307: logRecord.setLoggerName(logger.getName());
308: logRecord.setThrown(th);
309: logRecord.setParameters(parameters);
310: logger.log(logRecord);
311: }
312: }
|