Table of Contents |
---|
Asynchronous Client HTTP Transport
...
Code Block | ||
---|---|---|
| ||
Bus bus = BusFactory.getDefaultBus(); // insist on the async connector to use PATCH bus.setProperty(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE); // allows the async connector to use HTTP/2 protocol (if supported by the server) bus.setProperty(AsyncHTTPConduit.ENABLE_HTTP2, enableHttp2); |
Setting
...
Credentials
The "normal" CXF/JAX-WS method of setting user credentials via the BindingProvider.USERNAME_PROPERTY/PASSWORD_PROPERTY will work with the Async transport as well. However, the HttpAsyncClient library does have some additional capabilities around NTLM that can be leveraged. In order to use that, you need to:
- Turn on the AutoRedirect and turn off the Chunking for the Conduit. This will allow CXF to cache the response in a manner that will allow the transport to keep resending the request during the authentication negotiation.
Force the use of the Async transport even for synchronous calls
Code Block java java bp.getRequestContext().put("use.async.http.conduit", Boolean.TRUE); bp.getRequestContext().put("org.apache.cxf.transports.http2.enabled", Boolean.TRUE); // optionally, enable HTTP/2
or using AsyncHTTPConduit.USE_ASYNC constant
Code Block java java bp.getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE); bp.getRequestContext().put(AsyncHTTPConduit.ENABLE_HTTP2, Boolean.TRUE); // optionally, enable HTTP/2
Set the property "org.apache.http.auth.Credentials" to an instance of the Credentials. For example:
Code Block java java Credentials creds = new NTCredentials("username", "pswd", null, "domain"); bp.getRequestContext().put(Credentials.class.getName(), creds);
Instrumenting Response Processing
In certain circumstances, it is beneficial to wrap (or instrument) the async client transport response processing. Starting from Apache CXF 4.0.4 / 3.6.3 / 3.5.8 releases, it is now possible using AsyncHttpResponseWrapperFactory bus extension, for example:
Code Block | ||||
---|---|---|---|---|
| ||||
final AsyncHttpResponseWrapper wrapper = new AsyncHttpResponseWrapper() { @Override public void responseReceived(HttpResponse response, Consumer<HttpResponse> delegate) { delegate.accept(response); } }; bus.setExtension(() -> wrapper, AsyncHttpResponseWrapperFactory.class); ... |
Warning |
---|
It is very important for the AsyncHttpResponseWrapper to pass the call to |
Netty 4.x
Apache CXF also offers an HTTP client transport that is based on Netty 4.x. Its Maven artifactId is cxf-rt-transports-http-netty-client.
...