Skip to Content
0
Jun 09, 2020 at 05:36 PM

Passing Oauth Token & Credentials from the UDF itself

559 Views

Hi Experts,

We have created below UDF to fetch the OAuth 2.0 Token with Rest lookup.

As per requirement, We need to pass Authentication header to the UDF itself to get the Token rather than passing the authorization in channel.

String token = "";
StringBuffer sb = new StringBuffer();
String party = "" ;
// String service = "*****";
//String channel = "**********";
// String channel = "**************";
//String authorization = new String("");
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey auth = DynamicConfigurationKey.create("http:/"+"/sap.com/xi/XI/System/REST","token");
//code to fetch token
// 1. Get a system accessor for a channel.
//String party = ""; // emtpy values cannot be passed from Directory to the mapping parameters.
Channel RESTchannel = LookupService.getChannel(service,channel);
SystemAccessor accessor = LookupService.getSystemAccessor(RESTchannel);
// getTrace().addInfo("Parameters for Token Lookup - Service: "+service+", Channel: "+channel);
try{
// 2. Create a payload according to the data type.
// Use service.getBinaryPayload() for binary payload,
// or service.getTextPayload() for text payloads.
InputStream inputStream;
String granttype = "grant_type=password";
String clientid = "client_id=**************";
String reqString = granttype +"&"+ clientid;
// getTrace().addDebugMessage("Request: "+reqString);
inputStream = (InputStream) new ByteArrayInputStream(reqString.getBytes());
//getTrace().addInfo("InputStream: "+ inputStream);

com.sap.aii.mapping.lookup.Payload payload = LookupService.getXmlPayload(inputStream); // getTrace().addInfo("Payoad: "+ payload);
// 3. Execute lookup.
com.sap.aii.mapping.lookup.Payload result = accessor.call(payload);
// getTrace().addInfo("Payoad: "+ payload);
// 4. Parse result, could be better realized with a real JSON parser
byte[] b = new byte[4096];
for (int n; (n = result.getContent().read(b)) != -1;){
sb.append(new String(b,0,n));
}
// getTrace().addDebugMessage("Response: "+sb);
int i =sb.indexOf("\"access_token\":\"")+16;
int j = sb.indexOf("\"token_type\"");
token = sb.substring(i,j - 20);
// getTrace().addInfo("Token: "+token); }
catch (Exception e){
e.printStackTrace();
}
finally{

// 5. close the accessor in order to free resources.
if (accessor!=null) accessor.close();
} //conf.put(auth, token);
return token;

Is anyone did the similar configurations, Kindly provide your valuable inputs.