You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Nick Heudecker <nh...@gmail.com> on 2010/06/19 09:43:57 UTC

Reading Binary Files and Passing to HTTP Component

I have the following route:

from("file:{{data.dir}}/query-logs/?initialDelay=600&delete=true&delay=30000")
            .setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST))
            .setHeader(Exchange.CONTENT_TYPE,
constant("multipart/form-data"))
            .to("http:properties:{{license.server}}/processLog/{{server.id
}}");

The files in data.dir are ZIPs, but when they're passed to the HTTP
component, the file contents are converted into Strings, effectively
corrupting the file.  How can I maintain the byte[]?

I've tested the HTTP endpoint independently and it works fine, so it's
something between reading the file and passing it to the HTTP component.
I've tried PUTs and POSTs, as well as changing out the content-type.  No
change.

Thanks in advance for any suggestions.

-Nick

Re: Reading Binary Files and Passing to HTTP Component

Posted by Nick Heudecker <nh...@gmail.com>.
Thanks for the prompt response.  I'm working on the patch now.

On Sat, Jun 19, 2010 at 2:46 AM, Claus Ibsen <cl...@gmail.com> wrote:

> On Sat, Jun 19, 2010 at 9:58 AM, Willem Jiang <wi...@gmail.com>
> wrote:
> > Hi,
> >
> > I don't think current camel-http support the multipart/form-data out of
> box,
> > as the HttpProducer will try to turn message body into an input stream if
> > the http method is POST.
> >
>
> Ah yeah we should most likely support FileRequestEntity and
> InputStreamRequestEntity out of the box.
>
> We should most likely do something like
>
> If body is file based -> FileRequestEntity
> If body is String based -> StringRequestEntity (only check body
> instanceof String, to avoid type converting as anything can be
> converted into String)
> Else fallback to the InputStreamRequestEntity
>
> I wonder for the file based that people may want to read the content
> of the file and send that instead?
> For that they can use the .convertBodyTo(String.class) in the DSL
> before hitting the to http.
> But we could also offer an option to set which request entity
> preferred, just as we can do that with JMS.
>
>
>
>

Re: Reading Binary Files and Passing to HTTP Component

Posted by manishpillai1540 <ma...@gmail.com>.
Hi,

Thanks for the reply!!!!
Yes i have created a dummy web service to test it.

After apply few patches i have fixed the issue with the following code
snippet.

package com.camelinaction;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

import java.io.File;
import java.util.logging.Logger;

/**
 * Created by Manish.Pillai on 7/15/2016.
 */
public class TestMain {

    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        try {
            camelContext.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                   
from("file:C:\\temp?delay=5000&move=processed&moveFailed=error&antExclude=**/processed/**,**/error/**")
                            .process(new Processor() {
                                public void process(Exchange exchange)
throws Exception {
                                    StringBody username = new
StringBody("username", ContentType.MULTIPART_FORM_DATA);
                                    StringBody password = new
StringBody("password", ContentType.MULTIPART_FORM_DATA);

                                    MultipartEntityBuilder
multipartEntityBuilder = MultipartEntityBuilder.create();
                                   
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                                   
multipartEntityBuilder.addPart("username", username);
                                   
multipartEntityBuilder.addPart("password", password);

                                    String filename = (String)
exchange.getIn().getHeader(Exchange.FILE_NAME);
                                    File file =
exchange.getIn().getBody(File.class);
                                    multipartEntityBuilder.addPart("upload",
new FileBody(file, ContentType.MULTIPART_FORM_DATA, filename));

                                   
exchange.getIn().setBody(multipartEntityBuilder.build());
                                }
                            })
                           
.to("http4://localhost:8080/JAX_RS_Application/resource/restwb/upload");
                }
            });

            camelContext.getRestConfiguration();
            camelContext.start();
            Thread.sleep(5000);
            camelContext.stop();

        } catch (Exception e) {
            //logger.error(e.getMessage());
        }
    }

}





--
View this message in context: http://camel.465427.n5.nabble.com/Reading-Binary-Files-and-Passing-to-HTTP-Component-tp510207p5785116.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Reading Binary Files and Passing to HTTP Component

Posted by Eduardo Raupp <ra...@gmail.com>.
are you the owner of the rest endpoint?

If yes, could you show how is it? Configs and what it expects.

2016-07-14 8:56 GMT-03:00 manishpillai1540 <ma...@gmail.com>:

