Saturday, June 27, 2015

Send email to user from OAAM 11G

We came across one of the interesting requirement few months back that is, when user either blocked or updated his/her self-profile in OAAM, user should notified with an email about their status. Even though OAAM has ability to send email to user using java mail API but, it is hard to main the template. We came out with option that design and create email templates in OIM as all emails templates in centralized in one location and easy to maintain. OAAM will make call to OIM to send emails to user.

Note: OIM-OAM-OAAM-OID-OVD were integrated together.

In this post, we are going to see how to read and connect to OIM from OAAM. Since OIM and OAAM were integrated, OIM connection details were maintained in OAAM properties. If Credential Store Factory (CSF) is enabled in OAAM then, OIM credentials available in Weblogic CSF. Otherwise, it will be in OAAM properties.

Make sure you need to pass OIM user login id as well, email template which you have created in OIM to the below method.
You need to import the below classes in order to read credentials from weblogic CSF:
import oracle.oaam.common.util.CSFUtil;
import oracle.security.jps.service.credstore.PasswordCredential;
private void sendEmailFromOIM(String userLogin, String emailTemplate)
{
 try {
  OIMClient mOimClient = null;
  String oimUsername = null;
  String oimPassword = null;

  boolean isCSFEnabled = BharosaConfig.getBoolean("oaam.oim.csf.credentials.enabled", false);
  System.out.println("isCSFEnabled : " + isCSFEnabled);

  if (isCSFEnabled) {
   // Read OIM Credentials from CSF if enabled. 
   PasswordCredential oimCredentials = (PasswordCredential)CSFUtil.getCredential("oaam", "oim.credentials");
   if (oimCredentials != null) {
    oimUsername = oimCredentials.getName();
    oimPassword = new String(oimCredentials.getPassword());
   } else {
    String message = "OIM CSF Credentials flag 'oaam.oim.csf.credentials.enabled' is set to true but there are no credentials under 'oim.credentials' alias";
    System.out.println("message : " + message);
   }
  } else {
   // Read OIM Credentials from OAAM properties. 
   oimUsername = BharosaConfig.get("oaam.oim.admin.loginid");
   oimPassword = BharosaConfig.get("oaam.oim.admin.password");
  }

  // Print OIM credentials :: TESTING PURPOSE ONLY
  System.out.println("oimUserName : " + oimUsername);
  System.out.println("oimPassword : " + oimPassword);
  
  String oimUrl = BharosaConfig.get("oaam.oim.url");
  System.out.println("oimUrl : " + oimUrl);

  String oimInitCtxFactory = BharosaConfig.get("oaam.oim.initial.context.factory", "weblogic.jndi.WLInitialContextFactory");
  System.out.println("oimInitCtxFactory : " + oimInitCtxFactory);

  String xlHomeDir = BharosaConfig.get("oaam.oim.xl.homedir", "${oracle.oaam.home}/../designconsole");
  System.out.println("xlHomeDir : " + xlHomeDir);

  String authCfg = BharosaConfig.get("oaam.oim.auth.login.config", "${oracle.oaam.home}/../designconsole/config/authwl.conf");
  System.out.println("authCfg : " + authCfg);

  oimUrl = StringUtil.expandProperties(oimUrl);
  oimInitCtxFactory = StringUtil.expandProperties(oimInitCtxFactory);
  xlHomeDir = StringUtil.expandProperties(xlHomeDir);
  authCfg = StringUtil.expandProperties(authCfg);

  System.setProperty("XL.HomeDir", xlHomeDir);
  System.setProperty("java.security.auth.login.config", authCfg);

  Hashtable env = new Hashtable();
  env.put("java.naming.factory.initial", oimInitCtxFactory);
  env.put("java.naming.provider.url", oimUrl);

  System.out.println("connecting to oim....");
  // Instantiate OIMClient
  mOimClient = new OIMClient(env);
  mOimClient.login(oimUsername, oimPassword.toCharArray());
  System.out.println("oim connection established");

  // Data required for email 
  HashMap emailData = new HashMap();
  emailData.put("Display_Name", userLogin);
  
  //Instantiate Notification Service for email
  NotificationService notifySrvc = (NotificationService)mOimClient.getService(NotificationService.class);
  NotificationEvent emailEvent = new NotificationEvent();
  emailEvent.setTemplateName(emailTemplate);
  emailEvent.setSender("NOTIFICATIONADMIN");
  emailEvent.setUserIds(new String[] { userLogin });
  emailEvent.setParams(emailData);
  
  try {
   // Send email to user
   notifySrvc.notify(emailEvent);
  } catch (Exception e) {
   System.out.println("Error on sending email : " + e.getMessage());
   e.printStackTrace();
  }
 }
 catch (Exception e) {
  System.out.println("OIM Changes :" + e.getLocalizedMessage());
  e.printStackTrace();
 }
}

NOTE: In this example, I have printed all values including OIM credentials for testing purpose. Make sure you should not print in any of value in your environment.