OIM API For Custom Access Pre Process Handler sample

  1. package com.oimacademy.provision;
  2. import java.io.Serializable;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.GregorianCalendar;
  6. import java.util.HashMap;
  7. import oracle.iam.platform.Platform;
  8. import oracle.iam.platform.kernel.ValidationException;
  9. import oracle.iam.platform.kernel.ValidationFailedException;
  10. import oracle.iam.platform.kernel.spi.ConditionalEventHandler;
  11. import oracle.iam.platform.kernel.spi.PreProcessHandler;
  12. import oracle.iam.platform.kernel.vo.AbstractGenericOrchestration;
  13. import oracle.iam.platform.kernel.vo.BulkEventResult;
  14. import oracle.iam.platform.kernel.vo.BulkOrchestration;
  15. import oracle.iam.platform.kernel.vo.EventResult;
  16. import oracle.iam.platform.kernel.vo.Orchestration;
  17. import oracle.iam.provisioning.api.ApplicationInstanceService;
  18. import oracle.iam.provisioning.util.Constants;
  19. import oracle.iam.provisioning.vo.ApplicationInstance;
  20. import oracle.iam.provisioning.vo.Entitlement;

  21. public class AccessPreProcessHandler implements PreProcessHandler, ConditionalEventHandler {
  22.  String handlerName;
  23.  String _appInstName;
  24.  private void log(String msg) {
  25.   System.out.println("[***" +handlerName+ "***]" + msg);
  26.  }
  27.  @Override
  28.  public void initialize(HashMap<String, String> arg0) {
  29.   handlerName = getClass().getSimpleName();
  30.  } 
  31.  private boolean checkIfApplicable(AbstractGenericOrchestration orchn){
  32.   String appInstName;
  33.   appInstName = getAppInstanceName(orchn);
  34.   if(appInstName == null) 
  35.    return false;
  36.   _appInstName = appInstName;
  37.   log("Enhanced check: AppInstName [" + appInstName + "] is configured for BreakGlass");
  38.   return true;
  39.  }
  40.  private String getAppInstanceName(AbstractGenericOrchestration orchn) {
  41.   HashMap<String, Serializable> orchParams = orchn.getParameters();
  42.   String appInstanceId = (String)orchParams.get(Constants.ORCH_PARAM_APPINSTANCE_KEY);
  43.   ApplicationInstance appInst = getApplicationInstance(appInstanceId);
  44.   if(appInst == null) 
  45.    return null;
  46.   log("App instance name for this ENT is: " + appInst.getApplicationInstanceName());
  47.   return appInst.getApplicationInstanceName();
  48.  }
  49.  private ApplicationInstance getApplicationInstance(String appInstanceId){
  50.   ApplicationInstance ai = null;
  51.   try {
  52.    ApplicationInstanceService aiService = getService(ApplicationInstanceService.class);
  53.    if(aiService == null) 
  54.     return null;
  55.    if (appInstanceId != null) {
  56.     ai = aiService.findApplicationInstanceByKey(new Long(appInstanceId)); 
  57.    }
  58.   } catch (Exception e) {
  59.    log("Exception getting AppInst based on Id: " + appInstanceId + ". Error: " + e);
  60.   }
  61.   return ai;
  62.  }
  63.  @Override
  64.  public boolean isApplicable(AbstractGenericOrchestration orchn) {
  65.   return checkIfApplicable(orchn);
  66.  }
  67.  @Override
  68.  public boolean cancel(long arg0, long arg1,
  69.    AbstractGenericOrchestration arg2) {
  70.   return false;
  71.  }
  72.  @Override
  73.  public void compensate(long arg0, long arg1,
  74.    AbstractGenericOrchestration arg2) {
  75.  }
  76.  @Override
  77.  public EventResult execute(long processId, long eventId, Orchestration orchestration) throws ValidationException, ValidationFailedException {
  78.   try {
  79.    log("In execute()");
  80.    if(!checkIfApplicable(orchestration))
  81.     return new EventResult();   
  82.    HashMap<String, Serializable> orchParams = orchestration.getParameters();
  83.    Entitlement entitlement = (Entitlement) orchParams.get(Constants.ORCH_PARAM_ENTITLEMENT);
  84.    String entitlementDisplayName = entitlement.getEntitlementValue();
  85.    String beneficiaryKey = (String) orchParams.get(Constants.ORCH_PARAM_BENEFICIARY_KEY);
  86.    HashMap orchParamsChildDataMap = (HashMap)orchParams.get(Constants.ORCH_PARAM_CHILD_DATA);
  87.    String mins = "15";
  88.    if(mins == null) {
  89.     log(entitlementDisplayName + " is setup without prefixing AppInstName tilde. Pls check.");
  90.     return new EventResult();
  91.    }
  92.    Date endDate = getEntitlementEndDate(mins);
  93.    log("Benefeciary: " + beneficiaryKey);
  94.    log("Entitlement assigned: " + entitlementDisplayName);
  95.    log("Entitlement END DATE is set to value:  " + endDate);
  96.    orchParamsChildDataMap.put("endDate", endDate);
  97.    return new EventResult();
  98.   } catch (Exception exc) {
  99.    return new EventResult();
  100.   }
  101.  } 
  102.  private Date getEntitlementEndDate(String minsToRevoke){
  103.   int mins = Integer.parseInt(minsToRevoke);
  104.   Calendar calender = new GregorianCalendar();
  105.   log("GENERIC CODE - CURRENT DATE: " + calender.getTime());
  106.   calender.add(Calendar.MINUTE, mins);
  107.   return calender.getTime();  
  108.  }
  109.  private <T> T getService(Class<T> serviceClass) {
  110.   return Platform.getService(serviceClass);
  111.  }
  112.  @Override
  113.  public BulkEventResult execute(long arg0, long arg1, BulkOrchestration arg2) {
  114.   log("REQUEST CAME IN BULK ORCHESTRATION. NUMBER OF REQUEST PARAMS ARE: " + arg2.getBulkParameters().length);
  115.   return new BulkEventResult();
  116.  } 
  117. }

No comments:

Post a Comment