camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roman Vottner <r...@gmx.at>
Subject Re: REST DSL process multipart/form-data binary file?
Date Sun, 01 Oct 2017 01:13:24 GMT
Our Camel REST DSL configuration looks like this:

     onException(Exception.class)
         .handled(true)
         .logExhausted(false)
         .log(LoggingLevel.ERROR, ">> In default/message exception 
handler. ${exception}, message: ${exception.message}, stacktrace: 
${exception.stacktrace}")
         .onRedelivery((Exchange exchange) -> LOG.debug(">> Default 
exception handler"))
         .bean(PrepareErrorResponse.class).id("ErrorResponse")
         .end();

     restConfiguration()
         .component("undertow")
         .port("{{server.port}}")
         .contextPath("/api")
         .endpointProperty("matchOnUriPrefix", "true")
         .endpointProperty("sendServerVersion", "false")
         .endpointProperty("chunked", "true");

where the actual route handling the multipart-request does look 
something along the line:

     rest("/someEndpoint")
         .post("/{id}")
         .consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
         .route().routeId("upload-multipart-route")
           // Spring Security authentication check via Authorization header
           .bean(SpringSecurityContextLoader.class)
               .policy(authorizationPolicy)
           .log("Uploading to resource id ${header.id}")
           .log(LoggingLevel.INFO, LOG, CONFIDENTIAL,"Headers: ${headers}")
           .bean(HandleUpload.class)
           .process((Exchange exchange) -> {
             exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
             exchange.getIn().setHeader(Exchange.CONTENT_TYPE, 
MediaType.APPLICATION_JSON_UTF8_VALUE);
             ...
           })
     .endRest();

and the HandleUpload class looking something like this:

public class HandleUpload {

   private static final Logger LOG = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

   @Autowired
   private SomeRepository someRepository;

   @Handler
   public void processUpload(@Attachments Map<String, DataHandler> 
attachments,
                             @Header("Content-Type") String contentType,
                             @Header("id") String id,
                             Exchange exchange)
       throws Exception {
     LOG.info("Upload received");

     LOG.info("Exchange body: " + exchange.getIn().getBody());
     LOG.info("Attachment size: " + (attachments == null ? 0 : 
attachments.size()));
     LOG.info("Attachment object size: " + 
(exchange.getIn().getAttachmentObjects() == null ? 0
: exchange.getIn().getAttachmentObjects().size()));
     if (exchange.getIn().getAttachmentNames() != null) {
       for (String name : exchange.getIn().getAttachmentNames()) {
         LOG.info("Attachment name: " + name);
       }
     }

     if (contentType == null || 
!contentType.startsWith(MediaType.MULTIPART_FORM_DATA_VALUE)) {
       LOG.warn("Unsupported media type!");
       throw new UnSupportedUploadMediaTypeException("Content-Type has 
to be 'multipart/form-data'");
     }

     if (attachments.size() == 0) {
       LOG.warn("No attachments found!");
     } else {
       for (String key : attachments.keys()) {
         LOG.info("Filename: " + key);

         String uploadKey = id + "_" + new 
Date().toInstant().toEpochMilli() + "_" +
attachments.get(key).getDataSource().getName();

         // stream data directly to a file to save memory footprint
         File targetFile = new File(uploadKey);
         try (OutputStream outStream = new FileOutputStream(targetFile, 
false)) {
             attachments.get(key).writeTo(outStream);
         }

         ...
       }
     }

     ...
   }
}

Although we currently test Undertow, switching it with Jetty shouldn't 
be an issue.

HTH,
roman


Am 01.10.2017 um 02:18 schrieb Mark:
> I understand that, problem is that I can't figure out how to configure the
> Camel Route to properly receive/parse the data.  If I was receiving
> JSON/KML, this would be easy using the functionality in Camel.  Binary
> files seem to be totally different.
>
>
> On Sat, Sep 30, 2017 at 8:13 PM, Mark Nuttall <mknuttall@gmail.com> wrote:
>
>> it is just a file. any example of processing a file should work.  you will
>> be able to save it somewhere and then you will have to call some processor
>> to read/process it.
>>
>> On Fri, Sep 29, 2017 at 7:47 PM, Mark <elihusmails@gmail.com> wrote:
>>
>>> I'm trying to figure out how to process a binary file that is sent to my
>>> Camel REST service via POST.  I have not found any good examples on how
>>> this can be done using Camel.  Does anyone have any experiences they
>> could
>>> share or links to more documentation?
>>>
>>> I'm currently using the jetty component for the restconfiguration.
>>>
>>> Thanks,
>>> Mark
>>>


Mime
View raw message