/** * Copyright (c) 1996-2006 Cafesoft, LLC. All Rights Reserved. * * This software is the confidential and proprietary information of * Cafesoft, LLC. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Cafesoft. * * CAFESOFT MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. CAFESOFT SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ package examples.service; import examples.service.TextNotifierService; import com.cafesoft.cams.Config; import com.cafesoft.cams.Context; import com.cafesoft.cams.ConfigException; import com.cafesoft.cams.session.ManagedSession; import com.cafesoft.cams.session.ManagedSessionEvent; import com.cafesoft.cams.session.ManagedSessionEventType; import com.cafesoft.cams.session.ManagedSessionEventHandler; import com.cafesoft.cams.auth.CSRolePrincipal; import com.cafesoft.cams.auth.SubjectUtils; import com.cafesoft.core.service.Service; import com.cafesoft.core.service.ServiceFinder; import com.cafesoft.core.service.ServiceException; import com.cafesoft.core.log.Logger; import javax.security.auth.Subject; import java.security.Principal; import java.util.Date; import java.util.Iterator; /** * RoleLoginNotifier is a ManagedSessionEventHandler that * sends a text message whenever a user successfully authenticates and has the * defined role. This class demonstrates the lookup and use of a Cams service * that implements the text notification. * * @version $Revision: 1.5 $ $Date: 2006/08/30 18:31:01 $ * @author Norbert K. Kuhnert */ public class RoleLoginNotifier implements ManagedSessionEventHandler { /** The Config object provided at initialization */ private Config config; /** The Context object provided at initialization. */ private Context context; /** The Logger used to report DEBUG, INFO, WARNING, ERROR, and FATAL messages */ private Logger logger; /** The flag that enables/disables DEBUG-level messages. */ private boolean debug = false; /** The configureable sender (from) address. */ private String fromAddress; /** The configureable message subject. */ private String msgSubject; /** The configureable role. */ private String roleName; /** * Initialize the ManagedSessionEventHandler. * * @param config a Config object that provides access to configuration * parameters and a runtime Context. */ public void initialize(Config config) throws ConfigException { this.config = config; // Check required configuration parameters. this.fromAddress = config.getInitParameter("fromAddress"); if (fromAddress == null) throw new ConfigException("missing required parameter: fromAddress"); // Set the message subject. this.msgSubject = config.getInitParameter("msgSubject", "Role Name Login Notification"); // Set the role. this.roleName = config.getInitParameter("roleName"); if (roleName == null) throw new ConfigException("missing required parameter: roleName"); this.context = config.getContext(); this.logger = context.getLogger(); } /** * Enable or disable debugging. * * @param enable if true, enable debugging, else disable debugging. * @return the previous value of the debug flag, which is useful for * restoring the debug state if temporarily changed. */ public boolean setDebug(boolean enable) { boolean prevDebug = this.debug; this.debug = enable; return prevDebug; } /** * Handle a ManagedSessionEvent. * * @param event the ManagedSessionEvent to be handled. */ public void handleManagedSessionEvent(ManagedSessionEvent event) { // If the session is newly created ... if (event.getEventType() == ManagedSessionEventType.SESSION_CREATED) { // Then scan the principals associated with the Subject ... Subject subject = event.getManagedSession().getSubject(); Iterator i = subject.getPrincipals().iterator(); while (i.hasNext()) { Principal p = (Principal)i.next(); // And if the Subject has the defined role if (roleName.equals(p.getName())) { sendTextMessage(event); break; } } } } /** * Send the text message. * * @param event the ManagedSessionEvent. */ private void sendTextMessage(ManagedSessionEvent event) { try { if (debug) logger.debug(this, "Looking up TextNotifierService instances"); // Lookup the TextNotifierService instance(s) by type Service[] serviceArray = context.getServiceFinder().find(TextNotifierService.class); // Send the message to every available TextNotifierService String body = createMessageBody(event); for (int i = 0; i < serviceArray.length; i++) { TextNotifierService tns = (TextNotifierService)serviceArray[i]; if (debug) logger.debug(this, "Sending text message to service:\n" + " id=" + tns.getServiceConfig().getId() + "\n" + " type=" + tns.getClass().getName()); tns.sendText(fromAddress, msgSubject, body); } } catch (ServiceException e) { logger.error(this, "Unable to find TextNotifierService"); } } /** * Create the body of the message. * * @param event the event through which information about the session * is available. * @return a String containing the body of the notification message. */ private String createMessageBody(ManagedSessionEvent event) { ManagedSession session = event.getManagedSession(); Subject subject = session.getSubject(); StringBuffer b = new StringBuffer(); b.append("The following user just authenticated:\n"); b.append("\n"); b.append(" user: " + SubjectUtils.getUserName(subject) + "\n"); b.append(" security domain: " + session.getSecurityDomainName() + "\n"); b.append(" time: " + new Date(session.getCreationTime()) + "\n"); b.append(" roles: "); int num = 0; Iterator i = subject.getPrincipals(CSRolePrincipal.class).iterator(); while (i.hasNext()) { Principal p = (Principal)i.next(); if (num > 0) b.append(", "); b.append(p.getName()); num++; } b.append("\n"); return b.toString(); } } // End of class: RoleLoginNotifier