Skip to Content

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

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?

payload.png (66.6 kB)
groovy.png (40.2 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Nov 08 at 02:35 PM

    Can you please try this

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

    -Bhargav

    Add comment
    10|10000 characters needed characters exceeded