0

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.

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Used a service activator as an implemention of GenericHandler but got "nested exception is java.lang.UnsupportedOperationException: MessageHeaders is immutable" when tried to modify headers.
Use MessageBuilder.fromMessage(msg).setHeader(...).setHeader(...).buld();.
I did in the end but also the problem was that I was using Object handle(String payload, MessageHeaders headers) instead of Object handle(Message<String> message, MessageHeaders headers) so I was using directly the payload
Adding an example would certainly improve this response value
1

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();
}

1 Comment

this is not working as method accept from MessageSelector is only returning boolean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.