Undertow
Since Camel 2.16
Both producer and consumer are supported
The Undertow component provides HTTP and WebSocket based endpoints for consuming and producing HTTP/WebSocket requests.
That is, the Undertow component behaves as a simple Web server. Undertow can also be used as a http client which mean you can also use it with Camel as a producer.
Since the component also supports WebSocket connections, it can serve as a drop-in replacement for Camel websocket component or atmosphere-websocket component.
Maven users will need to add the following dependency to their pom.xml
for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-undertow</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
URI format
undertow:http://hostname[:port][/resourceUri][?options] undertow:https://hostname[:port][/resourceUri][?options] undertow:ws://hostname[:port][/resourceUri][?options] undertow:wss://hostname[:port][/resourceUri][?options]
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
The component level is the highest level which holds general and common configurations that are inherited by the endpoints. For example a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
Configuring components can be done with the Component DSL, in a configuration file (application.properties|yaml), or directly with Java code.
Configuring Endpoint Options
Where you find yourself configuring the most is on endpoints, as endpoints often have many options, which allows you to configure what you need the endpoint to do. The options are also categorized into whether the endpoint is used as consumer (from) or as a producer (to), or used for both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders, which allows to not hardcode urls, port numbers, sensitive information, and other settings. In other words placeholders allows to externalize the configuration from your code, and gives more flexibility and reuse.
The following two sections lists all the options, firstly for the component followed by the endpoint.
Component Options
The Undertow component supports 11 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
|
If enabled and an Exchange failed processing on the consumer side the response’s body won’t contain the exception’s stack trace. |
false |
boolean |
|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
|
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
boolean |
|
To configure common options, such as thread pools. |
UndertowHostOptions |
||
To use a custom HttpBinding to control the mapping between Camel message and HttpClient. |
UndertowHttpBinding |
||
Configuration used by UndertowSecurityProvider. Comma separated list of allowed roles. |
String |
||
Configuration used by UndertowSecurityProvider. Security configuration object for use from UndertowSecurityProvider. Configuration is UndertowSecurityProvider specific. Each provider decides, whether it accepts configuration. |
Object |
||
Security provider allows plug in the provider, which will be used to secure requests. SPI approach could be used too (component then finds security provider using SPI). |
UndertowSecurityProvider |
||
To configure security using SSLContextParameters. |
SSLContextParameters |
||
Enable usage of global SSL context parameters. |
false |
boolean |
Endpoint Options
The Undertow endpoint is configured using URI syntax:
undertow:httpURI
with the following path and query parameters:
Query Parameters (29 parameters)
Name | Description | Default | Type |
---|---|---|---|
For HTTP endpoint: if true, text and binary messages will be wrapped as java.io.InputStream before they are passed to an Exchange; otherwise they will be passed as byte. For WebSocket endpoint: if true, text and binary messages will be wrapped as java.io.Reader and java.io.InputStream respectively before they are passed to an Exchange; otherwise they will be passed as String and byte respectively. |
false |
boolean |
|
Whether or not the consumer should write access log. |
false |
Boolean |
|
Used to only allow consuming if the HttpMethod matches, such as GET/POST/PUT etc. Multiple methods can be specified separated by comma. |
String |
||
Whether or not the consumer should try to find a target consumer by matching the URI prefix if no exact match is found. |
false |
Boolean |
|
If enabled and an Exchange failed processing on the consumer side the response’s body won’t contain the exception’s stack trace. |
false |
Boolean |
|
Specifies whether to enable HTTP OPTIONS for this Servlet consumer. By default OPTIONS is turned off. |
false |
boolean |
|
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 application/x-java-serialized-object content type. On the producer side the exception will be deserialized and thrown as is instead of the HttpOperationFailedException. The caused exception is required to be serialized. This is by default turned off. If you enable this then be aware that Java will deserialize the incoming data from the request to Java and that can be a potential security risk. |
false |
Boolean |
|
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
|
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
ExceptionHandler |
||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
|
ExchangePattern |
||
Specifies a comma-delimited set of io.undertow.server.HttpHandler instances to lookup in your Registry. These handlers are added to the Undertow handler chain (for example, to add security). Important: You can not use different handlers with different Undertow endpoints using the same port number. The handlers is associated to the port number. If you need different handlers, then use different port numbers. |
String |
||
Configure a cookie handler to maintain a HTTP session. |
CookieHandler |
||
Setting to ensure socket is not closed due to inactivity. |
true |
Boolean |
|
Sets additional channel options. The options that can be used are defined in org.xnio.Options. To configure from endpoint uri, then prefix each option with option., such as option.close-abort=true&option.send-buffer=8192. |
Map |
||
If the option is true, UndertowProducer will set the Host header to the value contained in the current exchange Host header, useful in reverse proxy applications where you want the Host header received by the downstream server to reflect the URL called by the upstream client, this allows applications which use the Host header to generate accurate URL’s for a proxied service. |
true |
boolean |
|
Setting to facilitate socket multiplexing. |
true |
Boolean |
|
Setting to improve TCP protocol performance. |
true |
Boolean |
|
Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code. |
true |
Boolean |
|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
|
Which Undertow AccessLogReceiver should be used Will use JBossLoggingAccessLogReceiver if not specified. |
AccessLogReceiver |
||
To use a custom HeaderFilterStrategy to filter header to and from Camel message. |
HeaderFilterStrategy |
||
To use a custom UndertowHttpBinding to control the mapping between Camel message and undertow. |
UndertowHttpBinding |
||
Configuration used by UndertowSecurityProvider. Comma separated list of allowed roles. |
String |
||
OConfiguration used by UndertowSecurityProvider. Security configuration object for use from UndertowSecurityProvider. Configuration is UndertowSecurityProvider specific. Each provider decides whether accepts configuration. |
Object |
||
Security provider allows plug in the provider, which will be used to secure requests. SPI approach could be used too (endpoint then finds security provider using SPI). |
UndertowSecurityProvider |
||
To configure security using SSLContextParameters. |
SSLContextParameters |
||
if true, the consumer will post notifications to the route when a new WebSocket peer connects, disconnects, etc. See UndertowConstants.EVENT_TYPE and EventType. |
false |
boolean |
|
Timeout in milliseconds when sending to a websocket channel. The default timeout is 30000 (30 seconds). |
30000 |
Integer |
|
To send to all websocket subscribers. Can be used to configure on endpoint level, instead of having to use the UndertowConstants.SEND_TO_ALL header on the message. |
Boolean |
Message Headers
The Undertow component supports 15 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
websocket.connectionKey (common) Constant: |
An identifier of WebSocketChannel through which the message was received or should be sent. |
String |
|
websocket.connectionKey.list (producer) Constant: |
The list of websocket connection keys. |
List |
|
Constant: |
To send to all websocket subscribers. Can be used to configure on endpoint level, instead of having to use the UndertowConstants.SEND_TO_ALL header on the message. |
Boolean |
|
websocket.eventType (consumer) Constant: |
The numeric identifier of the type of websocket event. |
Integer |
|
websocket.eventTypeEnum (consumer) Constant: |
The type of websocket event. Enum values:
|
EventType |
|
Constant: |
The WebSocketChannel through which the message was received. |
WebSocketChannel |
|
Constant: |
The exchange for the websocket transport, only available for ON_OPEN events. |
WebSocketHttpExchange |
|
CamelHttpResponseCode (common) Constant: |
The http response code. |
Integer |
|
Constant: |
The content type. |
String |
|
CamelHttpCharacterEncoding (consumer) Constant: |
The http character encoding. |
String |
|
Constant: |
The http path. |
String |
|
Constant: |
The http query. |
String |
|
Constant: |
The http URI. |
String |
|
Constant: |
The http method. |
String |
|
Constant: |
The host http header. |
String |
Message Headers
Camel uses the same message headers as the HTTP
component. It also uses Exchange.HTTP_CHUNKED,CamelHttpChunked
header to turn on or turn off
the chunked encoding on the camel-undertow 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.
HTTP Producer Example
The following is a basic example of how to send an HTTP request to an existing HTTP endpoint.
in Java DSL
from("direct:start")
.to("undertow:http://www.google.com");
or in XML
<route>
<from uri="direct:start"/>
<to uri="undertow:http://www.google.com"/>
</route>
HTTP Consumer Example
In this sample we define a route that exposes a HTTP service at
http://localhost:8080/myapp/myservice
:
<route>
<from uri="undertow:http://localhost:8080/myapp/myservice"/>
<to uri="bean:myBean"/>
</route>
WebSocket Example
In this sample we define a route that exposes a WebSocket service at
http://localhost:8080/myapp/mysocket
and returns back a response to the same channel:
<route>
<from uri="undertow:ws://localhost:8080/myapp/mysocket"/>
<transform><simple>Echo ${body}</simple></transform>
<to uri="undertow:ws://localhost:8080/myapp/mysocket"/>
</route>
Using localhost as host
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 an Undertow endpoint on a specific network interface,
the numerical IP address of this interface should be used as the host.
If you need to expose an Undertow 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.
Security provider
To plugin security provider for endpoint authentication, implement SPI interface
org.apache.camel.component.undertow.spi.UndertowSecurityProvider
.
Undertow components locates all implementations of UndertowSecurityProvider
using
Java SPI (Service Provider Interfaces). If there is an object passed to component
as parameter securityConfiguration
and provider accepts it. Provider will be used
for authentication of all requests.
Property requireServletContext
of security providers forces udertow server to start
with servlet context. There will be no servlet actually handled. This feature is meant only
for use with servlet filters, which needs servlet context for their functionality.
Spring Boot Auto-Configuration
When using undertow with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-undertow-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 11 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Configuration used by UndertowSecurityProvider. Comma separated list of allowed roles. |
String |
||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
Boolean |
|
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
Boolean |
|
Whether to enable auto configuration of the undertow component. This is enabled by default. |
Boolean |
||
To configure common options, such as thread pools. The option is a org.apache.camel.component.undertow.UndertowHostOptions type. |
UndertowHostOptions |
||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
Boolean |
|
If enabled and an Exchange failed processing on the consumer side the response’s body won’t contain the exception’s stack trace. |
false |
Boolean |
|
Security provider allows plug in the provider, which will be used to secure requests. SPI approach could be used too (component then finds security provider using SPI). The option is a org.apache.camel.component.undertow.spi.UndertowSecurityProvider type. |
UndertowSecurityProvider |
||
To configure security using SSLContextParameters. The option is a org.apache.camel.support.jsse.SSLContextParameters type. |
SSLContextParameters |
||
To use a custom HttpBinding to control the mapping between Camel message and HttpClient. The option is a org.apache.camel.component.undertow.UndertowHttpBinding type. |
UndertowHttpBinding |
||
Enable usage of global SSL context parameters. |
false |
Boolean |