Questo contenuto non è disponibile nella lingua selezionata.
Chapter 358. Undertow Component
Available as of Camel version 2.16
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 Camel version 2.21, the undertow component also supports WebSocket connections and can thus 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>
358.1. 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]
You can append query options to the URI in the following format, ?option=value&option=value&…
358.2. Options
The Undertow component supports 5 options which are listed below.
Name | Description | Default | Type |
---|---|---|---|
undertowHttpBinding (advanced) | To use a custom HttpBinding to control the mapping between Camel message and HttpClient. | UndertowHttpBinding | |
sslContextParameters (security) | To configure security using SSLContextParameters | SSLContextParameters | |
useGlobalSslContext Parameters (security) | Enable usage of global SSL context parameters. | false | boolean |
hostOptions (advanced) | To configure common options, such as thread pools | UndertowHostOptions | |
resolveProperty Placeholders (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean |
The Undertow endpoint is configured using URI syntax:
undertow:httpURI
with the following path and query parameters:
358.2.1. Path Parameters (1 parameters):
Name | Description | Default | Type |
---|---|---|---|
httpURI | Required The url of the HTTP endpoint to use. | URI |
358.2.2. Query Parameters (22 parameters):
Name | Description | Default | Type |
---|---|---|---|
bridgeErrorHandler (consumer) | 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 |
httpMethodRestrict (consumer) | Used to only allow consuming if the HttpMethod matches, such as GET/POST/PUT etc. Multiple methods can be specified separated by comma. | String | |
matchOnUriPrefix (consumer) | 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 |
optionsEnabled (consumer) | Specifies whether to enable HTTP OPTIONS for this Servlet consumer. By default OPTIONS is turned off. | false | boolean |
exceptionHandler (consumer) | 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 | |
exchangePattern (consumer) | Sets the exchange pattern when the consumer creates an exchange. | ExchangePattern | |
handlers (consumer) |
Specifies a comma-delimited set of Note 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 | |
cookieHandler (producer) | Configure a cookie handler to maintain a HTTP session | CookieHandler | |
keepAlive (producer) | Setting to ensure socket is not closed due to inactivity | true | Boolean |
options (producer) | 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 | |
reuseAddresses (producer) | Setting to facilitate socket multiplexing | true | Boolean |
tcpNoDelay (producer) | Setting to improve TCP protocol performance | true | Boolean |
throwExceptionOnFailure (producer) | 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 |
transferException (producer) | 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 |
headerFilterStrategy (advanced) | To use a custom HeaderFilterStrategy to filter header to and from Camel message. | HeaderFilterStrategy | |
synchronous (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean |
undertowHttpBinding (advanced) | To use a custom UndertowHttpBinding to control the mapping between Camel message and undertow. | UndertowHttpBinding | |
fireWebSocketChannelEvents (websocket) | 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 |
sendTimeout (websocket) | Timeout in milliseconds when sending to a websocket channel. The default timeout is 30000 (30 seconds). | 30000 | Integer |
sendToAll (websocket) | 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 | |
useStreaming (websocket) | if true, text and binary messages coming through a WebSocket 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 |
sslContextParameters (security) | To configure security using SSLContextParameters | SSLContextParameters |
358.3. Spring Boot Auto-Configuration
The component supports 10 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
camel.component.undertow.enabled | Enable undertow component | true | Boolean |
camel.component.undertow.host-options.buffer-size | The buffer size of the Undertow host. | Integer | |
camel.component.undertow.host-options.direct-buffers | Set if the Undertow host should use direct buffers. | Boolean | |
camel.component.undertow.host-options.http2-enabled | Set if the Undertow host should use http2 protocol. | Boolean | |
camel.component.undertow.host-options.io-threads | The number of io threads to use in a Undertow host. | Integer | |
camel.component.undertow.host-options.worker-threads | The number of worker threads to use in a Undertow host. | Integer | |
camel.component.undertow.resolve-property-placeholders | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | Boolean |
camel.component.undertow.ssl-context-parameters | To configure security using SSLContextParameters. The option is a org.apache.camel.util.jsse.SSLContextParameters type. | String | |
camel.component.undertow.undertow-http-binding | 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. | String | |
camel.component.undertow.use-global-ssl-context-parameters | Enable usage of global SSL context parameters. | false | Boolean |
358.4. 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 turn on or turn off the chuched 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.
358.5. 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>
358.6. 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>
358.7. 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>
358.8. HTTP/2 Example
In this sample, we configure the camel-undertow component to support the HTTP/2 protocol and expose a HTTP service at http://localhost:7766/foo/bar.
The directory structure of this sample is as follows:
├── pom.xml 1 ├── README.md └── src └── main └── resources └── META-INF └── spring └── camel-context.xml 2
The following files are important for configuring the camel-undertow component to support the HTTP/2 protocol:
- 1
- pom.xml: Includes the following properties and dependencies:
<properties> <camel.version>2.23.1</camel.version> </properties> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>${camel.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>${camel.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-undertow</artifactId> <version>${camel.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-http-common</artifactId> <version>${camel.version}</version> </dependency> <plugin> <groupId>org.apache.camel</groupId> <artifactId>camel-maven-plugin</artifactId> <version>${camel.version}</version> <configuration> <fileApplicationContextUri>src/main/resources/META-INF/spring/camel-context.xml</fileApplicationContextUri> </configuration> </plugin>
- 2
- camel-context.xml: Configures the camel-undertow component as follows:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <camelContext id="cbr-example-context" xmlns="http://camel.apache.org/schema/spring"> <route id="cbr-route" trace="true"> <from id="_from1" uri="undertow:http://localhost:7766/foo/bar"/> <setBody id="_setBody1"> <constant>Sending Response</constant> </setBody> <log id="_log5" message="Headers ${in.headers}"/> <log id="_log5" message="Done processing ${body}"/> </route> </camelContext> <bean class="org.apache.camel.component.undertow.UndertowComponent" id="undertow"> <property name="hostOptions" ref="undertowHostOptions"/> </bean> <bean class="org.apache.camel.component.undertow.UndertowHostOptions" id="undertowHostOptions"> <property name="http2Enabled" value="true"/> </bean> </beans>
The http2Enabled
property which is set to false
by default, is set to true
for UndertowHostOptions
class. This class is then referred to UndertowComponent
which is used in camel route.
In this example, by using the camel-maven-plugin
in the pom.xml
file, we can expose a HTTP service at http://localhost:7766/foo/bar
when we execute the Maven command mvn camel:run
.
We can test this example using the curl command as follows:
$ curl -v --http2 http://localhost:7766/foo/bar * Trying ::1... * TCP_NODELAY set * connect to ::1 port 7766 failed: Connection refused * Trying 127.0.0.1... * TCP_NODELAY set * Connected to localhost (127.0.0.1) port 7766 (#0) > GET /foo/bar HTTP/1.1 > Host: localhost:7766 > User-Agent: curl/7.53.1 > Accept: */* > Connection: Upgrade, HTTP2-Settings > Upgrade: h2c > HTTP2-Settings: AAMAAABkAARAAAAAAAIAAAAA > < HTTP/1.1 101 Switching Protocols < Connection: Upgrade < Upgrade: h2c < Date: Sun, 10 Dec 2017 08:43:58 GMT * Received 101 * Using HTTP2, server supports multi-use * Connection state changed (HTTP/2 confirmed) * Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0 * Connection state changed (MAX_CONCURRENT_STREAMS updated)! < HTTP/2 200 < accept: */* < http2-settings: AAMAAABkAARAAAAAAAIAAAAA < breadcrumbid: ID-dhcppc1-1512886066149-0-25 < content-length: 16 < user-agent: curl/7.53.1 < date: Sun, 10 Dec 2017 08:43:58 GMT < * Connection #0 to host localhost left intact Sending Response
358.9. 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 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.
358.10. Undertow consumers on WildFly
The configuration of camel-undertow consumers on WildFly is different to that of standalone Camel. Producer endpoints work as per normal.
On WildFly, camel-undertow consumers leverage the default Undertow HTTP server provided by the container. The server is defined within the undertow subsystem configuration. Here’s an excerpt of the default configuration from standalone.xml:
<subsystem xmlns="urn:jboss:domain:undertow:4.0"> <buffer-cache name="default" /> <server name="default-server"> <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true" /> <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true" /> <host name="default-host" alias="localhost"> <location name="/" handler="welcome-content" /> <filter-ref name="server-header" /> <filter-ref name="x-powered-by-header" /> <http-invoker security-realm="ApplicationRealm" /> </host> </server> </subsystem>
In this instance, Undertow is configured to listen on interfaces / ports specified by the http and https socket-binding. By default this is port 8080 for http and 8443 for https.
This has the following implications:
- camel-undertow consumers will only bind to localhost:8080 or localhost:8443
- Some endpoint consumer configuration options have no effect (see below), since these settings are managed by the WildFly container
For example, if you configure an endpoint consumer using different host or port combinations, a warning will appear within the server log file. For example the following host & port configurations would be ignored:
from("undertow:http://somehost:1234/path/to/resource")
[org.wildfly.extension.camel] (pool-2-thread-1) Ignoring configured host: http://somehost:1234/path/to/resource
However, the consumer is still available on the default host & port localhost:8080 or localhost:8443.
358.10.1. Configuring alternative ports
If alternative ports are to be accepted, then these must be configured via the WildFly subsystem configuration. This is explained in the server documentation:
358.10.2. Ignored camel-undertow consumer configuration options on WildFly
hostOptions
Refer to the WildFly undertow configuration guide for how to configure server host options:
sslContextParameters
To configure SSL, refer to the WildFly SSL configuration guide: