How To Modify The Raw XML message of an Outbound CXF Request?

前端 未结 6 668
滥情空心
滥情空心 2020-11-27 17:07

I would like to modify an outgoing SOAP Request. I would like to remove 2 xml nodes from the Envelope\'s body. I managed to set up an Interceptor and get the generated Stri

6条回答
  •  孤街浪徒
    2020-11-27 17:47

    a better way would be to modify the message using the DOM interface, you need to add the SAAJOutInterceptor first (this might have a performance hit for big requests) and then your custom interceptor that is executed in phase USER_PROTOCOL

    import org.apache.cxf.binding.soap.SoapMessage;
    import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
    import org.apache.cxf.interceptor.Fault;
    import org.apache.cxf.phase.Phase;
    import org.w3c.dom.Node;
    
    import javax.xml.soap.SOAPException;
    import javax.xml.soap.SOAPMessage;
    
    abstract public class SoapNodeModifierInterceptor extends AbstractSoapInterceptor {
        SoapNodeModifierInterceptor() { super(Phase.USER_PROTOCOL); }
    
        @Override public void handleMessage(SoapMessage message) throws Fault {
            try {
                if (message == null) {
                    return;
                }
                SOAPMessage sm = message.getContent(SOAPMessage.class);
                if (sm == null) {
                    throw new RuntimeException("You must add the SAAJOutInterceptor to the chain");
                }
    
                modifyNodes(sm.getSOAPBody());
    
            } catch (SOAPException e) {
                throw new RuntimeException(e);
            }
        }
    
        abstract void modifyNodes(Node node);
    }
    

提交回复
热议问题