|
Jetty has been edited by Claus Ibsen (Dec 30, 2008). Content:Jetty ComponentThe jetty: component provides HTTP based endpoints for consuming HTTP requests that arrive at a http endpoint. URI formatjetty:http://hostname[:port][/resourceUri][?options]
Message HeadersCamel will add the following headers to the input message on the exchange
Camel will also populate all request.parameter and request.headers. For instance of a client request with http://myserver/myserver?orderid=123 UsageYou can only consume from endpoints generated by the Jetty component. Therefore it should only be used as input into your camel Routes. To issue HTTP requests against other HTTP endpoints you can use the HTTP Component SampleIn this sample we define a route where we expose a http service at http://localhost:8080/myapp/myservice from("jetty:http://localhost:8080/myapp/myservice").process(new MyBookService());
Our business logic is implemented in our MyBookService class where we can access the http request stuff and return a response. public class MyBookService implements Processor { public void process(Exchange exchange) throws Exception { // just get the body as a string String body = exchange.getIn().getBody(String.class); // we have access to the HttpServletRequest here and we can grab it if we need it HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class); assertNotNull(req); // for unit testing assertEquals("bookid=123", body); // send a html response exchange.getOut(true).setBody("<html><body>Book 123 is Camel in Action</body></html>"); } } In the sample below we have a content based route that routes all requests that contain the URI parameter one to mock:one and all others to mock:other. from("jetty:" + serverUri) .choice() .when().simple("in.header.one").to("mock:one") .otherwise() .to("mock:other"); So if a client sends the http request: http://serverUri?> Session SupportSession support can be used to enable HttpSession and being able to get this while processing the exchange. <route> <from uri="jetty:http://0.0.0.0/myapp/myservice/?sessionSupport=true"/> <processRef ref="myCode"/> <route> And then we have a Processor that we configure as: <bean id="myCode" class="com.mycompany.MyCodeProcessor"/>
And in this processor we can get the HttpSession: public void process(Exchange exchange) throws Exception { HttpSession session = ((HttpExchange)exchange).getRequest().getSession(); ... } SSL https SupportJetty Provides SSL support out of the box. To configure Jetty to run in SSL mode, you simply set the uri to have https:// as the parameter. <from uri="jetty:https://0.0.0.0/myapp/myservice/"/>
Jetty will need to know where to load your keystore from and what passwords to use in order to load the correct SSL certificate. The relevant System Properties set will point jetty in the right direction. For the keystore path, use jetty.ssl.keystore To create a certificate, and for Password issues, read the following documentation at the Jetty Site. http://docs.codehaus.org/display/JETTY/How+to+configure+SSL Default behavior for returning HTTP status codesCamel will default use org.apache.camel.component.http.DefaultHttpBinding that handles how response is written, and also setting the http status code. If the exchange could be processed with success http status code 200 is returned. However if the OUT message contains a header HttpProducer.HTTP_RESPONSE_CODE then this code is used instead. To allow end users to set a specific status code. Customizing HttpBindingAvailable as of Camel 1.5.1/2.0 Camel will default use org.apache.camel.component.http.DefaultHttpBinding that handles how response is written. In the sample below we use our own binding to change how exceptions should be returned: public class MyHttpBinding extends DefaultHttpBinding { @Override public void doWriteExceptionResponse(Throwable exception, HttpServletResponse response) throws IOException { // we override the doWriteExceptionResponse as we only want to alter the binding how exceptions is // written back to the client. // we just return HTTP 200 so the client thinks its okay response.setStatus(200); // and we return this fixed text response.getWriter().write("Something went wrong but we dont care"); } } Then we can have our binding registered in the registry as: <bean id="mybinding" class="com.mycompany.MyHttpBinding"/>
And then we can refer to this binding when we configure the route: <route> <from uri="jetty:http://0.0.0.0:8080/myapp/myservice?httpBindingRef=mybinding"/> <to uri="bean:doSomething"/> </route> See Also |
Unsubscribe or edit your notifications preferences