> Hi All,
>
> I am facing a similar issue following is the code snippet to send form data
> to a legacy web service :
>
> public static void main(String[] args) throws  Exception{
>         CamelContext camelContext =new DefaultCamelContext();
>         try {
>             camelContext.addRoutes(new RouteBuilder() {
>                 @Override
>                 public void configure() throws Exception {
>
>
> from("file:C:\\temp?delay=5000&move=processed&moveFailed=error&antExclude=**/processed/**,**/error/**")
>
> .setHeader(Exchange.HTTP_METHOD,constant("POST"))
>                             .process(new Processor() {
>                                 public void process(Exchange exchange)
> throws Exception {
>
>
> exchange.getContext().getTypeConverterRegistry().addTypeConverter(HttpEntity.class,InputStream.class,new
> InputStreamToHttpEntityConvertor());
>
> exchange.getOut().setBody(exchange.getIn().getBody(),HttpEntity.class);
>                                 }
>                             })
>
> .to("http4://localhost:8080/JAX_RS_Application/resource/restwb/upload");
>                 }
>             });
>
>             camelContext.getRestConfiguration();
>             camelContext.start();
>             Thread.sleep(5000);
>             camelContext.stop();
>
>         } catch (Exception e) {
>             logger.error(e.getMessage());
>         }
>     }
>
>     static class InputStreamToHttpEntityConvertor implements TypeConverter
> {
>
>         public boolean allowNull() {
>             return false;
>         }
>
>         public <T> T convertTo(Class<T> type, Object value) throws
> TypeConversionException {
>             Exchange exchange=(Exchange)value;
>
>             StringBody username = new StringBody("USERNAME",
> ContentType.MULTIPART_FORM_DATA);
>             StringBody password = new StringBody("PASSWORD",
> ContentType.MULTIPART_FORM_DATA);
>             MultipartEntityBuilder
> multipartEntityBuilder=MultipartEntityBuilder.create();
>
> multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
>             multipartEntityBuilder.addPart("upload", new
> FileBody(exchange.getIn().getBody(File.class),
> ContentType.MULTIPART_FORM_DATA, (String)
> exchange.getIn().getHeader(Exchange.FILE_NAME)));
>             multipartEntityBuilder.addPart("username",username);
>             multipartEntityBuilder.addPart("password",password);
>             return (T)multipartEntityBuilder.build();
>         }
>
>         public <T> T convertTo(Class<T> aClass, Exchange exchange, Object
> o)
> throws TypeConversionException {
>             return convertTo(aClass,o);
>         }
>
>         public <T> T mandatoryConvertTo(Class<T> type, Object value) throws
> TypeConversionException, NoTypeConversionAvailableException {
>             return convertTo(type,value);
>         }
>
>         public <T> T mandatoryConvertTo(Class<T> type, Exchange exchange,
> Object value) throws TypeConversionException,
> NoTypeConversionAvailableException {
>             return convertTo(type,value);
>         }
>
>         public <T> T tryConvertTo(Class<T> type, Object value) {
>             return convertTo(type,value);
>         }
>
>         public <T> T tryConvertTo(Class<T> type, Exchange exchange, Object
> value) {
>             return convertTo(type,value);
>         }
>     }
>
>
> I am getting the following error :
>
>
> Message History
>
> ---------------------------------------------------------------------------------------------------------------------------------------
> RouteId              ProcessorId          Processor
> Elapsed (ms)
> [route1            ] [route1            ]
>
> [file://C:%5Ctemp?antExclude=**%2Fprocessed%2F**%2C**%2Ferror%2F**&delay=5000&m]
> [       238]
> [route1            ] [setHeader1        ] [setHeader[CamelHttpMethod]
> ] [         7]
> [route1            ] [process1          ] [Processor@0x4e57449e
> ] [        15]
> [route1            ] [to1               ]
> [http4://localhost:8080/JAX_RS_Application/resource/restwb/upload
> ] [       205]
>
> Stacktrace
>
> ---------------------------------------------------------------------------------------------------------------------------------------
> org.apache.camel.http.common.HttpOperationFailedException: HTTP operation
> failed invoking
> http://localhost:8080/JAX_RS_Application/resource/restwb/upload with
> statusCode: 415
>         at
>
> org.apache.camel.component.http4.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:279)
>         at
>
> org.apache.camel.component.http4.HttpProducer.process(HttpProducer.java:187)
>         at
>
> org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
>         at
> org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145)
>         at
>
> org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
>         at
>
> org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)
>         at
>
> org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
>         at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)
>         at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
>         at
>
> org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
>         at
>
> org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:454)
>         at
>
> org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:226)
>         at
>
> org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:190)
>         at
>
> org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:175)
>         at
>
> org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:102)
>         at
> java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
>         at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
>         at
>
> java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
>         at
>
> java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
>         at
>
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
>         at
>
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
>         at java.lang.Thread.run(Thread.java:745)
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Reading-Binary-Files-and-Passing-to-HTTP-Component-tp510207p5785109.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Reading Binary Files and Passing to HTTP Component

