TERACytE
TERACytE

Reputation: 7863

How do I check the inbound header "content-type" response in mule?

I've got a flow that processes the response from a REST service:

    <http:outbound-endpoint method="GET"
                            address="http://www.SomeAddress.com"
                            exchange-pattern="request-response">
        ...
        <response>
            <json:json-to-object-transformer/>
            <custom-transformer ... />
        </response>
    </http:outbound-endpoint>

Recently the service has been updated to return html content. I'd like to be able to identify the content-type in the response:

    <http:outbound-endpoint method="GET"
                            address="http://www.SomeAddress.com"
                            exchange-pattern="request-response">
        ...
        <response>
            <choice>
                <when content-type is html>
                    <custom-transformer-for-html ... />
                </when>
                <otherwise>
                    <json:json-to-object-transformer/>
                    <custom-transformer ... />
                </otherwise>
            </choice>
        </response>
    </http:outbound-endpoint>

Even better, it would be nice to simply throw an exception if the content-type is unrecognized, similar to a traditional switch statement:

switch (content-type) {
    case (html) : ...
    case (json): ...
    default: throw exception
}

Can a choice statement check the inbound header content-type? Is the switch with default check possible? Are there any examples I can look at?

--- Update ---

I tried Davids suggestion below:

        <response>                
            <choice>
                <when expression="#[message.inboundProperties['Content-Type'].contains('text/html')]">
                    <logger message="when number one invoked" level="WARN"/>
                </when>
                <when expression="#[message.inboundProperties['Content-Type'].contains('application/json')]">
                   <logger message="when number two invoked" level="WARN"/>
                </when>
                <otherwise>
                    <logger message="otherwise invoked" level="WARN"/>
                </otherwise>
            </choice>
        </response>

But now I'm getting the following error when I compile:

    cvc-complex-type.2.4.a: Invalid content was found starting with element 'choice'. 
    One of '{"http://www.mulesoft.org/schema/mule/core":abstract-transformer,
    "http://www.mulesoft.org/schema/mule/core":abstract-filter,
    "http://www.mulesoft.org/schema/mule/core":abstract-security-filter, 
    "http://www.mulesoft.org/schema/mule/core":abstract-intercepting-message-processor, 
    "http://www.mulesoft.org/schema/mule/core":abstract-observer-message-processor,
    "http://www.mulesoft.org/schema/mule/core":processor, 
    "http://www.mulesoft.org/schema/mule/core":custom-processor}' is expected. 
    (org.xml.sax.SAXParseException)
      org.apache.xerces.util.ErrorHandlerWrapper:-1 (null)

Upvotes: 1

Views: 6277

Answers (1)

David Dossot
David Dossot

Reputation: 33413

  • Use a MEL expression in each <when> element to test the Content-Type header: #[message.inboundProperties['Content-Type'] == 'text/html']
  • Use the <otherwise> element to act as the default statement of a switch: throw your exception from there.

Upvotes: 6

Related Questions