You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by jamalissimo <ro...@gmail.com> on 2013/03/25 14:08:32 UTC

Upload file - How to pass InputStream to Camel's route

Hi everyone,

lately, I was working on @POST Consumer which was able to upload a file(
link
<http://camel.465427.n5.nabble.com/Send-file-via-POST-request-td5728674.html> 
). It was really challenging but it still has one "bug". I am not able to
directly upload file from my local drive. Firstly I have to upload file to
server where Karaf runs and after that I pass a variable with path to this
file and I don't like this approach 

I would like to pass the file as an InputStream to my route. I am aware of
stream component but I think I am using different approach to use it.

As you can notice at my  route
<http://camel.465427.n5.nabble.com/Send-file-via-POST-request-td5728674.html>  
I am using Apache's HTTP Client to perform the @POST request and now I can
pass an InputStream to it. 

My question how can I pass InputStream to my route?


Thanks

-Br, Roman



--
View this message in context: http://camel.465427.n5.nabble.com/Upload-file-How-to-pass-InputStream-to-Camel-s-route-tp5729752.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Upload file - How to pass InputStream to Camel's route

Posted by Raul Kripalani <ra...@evosent.com>.
Exactly. I copied/pasted your example from your first thread, and the @GET
slipped in.

You can only send content with @POST or @PUT. So just replace @GET with
@POST in my example and it should work as expected.

Please try it and loop back to us.

Regards,

*Raúl Kripalani*
Enterprise Architect, Open Source Integration specialist, Program
Manager | Apache
Camel Committer
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk

On Tue, Mar 26, 2013 at 9:23 AM, Christian Müller <
christian.mueller@gmail.com> wrote:

> Hi Romain!
>
> With @GET you cannot send content with your request. This is possible with
> @POST.
>
> Best,
> Christian
>
> On Tue, Mar 26, 2013 at 9:14 AM, jamalissimo <roman.janulek@gmail.com
> >wrote:
>
> > Hi Raul,
> >
> > thanks for quick answer. This looks easy when you are using @GET, but I
> am
> > sending @POST request. If I am right, @POST sends data only in headers. I
> > was thinking about attachments, may be, but here it gets more confusing
> for
> > me :-)
> >
> > Here is my REST implementation:
> >
> >
> ------------------------------------------------------------------------------------
> >         @POST
> >         @Consumes(MediaType.MULTIPART_FORM_DATA)
> >         @Path("/upload")
> >         @Produces(MediaType.APPLICATION_JSON)
> >         public Response uploadFile(
> >                                 @QueryParam("host") String host,
> >                                 @QueryParam("port") String port,
> >                                 @QueryParam("user") String user,
> >                                 @QueryParam("password") String password,
> >                                 @Multipart(value="folderId") String
> > folderId,
> >                                 @Multipart(value="filename") String
> > filename,
> >                                 @Multipart(value="myfile") DataHandler
> > myfile
> >                         ){}
> >
> >
> > Thanks
> >
> > -Br, Roman
> >
> >
> >
> > --
> > View this message in context:
> >
> http://camel.465427.n5.nabble.com/Upload-file-How-to-pass-InputStream-to-Camel-s-route-tp5729752p5729829.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
> >
>

Re: Upload file - How to pass InputStream to Camel's route

Posted by Christian Müller <ch...@gmail.com>.
Hi Romain!

With @GET you cannot send content with your request. This is possible with
@POST.

Best,
Christian

On Tue, Mar 26, 2013 at 9:14 AM, jamalissimo <ro...@gmail.com>wrote:

> Hi Raul,
>
> thanks for quick answer. This looks easy when you are using @GET, but I am
> sending @POST request. If I am right, @POST sends data only in headers. I
> was thinking about attachments, may be, but here it gets more confusing for
> me :-)
>
> Here is my REST implementation:
>
> ------------------------------------------------------------------------------------
>         @POST
>         @Consumes(MediaType.MULTIPART_FORM_DATA)
>         @Path("/upload")
>         @Produces(MediaType.APPLICATION_JSON)
>         public Response uploadFile(
>                                 @QueryParam("host") String host,
>                                 @QueryParam("port") String port,
>                                 @QueryParam("user") String user,
>                                 @QueryParam("password") String password,
>                                 @Multipart(value="folderId") String
> folderId,
>                                 @Multipart(value="filename") String
> filename,
>                                 @Multipart(value="myfile") DataHandler
> myfile
>                         ){}
>
>
> Thanks
>
> -Br, Roman
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Upload-file-How-to-pass-InputStream-to-Camel-s-route-tp5729752p5729829.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Upload file - How to pass InputStream to Camel's route

Posted by jamalissimo <ro...@gmail.com>.
Hi Raul,

thanks for quick answer. This looks easy when you are using @GET, but I am
sending @POST request. If I am right, @POST sends data only in headers. I
was thinking about attachments, may be, but here it gets more confusing for
me :-)

Here is my REST implementation:
------------------------------------------------------------------------------------
	@POST
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Path("/upload")
	@Produces(MediaType.APPLICATION_JSON)
	public Response uploadFile(
				@QueryParam("host") String host,
				@QueryParam("port") String port, 
				@QueryParam("user") String user,
				@QueryParam("password") String password,
				@Multipart(value="folderId") String folderId,
				@Multipart(value="filename") String filename,
				@Multipart(value="myfile") DataHandler myfile
			){}


Thanks

-Br, Roman



--
View this message in context: http://camel.465427.n5.nabble.com/Upload-file-How-to-pass-InputStream-to-Camel-s-route-tp5729752p5729829.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Upload file - How to pass InputStream to Camel's route

Posted by Raul Kripalani <ra...@evosent.com>.
Hi Roman,

Yes, this is doable and is in fact quite simple.

Just add an argument of type DataHandler or InputStream to the method
signature, and annotate it with @Multipart, e.g.

@GET
@Path("/{myServerHost}/upload")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@PathParam("myHost") String knowledgetreeHost,
                        @QueryParam("myPort") String knowledgetreePort,
@QueryParam("user") String user,
                        @QueryParam("password") String password,
@QueryParam("file") String file,
                        @Multipart("file_binary") DataHandler binary) { }

Make sure your request is a standard HTTP Multipart request that contains a
body part with name = "file_binary".

More info here: http://cxf.apache.org/docs/jax-rs-multiparts.html.

Regards,

*Raúl Kripalani*
Enterprise Architect, Open Source Integration specialist, Program
Manager | Apache
Camel Committer
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk

On Mon, Mar 25, 2013 at 1:51 PM, jamalissimo <ro...@gmail.com>wrote:

> To be more specific, on client side there is classic form for uploading
> files
> which sends data to my service via @GET request. I can handle variables but
> I don't now how to handle file which comes with variables.
>
> -Br, Roman
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Upload-file-How-to-pass-InputStream-to-Camel-s-route-tp5729752p5729756.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Upload file - How to pass InputStream to Camel's route

Posted by jamalissimo <ro...@gmail.com>.
To be more specific, on client side there is classic form for uploading files
which sends data to my service via @GET request. I can handle variables but
I don't now how to handle file which comes with variables.

-Br, Roman



--
View this message in context: http://camel.465427.n5.nabble.com/Upload-file-How-to-pass-InputStream-to-Camel-s-route-tp5729752p5729756.html
Sent from the Camel - Users mailing list archive at Nabble.com.