You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by navaltiger <vi...@gmail.com> on 2014/11/12 05:21:48 UTC
Multipart form data and servlet/restlet endpoint
dear all,
I am looking to implement a route where reslet/cxfrs end point will accept
file as multipart request and process. (Request may have some JSON data as
well.
For sake of simplify thing and get it working with servlet first have tried
following code. Also tried sending file using curl. I can see file related
info in headers and debug output, but not able to retrieve attachment. But
no luck.
(Additional info: I am running project as mvn jetty:run. The package is in
form of war and will be deployed on web logic server. Moreover, stream
caching is not in place.)
from("servlet:///hello").process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
StringBuffer v = new StringBuffer();
HttpServletRequest request = (HttpServletRequest) in
.getHeaders().get(Exchange.HTTP_SERVLET_REQUEST);
DiskFileItemFactory diskFile = new DiskFileItemFactory();
FileItemFactory factory = diskFile;
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
.....
--
View this message in context: http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Multipart form data and servlet/restlet endpoint
Posted by navaltiger <vi...@gmail.com>.
Hi Claus,
Thanks for reply.
I also tried following logic (in fact this was first attempt which did not
work)
Set<String> names = in.getAttachmentNames();
for (String n : names) {
DataHandler h = in.getAttachment(n);
if (h != null) {
try {
int read = 0;
byte[] bytes = new byte[1024];
Object o = h.getContent();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Actually, 'names' Set is empty.
--
View this message in context: http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5758953.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Multipart form data and servlet/restlet endpoint
Posted by navaltiger <vi...@gmail.com>.
Hi,
Could do it following way. Will upload whole project at GitHub following
way.
-- web.xml --
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>CamelServlet</servlet-name>
<servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CamelServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>MultipartFilter</filter-name>
<filter-class>org.eclipse.jetty.servlets.MultiPartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MultipartFilter</filter-name>
<servlet-name>CamelServlet</servlet-name>
</filter-mapping>
</web-app>
--
View this message in context: http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5759081.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Multipart form data and servlet/restlet endpoint
Posted by Willem Jiang <wi...@gmail.com>.
I’m not sure what kind of web container that you use.
Here is a link[1] about how to do it with tomcat 7.
[1]http://stackoverflow.com/questions/8047173/how-to-enable-multipart-form-data-in-tomcat-7-0-8-server
--
Willem Jiang
Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang
Weibo: 姜宁willem
On November 13, 2014 at 12:06:06 PM, navaltiger (vijay.rathi@gmail.com) wrote:
> Any pointer towards how to apply such filter?
>
> On Thu, Nov 13, 2014 at 9:30 AM, Willem.Jiang [via Camel] <
> ml-node+s465427n5759023h46@n5.nabble.com> wrote:
>
> > We apply the multipart handler to jetty endpoint by default.
> > If you use camel-servlet, it’s your job to setup that kind of filter in
> > the web.xml to parser the Multipart form data for you.
> >
> > --
> > Willem Jiang
> >
> > Red Hat, Inc.
> > Web: http://www.redhat.com
> > Blog: http://willemjiang.blogspot.com (English)
> > http://jnn.iteye.com (Chinese)
> > Twitter: willemjiang
> > Weibo: 姜宁willem
> >
> >
> >
> > On November 12, 2014 at 6:35:59 PM, navaltiger ([hidden email]
> > ) wrote:
> >
> > > Hi,
> > >
> > > With Jetty based consumer it worked but no luck with servlet. I am
> > > executing this package as jetty:run
> > >
> > > public void configure() throws Exception {
> > > // getContext().getProperties().put("CamelJettyTempDir", "target");
> > >
> > > // Jetty://http works file
> > > // from("jetty://http://0.0.0.0:9090/test").process(new Processor() {
> > > .// servlet:// does not work
> > > from("servlet:///test").process(new Processor() {
> > > public void process(Exchange exchange) throws Exception {
> > >
> > > Message in = exchange.getIn();
> > > StringBuffer v = new StringBuffer();
> > >
> > > v.append("\n" + in.getAttachmentNames().size() + "\n");
> > >
> > > Set names = in.getAttachmentNames();
> > > for (String n : names) {
> > > DataHandler h = in.getAttachment(n);
> > > v.append("\n" + h + "\n");
> > > if (h != null) {
> > > try {
> > > int read = 0;
> > > byte[] bytes = new byte[1024];
> > > // Object o = h.getContent();
> > > InputStream inputStream = h.getInputStream();
> > > FileOutputStream fout = new FileOutputStream(
> > > new File(
> > > "/Users/navaltiger/workspace/vr.bin"));
> > >
> > > while ((read = inputStream.read(bytes)) != -1) {
> > > fout.write(bytes, 0, read);
> > > }
> > > v.append("\n done \n");
> > > } catch (Exception e) {
> > > e.printStackTrace();
> > > }
> > > }
> > > }
> > > exchange.getOut().setBody(v);
> > > }
> > >
> > > });
> > > }
> > >
> > >
> > >
> > > --
> > > View this message in context:
> > http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5758976.html
> >
> > > Sent from the Camel - Users mailing list archive at Nabble.com.
> > >
> >
> >
> >
> > ------------------------------
> > If you reply to this email, your message will be added to the discussion
> > below:
> >
> > http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5759023.html
> > To unsubscribe from Multipart form data and servlet/restlet endpoint, click
> > here
> >
> > .
> > NAML
> >
> >
>
>
>
> --
>
> *Vijay Rathi | T: +91 93734 33545 | vijay.rathi@gmail.com
> | *http://gadgetnfun.blogspot.com/
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5759024.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Multipart form data and servlet/restlet endpoint
Posted by navaltiger <vi...@gmail.com>.
Any pointer towards how to apply such filter?
On Thu, Nov 13, 2014 at 9:30 AM, Willem.Jiang [via Camel] <
ml-node+s465427n5759023h46@n5.nabble.com> wrote:
> We apply the multipart handler to jetty endpoint by default.
> If you use camel-servlet, it’s your job to setup that kind of filter in
> the web.xml to parser the Multipart form data for you.
>
> --
> Willem Jiang
>
> Red Hat, Inc.
> Web: http://www.redhat.com
> Blog: http://willemjiang.blogspot.com (English)
> http://jnn.iteye.com (Chinese)
> Twitter: willemjiang
> Weibo: 姜宁willem
>
>
>
> On November 12, 2014 at 6:35:59 PM, navaltiger ([hidden email]
> <http://user/SendEmail.jtp?type=node&node=5759023&i=0>) wrote:
>
> > Hi,
> >
> > With Jetty based consumer it worked but no luck with servlet. I am
> > executing this package as jetty:run
> >
> > public void configure() throws Exception {
> > // getContext().getProperties().put("CamelJettyTempDir", "target");
> >
> > // Jetty://http works file
> > // from("jetty://http://0.0.0.0:9090/test").process(new Processor() {
> > .// servlet:// does not work
> > from("servlet:///test").process(new Processor() {
> > public void process(Exchange exchange) throws Exception {
> >
> > Message in = exchange.getIn();
> > StringBuffer v = new StringBuffer();
> >
> > v.append("\n" + in.getAttachmentNames().size() + "\n");
> >
> > Set names = in.getAttachmentNames();
> > for (String n : names) {
> > DataHandler h = in.getAttachment(n);
> > v.append("\n" + h + "\n");
> > if (h != null) {
> > try {
> > int read = 0;
> > byte[] bytes = new byte[1024];
> > // Object o = h.getContent();
> > InputStream inputStream = h.getInputStream();
> > FileOutputStream fout = new FileOutputStream(
> > new File(
> > "/Users/navaltiger/workspace/vr.bin"));
> >
> > while ((read = inputStream.read(bytes)) != -1) {
> > fout.write(bytes, 0, read);
> > }
> > v.append("\n done \n");
> > } catch (Exception e) {
> > e.printStackTrace();
> > }
> > }
> > }
> > exchange.getOut().setBody(v);
> > }
> >
> > });
> > }
> >
> >
> >
> > --
> > View this message in context:
> http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5758976.html
>
> > Sent from the Camel - Users mailing list archive at Nabble.com.
> >
>
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5759023.html
> To unsubscribe from Multipart form data and servlet/restlet endpoint, click
> here
> <http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5758948&code=dmlqYXkucmF0aGlAZ21haWwuY29tfDU3NTg5NDh8MTA4NTUxMjc3Mg==>
> .
> NAML
> <http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
--
*Vijay Rathi | T: +91 93734 33545 | vijay.rathi@gmail.com
<vi...@kahunasystems.com> | *http://gadgetnfun.blogspot.com/
--
View this message in context: http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5759024.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Multipart form data and servlet/restlet endpoint
Posted by Willem Jiang <wi...@gmail.com>.
We apply the multipart handler to jetty endpoint by default.
If you use camel-servlet, it’s your job to setup that kind of filter in the web.xml to parser the Multipart form data for you.
--
Willem Jiang
Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang
Weibo: 姜宁willem
On November 12, 2014 at 6:35:59 PM, navaltiger (vijay.rathi@gmail.com) wrote:
> Hi,
>
> With Jetty based consumer it worked but no luck with servlet. I am
> executing this package as jetty:run
>
> public void configure() throws Exception {
> // getContext().getProperties().put("CamelJettyTempDir", "target");
>
> // Jetty://http works file
> // from("jetty://http://0.0.0.0:9090/test").process(new Processor() {
> .// servlet:// does not work
> from("servlet:///test").process(new Processor() {
> public void process(Exchange exchange) throws Exception {
>
> Message in = exchange.getIn();
> StringBuffer v = new StringBuffer();
>
> v.append("\n" + in.getAttachmentNames().size() + "\n");
>
> Set names = in.getAttachmentNames();
> for (String n : names) {
> DataHandler h = in.getAttachment(n);
> v.append("\n" + h + "\n");
> if (h != null) {
> try {
> int read = 0;
> byte[] bytes = new byte[1024];
> // Object o = h.getContent();
> InputStream inputStream = h.getInputStream();
> FileOutputStream fout = new FileOutputStream(
> new File(
> "/Users/navaltiger/workspace/vr.bin"));
>
> while ((read = inputStream.read(bytes)) != -1) {
> fout.write(bytes, 0, read);
> }
> v.append("\n done \n");
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> }
> exchange.getOut().setBody(v);
> }
>
> });
> }
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5758976.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Re: Multipart form data and servlet/restlet endpoint
Posted by navaltiger <vi...@gmail.com>.
Hi,
With Jetty based consumer it worked but no luck with servlet. I am
executing this package as jetty:run
public void configure() throws Exception {
// getContext().getProperties().put("CamelJettyTempDir", "target");
// Jetty://http works file
// from("jetty://http://0.0.0.0:9090/test").process(new Processor() {
.// servlet:// does not work
from("servlet:///test").process(new Processor() {
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
StringBuffer v = new StringBuffer();
v.append("\n" + in.getAttachmentNames().size() + "\n");
Set<String> names = in.getAttachmentNames();
for (String n : names) {
DataHandler h = in.getAttachment(n);
v.append("\n" + h + "\n");
if (h != null) {
try {
int read = 0;
byte[] bytes = new byte[1024];
// Object o = h.getContent();
InputStream inputStream = h.getInputStream();
FileOutputStream fout = new FileOutputStream(
new File(
"/Users/navaltiger/workspace/vr.bin"));
while ((read = inputStream.read(bytes)) != -1) {
fout.write(bytes, 0, read);
}
v.append("\n done \n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
exchange.getOut().setBody(v);
}
});
}
--
View this message in context: http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948p5758976.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Multipart form data and servlet/restlet endpoint
Posted by Claus Ibsen <cl...@gmail.com>.
Hi
Maybe see if you can find some samples in camel-servlet / camel-jetty
unit tests that deals with attachments.
And attachments would be on the Camel Message with the getAttachments method.
On Wed, Nov 12, 2014 at 5:21 AM, navaltiger <vi...@gmail.com> wrote:
> dear all,
>
> I am looking to implement a route where reslet/cxfrs end point will accept
> file as multipart request and process. (Request may have some JSON data as
> well.
>
> For sake of simplify thing and get it working with servlet first have tried
> following code. Also tried sending file using curl. I can see file related
> info in headers and debug output, but not able to retrieve attachment. But
> no luck.
> (Additional info: I am running project as mvn jetty:run. The package is in
> form of war and will be deployed on web logic server. Moreover, stream
> caching is not in place.)
>
> from("servlet:///hello").process(new Processor() {
> @Override
> public void process(Exchange exchange) throws Exception {
> Message in = exchange.getIn();
> StringBuffer v = new StringBuffer();
> HttpServletRequest request = (HttpServletRequest) in
> .getHeaders().get(Exchange.HTTP_SERVLET_REQUEST);
>
> DiskFileItemFactory diskFile = new DiskFileItemFactory();
> FileItemFactory factory = diskFile;
> ServletFileUpload upload = new ServletFileUpload(factory);
> List items = upload.parseRequest(request);
> .....
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Multipart-form-data-and-servlet-restlet-endpoint-tp5758948.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
--
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/