Posted by manishpillai1540 <ma...@gmail.com>.
Hi All, 

I am facing a similar issue following is the code snippet to send form data
to a legacy web service :

public static void main(String[] args) throws  Exception{
        CamelContext camelContext =new DefaultCamelContext();
        try {
            camelContext.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                   
from("file:C:\\temp?delay=5000&move=processed&moveFailed=error&antExclude=**/processed/**,**/error/**")
                           
.setHeader(Exchange.HTTP_METHOD,constant("POST"))
                            .process(new Processor() {
                                public void process(Exchange exchange)
throws Exception {
                                   
exchange.getContext().getTypeConverterRegistry().addTypeConverter(HttpEntity.class,InputStream.class,new
InputStreamToHttpEntityConvertor());
                                   
exchange.getOut().setBody(exchange.getIn().getBody(),HttpEntity.class);
                                }
                            })
                           
.to("http4://localhost:8080/JAX_RS_Application/resource/restwb/upload");
                }
            });

            camelContext.getRestConfiguration();
            camelContext.start();
            Thread.sleep(5000);
            camelContext.stop();

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    static class InputStreamToHttpEntityConvertor implements TypeConverter {

        public boolean allowNull() {
            return false;
        }

        public <T> T convertTo(Class<T> type, Object value) throws
TypeConversionException {
            Exchange exchange=(Exchange)value;

            StringBody username = new StringBody("USERNAME",
ContentType.MULTIPART_FORM_DATA);
            StringBody password = new StringBody("PASSWORD",
ContentType.MULTIPART_FORM_DATA);
            MultipartEntityBuilder
multipartEntityBuilder=MultipartEntityBuilder.create();
           
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            multipartEntityBuilder.addPart("upload", new
FileBody(exchange.getIn().getBody(File.class),
ContentType.MULTIPART_FORM_DATA, (String)
exchange.getIn().getHeader(Exchange.FILE_NAME)));
            multipartEntityBuilder.addPart("username",username);
            multipartEntityBuilder.addPart("password",password);
            return (T)multipartEntityBuilder.build();
        }

        public <T> T convertTo(Class<T> aClass, Exchange exchange, Object o)
throws TypeConversionException {
            return convertTo(aClass,o);
        }

        public <T> T mandatoryConvertTo(Class<T> type, Object value) throws
TypeConversionException, NoTypeConversionAvailableException {
            return convertTo(type,value);
        }

        public <T> T mandatoryConvertTo(Class<T> type, Exchange exchange,
Object value) throws TypeConversionException,
NoTypeConversionAvailableException {
            return convertTo(type,value);
        }

        public <T> T tryConvertTo(Class<T> type, Object value) {
            return convertTo(type,value);
        }

        public <T> T tryConvertTo(Class<T> type, Exchange exchange, Object
value) {
            return convertTo(type,value);
        }
    }


I am getting the following error :


Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                       
Elapsed (ms)
[route1            ] [route1            ]
[file://C:%5Ctemp?antExclude=**%2Fprocessed%2F**%2C**%2Ferror%2F**&delay=5000&m]
[       238]
[route1            ] [setHeader1        ] [setHeader[CamelHttpMethod]                                                   
] [         7]
[route1            ] [process1          ] [Processor@0x4e57449e                                                         
] [        15]
[route1            ] [to1               ]
[http4://localhost:8080/JAX_RS_Application/resource/restwb/upload             
] [       205]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation
failed invoking
http://localhost:8080/JAX_RS_Application/resource/restwb/upload with
statusCode: 415
	at
org.apache.camel.component.http4.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:279)
	at
org.apache.camel.component.http4.HttpProducer.process(HttpProducer.java:187)
	at
org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
	at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145)
	at
org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
	at
org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)
	at
org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
	at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)
	at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
	at
org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
	at
org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:454)
	at
org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:226)
	at
org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:190)
	at
org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:175)
	at
org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:102)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
	at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
	at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)



--
View this message in context: http://camel.465427.n5.nabble.com/Reading-Binary-Files-and-Passing-to-HTTP-Component-tp510207p5785109.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Reading Binary Files and Passing to HTTP Component

