Monday, September 12, 2016

Apache camel routing to return the Exact HTTP error code

Let's say we need an apache camel routing configuration which acts as a HTTP proxy. It does only hand over the message to an external system which is given to the ESB by another external system. No any message modification happens in the ESB.

System A >>>>> ESB >>>>> System B

Let's take an example.

System A sends a request to the ESB without required Authorization header. ESB just forewards that message to the System B. So, System B returns a 401(Unauthorized) status code to the ESB. According to the default behavior ESB returns HTTP 500 back to the System A which we don't like to expect from a proxy setup. We need exactly what System B has sent to ESB. To achieve that behavior from ESB, we can apply following routing configuration.

<route>
            <from
                uri="jetty:http://{{xxx.esb.host}}:{{xxx.esb.port}}/?matchOnUriPrefix=true" />
            <setHeader headerName="CamelHttpMethod">
                <simple>${headers.CamelHttpMethod}</simple>
            </setHeader>
            <setHeader headerName="RestEndpointURL">
                <simple>
                    http://{{xxx.rest.endpoint.host}}:{{cgx.rest.endpoint.port}}${headers.CamelHttpUri}?bridgeEndpoint=true
                </simple>
            </setHeader>
            <setHeader headerName="Exchange.HTTP_PATH">
                <constant></constant>
            </setHeader>
            <recipientList>
                <simple>${headers.RestEndpointURL}</simple>
            </recipientList>

            <onException>
                <exception>java.lang.Exception</exception>
                <handled>
                    <simple> true </simple>
                </handled>
                <setHeader headerName="Exchange.HTTP_RESPONSE_CODE">
                    <simple>${exception.statusCode}</simple>
                </setHeader>
            </onException>


        </route>