Kirill
Kirill

Reputation: 8096

How to declare reusable transformer in XML without 'input-channel'?

In my application I would like to re-use the same message transformer inside of multiple <int:chain>.

In such chains I perform http requests to different endpoints and I need to add the same basic authentication header. I would like to declare the code for adding a header only once, i.e:

<int:header-enricher id="authHeaderAdder">
    <int:header expression="'Basic ' + new String(T(java.util.Base64).encoder.encode(('${http.user}' + ':' + '${http.password}').bytes))"
                name="Authorization"/>
</int:header-enricher>

And then I would like to use it with ref in my chain before making http request:

<int:chain input-channel="someHttpChain">
    <int:transformer ref="authHeaderAdder"/>
    <http:outbound-gateway.../>
    <int:transformer ref="someResponseTransformer"/>
</int:chain>

The problem is that I get an error on application startup:

Configuration problem: The 'input-channel' attribute is required for the top-level endpoint element: 'int:header-enricher' with id='authHeaderAdder'

How can I re-use authHeaderAdder without writing any java code and making a <bean/>?

Upvotes: 2

Views: 254

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121542

You definitely need to use an input-channel on that <int:header-enricher>, e.g. input-channel="authChannel" but inside the <chain> you get a gain to use something like <int:gateway request-channel="authChannel"/>. That's all: you are reusing the same transformer, but via the Spring Integration trick with the MessageChannel.

Such an approach is cool the way that you can add more endpoint in that authChannel flow without any changes in the original flow where you use that gateway.

Upvotes: 2

Related Questions