...
Maven users should add the following dependency to their pom.xml
to use this component:
Code Block |
---|
|
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jetty</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
|
...
The following is a basic example of how to send an HTTP request to an existing HTTP endpoint.
Java DSL:
Code Block |
---|
|
from("direct:start")
.to("jetty://http://www.google.com");
|
XML DSL:
Code Block |
---|
|
<route>
<from uri="direct:start"/>
<to uri="jetty://http://www.google.com"/>
<route>
|
...
For example, the following route enables sessions:
Code Block |
---|
|
<route>
<from uri="jetty:http://0.0.0.0/myapp/myservice/?sessionSupport=true"/>
<processRef ref="myCode"/>
<route>
|
The myCode
Processor can be instantiated by a Spring bean
element:
Code Block |
---|
|
<bean id="myCode"class="com.mycompany.MyCodeProcessor"/>
|
Where the processor implementation can access the HttpSession
as follows:
Code Block |
---|
|
public void process(Exchange exchange) throws Exception {
HttpSession session = exchange.getIn(HttpMessage.class).getRequest().getSession();
// ...
}
|
SSL Support (HTTPS)
Using the JSSE Configuration Utility
...
Spring DSL based configuration of endpoint
Code Block |
---|
|
...
<camel:sslContextParameters
id="sslContextParameters">
<camel:keyManagers
keyPassword="keyPassword">
<camel:keyStore
resource="/users/home/server/keystore.jks"
password="keystorePassword"/>
</camel:keyManagers>
</camel:sslContextParameters>...
...
<to uri="jetty:https://127.0.0.1/mail/?sslContextParametersRef=sslContextParameters"/>
...
|
Configuring Jetty Directly
Jetty provides SSL support out of the box. To enable Jetty to run in SSL mode, simply format the URI using the https://
prefix.
Example:
Code Block |
---|
|
<from uri="jetty:https://0.0.0.0/myapp/myservice/"/>
|
Jetty also needs to know where to load your keystore from and what passwords to use in order to load the correct SSL certificate. Set the following JVM System Properties:
Before Camel 2.3:
...
...
class | confluenceTableSmall |
---|
|
Property | Description |
---|
jetty.ssl.keystore
| Specifies the location of the Java keystore file, which contains the Jetty server's own X.509 certificate in a key entry. A key entry stores the X.509 certificate (effectively, the public key) and also its associated private key. | jetty.ssl.password
| The store password, which is required to access the keystore file (this is the same password that is supplied to the keystore command's -storepass option). | jetty.ssl.keypassword
| The key password, which is used to access the certificate's key entry in the keystore (this is the same password that is supplied to the keystore command's -keypass option). |
|
From Camel 2.3:
Div |
---|
class | confluenceTableSmall |
---|
|
Property | Description |
---|
org.eclipse.jetty.ssl.keystore
| Specifies the location of the Java keystore file, which contains the Jetty server's own X.509 certificate in a key entry. A key entry stores the X.509 certificate (effectively, the public key) and also its associated private key. | org.eclipse.jetty.ssl.password
| The store password, which is required to access the keystore file (this is the same password that is supplied to the keystore command's keystore option). | org.eclipse.jetty.ssl.keypassword
| The key password, which is used to access the certificate's key entry in the keystore (this is the same password that is supplied to the keystore command's keystore option). |
|
For details of how to configure SSL on a Jetty endpoint, read the following Jetty documentation. 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.
...
Up to Camel 2.2
Code Block |
---|
|
<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>
</bean>
</entry>
</map>
</property>
</bean>
|
Camel 2.3, 2.4
Code Block |
---|
|
<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 *
Code Block |
---|
|
<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.
...
From 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).
Code Block |
---|
|
<bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent">
<property name="sslSocketConnectorProperties">
<map>
<entry key="password"value="..."/>
<entry key="keyPassword"value="..."/>
<entry key="keystore"value="..."/>
<entry key="needClientAuth"value="..."/>
<entry key="truststore"value="..."/>
</map>
</property>
</bean>
|
How to Obtain A Reference to the X509Certificate
...
From 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).
Code Block |
---|
|
<bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent">
<property name="socketConnectorProperties">
<map>
<entry key="acceptors" value="4"/>
<entry key="maxIdleTime" value="300000"/>
</map>
</property>
</bean>
|
...
Code Block |
---|
|
<bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent">
<property name="socketConnectors">
<map>
<entry key="8080">
<bean class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<property name="forwarded" value="true"/>
</bean>
</entry>
</map>
</property>
</bean> |
This is particularly useful when an existing Apache server handles TLS connections for a domain and proxies them to application servers internally.
...
The following example shows how to customize the DefaultHttpBinding
in order to change how exceptions are returned:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBindingRefTest.java} |
We can then create an instance of our binding and register it in the Spring registry as follows:
Code Block |
---|
|
<bean id="mybinding"class="com.mycompany.MyHttpBinding"/>
|
And then we can reference this binding when we define the route:
Code Block |
---|
|
<route><from<route>
<from uri="jetty:http://0.0.0.0:8080/myapp/myservice?httpBindingRef=mybinding"/><to"/>
<to uri="bean:doSomething"/><>
</route>
|
Jetty Handlers and Security Configuration
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:
Code Block |
---|
|
<-- 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>
|
From Camel 2.3: you can configure a list of Jetty handlers as follows:
Code Block |
---|
|
<-- 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:
...
If you need more handlers, set the handlers
option equal to a comma-separated list of bean IDs.
How to
...
Customize the Response on an HTTP 500
...
Server Error
You may want to return a custom reply message when something goes wrong, instead of the default reply message Camel Jetty replies with. You could use a custom HttpBinding
to be in control of the message mapping, but often it may be easier to use Camel's Exception Clause to construct the custom reply message.
Example: return the message: Dude something went wrong
for the HTTP error code 500
:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyOnExceptionHandledTest.java} |
Multi-Part Form
...
Support
From Camel 2.3.0: camel-jetty
support to multi-part 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.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/multi-partFormTestMultiPartFormTest.java|title=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} |
...
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
.
Code Block |
---|
|
<from uri="jetty:https://0.0.0.0/myapp/myservice1/?enableJmx=true"/>
|
Code Block |
---|
|
<from uri="jetty:https://0.0.0.0/myapp/myservice2/?enableJmx=false"/>
|
...