Reputation: 638
I have put together a file uploader with HTTP4 to push data to HTTP server environments. there are 2 systems, 1 for CSV data files and 1 for JSON delivery. sending a CSV to the CSV system works fine. however, converting the data to JSON and sending to the JSON system fails with the below exception. the error is intuitive, however, I don't really know what to do about it.
Does JSON not use multipart form data and should specifically be an IO Stream? being my first HTTP service I'm at a lose of protocol acceptance. apparently the JSON system does not expect Multipart data, I'm not sure what a Json http send should be?? thank you for your help!
Caused by: org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: org.apache.http.entity.mime.MultipartFormEntity
Caused by: No type converter available to convert from type: org.apache.http.entity.mime.MultipartFormEntity to the required type: java.io.InputStream with value org.apache.http.entity.mime.MultipartFormEntity
here is the uploader class that works for CSV data files but not for Json?
LOG.info("Uploading File for CustKey: " + custKey + " and Tenant: " + tenant);
StringBuilder authHeader = new StringBuilder("Bearer ");
authHeader.append(token);
LOG.info("Authorization: " + authHeader.toString());
exchange.setProperty("CamelCharsetName", "UTF-8"); //"CamelCharsetName" Exchange.CHARSET_NAME
exchange.getIn().setHeader("CamelHttpCharacterEncoding", "UTF-8"); //"CamelHttpCharacterEncoding" Exchange.HTTP_CHARACTER_ENCODING
exchange.getIn().setHeader("CamelAcceptContentType", "application/json"); //"CamelAcceptContentType" Exchange.ACCEPT_CONTENT_TYPE
exchange.getIn().setHeader("CamelHttpUri", uploadUrl); //"CamelHttpUri" Exchange.HTTP_URI
exchange.getIn().setHeader("CamelHttpMethod", "POST"); //"CamelHttpMethod" Exchange.HTTP_METHOD
exchange.getIn().setHeader("x-ge-csvformat", "ODB");
exchange.getIn().setHeader("Tenant", tenant);
// exchange.getIn().setHeader("Content-Type", "multipart/form-data"); //"Content-Type" ; boundary=
exchange.getIn().setHeader("Authorization", authHeader.toString());
// Process the file in the exchange body
File file = exchange.getIn().getBody(File.class);
String fileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
LOG.info("fileName: " + fileName);
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addBinaryBody("file", file);
entity.addTextBody("name", fileName);
exchange.getIn().setBody(entity.build()); //*** use for CSV uploads
HERE IS the route, the only difference in the route is the JsonMapper process
<route
id="core.predix.upload.route"
autoStartup="false" >
<from uri="{{uploadEntranceEndpoint}}" />
<process ref="customerEntitesProcessor" /> <!-- sets up the message with the customer environment entities to upload data -->
<process ref="customerTokenProcessor" /> <!-- sets up the message with the cusotmer's token -->
<process ref="jsonMapper" />
<to uri="{{jsonEndpoint}}" />
<process ref="uploadProcessor" /> <!-- conditions the message with the HTTP header info per customer env -->
<setHeader headerName="CamelHttpUri">
<simple>${header.UPLOADURL}?throwExceptionOnFailure=false</simple>
</setHeader>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<to uri="http4://apm-timeseries-query-svc-prod.app-api.aws-usw02-pr.predix.io:443/v2/time_series/upload?throwExceptionOnFailure=false" />
<log message="After POSTING JSON: ${body}" loggingLevel="INFO"/>
<to uri="{{afteruploadLocation}}" />
<!-- <log message="JSON Route: ${body}" loggingLevel="INFO"/> -->
<!-- <to uri="{{jsonEndpoint}}" /> -->
</route>
Upvotes: 1
Views: 6882
Reputation: 638
aparently sending json is as an IO Stream. Multipart form data Content-type is for file transfers only, and system is expecting Multipart form data as Content-Type.
to get this to work I am simply sending data as IO Stream and changed the Content-Type to application/json.
Code changed to, to send Json.
StringBuilder authHeader = new StringBuilder("Bearer ");
authHeader.append(token);
LOG.info("Authorization: " + authHeader.toString());
exchange.setProperty("CamelCharsetName", "UTF-8"); //"CamelCharsetName" Exchange.CHARSET_NAME
exchange.getIn().setHeader("CamelHttpCharacterEncoding", "UTF-8"); //"CamelHttpCharacterEncoding" Exchange.HTTP_CHARACTER_ENCODING
exchange.getIn().setHeader("CamelAcceptContentType", "application/json"); //"CamelAcceptContentType" Exchange.ACCEPT_CONTENT_TYPE
exchange.getIn().setHeader("CamelHttpUri", uploadUrl); //"CamelHttpUri" Exchange.HTTP_URI
exchange.getIn().setHeader("CamelHttpMethod", "POST"); //"CamelHttpMethod" Exchange.HTTP_METHOD
exchange.getIn().setHeader("x-ge-csvformat", "ODB");
exchange.getIn().setHeader("Tenant", tenant);
exchange.getIn().setHeader("Content-Type", "application/json");
exchange.getIn().setHeader("Authorization", authHeader.toString());
//*** use for CSV uploads - uncomment all commented lines for CSV file upload
// Process the file in the exchange body
// File file = exchange.getIn().getBody(File.class);
// String fileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
// LOG.info("fileName: " + fileName);
// MultipartEntityBuilder entity = MultipartEntityBuilder.create();
// entity.addBinaryBody("file", file);
// entity.addTextBody("name", fileName);
// exchange.getIn().setBody(entity.build());
Upvotes: 1