Posted by Nick Heudecker <nh...@gmail.com>.
https://issues.apache.org/activemq/browse/CAMEL-2833

Patch included.

On Sat, Jun 19, 2010 at 2:46 AM, Claus Ibsen <cl...@gmail.com> wrote:

> On Sat, Jun 19, 2010 at 9:58 AM, Willem Jiang <wi...@gmail.com>
> wrote:
> > Hi,
> >
> > I don't think current camel-http support the multipart/form-data out of
> box,
> > as the HttpProducer will try to turn message body into an input stream if
> > the http method is POST.
> >
>
> Ah yeah we should most likely support FileRequestEntity and
> InputStreamRequestEntity out of the box.
>
> We should most likely do something like
>
> If body is file based -> FileRequestEntity
> If body is String based -> StringRequestEntity (only check body
> instanceof String, to avoid type converting as anything can be
> converted into String)
> Else fallback to the InputStreamRequestEntity
>
> I wonder for the file based that people may want to read the content
> of the file and send that instead?
> For that they can use the .convertBodyTo(String.class) in the DSL
> before hitting the to http.
> But we could also offer an option to set which request entity
> preferred, just as we can do that with JMS.
>
>
>
>
>

Re: Reading Binary Files and Passing to HTTP Component

Posted by Claus Ibsen <cl...@gmail.com>.
On Sat, Jun 19, 2010 at 9:58 AM, Willem Jiang <wi...@gmail.com> wrote:
> Hi,
>
> I don't think current camel-http support the multipart/form-data out of box,
> as the HttpProducer will try to turn message body into an input stream if
> the http method is POST.
>

Ah yeah we should most likely support FileRequestEntity and
InputStreamRequestEntity out of the box.

We should most likely do something like

If body is file based -> FileRequestEntity
If body is String based -> StringRequestEntity (only check body
instanceof String, to avoid type converting as anything can be
converted into String)
Else fallback to the InputStreamRequestEntity

I wonder for the file based that people may want to read the content
of the file and send that instead?
For that they can use the .convertBodyTo(String.class) in the DSL
before hitting the to http.
But we could also offer an option to set which request entity
preferred, just as we can do that with JMS.




> Please fill a JIRA for it , and you can write a processor to call the
> HttpClient yourself to send this kind of message.
>
> Willem
>
> Nick Heudecker wrote:
>>
>> I have the following route:
>>
>>
>> from("file:{{data.dir}}/query-logs/?initialDelay=600&delete=true&delay=30000")
>>            .setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST))
>>            .setHeader(Exchange.CONTENT_TYPE,
>> constant("multipart/form-data"))
>>            .to("http:properties:{{license.server}}/processLog/{{server.id
>> }}");
>>
>> The files in data.dir are ZIPs, but when they're passed to the HTTP
>> component, the file contents are converted into Strings, effectively
>> corrupting the file.  How can I maintain the byte[]?
>>
>> I've tested the HTTP endpoint independently and it works fine, so it's
>> something between reading the file and passing it to the HTTP component.
>> I've tried PUTs and POSTs, as well as changing out the content-type.  No
>> change.
>>
>> Thanks in advance for any suggestions.
>>
>> -Nick
>>
>
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Re: Reading Binary Files and Passing to HTTP Component

Posted by Willem Jiang <wi...@gmail.com>.
Hi,

I don't think current camel-http support the multipart/form-data out of 
box, as the HttpProducer will try to turn message body into an input 
stream if the http method is POST.

Please fill a JIRA for it , and you can write a processor to call the 
HttpClient yourself to send this kind of message.

Willem

Nick Heudecker wrote:
> I have the following route:
> 
> from("file:{{data.dir}}/query-logs/?initialDelay=600&delete=true&delay=30000")
>             .setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST))
>             .setHeader(Exchange.CONTENT_TYPE,
> constant("multipart/form-data"))
>             .to("http:properties:{{license.server}}/processLog/{{server.id
> }}");
> 
> The files in data.dir are ZIPs, but when they're passed to the HTTP
> component, the file contents are converted into Strings, effectively
> corrupting the file.  How can I maintain the byte[]?
> 
> I've tested the HTTP endpoint independently and it works fine, so it's
> something between reading the file and passing it to the HTTP component.
> I've tried PUTs and POSTs, as well as changing out the content-type.  No
> change.
> 
> Thanks in advance for any suggestions.
> 
> -Nick
>