WebService Connector - Before Operation Rule - It's possible to use complex code?

Hello everyone!

In Web Services Before Operation Rule can I use Java structure like methods and classes? can I add a class with my operation methods and use it?

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import connector.common.JsonUtil;
import connector.common.Util;
import sailpoint.connector.webservices.EndPoint;
import sailpoint.connector.webservices.WebServicesClient;
import sailpoint.object.Application;
import sailpoint.object.ProvisioningPlan;
import sailpoint.object.ProvisioningPlan.AccountRequest;


public class MyTeste {


  sailpoint.connector.webservices.EndPoint requestEndPoint;

  public MyTeste(sailpoint.connector.webservices.EndPoint requestEndPoint) {
    this.requestEndPoint = requestEndPoint;
  }

  public String doSomething(){
    return "TESTE";
  }

  public String doAnotherThing(){
    return "TESTE";
  }
}


Map body = requestEndPoint.getBody();
String jsonBody = (String) body.get("jsonBody");
log.info("Modify Body: running");

try {
    MyTeste myTesteObj = new MyTeste(requestEndPoint); 


    Map jsonMap = JsonUtil.toMap(jsonBody);
    if (jsonMap != null) {
    if (provisioningPlan != null) {
        log.info("Rule - Modify Body: plan is not null == " + provisioningPlan);
    } else {
        log.info("Rule - Modify Body: plan is null");
    }

    String finalBody = JsonUtil.render(jsonMap);
    body.put("jsonBody", finalBody);
    requestEndPoint.setBody(body);
    }
} catch (Exception ex) {
    log.error("Rule - Modify Body: " + ex);
}

return requestEndPoint;

HI Lima,

Rule itself acts as a class where you can start writing methods and can call those methods separately from another rule using referencedRules tag or even calling the method within rule itself.

You do not need to define a class and then write methods within it.

HI @leonardo_lsilva,

If you are getting started with rules and wondering where to start with you can check on this comment of mine. And the solution the thread that has a sample working rule.

As @dheerajk27, said you don’t have to consider it to be a java application with public class main function and etc.

