1
votes

I have an HTTP producer that calls a remote HTTP Server. I need to read the response from the HTTP Server and convert it into json and do some further processing. I am experiencing some challenges dealing with big message bodies. I am not quite sure what the size threshold is. My code look

   <from uri="restlet:/foo"/>
   <setHeader headerName="Exchange.HTTP_URI">
            <simple>{{ConfigurableURL}}}</simple>
   </setHeader>
   <to uri="http://dymmyhost"/>
   <transform>
            <simple>${bodyAs('java.lang.String')}</simple>
   </transform>
   <transform>
        <groovy>org.json.simple.JSONValue.parse(request.body.trim())</groovy>
   </transform>

When the size is small enough, this works without problem. When it is big, the body gets truncated in the last transformation. And if I remove org.json.simple.JSONValue.parse from this last transformation, I end up with a error message such as: org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 2263; received: 2233

My digging suggested that there is a buffering issue going on. So, I added the parameter disableStreamCache=true to the URL. It did not help.

I also replaced the JSON parsing line with a proper Camel unmarshaller:

   <unmarshal ref="json"/>

Still it did not help. I get the exception: Premature end of Content-Length delimited message body.

Any clue?

1

1 Answers

0
votes

Write the response from the remote HTTP server to a file before parsing it such as

<camel:to uri="file:data?fileName=download.htm" />

See http://camel.apache.org/file2.html for more options. As a nice side effect, this protects your server ram resources. When parsing the file, use a Reader instead of transfering the whole content into a String. Beside that, Camel provides powerful JSON support, see http://camel.apache.org/json.html.