Saturday, 11 March 2017

Create a Custom Resolver in OIM



In this post I will create a custom resolver.

Whenever a template is called for sending email notification it resolves data  like ($User_ID) in the template and replaces them with the user information.

This functionality can be customized by creating a custom resolver class. I had a requirement that a password warning template should also send count down to expiry date
e.g. 3 days left in your password expiry.

To do this I created a custom resolver class and changed the previous event resolver class with mine in Metadata of OIM.




=========================================================================
package com.handler.iam;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 import static oracle.iam.identity.utils.Constants.USERID;
 import static oracle.iam.identity.utils.Constants.USERKEY;

import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.vo.Identity;
import oracle.iam.notification.api.NotificationService;
import oracle.iam.notification.impl.NotificationEventResolver;
import oracle.iam.notification.vo.NotificationAttribute;
import oracle.iam.platform.Platform;


public class CustomNotificationHandler implements NotificationEventResolver{

    public CustomNotificationHandler() {}


    public List<NotificationAttribute> getAvailableData(String eventType, Map<String, Object> map) {

        List<NotificationAttribute> list =
new ArrayList<NotificationAttribute>();
        NotificationAttribute na= new NotificationAttribute();
        na.setEntityName("DaysLeftinPWDExpiry");
        na.setName("DaysLeftinPWDExpiry");
        list.add(na);
        return list;
    }

    public HashMap<String, Object> getReplacedData(String eventType, Map<String, Object> eventParams) throws Exception {
       
        HashMap<String, Object> resolvedNotificationData = new HashMap<String, Object>();
        GetUserName gtn=new GetUserName();
        String UserName=gtn.getUserInfo(eventParams.get("usr_key").toString());
        resolvedNotificationData.put("User_Name", UserName);
        String Date1=gtn.getDate(eventParams.get("usr_key").toString());
        resolvedNotificationData.put("PwdExpDate", Date1);
        Date CurrentDate=java.util.Calendar.getInstance().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date PwdExpDate=null;
        try{
        PwdExpDate= sdf.parse(Date1);
        //CurrentDate=sdf.parse(CurrentDate.toString());
        } catch (ParseException e) {
           System.out.println(e.toString());
        }
        long diff = PwdExpDate.getTime() - CurrentDate.getTime();//in Milli seconds
        long numOfDays = diff/(1000*60*60*24);
        resolvedNotificationData.put("DaysLeft", (numOfDays));
        return resolvedNotificationData;
    }
}
==========================================================================

package com.handler.iam;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class GetUserName {
    public GetUserName() {
        super();
    }
    public  String getUserInfo(String user_key) {
                String ul = null;
                try {
                        // step1 load the driver class
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        // step2 create the connection object
                        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@IPADDRESSofDB:PORT:ServiceName", "OIM SCHEMA NAME",
                                        "Password");
                        // step3 create the statement object
                        Statement stmt = con.createStatement();
                        // step4 execute query
                        ResultSet rs = stmt.executeQuery("select USR_LOGIN from OIG_OIM.USR where USR_KEY ='"+user_key+"'");
                        while (rs.next()) {
                                ul = rs.getString("USR_LOGIN");
                        }
                        con.close();
                } catch (Exception e) {
                        System.out.println(e);
                }
          
                return ul;
        }
    public  String getDate(String user_key) {
                String ul = null;
                try {
                        // step1 load the driver class
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        // step2 create the connection object
                        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@IPADDRESSofDB:PORT:ServiceName", "OIM SCHEMA NAME",
                                        "Password");
                        // step3 create the statement object
                        Statement stmt = con.createStatement();
                        // step4 execute query
                        ResultSet rs = stmt.executeQuery("select USR_PWD_EXPIRE_DATE from OIG_OIM.USR where USR_KEY ='"+user_key+"'");
                        while (rs.next()) {
                                ul = rs.getString("USR_PWD_EXPIRE_DATE");
                        }
                        con.close();
                } catch (Exception e) {
                        System.out.println(e);
                }
          
                return ul;
        }
}
==========================================================================

These 3 variable are Resolved:

$DaysLeft $User_Name $PwdExpDate

Now Create a plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<oimplugins xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <plugins pluginpoint="oracle.iam.notification.impl.NotificationEventResolver">
    <plugin pluginclass="com.handler.iam.CustomNotificationHandler" version="2.1" name="Custom Notification Handler">
    </plugin>
  </plugins>
</oimplugins>

Register this Plugin & put resolver class in metadata event of your choice as com.handler.iam.CustomNotificationHandler


1 comment:

  1. This is a nice blog. Thanks for that. I am wondering how the Event Metadata xml for this. Could you please post that too in this blog?

    ReplyDelete