To have a simple visualisation, Think that every line you are placing in the rule will be placed in a function and called into the main function that IDN maintains. Meaning whatever write into it gets executed and you don’t have to worry about anything outside it (like file structure, compilation and execution.

Do make sure to add appropriate JARs, that you use in your rules in the Source config.

Hey man… I will check you post… but this schema from rules coding is a quite hard to image without seen a example rs

Sure @leonardo_lsilva,

I think that has a simple example for Webservice rule. If you have any further issues even after seeing that post.

Maybe try sharing the objectives of what you are trying to accomplish. We can try to help you.

Where can I read more about this? How to do this?

I need get the next available log in for an input…
It’s a recursive function that will make 2 REST Requests one to look for the $plan.cpf$ and another to look for $plan.login$

Example…

I try create { “login”: “usernameX”, “cpf”: “00000” }

I need check first if exists some registry in Target API with cpf: “00000”, if true I get the login information from Target API and update plan.login

Later, if do not exists any registry with cpf: “00000” I will check if exists any registry with login “usernameX” if exists I will add a suffix .001 and try again checking if login exists, until get a suffix available…

This code in Pure Java is ready… but transfer it to rule is breaking my mind :frowning:

Hi Leonardo, try to think that the rule is the code inside a static method. In fact, when I code it, I use eclipse to validate compilation errors. I get used to make a java class, use a static method and use code inside to copy later to rule. All I can test, I do it in a main. When you bring this to the rule, you only have to copy imports, and inmediatly after then, I copy the content of the class. I saw rules that use methods, as they were functions. As my codes are not lenght enough, I put them all in a unique method.

I can give you an example that does not avoid client compliance. It was a web service that has to be invoked with the actual date inside body. As you can not parametrize body on configuration, I left the text to replace in a “DATAFORREPLACE”, and then the before operation rule does the rest:

import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import sailpoint.connector.webservices.EndPoint;
import sailpoint.object.*;
import sailpoint.connector.*;

		Calendar calendar = Calendar.getInstance();			
		int year = calendar.get(Calendar.YEAR);
		String yearString = Integer.toString(year);
		int month = calendar.get(Calendar.MONTH) + 1;
		String monthString = "";
		if(month < 10) {
			monthString += "0";
		}
		monthString += Integer.toString(month);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		String dayString = "";
		if(day < 10) {
			dayString += "0";
		}
		dayString += Integer.toString(day);

		String today = dayString + "/" + monthString + "/" + yearString;


		log.info("################################ START BEFORE PROVISIONING RULE ##############################################");

		Map body = requestEndPoint.getBody();
		String jsonBody = (String) body.get("jsonBody");
		jsonBody = jsonBody.replaceAll("DATAFORREPLACE", today);
		body.put("jsonBody", jsonBody);
		requestEndPoint.setBody(body);
		
		log.info("returning request body: " + requestEndPoint.getBody().get("jsonBody"));

		log.info("################################ END BEFORE PROVISIONING RULE ##############################################");
		return requestEndPoint;

If you have a code in Pure Java can share if us, I think we can help more specific to your use case. Transferring to rule should be easy. For example in the code you shared at the start of the discussion pure java code and rule may something like this,

Pure Java Code

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import connector.common.JsonUtil;
import connector.common.Util;
import sailpoint.connector.webservices.EndPoint;
import sailpoint.connector.webservices.WebServicesClient;
import sailpoint.object.Application;
import sailpoint.object.ProvisioningPlan;
import sailpoint.object.ProvisioningPlan.AccountRequest;


public class MyTeste {

  sailpoint.connector.webservices.EndPoint requestEndPoint;

  public MyTeste(sailpoint.connector.webservices.EndPoint requestEndPoint) {
    this.requestEndPoint = requestEndPoint;
  }

  public String doSomething(){
    // some operations and consuming some variables inside this class.
    return "TESTE";
  }

  public String doAnotherThing(){
    return "TESTE";
  }
}


Map body = requestEndPoint.getBody();
String jsonBody = (String) body.get("jsonBody");
log.info("Modify Body: running");

try {
    MyTeste myTesteObj = new MyTeste(requestEndPoint); 


    Map jsonMap = JsonUtil.toMap(jsonBody);
    if (jsonMap != null) {
    if (provisioningPlan != null) {
        log.info("Rule - Modify Body: plan is not null == " + provisioningPlan);
    } else {
        log.info("Rule - Modify Body: plan is null");
    }

    String finalBody = JsonUtil.render(jsonMap);
    body.put("jsonBody", finalBody);

    // Let's say I consume or further process using some class  functionalities
    myTesteObj.doSomething()

    requestEndPoint.setBody(body);
    }
} catch (Exception ex) {
    log.error("Rule - Modify Body: " + ex);
}

return requestEndPoint;

Rule may look something like,

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import connector.common.JsonUtil;
import connector.common.Util;
import sailpoint.connector.webservices.EndPoint;
import sailpoint.connector.webservices.WebServicesClient;
import sailpoint.object.Application;
import sailpoint.object.ProvisioningPlan;
import sailpoint.object.ProvisioningPlan.AccountRequest;

Map body = requestEndPoint.getBody();
String jsonBody = (String) body.get("jsonBody");
log.info("Modify Body: running");

try {
    Map jsonMap = JsonUtil.toMap(jsonBody);
    if (jsonMap != null) {
    if (provisioningPlan != null) {
        log.info("Rule - Modify Body: plan is not null == " + provisioningPlan);
    } else {
        log.info("Rule - Modify Body: plan is null");
    }

    String finalBody = JsonUtil.render(jsonMap);
    body.put("jsonBody", finalBody);

    // Do all the operations directly here

    requestEndPoint.setBody(body);
    }
} catch (Exception ex) {
    log.error("Rule - Modify Body: " + ex);
}

return requestEndPoint;

Some migration guidelines I can suggest would be,

  • To migrate all the variables and functions outside the class
  • Move any logics you may have in constructors out to the start of the code.
  • In short try writing the entire code without classes. Even writing functions should work.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.