karruma
karruma

Reputation: 778

How to access custom headers in Spring Integration after receiving http error

I am trying to define an error flow with spring integration. Before I make an http call I am setting an id in the form of a custom header for the resource that I would like to process - this however returns an error (not 2xx http code), so I am defining GenericTransform class to take out the error and take also the id of the resource (extracting that from the headeR). But this doesn't seem to be working like that and I no longer have access to my custom headers defined before the HTTP code was executed.

How can this be achieved? In the code below I am able to add custom header (messageId) but if the http call returned an unsuccessful http code then I am loosing the header and its value - this works fine when a successful response comes after calling http endpoint.

@Bean
public IntegrationFlow makeHttpCall() {
    return message -> message
                             .enrich(e -> e
                                    .requestPayload(Message::getPayload)
                                    .shouldClonePayload(false)
                                    .<Map<String, String>>headerFunction("messageId", m -> m.getPayload().substring(0,10), true)
                             )                               
                             .handle(makeHttpRequest())
                             .transform(new GenericTransformer<Message<ResponseEntity>, Message<ResponseEntity>>() {
                                 @Override
                                    public Message<ResponseEntity> transform(Message<ResponseEntity> message) {
                                     if (message.getHeaders().get("http_statusCode").equals("http_statusCode=200")) {
                                         //This is failing, only http headers available                                          
                                         logger.debug("Response from the endpoint: " + message.getPayload());                                            
                                         return message;
                                     } else {
                                         String messageId = message.getHeaders().get("messageId").toString();
                                         MutableMessageHeaders headers = new MutableMessageHeaders(message.getHeaders());                                                                                        
                                         headers.put("error_message", "1");                                                                                      
                                         headers.put("messageId", messageId);
                                         return new GenericMessage<ResponseEntity>(message.getPayload(), headers);   
                                     }

                                 }
                             })                          
                             .channel("entrypoint");
}

Any ideas highly appreciated.

Upvotes: 2

Views: 2094

Answers (1)

karruma
karruma

Reputation: 778

This works fine, the problem I was due to the fact that I had a transformer that have been also modyfinig headers without passing the original ones.

.transform(new GenericTransformer<Message<String>, Message<String>>() {
                                    @Override
                                    public Message<String> transform(Message<String> message) {     //incorrectly instantiated                              
                                        MutableMessageHeaders headers = new MutableMessageHeaders();
                                        headers.put("Content-Type", "application/json");
                                        headers.put("Accept", "application/json");
                                        Message<String> request = new GenericMessage<String>(json.toString(), headers);
                                        return request;
                                    }
                             })

So the line 3 should read:

MutableMessageHeaders headers = new MutableMessageHeaders(message.getHeaders());

Thanks!

Upvotes: 1

Related Questions