0

Is it possible to register a javax.ws.rs.client.ClientRequestFilter server wide on JBoss EAP 7? I would like to intercept all outbound JAX-RS calls to dynamically add some context information in HTTP headers.

For JAX-WS calls I was able to do this with https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.0/html-single/developing_web_services_applications/#jax_ws_handler_chains. I can't find any documentation on a similar mechanism for JAX-RS.

Or alternatively, is there maybe another way to intercept outbound HTTP calls in general?

Jimmy Praet
  • 2,230
  • 1
  • 15
  • 14

2 Answers2

0

For a per server solution, according to Using HttpHandler class in Undertow "you need to package your handler(s) into a module, and configure custom-filter in undertow subsystem."

The module.xml example and undertow configuration has been given as well as filter source code!

Update There's an example of using the HTTPExchange here though I dont really care much for that site. SO also has this slightly related example - it does look like it can work similarly to the JAX-WS Handlers/Interceptor How to properly read post request body in a handler

Another good example file upload using httphandler I know they're different that dealing with JAX-RS but still may apply.

JGlass
  • 1,427
  • 2
  • 12
  • 26
  • Correct me if I'm wrong but I believe the Undertow HttpHandlers can only be used to intercept inbound requests sent TO server, and not outbound requests sent BY applications running on the server? – Jimmy Praet Jan 30 '18 at 20:32
  • No, I may be completely wrong ;-) but this sounds promising "The root handler is then executed via io.undertow.server.HttpHandlers#executeRootHandler . Handlers are chained together, and each handler can modify the exchange, send a response, or delegate to a different handler. At this point there are a few different things that can happen: The exchange can be finished." I was also thinking it was similar to the JAX-WS Handlers in that you can alter the request and/or response as its going through the chain – JGlass Jan 30 '18 at 20:44
  • And based on this [HTTPHandler](https://github.com/undertow-io/undertow/blob/master/core/src/main/java/io/undertow/server/HttpHandler.java) using [HttpServerExchange](https://repository.jboss.org/nexus/content/unzip/unzip/io/undertow/undertow-core/1.0.14.Final/undertow-core-1.0.14.Final-javadoc.jar-unzip/io/undertow/server/HttpServerExchange.html) it *looks* like you can access just about everything down to the output stream and headers – JGlass Jan 30 '18 at 20:46
  • I don't need to intercept the "server" exchange though. The undertow HTTPHandler allows for intercepting inbound requests and outbound responses. I need to intercept outbound request sent by the server. The RESTEasy JAX-RS client uses Apache HTTP Client, which uses the java.net.Socket API underneath. Undertow is not involved. – Jimmy Praet Jan 31 '18 at 19:37
  • Damn, oh well, I tried! If you find out the solution be sure to answer your question! Good Luck! – JGlass Jan 31 '18 at 21:08
0

I implemented it by creating a module with the following contents:

package be.fgov.kszbcss.tracer.jaxrs;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;

public class TracerResteasyClientBuilder extends ResteasyClientBuilder {
    @Override
    public ResteasyClient build() {
        return super.build().register(TracerJaxRsClientRequestFilter.class);
    }
}

/META-INF/services/javax.ws.rs.client.ClientBuilder

be.fgov.kszbcss.tracer.jaxrs.TracerResteasyClientBuilder

And registering it as a global module on JBoss EAP.

Jimmy Praet
  • 2,230
  • 1
  • 15
  • 14