Ashok.N
Ashok.N

Reputation: 1391

How to handle invalid xml content in xpath-header-enricher

I have the following piece of spring integration code inside a chain component:

<int:chain input-channel="PQIssueDetails-PQMemberSearchMemAltID-Request" output-channel="PQIssueDetails-PQMemberSearchRequest-CheckLegacySystem">
        <!-- Transformer to transform the resultXml to the user understandable form using XSLT -->
        <int-xml:xslt-transformer xsl-resource="${stylesheet.PQIssueDetailsPQMemberSearchMemAltIDRequest}"/>
        <!-- Store the original payload in header for future purpose -->
        <int:header-enricher default-overwrite="true" should-skip-nulls="true">
            <int:header name="${headerNames.originalPayload}" expression="payload"/>
        </int:header-enricher>
        <!-- Store the original payload in header for future purpose -->
        <int-xml:xpath-header-enricher default-overwrite="true" id="ToWorkForMembSearch" should-skip-nulls="true">
            <int-xml:header name="legacySystem" evaluation-type="STRING_RESULT" overwrite="true" xpath-expression="//MemberSearch/LegacySystem"/>
            <int-xml:header name="businessArea" evaluation-type="STRING_RESULT" overwrite="true" xpath-expression="//MemberSearch/businessArea"/>
        </int-xml:xpath-header-enricher>
    </int:chain>

In one scenario, the transformed xml is coming as below:

<?xml version="1.0" encoding="UTF-8"?>

Since the payload is not having a valid xml string, the following exception is thrown by the xpath-header-enricher:

org.springframework.integration.MessagingException: failed to transform message headers
2016-02-16 12:34:54,431 WARN  http-0.0.0.0-8080-3 [org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway] failure occurred in gateway sendAndReceive
org.springframework.integration.MessageDeliveryException: no channel resolved by router and no default output channel defined

How to handle this scenario using xpath-expression if the invalid xml is passed to the xpath-header-enricher.

In a nutshell, The flow of execution should not be halted and exception should not be thrown even when the invalid xml content is passed.

Any ideas on the problem that I am facing?

Upvotes: 0

Views: 798

Answers (1)

Gary Russell
Gary Russell

Reputation: 174604

The flow of execution should not be halted

Take the header enricher out of the chain and invoke it via a gateway, with an error-channel to handle errors:

<int:chain input-channel="PQIssueDetails-PQMemberSearchMemAltID-Request" output-channel="PQIssueDetails-PQMemberSearchRequest-CheckLegacySystem">
    ...
    <int:gateway request-channel="enrich" error-channel="enrichErrors"/>
</int:chain>

    <int-xml:xpath-header-enricher input-channel="enrich"
         default-overwrite="true" id="ToWorkForMembSearch" should-skip-nulls="true">
        <int-xml:header name="legacySystem" evaluation-type="STRING_RESULT" overwrite="true" xpath-expression="//MemberSearch/LegacySystem"/>
        <int-xml:header name="businessArea" evaluation-type="STRING_RESULT" overwrite="true" xpath-expression="//MemberSearch/businessArea"/>
    </int-xml:xpath-header-enricher>

When the transformer is successful, the message will go to the next element in the chain; if it throws an exception, an ErrorMessage with a payload (MessagingException) with failedMessage and cause properties will go to the error channel; if the flow on that channel returns a result, it will go to the next element in the chain.

Upvotes: 1

Related Questions