Home > Software engineering >  Spring boot WS: Is posible remove all attachments from a instance of MessageContext class?
Spring boot WS: Is posible remove all attachments from a instance of MessageContext class?

Time:12-29

I have developed a WS with Spring boot. In the interceptor I want to modify the request and delete all attachments. I have tried the following:

public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
    WebServiceMessage requestReceived = messageContext.getRequest();
    if (requestReceived instanceof SaajSoapMessage) {
        SaajSoapMessage message = ((SaajSoapMessage) requestReceived);
        message.getAttachments().remove();
    }
    return true;
}

but remove() is not supported in that iterator.

Is it possible to delete all attachments?

Cheers

CodePudding user response:

I haven't tried this myself, but the way you are trying to remove the attachments does not seem to be right. Ideally you get an iterator and use it to iterate over the elements and deleting the ones you desire (in your case 'all' elements). Something like below:

if (requestReceived instanceof SaajSoapMessage) {
    SaajSoapMessage message = ((SaajSoapMessage) requestReceived);
    Iterator<Attachment> itr = message.getAttachments();

    while(itr.hasNext()) {
        itr.next();
        itr.remove();
    }
}
  • Related