Kaustubh
Kaustubh

Reputation: 64

Set dynamic custom headers in http-outbound gateway

I have http outbound gateway :

<int-http:outbound-gateway encode-uri="true" http-method="POST" header-mapper="headerMappper"
        charset="UTF-8" url="{url}"  expected-response-type="java.lang.String">
        <int-http:uri-variable name="url" expression="headers.uri"/>
    </int-http:outbound-gateway>

Header Mapper bean configuration :

<bean    class="com.cc.gateway.HeaderMapper"/>

public class HeaderMapper extends org.springframework.integration.http.support.DefaultHttpHeaderMapper{

    @Bean("headerMappper")
    public HeaderMapper mapHeader()
    {
        this.setOutboundHeaderNames(getHeaderMapper());
        this.setUserDefinedHeaderPrefix("");
        return this;
    }

    public String[] getHeaderMapper()
    {
        Object [] headersArray =new HeadersConfig().getHeaders().keySet().toArray();

        return Arrays.copyOf(headersArray,headersArray.length,String[].class);

    }

}

How I can set header mapper configuration on every request ? My configuration reads only once at deployment time.

Upvotes: 2

Views: 617

Answers (1)

Gary Russell
Gary Russell

Reputation: 174739

The DefaultHttpHeaderMapper isn't structured for that kind of use; it is not thread-safe to change the mapped headers for each request.

You would have to override so many methods to make it thread-safe, it would probably be easier to just implement your own custom HeaderMapper<HttpHeaders>.

If you only have one message being sent at a time, though, simply override fromHeaders() and update the headers to map before calling super.fromHeaders().

That said, it's rather unusual to want to dynamically change mapped headers.

Upvotes: 1

Related Questions