cancel
Showing results for 
Search instead for 
Did you mean: 

CPI/Groovy how to calculate Message body size after changing it?

0 Kudos

Dear CPI and Groovy experts.

I have a requiremend to delete “heavy” attachments from the payload before passing it to Cloud For Customer.

The sample payload looks as follow (see payload.png).

It has 3 document nodes containing encoded attachments.

In real situations documents can be very big (up to 70 MB) and C4C of course cannot accept them.

Moreover CPI setting limit of 40MB for body size.

The agreed solution is to delete documents one by one and check body size after every iteration of loop.

I’ve created a script in Groovy, which is checking the original Message size.

If it is higher than 40 MB it is doing loop against Document segment and deleting them one by one unless Message size is less than 40 MB or unless there is no more Documents to delete.

The code is failing on line 29 with the following exception:

javax.script.ScriptException: java.lang.Exception: org.apache.camel.TypeConversionException: Error during type conversion from type: java.lang.String to the required type: java.lang.String with value [Body is not logged] due javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.@ line 29 in script6.groovy, cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.

See code attached as an image (groovy.png).

The error is telling that i am not using correct Type, but at the same time it should be String.

I am not competent enough in Java to understand where is the issue. Can you please advice how to solve it?

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member522497
Participant
0 Kudos

Hi olegs.veliks,

Please try the below snippet of code for fetching the payload size and storing the value in header.

Hope this helps!

Please mark the response as correct if it resolves the issue.

Thanks,

Ameer

babruvahana
Contributor
0 Kudos

Hi olegs.veliks,

Hope you are doing well!

I have similar request in project. Could please tell me how did you resolve the issue ?

Regards,

Pavan

former_member26700
Participant
0 Kudos

Can you please try this

message.getBody(java.lang.String).getBytes().length

-Bhargav

0 Kudos

Thanks, but did not helped. Same error.