You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Andreas Pokrzywinski <an...@cubic.com> on 2014/10/27 09:59:42 UTC

Log in- and outbound camel http messages

Hi,

Actually I would like to do a simple thing. Log all incoming and outgoing
HTTP Message to a database. However, even camel seems to be the right
choice, it is not so simple to do it.

I read the documentation, but could not find any simple solution for this.
So far my solution is the following:

Camel routes:

    <onException>
        <exception>java.lang.Exception</exception>
        <to uri="direct:saveResponse" />
    </onException>

    <route customId="true" id="test" streamCache="true"  >
        <from uri="servlet:///test?matchOnUriPrefix=true" />
        <wireTap uri="direct:saveRequest"  copy="true"/>
        <to uri=&quot;http://test.de:8080/test&quot;
        &lt;camel:wireTap uri=&quot;direct:saveResponse&quot;
copy=&quot;true&quot; />
        <removeHeaders pattern="*" excludePattern="Content-*"/>
    </route>

    <route customId="true" id="saveRequest">
        <description>Saves the request message.</description>
        <from uri="direct:saveRequest" />
        <setHeader headerName="messageType">
            <constant>IN</constant>
        </setHeader>
        <to uri="direct:saveMessage" />
    </route>

    <route customId="true" id="saveResponse">
        <description>Saves the response message.</description>
        <from uri="direct:saveResponse" />
        <setHeader headerName="messageType">
            <constant>OUT</constant>
        </setHeader>
        <to uri="direct:saveMessage" />
    </route>

    <route customId="true" id="saveMessage">
        <description>Saves the request as API message in the
DB.</description>
        <from uri="direct:saveMessage" />
        <log message="Header request UID: ${header[request_UID]}" />
        <process ref="messageToApiRequestProcessor" />
        <to uri="jpa:com.cubic.cumo.mip.model.ApiMessage" />
    </route>


The route should log all incoming http requests to the test servlet and
after receiving the answer from http://test.de:8080/test save the response.
Of course the response should also be stored on exception. And of course the
camel header must be removed, cause this proxy setup shouldn't polute
response.

The MessageToApiMessage Processor converts the exchange message to the
custom ApiMessage. The appropriate code part:

private ApiMessage createApiRequest(Message message) {
    ApiRequest request = new ApiRequest();

    request.setContent(message.getBody(String.class));
    request.setTimestamp(new Date());

    request.setMethodName(message.getHeader("CamelHttpMethod",
String.class));

   
request.setHttpHeaders(toString(message.getHeader(Exchange.HTTP_SERVLET_REQUEST,
HttpServletRequest.class)));

    request.setUrl(message.getHeader(Exchange.HTTP_URL, String.class));
    String urlQuery = message.getHeader(Exchange.HTTP_QUERY, String.class);
    request.setUrlQuery(urlQuery);
    return request;
}
This works fine. For the response part the code looks like this:

private ApiMessage createApiResponse(Message message) {
    ApiResponse response = new ApiResponse();

    response.setContent(message.getBody(String.class));
    response.setTimestamp(new Date());
   
response.setHttpResponseCode(message.getHeader(Exchange.HTTP_RESPONSE_CODE,
Integer.class));
    response.setHttpHeaders(toString(message.getHeaders()));
    return response;
}
However, the header Exchange.HTTP_SERVLET_REQUEST does not contain the
response header.

How do I get the HTTP response header?

When does camel transform the message header to the http header?

Is there a more simple approach to log http requests and response?

Different question: How can I get the uri from last endpoint. The
ApiResponse should also contain lastEndpoint column and store f.e. the value
"http://test.de:8080". How to get this?

Thanks and regards

Andreas

Sorry, I don't know, how to embed code into this post.

This message is a copy of:
https://stackoverflow.com/questions/26572435/log-in-and-outbound-camel-http-messages



--
View this message in context: http://camel.465427.n5.nabble.com/Log-in-and-outbound-camel-http-messages-tp5758068.html
Sent from the Camel - Users mailing list archive at Nabble.com.