Jetty Component
The producer is deprecated - do not use. We only recommend using jetty as consumer (eg from jetty)
The jetty
component provides HTTP-based endpoints for consuming and producing HTTP requests. That is, the Jetty component behaves as a simple Web server. Jetty can also be used as an HTTP client which mean you can also use it with Camel as a producer.
The assert
call appears in this example, because the code is part of an unit test. Jetty is stream based, which means the input it receives is submitted to Camel as a stream. That means you will only be able to read the content of the stream once.
If you find a situation where the message body appears to be empty or you need to access the Exchange.HTTP_RESPONSE_CODE
data multiple times, e.g., doing multi-casting, or re-delivery error handling, you should use Stream caching or convert the message body to a String
which is safe to be re-read multiple times.
Maven users should add the following dependency to their pom.xml
to use this component:
URI Format
Query options should be appended to the URI using the following format: ?option=value&option=value&...
Options
Option | Default Value | Description |
---|---|---|
|
| Camel 2.1: If the option is Camel 2.3: If the option is true, Consider setting |
|
| Camel 2.2: If this option is |
|
| Camel 2.6: Allows to set a timeout in milliseconds when using Jetty as consumer (server). By default Jetty uses This option is only in use when using Jetty with the Asynchronous Routing Engine. |
|
| Camel 2.19: Producer only Configure a cookie handler to maintain a HTTP session. |
|
| Camel 2.3: Determines whether or not the raw input stream from Jetty is cached or not (Camel will read the stream into a in memory/overflow to file, Stream caching) cache. By default Camel will cache the Jetty input stream to support reading it multiple times to ensure it Camel can retrieve all data from the stream. However, you can set this option to
|
|
| Camel 2.15: if the option is |
|
| Camel 2.3: If this option is |
|
| Camel 2.5: Whether Jetty Set this option to |
|
| Camel 2.17: Configuration for the For example, setting |
|
| Camel 2.9: Allows using a custom filters which is putted into a list and can be find in the Registry |
|
| Specifies a comma-delimited set of Note: you can not use different handlers with different Jetty endpoints using the same port number. The handlers is associated to the port number. If you need different handlers, then use different port numbers. |
|
| Camel 2.11: Reference to a instance of |
|
| Reference to an |
|
| Configuration of Jetty's HttpClient. For example, setting |
|
| To use a shared |
|
| Camel 2.11: Producer only: To set a value for minimum number of threads in |
|
| Camel 2.11: Producer only: To set a value for maximum number of threads in |
|
| Camel 2.11: Consumer only: Used to only allow consuming if the |
|
| Camel 2.6.0+: Reference to an |
|
| Whether or not the See here How do I let Jetty match wildcards. |
|
| Camel 2.6: Allows using a custom multi-part filter. Note: setting |
|
| Camel 2.16: Producer only The status codes which is considered a success response. The values are inclusive. The range must be defined as from-to with the dash included. |
|
| Camel 2.17: Specifies whether to enable |
|
| Camel 2.11: Producer only The HTTP proxy Host URL which will be used by Jetty client. |
|
| Camel 2.11: Producer only The HTTP proxy port which will be used by Jetty client. |
|
| Camel 2.12: To use a custom buffer size on the |
|
| Camel 2.14: if the option is true, jetty server will send the date header to the client which sends the request. Note: ensure that there are no any other |
|
| Camel 2.13: if the option is true, jetty will send the server header with the jetty version information to the client which sends the request. Note: ensure that there are no any other |
|
| Specifies whether to enable the session manager on the server side of Jetty. |
|
| Camel 2.17: Reference to a |
|
| Camel 2.8: Deprecated Reference to a |
|
| Option to disable throwing the |
|
| Specifies whether to enable |
|
| Camel 2.6: If enabled and an Exchange failed processing on the consumer side, and if the caused Exception was send back serialized in the response as a On the producer side the exception will be deserialized and thrown as is, instead of the |
|
| Camel 2.11: Producer only Refers to a custom See more details at UrlRewrite and How to use Camel as a HTTP proxy between a client and server. |
|
| Camel 2.6: Whether or not to use Jetty continuations for the Jetty Server. |
Message Headers
Camel uses the same message headers as the HTTP component. From Camel 2.2, it also uses (Exchange.HTTP_CHUNKED
, CamelHttpChunked
) header to toggle chunked encoding on the camel-jetty
consumer. Camel also populates all request.parameter
and request.headers
. For example, given a client request with the URL, http://myserver/myserver?orderid=123
, the exchange will contain a header named orderid
with the value 123
.
From Camel 2.2.0: you can get the request.parameter from the message header not only from GET
HTTP Method, but also other HTTP method.
Usage
The Jetty component supports both consumer and producer endpoints. Another option for producing to other HTTP endpoints, is to use the HTTP Component
Component Options
The JettyHttpComponent
provides the following options:
Option | Default Value | Description |
---|---|---|
|
| Camel 2.16.1/2.15.5: Whether to allow java serialization when a request uses When |
|
| Camel 2.3: If this option is true, Jetty JMX support will be enabled for this endpoint. See Jetty JMX support for more details. |
|
| Camel 2.15: This option is used to set the |
|
| Deprecated: Producer only: To use a custom Note: from Camel 2.11 this option has been removed. Set the option on the endpoint instead. |
|
| Producer only: To set a value for maximum number of threads in |
|
| Producer only: To set a value for minimum number of threads in |
|
| Deprecated: Producer only: To use a custom thread pool for the client. Note: this option has been removed from Camel 2.11. |
|
| Camel 2.5 Consumer only: To set a value for maximum number of threads in server thread pool. Notice that both a min and max size must be configured. |
|
| Camel 2.5 Consumer only: To set a value for minimum number of threads in server thread pool. Notice that both a min and max size must be configured. |
|
| Camel 2.12.2/2.11.3 To use an HTTP proxy. |
|
| Camel 2.12.2/2.11.3: To use an HTTP proxy. |
|
| Camel 2.5 Consumer only: A map which contains per port number specific HTTP connectors. Uses the same principle as |
|
| Camel 2.5 Consumer only. A map which contains general HTTP connector properties. Uses the same principle as |
|
| Camel 2.8: To configure a custom SSL/TLS configuration options at the component level. See Using the JSSE Configuration Utility for more details. |
|
| Consumer only: The password for the keystore when using SSL. |
|
| Consumer only: The path to the keystore. |
|
| Consumer only: The password when using SSL. |
|
| Camel 2.3 Consumer only: A map which contains per port number specific SSL connectors. See section SSL support for more details. |
|
| Camel 2.5 Consumer only. A map which contains general SSL connector properties. See section SSL support for more details. |
|
| Camel 2.11.2: Allows to configure a custom value of the request buffer size on the Jetty connectors. |
|
| Camel 2.11.2: Allows to configure a custom value of the request header size on the Jetty connectors. |
|
| Camel 2.11.2: Allows to configure a custom value of the response buffer size on the Jetty connectors. |
|
| Camel 2.11.2: Allows to configure a custom value of the response header size on the Jetty connectors. |
|
| Camel 2.5 Consumer only: To use a custom thread pool for the server. This option should only be used in special circumstances. |
Producer Example
The following is a basic example of how to send an HTTP request to an existing HTTP endpoint.
Java DSL:
XML DSL:
Consumer Example
In this sample we define a route that exposes a HTTP service at http://localhost:8080/myapp/myservice
:
When you specify localhost
in a URL, Camel exposes the endpoint only on the local TCP/IP network interface, so it cannot be accessed from outside the machine it operates on.
If you need to expose a Jetty endpoint on a specific network interface, the numerical IP address of this interface should be used as the host. If you need to expose a Jetty endpoint on all network interfaces, the 0.0.0.0
address should be used.
To listen across an entire URI prefix, see How do I let Jetty match wildcards.
If you actually want to expose routes by HTTP and already have a Servlet, you should instead refer to the Servlet Transport.
Our business logic is implemented in the MyBookService
class, which accesses the HTTP request contents and then returns a response.
Note: The assert
call appears in this example, because the code is part of an unit test.one
, to the endpoint, mock:one
, and all others to mock:other
.http://serverUri?one=hello
, the Jetty component will copy the HTTP request parameter, one
to the exchange's in.header
. We can then use the simple
language to route exchanges that contain this header to a specific endpoint and all others to another. If we used a language more powerful than Simple, e.g., EL or OGNL, then we can also test for the parameter value and route based on the header value as well.
Session Support
The session support option, sessionSupport
, can be used to enable a HttpSession
object and access the session object while processing the exchange.
For example, the following route enables sessions:
The myCode
Processor can be instantiated by a Spring bean
element:
Where the processor implementation can access the HttpSession
as follows:
SSL Support (HTTPS)
Using the JSSE Configuration Utility
From Camel 2.8: the camel-jetty
component supports SSL/TLS configuration through the Camel JSSE Configuration Utility. This utility greatly decreases the amount of component specific code you need to write and is configurable at the endpoint and component levels. The following examples demonstrate how to use the utility with the Jetty component.
Programmatic configuration of the component
Spring DSL based configuration of endpoint
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:
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:
Property | Description |
---|---|
| Specifies the location of the Java |
| The store password, which is required to access the |
| The key password, which is used to access the certificate's key entry in the |
From Camel 2.3:
Property | Description |
---|---|
| Specifies the location of the Java |
| The store password, which is required to access the |
| The key password, which is used to access the certificate's key entry in the |
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.
There's a slight difference between the various Camel versions:
Up to Camel 2.2
Camel 2.3, 2.4
From Camel 2.5: we switch to use SslSelectChannelConnector *
The value you use as keys in the above map is the port you configure Jetty to listen on.
Configuring General SSL Properties
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).
How to Obtain A Reference to the X509Certificate
Jetty stores a reference to the certificate in the HttpServletRequest
which you can access from code as follows:
Configuring General HTTP Properties
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).
How to Get the Value of The X-Forwarded-For
HTTP Header Using HttpServletRequest.getRemoteAddr()
If the HTTP requests are handled by an Apache server and forwarded to Jetty with mod_proxy
, the original client IP address is in the X-Forwarded-For
header and the HttpServletRequest.getRemoteAddr()
will return the address of the Apache proxy.
Jetty has a forwarded property which takes the value from X-Forwarded-For
and places it in the HttpServletRequest remoteAddr
property. This property is not available directly through the endpoint configuration but it can be easily added using the socketConnectors
property:
This is particularly useful when an existing Apache server handles TLS connections for a domain and proxies them to application servers internally.
Default Behavior for Returning HTTP Status Codes
The 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. If the exchange failed with an exception, the 500
HTTP status code is returned, and the stacktrace is returned in the body. If you want to specify which HTTP status code to return, set the code in the Exchange.HTTP_RESPONSE_CODE
header of the OUT
message.
Customizing HttpBinding
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:
And then we can reference this binding when we define the 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:
From Camel 2.3: you can configure a list of Jetty handlers as follows:
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
:
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.
Jetty JMX Support
From 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: 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.
As 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.
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
.
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 MBeanContainer
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.