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.app.communityportlets.faces;
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: import java.util.ArrayList;
050:
051: import javax.servlet.http.HttpServletRequest;
052: import javax.servlet.http.HttpServletResponse;
053:
054: import javax.faces.context.FacesContext;
055: import javax.faces.context.ExternalContext;
056: import javax.faces.application.Application;
057: import javax.faces.application.FacesMessage;
058:
059: import javax.portlet.PortletContext;
060: import javax.portlet.PortletRequest;
061: import javax.portlet.PortletResponse;
062:
063: import com.sun.portal.log.common.PortalLogger;
064:
065: import com.sun.portal.portlet.impl.PortletRequestConstants;
066:
067: import com.sun.portal.community.CommunityManager;
068: import com.sun.portal.community.CommunityFactory;
069: import com.sun.portal.community.CommunityException;
070: import com.sun.portal.community.Community;
071: import com.sun.portal.community.CommunityId;
072: import com.sun.portal.community.CommunityUser;
073:
074: import com.sun.portal.util.ResourceLoader;
075: import com.iplanet.sso.SSOToken;
076: import com.iplanet.sso.SSOTokenManager;
077: import com.iplanet.sso.SSOException;
078:
079: /**
080: * This is the base class that any JSF backing bean (i.e. handler) used
081: * in the JSF portlets can extend. This class makes some basic
082: * portlet-ish information available in the backing bean.
083: */
084: public class CommunityBaseHandler implements java.io.Serializable {
085: private static final String DP_PRIORITY_BASE_KEY = "dp-priority-base";
086:
087: public static final String COMMUNITY_MESSAGES_RESOURCE_BUNDLE_NAME = "com.sun.portal.app.communityportlets.bundle.communityMessages";
088:
089: public static final String CATEGORY_SESSION_ATTRIBUTE_KEY = "com.sun.portal.app.communityportlets.faces.currentCategory";
090: public static final String CATEGORY_ID_SESSION_ATTRIBUTE_KEY = "com.sun.portal.app.communityportlets.faces.currentCategoryId";
091: public static final String COMMUNITY_CREATED_DELETE_NOTIFICATION_SESSION_ATTRIBUTE_KEY = "com.sun.portal.app.communityportlets.faces.communityCreatedDelete";
092:
093: private static Properties configProperties = null;
094:
095: private static Logger logger = PortalLogger
096: .getLogger(CommunityBaseHandler.class);
097:
098: public static final String COMMUNITY_PORTLETS_CONFIG_FILE = "communityportlets.properties";
099:
100: static {
101: ResourceLoader rl = ResourceLoader.getInstance(System
102: .getProperties());
103: try {
104: configProperties = rl
105: .getProperties(COMMUNITY_PORTLETS_CONFIG_FILE);
106: } catch (FileNotFoundException fnfe) {
107: logger.log(Level.SEVERE, "PSCPL_CSPACF00000", fnfe);
108: throw new RuntimeException("CommunityBaseHandler: ", fnfe);
109: } catch (IOException ioe) {
110: logger.log(Level.SEVERE, "PSCPL_CSPACF00000", ioe);
111: throw new RuntimeException("CommunityBaseHandler: ", ioe);
112: }
113:
114: if (logger.isLoggable(Level.INFO)) {
115: logger.log(Level.INFO, "PSCPL_CSPACF00001",
116: configProperties);
117: }
118: }
119:
120: protected Locale userLocale;
121: protected String userName;
122:
123: protected String getConfigParameter(String key) {
124: //
125: // makes an assumption that every property is mandatory
126: //
127: String val = configProperties.getProperty(key);
128: if (val == null || val.length() == 0) {
129: logger.log(Level.SEVERE, "PSCPL_CSPACF00002", key);
130: throw new NullPointerException("config parameter, " + key
131: + " is either undefined or null.");
132: }
133: return val;
134: }
135:
136: protected String getPortalId() {
137: String id = ResourceLoader.getInstance(System.getProperties())
138: .getPortalId();
139: if (id == null || id.length() == 0) {
140: logger.severe("PSCPL_CSPACF00003");
141: throw new NullPointerException("portal id is null");
142: }
143:
144: if (logger.isLoggable(Level.FINER)) {
145: logger.log(Level.FINER, "PSCPL_CSPACF00004", id);
146: }
147:
148: return id;
149: }
150:
151: /**
152: * Get user's locale. If not found, use system (JVM) default locale.
153: *
154: * @return user's locale
155: */
156: public Locale getUserLocale() {
157:
158: ExternalContext eContext = getExternalContext();
159: if (eContext == null) {
160: logger.severe("PSCPL_CSPACF00005");
161: throw new RuntimeException(
162: "CommunityBaseHandler: eContext=null");
163: }
164:
165: PortletRequest pReq = (PortletRequest) eContext.getRequest();
166: if (pReq == null) {
167: logger.severe("PSCPL_CSPACF00006");
168: throw new RuntimeException(
169: "CommunityBaseHandler: pReq=null");
170: }
171:
172: Locale locale = pReq.getLocale();
173:
174: //
175: // default to system locale if none if found
176: //
177: if (locale == null) {
178: locale = Locale.getDefault();
179: }
180:
181: return locale;
182: }
183:
184: public String getUserName() {
185: // TODO: this must come from the SSO token
186: FacesContext fContext = FacesContext.getCurrentInstance();
187: ExternalContext eContext = fContext.getExternalContext();
188:
189: PortletRequest pReq = (PortletRequest) eContext.getRequest();
190:
191: return pReq.getRemoteUser();
192: }
193:
194: protected String getPreference(String name, String defaultValue) {
195:
196: FacesContext fContext = FacesContext.getCurrentInstance();
197: ExternalContext eContext = fContext.getExternalContext();
198: PortletRequest pReq = (PortletRequest) eContext.getRequest();
199: return pReq.getPreferences().getValue(name, defaultValue);
200: }
201:
202: protected String[] getPreferenceValues(String name,
203: String[] defaultValue) {
204:
205: FacesContext fContext = FacesContext.getCurrentInstance();
206: ExternalContext eContext = fContext.getExternalContext();
207: PortletRequest pReq = (PortletRequest) eContext.getRequest();
208: return pReq.getPreferences().getValues(name, defaultValue);
209: }
210:
211: protected static ExternalContext getExternalContext() {
212:
213: FacesContext fContext = FacesContext.getCurrentInstance();
214: ExternalContext eContext = fContext.getExternalContext();
215:
216: return eContext;
217: }
218:
219: protected static HttpServletRequest getHttpServletRequest() {
220:
221: ExternalContext eContext = getExternalContext();
222: PortletRequest pReq = (PortletRequest) eContext.getRequest();
223:
224: HttpServletRequest req = (HttpServletRequest) pReq
225: .getAttribute(PortletRequestConstants.HTTP_SERVLET_REQUEST);
226:
227: return req;
228: }
229:
230: protected static HttpServletResponse getHttpServletResponse() {
231:
232: ExternalContext eContext = getExternalContext();
233: PortletRequest pReq = (PortletRequest) eContext.getRequest();
234:
235: HttpServletResponse res = (HttpServletResponse) pReq
236: .getAttribute(PortletRequestConstants.HTTP_SERVLET_RESPONSE);
237:
238: return res;
239: }
240:
241: protected FacesMessage getFacesMessage(FacesContext context,
242: String key) {
243:
244: //
245: // load resource bundle
246: //
247: Application app = context.getApplication();
248: String messageBundleName = app.getMessageBundle();
249: Locale locale = context.getViewRoot().getLocale();
250: if (locale == null) {
251: locale = Locale.getDefault();
252: }
253: ResourceBundle rb = ResourceBundle.getBundle(
254: COMMUNITY_MESSAGES_RESOURCE_BUNDLE_NAME, locale);
255:
256: //
257: // fetch message from the resource bundle
258: //
259: String msg = rb.getString(key);
260: String msgDetail;
261: try {
262: msgDetail = rb.getString(key + "_detail");
263: } catch (MissingResourceException mre) {
264: msgDetail = msg;
265: }
266:
267: if (msgDetail == null || msgDetail.length() < 1) {
268: msgDetail = msg;
269: }
270: FacesMessage facesMessage = new FacesMessage(
271: FacesMessage.SEVERITY_ERROR, msg, msgDetail);
272:
273: return facesMessage;
274: }
275:
276: protected String getMessage(FacesContext context, String key) {
277:
278: //
279: // load resource bundle
280: //
281: Application app = context.getApplication();
282: String messageBundleName = app.getMessageBundle();
283: Locale locale = context.getViewRoot().getLocale();
284: ResourceBundle rb = ResourceBundle.getBundle(
285: COMMUNITY_MESSAGES_RESOURCE_BUNDLE_NAME, locale);
286:
287: //
288: // fetch message from the resource bundle
289: //
290: String msg = rb.getString(key);
291:
292: return msg;
293: }
294:
295: protected CommunityManager getCommunityManager() {
296:
297: HttpServletRequest req = getHttpServletRequest();
298: HttpServletResponse res = getHttpServletResponse();
299:
300: CommunityManager cm = null;
301: try {
302: cm = CommunityFactory.getInstance().getCommunityManager(
303: req, res);
304: } catch (CommunityException ce) {
305: logger.log(Level.SEVERE, "PSCPL_CSPACF00007", ce);
306: throw new RuntimeException(ce);
307: }
308:
309: return cm;
310: }
311:
312: protected CommunityId getCommunityId() {
313: String cidString = getPreference(
314: CommunityFactory.COMMUNITY_ID_PREF, null);
315: if (cidString == null || cidString.length() == 0) {
316: logger.severe("PSCPL_CSPACF00008");
317: throw new NullPointerException(
318: "cannot get community ID from preferences");
319: }
320: CommunityId cid = new CommunityId(cidString);
321: return cid;
322: }
323:
324: protected Community getCommunity() throws CommunityException {
325: return getCommunity(getCommunityId());
326: }
327:
328: protected Community getCommunity(CommunityId cid)
329: throws CommunityException {
330: HttpServletRequest req = getHttpServletRequest();
331: HttpServletResponse res = getHttpServletResponse();
332:
333: Community c = null;
334: c = CommunityFactory.getInstance().getCommunity(req, res,
335: getSSOToken(), cid);
336:
337: return c;
338: }
339:
340: protected CommunityUser getCommunityUser() {
341: return getCommunityUser(null);
342: }
343:
344: protected CommunityUser getCommunityUser(String userID) {
345: HttpServletRequest req = getHttpServletRequest();
346: HttpServletResponse res = getHttpServletResponse();
347:
348: CommunityUser cu = null;
349: try {
350: if (userID != null) {
351: cu = CommunityFactory.getInstance().getCommunityUser(
352: req, userID);
353: } else {
354: cu = CommunityFactory.getInstance().getCommunityUser(
355: req);
356: }
357: } catch (CommunityException ce) {
358: if (userID != null) {
359: logRecord(logger, Level.SEVERE, "PSCPL_CSPACF00009",
360: ce, userID);
361: } else {
362: logger.log(Level.SEVERE, "PSCPL_CSPACF00010", ce);
363: }
364: throw new RuntimeException(ce);
365: }
366:
367: return cu;
368: }
369:
370: public SSOToken getSSOToken() throws CommunityException {
371: try {
372: SSOTokenManager manager = SSOTokenManager.getInstance();
373: return manager.createSSOToken(getHttpServletRequest());
374: } catch (SSOException ex) {
375: throw new CommunityException(ex);
376: }
377: }
378:
379: public String getAppContext() {
380: return FacesContext.getCurrentInstance().getExternalContext()
381: .getRequestContextPath();
382: }
383:
384: public void logRecord(Logger logger, Level level, String msgKey,
385: Throwable th, Object parameter) {
386: LogRecord logRecord = new LogRecord(level, msgKey);
387: logRecord.setLoggerName(logger.getName());
388: logRecord.setThrown(th);
389: logRecord.setParameters(new Object[] { parameter });
390: logger.log(logRecord);
391: }
392:
393: public void logRecord(Logger logger, Level level, String msgKey,
394: Throwable th, Object[] parameters) {
395: LogRecord logRecord = new LogRecord(level, msgKey);
396: logRecord.setLoggerName(logger.getName());
397: logRecord.setThrown(th);
398: logRecord.setParameters(parameters);
399: logger.log(logRecord);
400: }
401:
402: }
|