JettyPage edited by Claus IbsenChanges (4)
Full ContentJetty ComponentThe jetty component provides HTTP-based endpoints for consuming HTTP requests. That is, the Jetty component behaves as a simple Web server.
URI format
jetty:http://hostname[:port][/resourceUri][?options]
from Camel 2.3 onwards
For details of how to configure SSL on a Jetty endpoint, read the following documentation at the Jetty Site: http://docs.codehaus.org/display/JETTY/How+to+configure+SSL Some SSL properties aren't exposed directly by Camel, however Camel does expose the underlying SslSocketConnector, which will allow you to set properties like needClientAuth for mutual authentication requiring a client certificate or wantClientAuth for mutual authentication where a client doesn't need a certificate but can have one. There's a slight difference between Camel 1.6.x and 2.x: Camel 1.x <bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="sslSocketConnector"> <bean class="org.mortbay.jetty.security.SslSocketConnector"> <property name="password"value="..."/> <property name="keyPassword"value="..."/> <property name="keystore"value="..."/> <property name="wantClientAuth"value="..."/> <property name="truststore"value="..."/> </bean> </property> </bean> until Camel 2.2 <bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="sslSocketConnectors"> <map> <entry key="8043"> <bean class="org.mortbay.jetty.security.SslSocketConnector"> <property name="password"value="..."/> <property name="keyPassword"value="..."/> <property name="keystore"value="..."/> <property name="needClientAuth"value="..."/> <property name="truststore"value="..."/> </bean> </entry> </map> </property> </bean> Camel 2.3, 2.4 <bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="sslSocketConnectors"> <map> <entry key="8043"> <bean class="org.eclipse.jetty.server.ssl.SslSocketConnector"> <property name="password"value="..."/> <property name="keyPassword"value="..."/> <property name="keystore"value="..."/> <property name="needClientAuth"value="..."/> <property name="truststore"value="..."/> </bean> </entry> </map> </property> </bean> *From Camel 2.5 we switch to use SslSelectChannelConnector * <bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="sslSocketConnectors"> <map> <entry key="8043"> <bean class="org.eclipse.jetty.server.ssl.SslSelectChannelConnector"> <property name="password"value="..."/> <property name="keyPassword"value="..."/> <property name="keystore"value="..."/> <property name="needClientAuth"value="..."/> <property name="truststore"value="..."/> </bean> </entry> </map> </property> </bean> The value you use as keys in the above map is the port you configure Jetty to listen on. Configuring general SSL propertiesAvailable as of Camel 2.5 Instead of a per port number specific SSL socket connector (as shown above) you can now configure general properties which applies for all SSL socket connectors (which is not explicit configured as above with the port number as entry). <bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="sslSocketConnectorProperties"> <properties> <property name="password"value="..."/> <property name="keyPassword"value="..."/> <property name="keystore"value="..."/> <property name="needClientAuth"value="..."/> <property name="truststore"value="..."/> </properties> </property> </bean> How to obtain reference to the X509CertificateJetty stores a reference to the certificate in the HttpServletRequest which you can access from code as follows:
HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
X509Certificate cert = (X509Certificate) req.getAttribute("javax.servlet.request.X509Certificate")
Configuring general HTTP propertiesAvailable as of Camel 2.5 Instead of a per port number specific HTTP socket connector (as shown above) you can now configure general properties which applies for all HTTP socket connectors (which is not explicit configured as above with the port number as entry). <bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="socketConnectorProperties"> <properties> <property name="acceptors" value="4"/> <property name="maxIdleTime" value="300000"/> </properties> </property> </bean> Default behavior for returning HTTP status codesThe default behavior of HTTP status codes is defined by the org.apache.camel.component.http.DefaultHttpBinding class, which handles how a response is written and also sets the HTTP status code. If the exchange was processed successfully, the 200 HTTP status code is returned. Customizing HttpBindingAvailable as of Camel 1.5.1/2.0 By default, Camel uses the org.apache.camel.component.http.DefaultHttpBinding to handle how a response is written. If you like, you can customize this behavior either by implementing your own HttpBinding class or by extending DefaultHttpBinding and overriding the appropriate methods. The following example shows how to customize the DefaultHttpBinding in order to change how exceptions are returned: public class MyJettyHttpBinding extends DefaultJettyHttpBinding { @Override protected void populateResponse(Exchange exchange, JettyContentExchange httpExchange, Message in, HeaderFilterStrategy strategy, int responseCode) throws IOException { Message answer = exchange.getOut(); answer.setHeaders(in.getHeaders()); answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode); answer.setBody("Not exactly the message the server returned."); } } We can then create an instance of our binding and register it in the Spring registry as follows:
<bean id="mybinding"class="com.mycompany.MyHttpBinding"/>
And then we can reference this binding when we define the route: <route><from uri="jetty:http://0.0.0.0:8080/myapp/myservice?httpBindingRef=mybinding"/><to uri="bean:doSomething"/></route> Jetty handlers and security configurationAvailable as of Camel 1.6.1/2.0: You can configure a list of Jetty handlers on the endpoint, which can be useful for enabling advanced Jetty security features. These handlers are configured in Spring XML as follows: <-- Jetty Security handling --> <bean id="userRealm" class="org.mortbay.jetty.plus.jaas.JAASUserRealm"> <property name="name" value="tracker-users"/> <property name="loginModuleName" value="ldaploginmodule"/> </bean> <bean id="constraint" class="org.mortbay.jetty.security.Constraint"> <property name="name" value="BASIC"/> <property name="roles" value="tracker-users"/> <property name="authenticate" value="true"/> </bean> <bean id="constraintMapping" class="org.mortbay.jetty.security.ConstraintMapping"> <property name="constraint" ref="constraint"/> <property name="pathSpec" value="/*"/> </bean> <bean id="securityHandler" class="org.mortbay.jetty.security.SecurityHandler"> <property name="userRealm" ref="userRealm"/> <property name="constraintMappings" ref="constraintMapping"/> </bean> And from Camel 2.3 onwards you can configure a list of Jetty handlers as follows: <-- Jetty Security handling --> <bean id="constraint" class="org.eclipse.jetty.http.security.Constraint"> <property name="name" value="BASIC"/> <property name="roles" value="tracker-users"/> <property name="authenticate" value="true"/> </bean> <bean id="constraintMapping" class="org.eclipse.jetty.security.ConstraintMapping"> <property name="constraint" ref="constraint"/> <property name="pathSpec" value="/*"/> </bean> <bean id="securityHandler" class="org.eclipse.jetty.security.ConstraintSecurityHandler"> <property name="authenticator"> <bean class="org.eclipse.jetty.security.authentication.BasicAuthenticator"/> </property> <property name="constraintMappings"> <list> <ref bean="constraintMapping"/> </list> </property> </bean> You can then define the endpoint as:
from("jetty:http://0.0.0.0:9080/myservice?handlers=securityHandler")
If you need more handlers, set the handlers option equal to a comma-separated list of bean IDs. How to return a custom HTTP 500 reply messageYou may want to return a custom reply message when something goes wrong, instead of the default reply message Camel Jetty replies with. from("jetty://http://localhost:{{port}}/myserver") // use onException to catch all exceptions and return a custom reply message .onException(Exception.class) .handled(true) // create a custom failure response .transform(constant("Dude something went wrong")) // we must remember to set error code 500 as handled(true) // otherwise would let Camel thing its a OK response (200) .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(500)) .end() // now just force an exception immediately .throwException(new IllegalArgumentException("I cannot do this")); Multi-part Form supportFrom Camel 2.3.0, camel-jetty support to multipart form post out of box. The submitted form-data are mapped into the message header. Camel-jetty creates an attachment for each uploaded file. The file name is mapped to the name of the attachment. The content type is set as the content type of the attachment file name. You can find the example here. Note: getName() functions as shown below in versions 2.5 and higher. In earlier versions you receive the temporary file name for the attachment instead // Set the jetty temp directory which store the file for multi part form // camel-jetty will clean up the file after it handled the request. // The option works rightly from Camel 2.4.0 getContext().getProperties().put("CamelJettyTempDir", "target"); from("jetty://http://localhost:{{port}}/test").process(new Processor() { public void process(Exchange exchange) throws Exception { Message in = exchange.getIn(); assertEquals("Get a wrong attachement size", 1, in.getAttachments().size()); // The file name is attachment id DataHandler data = "" class="code-quote">"NOTICE.txt"); assertNotNull("Should get the DataHandle NOTICE.txt", data); assertEquals("Get a wrong content type", "text/plain", data.getContentType()); assertEquals("Got the wrong name", "NOTICE.txt", data.getName()); assertTrue("We should get the data from the DataHandle", data.getDataSource() .getInputStream().available() > 0); // The other form date can be get from the message header exchange.getOut().setBody(in.getHeader("comment")); } }); Jetty JMX supportFrom Camel 2.3.0, camel-jetty supports the enabling of Jetty's JMX capabilities at the component and endpoint level with the endpoint configuration taking priority. Note that JMX must be enabled within the Camel context in order to enable JMX support in this component as the component provides Jetty with a reference to the MBeanServer registered with the Camel context. Because the camel-jetty component caches and reuses Jetty resources for a given protocol/host/port pairing, this configuration option will only be evaluated during the creation of the first endpoint to use a protocol/host/port pairing. For example, given two routes created from the following XML fragments, JMX support would remain enabled for all endpoints listening on "https://0.0.0.0".
<from uri="jetty:https://0.0.0.0/myapp/myservice1/?enableJmx=true"/>
<from uri="jetty:https://0.0.0.0/myapp/myservice2/?enableJmx=false"/>
The camel-jetty component also provides for direct configuration of the Jetty MBeanContainer. Jetty creates MBean names dynamically. If you are running another instance of Jetty outside of the Camel context and sharing the same MBeanServer between the instances, you can provide both instances with a reference to the same MBeanContainer in order to avoid name collisions when registering Jetty MBeans. See Also
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > Jetty confluence
- [CONF] Apache Camel > Jetty confluence
- [CONF] Apache Camel > Jetty confluence
- [CONF] Apache Camel > Jetty confluence
- [CONF] Apache Camel > Jetty confluence
- [CONF] Apache Camel > Jetty confluence
- [CONF] Apache Camel > Jetty confluence
- [CONF] Apache Camel > Jetty confluence
- [CONF] Apache Camel > Jetty confluence
- [CONF] Apache Camel > Jetty confluence