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.

souvikc
  • 991
  • 1
  • 10
  • 25

2 Answers2

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • 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. – MS13 Jul 24 '23 at 14:33
  • Use `MessageBuilder.fromMessage(msg).setHeader(...).setHeader(...).buld();`. – Gary Russell Jul 24 '23 at 15:31
  • 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 message, MessageHeaders headers) so I was using directly the payload – MS13 Jul 24 '23 at 15:52
  • Adding an example would certainly improve this response value – MS13 Jul 26 '23 at 13:30
0

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();
}
  • this is not working as method accept from MessageSelector is only returning boolean – MS13 Jul 24 '23 at 12:19