cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Binary Value from Partner Directory via Groovy

cp11
Explorer

Hello SAP Experts,

I currently have a problem that I could not find out exactly how I can read a binary value from the Partner Directory via a Groovy Script ( inside the I-Flow).

Writing, updating and reading the binary value via Postman works as desired, here I have stored the XML as Base64 in the Partner Directory

{
    "Pid":"{{ _.pd_pid }}",
    "Id":"TEST",		
		"ContentType":"xml",
"Value":"PHJvb3Q+CiAgICA8Y3VzdG9tZXI+CiAgICAgICAgPEN1c3RvbWVySUQ+dGVzdDwvQ3VzdG9tZXJJRD4KICAgICAgICA8Q3JlZGVudGlhbF9FVkVOVE0+dGVzdDwvQ3JlZGVudGlhbF9FVkVOVE0+CiAgICA8L2N1c3RvbWVyPgo8L3Jvb3Q+"
}

i followed this methods to get the data in the groovy script: 

https://help.sap.com/doc/471310fc71c94c2d913884e2ff1b4039/Cloud/en-US/com/sap/it/api/pd/BinaryData.h... 

Here would be my approach how I would have read the binary value in my Groovy script

				//get Value from PartnerDirectory
				paramValue = partnerDirectoryService.getParameter(paramName, callerPID , com.sap.it.api.pd.BinaryData);
				if (paramValue == null){
					throw new IllegalStateException(paramName + " is not configured in " + paramName + " in Partner Directory");
				}
				
				
                message.setProperty("Test1", paramValue);
				message.setProperty("Test2_Data", paramValue.getData());
				message.setProperty("Test3_ContentType", paramValue.getContentType());

Reading a ContentType (with getContentType()) works fine, but it seems that a binary value is read in a strange way.

Here are the output of the Output of the Binary Value: 

com.sap.esb.pd.rt.external.BinaryDataImpl@5568b8e8

cp11_0-1713511697950.png

Does anyone have an idea what my error is or has an example code for me?

 

Many thanks in advance.

Best regards

Chris. P.

 

Accepted Solutions (1)

Accepted Solutions (1)

Ryan-Crosby
Active Contributor
0 Kudos

Something like this for your script instead (notice I added the required import too)...

import groovy.xml.*
import java.io.InputStream

//get Value from PartnerDirectory
paramValue = partnerDirectoryService.getParameter(paramName, callerPID , com.sap.it.api.pd.BinaryData)
if (paramValue == null){
	throw new IllegalStateException(paramName + " is not configured in " + paramName + " in Partner Directory")
}
				
def slurper = new XmlSlurper()
message.setProperty("Test1", slurper.parse(new ByteArrayInputStream(paramValue.getData())))

 

 

 

Regards,

Ryan Crosby

cp11
Explorer
0 Kudos

Hi Ryan,

I have tried your approach, but unfortunately I always get the following error:

cp11_0-1713773214533.png

 

com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occured: No signature of method: groovy.util.XmlSlurper.parse() is applicable for argument types: ([B) values: [[60, 114, 111, 111, 116, 62, 10, 32, 32, 32, 32, 60, 99, 117, ...]]
Possible solutions: parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader), parse(java.lang.String), parse(org.xml.sax.InputSource), use([Ljava.lang.Object;).

 

(import groovy.xml.* is also added to the script)

I also experimented around but couldn't find an approach to get any further.

Do you have any idea what I might be missing?

Best regards

Chris P. 

Ryan-Crosby
Active Contributor
0 Kudos
@cp11 I updated the answer to include ByteArrayInputStream - please try again with the updated code.
cp11
Explorer

Hi Ryan,

it works!

Thank you very much for your help!

Best regards

Chris

Answers (1)

Answers (1)

Andrzej_Filusz
Contributor
0 Kudos

Hi Chris,

Could you please add the following code to your script and make a test?

byte[] decoded = Base64.getDecoder().decode(paramValue.getData())
String content = new String(decoded, StandardCharsets.UTF_8)
message.setProperty("Test4_Decoded", content)

BR,

Andrzej

cp11
Explorer
0 Kudos

Hi Andrzej_Filusz,

thank you very much for your answer,

i tried to implement you coding, also added the 

import java.nio.charset.StandardCharsets Class to the script. 
 
But unfortunately i received following error on your first line of coding: 

cp11_0-1713528551460.png

Is there a additional coding which i need? 

Best Regards. 

Chris P. 

cp11
Explorer
0 Kudos

Hi Andrzej_Filusz,

i changed the coding to 

				//byte[] decoded = Base64.getDecoder().decode(testData)
				byte[] decoded =  Base64.getMimeDecoder().decode(testData);
                String content = new String(decoded, StandardCharsets.UTF_8)
                message.setProperty("Test4_Decoded", content)

now i get no errors, but the output seems not correct 

cp11_1-1713529446302.png

Do you have a idea how i could handle this?

Best Regards 

Chris P.