return binary image data in camel/restlet environment
I need to return image from route in restlet environment, if in normal camel environment I should be able to do byte[] imageData = getImage(); HttpServletResponse response = exchange.getIn().getBody(HttpServletResponse.class); response.setContentType("image/png"); OutputStream os = response.getOutputStream(); os.write(imageData); os.close(); Now, in restlet environment, the response is null. I am doing: exchange.getOut().setBody(imageData); While it executed correctly, the data is encoded and inflated(about 30-40% more), the header is set to: Content-Type: text/plain;charset=UTF-8 if I don't set "Content-Type", but even I set it to original type: "image/png", the header will be Content-Type: image/pgn;charset=UTF-8 The returning binary data is still inflated and encoded. Is there any way to prevent this to happen? I guess my another question would be: How do I get HttpServletResponse or OutputStream for response in restlet environment so I can simply set up and output my binary data as in normal servlet environment? Where is it hidden(I tried to find it under debugging environment but could not find it)? Thanks. -- View this message in context: http://camel.465427.n5.nabble.com/return-binary-image-data-in-camel-restlet-environment-tp5741062.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: return binary image data in camel/restlet environment
Thanks Willem. I will try the work around. How do I know the fix is ready? Do I need to upgrade to latest Camel version in order to get fix? -- View this message in context: http://camel.465427.n5.nabble.com/return-binary-image-data-in-camel-restlet-environment-tp5741062p5741140.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: return binary image data in camel/restlet environment
Thanks for quick fix, Willem. I will check it out. Actually your suggestion to set body to inputstream works very well, here is part of the code in case anybody wants to do the same: exchange.getOut().setHeader( Exchange.HTTP_RESPONSE_CODE, 200 ); exchange.getOut().setHeader( Exchange.CONTENT_TYPE, mediaType ); ByteArrayInputStream bais = new ByteArrayInputStream(imageData); exchange.getOut().setBody( bais ); Finally, I tried to use ByteArrayOutputStream at first place, it did not work, I am still wondering why it's inputsteam not outputstream since I am send data out from Camel point of view. -- View this message in context: http://camel.465427.n5.nabble.com/return-binary-image-data-in-camel-restlet-environment-tp5741062p5741238.html Sent from the Camel - Users mailing list archive at Nabble.com.
recipientList with POST and request body
Hi, I need to send request to a dynamically constructed URL list which provides RESTful web services. So my simplest need is to send POST requests with fixed JSON body, later I may need to construct different JSON body for each URL in the list as well, and since it's RESTful service, DELETE/PUT are also possible. how should I do it? This is standard recipientList pattern from document: myHeader Should I do: construct my urls here POST application/json {"name":"fixed text"} WEB_SERVICE_URLS Thanks for advice. -- View this message in context: http://camel.465427.n5.nabble.com/recipientList-with-POST-and-request-body-tp5725142.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: recipientList with POST and request body
Willem, Thanks, I read some of your posts in the past, they were very helpful. I appreciate your advice. My currently situation is: I used beans for complicated business logic, to send different requests to different resources and to aggregate the results in the past. But now our company is moving to cloud computing environment, our platform (modified/enhanced camel environment) moved to cloud environment. So Bean implementation is strongly discouraged since (auto/versioned) bean deployment could be very complicated in the environment. I am moving to write all the code in groovy within the route instead of bean as best practice in our group. ProducerTemplate could be good solution if bean implementation is allowed. I tried a simple test by sending single request in groovy(I need to aggregate the final response as part of requirement, also I used setBody tag to cheat route to send request which may not be best practice): import org.apache.camel.ProducerTemplate import org.apache.camel.impl.DefaultProducerTemplate def template = context.createProducerTemplate() def endpoint = "myendpoint" def body = ${header.REST_REQUEST_BODY} def headers = ["Content-Type":"application/json", CamelHttpMethod:"POST"] def response = template.requestBodyAndHeader(endpoint, body, headers, java.lang.String.class) return response I got expected exception: context is missing since context is NOT default build-in variable. How do I get context in "simple" DSL, or how do I get a producerTemplate without using context? If not, is there other solutions/patterns that I can use for this situation without using beans? I am looking at [splitter][broadcast] pattern, but have not found way to resolve it. Maybe this is too demanding for Camel without bean: dynamic URI list, dynamic HTTP method(optional), dynamic request body(optional), and aggregate all responses. I am using Spring DSL plus groovy only, java DSL is not allowed. -- View this message in context: http://camel.465427.n5.nabble.com/recipientList-with-POST-and-request-body-tp5725142p5725192.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: recipientList with POST and request body
I tried producerTemplate for bean approach (in groovy) too. I got exception when I tried to POST to an HTTP endpoint: def template = context.createProducerTemplate() def endpointUri = headers.END_POINT def xheaders = ["Content-Type":"application/json", "CamelHttpMethod":"POST"] def xbody ='''{"id":["48792683442"],"index":"0"}''' def response = template.requestBodyAndHeaders(endpointUri, xbody, xheaders, java.lang.String.class) exchange.in.setBody(response) the exception is: org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[Message: {"id":["48792683442"],"index":"0"}] at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1237)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:509)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:442)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.sendBodyAndHeaders(DefaultProducerTemplate.java:247)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:296)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:292)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:329)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.ProducerTemplate$requestBodyAndHeaders.call(Unknown Source) I changed to use template.sendBodyAndHeaders(endpointUri, xbody, xheaders) in case the result is causing problem, I got same exception. Can anybody help to use producerTemplate to POST/PUT/DELETE HTTP request? It should not be limited to do only GET, isn't it? no document says anything about setting HTTP methods for producerTemplate. It would be tedious to write my own httpClient based code. -- View this message in context: http://camel.465427.n5.nabble.com/recipientList-with-POST-and-request-body-tp5725142p5725201.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: recipientList with POST and request body
I added a line: template.start() before sending request, it did not help, got same exception. I changed to context.start() it did not work either. The code is within bean implementation, so context is build-in variable, and started already, why do I need to start it again? From other example code, the pattern to use it should be simple as 2 lines: def template = context.createProducerTemplate() def response = template.requestBodyAndHeaders(endpointUri, xbody, xheaders, java.lang.String.class) Can someone post working code to use producerTemplate to POST request? -- View this message in context: http://camel.465427.n5.nabble.com/recipientList-with-POST-and-request-body-tp5725142p5725283.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: recipientList with POST and request body
I posted the exception before, let me copy the exception again: the exception is: org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[Message: {"id":["48792683442"],"index":"0"}] at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1237)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:509)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:442)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.sendBodyAndHeaders(DefaultProducerTemplate.java:247)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:296)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:292)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:329)[camel-core-2.9.1.jar:2.9.1] at org.apache.camel.ProducerTemplate$requestBodyAndHeaders.call(Unknown Source) -- View this message in context: http://camel.465427.n5.nabble.com/recipientList-with-POST-and-request-body-tp5725142p5725288.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: recipientList with POST and request body
Thanks. The camelContext is not causing exception anymore. The cause of the exception is: Caused by: org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking https://locker.att.net/service/content/1/playlists/158040465976 with statusCode: 302, redirectLocation: https://cprodctnxsf.att.net/commonLogin/igate_edam/controller.do?TAM_OP=login&USERNAME=unauthenticated&ERROR_CODE=0x&ERROR_TEXT=HPDBA0521I%20%20%20Successful%20completion&METHOD=POST&URL=%2Fservice%2Fcontent%2F1%2Fplaylists%2F158040465976&REFERER=&HOSTNAME=locker.att.net&AUTHNLEVEL=&FAILREASON=&PROTOCOL=https&OLDSESSION= at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:222) at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:157) at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) at org.apache.camel.processor.UnitOfWorkProcessor.processAsync(UnitOfWorkProcessor.java:150) at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:117) at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:99) at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:86) at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:63) at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:352) at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:324) at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:223) at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:324) at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:186) at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:115) at org.apache.camel.impl.DefaultProducerTemplate.sendBodyAndHeaders(DefaultProducerTemplate.java:238) ... 107 more -- View this message in context: http://camel.465427.n5.nabble.com/recipientList-with-POST-and-request-body-tp5725142p5725296.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: recipientList with POST and request body
I think I understand now why I can't do this with producerTemplate: yes, there is intensive security checks for our web service site, at this point to use producerTemplate I already logged in and got session in previous steps in the same route, the problem is that the security token with session information is not passed to this site correctly since producerTemplate is passing simple body and headers without security tokens, that's why it failed. Do you have way to take tokens with the template? -- View this message in context: http://camel.465427.n5.nabble.com/recipientList-with-POST-and-request-body-tp5725142p5725300.html Sent from the Camel - Users mailing list archive at Nabble.com.
HTTP Endpoint construction in CamelSpringTest
I am trying to use CamelSpringTestSupport to build my unit tests. Most of my routes are in Spring XML format. Here is my first test case: public class SimpeRouteTest extends CamelSpringTestSupport { protected AbstractXmlApplicationContext createApplicationContext() { return new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml"); } @Test public void testSimpleRoute() throws Exception { String response = template.requestBodyAndHeader("http://localhost:8080/services/test/simple";, "body: Hello World", "MY_HEADER", "my name", String.class); System.err.println(response); } } The Spring XML route: http://camel.apache.org/schema/spring";> hello world I can see the log that route is started during(before) test run: [ main] SpringCamelContext INFO Route: route90 started and consuming from: Endpoint[servlet:///test/simple] if I use "servlet:///test/simple", I got: You cannot create producer with servlet endpoint, please consider to use http or http4 endpoint. if I use "http://localhost:8080/services/test/simple";, or "http://localhost:80/test/simple"; or other versions, I got: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect I read Testing with Camel in CamelInAction book, it used "file://" which is working, but there are no samples for "servlet:///" which is most commonly used. What's is correct way for the endpoint? How to test "servlet:///" or "http:" component without having to start routes in a web server? I am using camel-core 2.9.1, camel-test 2.9.1. Thanks. -- View this message in context: http://camel.465427.n5.nabble.com/HTTP-Endpoint-construction-in-CamelSpringTest-tp5726090.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: HTTP Endpoint construction in CamelSpringTest
Actually I have been considering using jetty, I just have not tried out yet. Let me know if you have some ready samples in hand for this purpose. I would imagine if I use jetty, I have to put "jetty:" in front of all URIs, then remove them after unit test, if you want to do test again, you have to repeat these steps. Can you think of some other elegant way to do it? And another thought: since I can see all the routes actually are started by SpringCamelContext when I ran this test, I can find them from the log: SpringCamelContext INFO Route: route90 started and consuming from: Endpoint[servlet:///test/simple] Where does SpringCamelContext keep them? are the routes in memory? are they accessible from some APIs if they are not published as web services? I looked at the unit test code for camel-servlet that you pointed out, they use something from com.meterware.httpunit to start another servlet environment, I already have jetty configured in my environment as plug-in, so I don't want to do that. Another reason is that all tests are built around Java DSL based routes, no Spring XML DSL route tests. I don't want to dig that deep. Thanks. -- View this message in context: http://camel.465427.n5.nabble.com/HTTP-Endpoint-construction-in-CamelSpringTest-tp5726090p5726165.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: HTTP Endpoint construction in CamelSpringTest
You are right. I was trying too hard to use CamelSpringTestSupport, thinking it might be simpler to start my route and to test it together. I am wrong. This CamelSpringTestSupport can do other tests, say file in Ibsen's book, but not for servlet. I am going to quit using CamelSpringTestSupport, just go straight with HttpUnit or HttpClient to do this kind of test. Besides, I also found it's hard to specify request method "GET/POST/PUT/DELETE" with ProducerTemplate. -- View this message in context: http://camel.465427.n5.nabble.com/HTTP-Endpoint-construction-in-CamelSpringTest-tp5726090p5726170.html Sent from the Camel - Users mailing list archive at Nabble.com.
How do I get instance of JobDetail from a quartz route
I have a route look like: ... Inside of this route, I need to access the JobDetail or StdScheduler since the configuration in the cron job may change, but I still can modify business logic within the route based on changed time schedule. I can see many information in the Camel HTTP headers: jobDetail=JobDetail 'DEFAULT.quartz-endpoint70': jobClass: 'org.apache.camel.component.quartz.CamelJob isStateful: false isVolatile: false isDurable: false requestsRecovers: false, fireTime=Thu Feb 14 11:40:00 EST 2013, result=null, triggerGroup=modelshop, scheduler=org.quartz.impl.StdScheduler@19d0c27, nextFireTime=Thu Feb 14 11:42:00 EST 2013, mergedJobDataMap=org.quartz.JobDataMap@b0eb8a97, triggerName=api_health_report, jobRunTime=-1, jobInstance=org.apache.camel.component.quartz.CamelJob@e37fe5, CamelHttpMethod=GET, refireCount=0, scheduledFireTime=Thu Feb 14 11:40:00 EST 2013, previousFireTime=Thu Feb 14 11:38:00 EST 2013, calendar=null but I need actual schedule, how can I access it? is there a bean I can access from context or register by using names "jobDetail", "scheduler" or "jobInstance" in the headers? -- View this message in context: http://camel.465427.n5.nabble.com/How-do-I-get-instance-of-JobDetail-from-a-quartz-route-tp5727629.html Sent from the Camel - Users mailing list archive at Nabble.com.
support for RESTful endpoint or dynamic endpoint construction
Hi, I am new here, I posted this to developer forum which seems not good place for this, so I am re-posting it here. I have couple of RESTful web service that I want to use Camel as integration point. I had a brief review for restlet which is supposed to be the solution, but from I have seen, it's not straight forward to use: 1. straight forward support/construct RESTful dynamic URL for both and : http://host:port/services/{uid}/table1/{item_id} 2. it supports request methods GET,POST,PUT, DELETE, I did not see how Camel handles these methods. Can Camel handle dynamic URL construction with variables? in ideal case, it should support: Since many social network APIs are in this format, for instance: http://graph.facebook.com/{uid}/picture http://graph.facebook.com/{uid}/friends http://graph.facebook.com/{uid}/{about_everything} Can Camel at least support dynamic construction of URL out of box even without PUT and DELETE support? I don't know how big effort is needed to do this. If required, I am willing to contribute. I looked at the this CXFRS component for Rest endpoint, it seems to me no use to me. If I am writing Spring controllers like the sample code: @RequestMapping("/services") @Controller class MyController { @RequestMapping("/{id1}/resource/{id2}") public @ResponseBody String method1(@PathVariable String id1, @PathVariable String id2) { ... } } I will be directly writing a regular web app for Restful web services with much more control like I am doing right now, why do I need Camel plus CXFRS? I looked restlet approach, it solved construction for but not for , and the configuration is very intrusive-it requires a separate servlet in addition to normal Camel servlet, it also requires to change appContext.xml and web.xml. I would use it only if it solve Restful endpoint for both an at same time. To me, solving dynamic construction of endpoint is only correct way to do it inside of Camel. Zemin -- View this message in context: http://camel.465427.n5.nabble.com/support-for-RESTful-endpoint-or-dynamic-endpoint-construction-tp5604487p5604487.html Sent from the Camel - Users mailing list archive at Nabble.com.