souvikc
souvikc

Reputation: 1031

Spring Integration - Adding custom headers in Message

I am using Spring Integration filter to do a structural validation of the incoming payload and if the validation fails then i want to add some custom headers to the original message.

The filter code is below :

@Service("structureValidationFilter")
public class StructureValidationFilter implements MessageSelector {

@Override
public boolean accept(Message<?> message) {
    // TODO Auto-generated method stub
    boolean status=true;

    if(message.getPayload() instanceof CFKRequestBody) {
        CFKRequestBody body=(CFKRequestBody)message.getPayload();
        if(!body.getInitiatingPartyId().equalsIgnoreCase("BPKV")) {
            message = MutableMessageBuilder.fromMessage(message).
                    setHeader("BPKV_ERROR_CODE", "Ïnvalid Initiating part id").
                    setHeader("HTTP_STATUS", "400").build();

            return false;   
        }

    }
    return status;
}

}

But the headers are not populating in the Message. Not able to see the headers added in the next component. What am i doing wrong here.

Upvotes: 0

Views: 3112

Answers (2)

Alexey Khetchikov
Alexey Khetchikov

Reputation: 11

Your should wrap your result in Message like this

@Override
public Message accept(Message<?> message) {
    // TODO Auto-generated method stub
    boolean status=true;

    if(message.getPayload() instanceof CFKRequestBody) {
        CFKRequestBody body=(CFKRequestBody)message.getPayload();
        if(!body.getInitiatingPartyId().equalsIgnoreCase("BPKV")) {
            return MutableMessageBuilder.fromMessage(message).                        
                    setHeader("BPKV_ERROR_CODE", "Ïnvalid Initiating part id").
                    setHeader("HTTP_STATUS", "400").
                    build();
        }

    }
    return MutableMessageBuilder.fromMessage(message).build();
}

Upvotes: 1

Gary Russell
Gary Russell

Reputation: 174484

You can't replace a parameter and expect it to be propagated to the next component; Java doesn't work that way; your new message is simply discarded.

Use a service activator instead of a filter and return the new message, or null which is a signal to end the flow at that point.

Upvotes: 1

Related Questions