We run a couple of Jetty servers through Camel and are curious if Camel 
provides any built-in facilities to log the raw HTTP request and also the 
response. We’d like an output similar to the one of an Apache HTTP Client (log 
messages for org.apache.http.wire) where an output may look like this:

2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "POST /api/v1/someEndpoint 
HTTP/1.1[\r][\n]" [o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "Content-Type: 
application/json[\r][\n]" [o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "Content-Length: 
1030[\r][\n]" [o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "Host: 
localhost:8393[\r][\n]" [o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "Connection: 
Keep-Alive[\r][\n]" [o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "User-Agent: 
Apache-HttpClient/4.4 (Java 1.5 minimum; Java/1.8.0_66)[\r][\n]" [o.a.h.wire] 
[Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "Accept-Encoding: 
gzip,deflate[\r][\n]" [o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "Authorization: ...[\r][\n]" 
[o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "[\r][\n]" [o.a.h.wire] 
[Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 >> "{"message“:“..."}" 
[o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 << "HTTP/1.1 200 OK[\r][\n]" 
[o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 << "Content-Type: 
application/json; charset=utf-8[\r][\n]" [o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 << "Transfer-Encoding: 
chunked[\r][\n]" [o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 << "[\r][\n]" [o.a.h.wire] 
[Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 << "34[\r][\n]" [o.a.h.wire] 
[Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 << "{"messageId“:“..."}" 
[o.a.h.wire] [Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 << "[\r][\n]" [o.a.h.wire] 
[Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 << "0[\r][\n]" [o.a.h.wire] 
[Console] 
2016-04-14 14:57:27 - [DEBUG] - http-outgoing-1 << "[\r][\n]" [o.a.h.wire] 
[Console] 

We use SLF4J/Logback for logging and stumbled over the logback-access library 
(http://logback.qos.ch/access.html#teeFilter) which seem to provide the 
functionality we are looking for already though I am not really able to get the 
TeeFilter (ServletFilter) for a JettyComponent(9) to work. We do not have any 
web.xml configs or the like as we use Camels Rest DSL over Jetty. 

Our configuration does look like this:

Spring Config:
------------------

@Configuration
public class ServicesSpringConfig extends CamelConfiguration {

  @Override
  protected void setupCamelContext(CamelContext camelContext) throws Exception {
    ...
    camelContext.addComponent("jetty", jettyHttpComponent());
  }

  @private JettyHttpComponent jettyHttpComponent() throws URISyntaxException {
    JettyHttpComponent jetty = new JettyHttpComponent9();
    jetty.setSslContextParameters(sslContextParameters());
    jetty.setErrorHandler(new SuppressJettyInfoErrorHandler());
    return jetty;
  }

  @Bean(name = "jettyRawRequestResponseLogger")
  public TeeFilter jettyRawRequestResponseLogger() {
    return new TeeFilter();
  }
}

Rest DSL:
-------------

restConfiguration()
    .component("jetty")
    .scheme("https")
    .host("0.0.0.0")
    .port("{{rest.port}}")
    .contextPath("/api/v1")
    .endpointProperty("matchOnUriPrefix", "true")
    .endpointProperty("sendServerVersion", "false")
    // we do want to make use of the streaming API which sends out the response 
in small chunks
    // instead of all at once. This, however, will replace the Content-Length 
header field with
    // a Transfer-Encoding: chunked header field. A chunk length of 0 indicates 
the end of the
    // stream
    .endpointProperty("chunked", "true")
    .endpointProperty("headerFilterStrategy", „#customHeaderFilter")
    .endpointProperty("filterRef", "#jettyRawRequestResponseLogger“);

How do I have to add the TeeFilter servlet filter to the Jetty component in 
order to log in- and outgoing messages as filterRef does not work in my case? 

Thanks for any help you can provide


Reply via email to