1

I am using apache cxf 3.5.2 and I am invoking a web service.

What i need for obscure boss reasons are the payloads (xml/soapenvelope) before sending the request and before handling the response. I can correctly get the payload of the request but not the one of the response. I am registering interceptors as follows:

org.apache.cxf.endpoint.Client clientProxy = ClientProxy.getClient(atposWSPortType);
clientProxy.getInInterceptors().add(new SoapActionInInterceptor());
clientProxy.getOutInterceptors().add(new SoapActionOutInterceptor());

The IN interceptor is:

public static class SoapActionInInterceptor extends AbstractSoapInterceptor {
    public SoapActionInInterceptor() {
        super(Phase.RECEIVE);
    }

    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        try {
            //Get the message body into payload[] and set a new non-consumed  inputStream into Message
            InputStream in = message.getContent(InputStream.class);
            byte payload[] = IOUtils.readBytesFromStream(in);
            ByteArrayInputStream bin = new ByteArrayInputStream(payload);
            message.setContent(InputStream.class, bin);
            String result = new String(payload, StandardCharsets.UTF_8);
            System.out.println("SOAP MESSAGE: " + result);
        } catch(Exception e) {
            //You'll do nothing
        }
    }
}

it works as expected:

SOAP MESSAGE: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><responseAuthorize xmlns="urn:...:ws:wsdl"><header reqRefNum="xxxxxx" timestamp="2022-08-29T15:17:47.713+02:00" shopId="xxxx"/><result>xxxx</result><transactionResult/></responseAuthorize></soap:Body></soap:Envelope>

the OUT intercetor does nothing:

public static class SoapActionOutInterceptor extends AbstractSoapInterceptor {
    public SoapActionOutInterceptor() {
        super(Phase.WRITE_ENDING); //I tried PRE_STREAM, SEND, etc...
    }

    public void handleMessage(SoapMessage message) throws Fault {
        try {
            //Get the message body into payload[] and set a new non-consumed  inputStream into Message
            InputStream in = message.getContent(InputStream.class);
            byte payload[] = IOUtils.readBytesFromStream(in); //log this
            ByteArrayInputStream bin = new ByteArrayInputStream(payload);
            message.setContent(InputStream.class, bin);
            String result = new String(payload, StandardCharsets.UTF_8);
            System.out.println("SOAP MESSAGE: " + result);
        } catch(Exception e) {
            System.out.println(e.getMessage());
            //You'll do nothing
        }
    }
}

message.getContent(InputStream.class) is always null, i am no expert and probably I got tired but i can't see a way out. Is there a smart way to get soap request and response without interceptors?

Thanks in advance

Sergio
  • 89
  • 1
  • 6
  • I was able to get payload using: `public static class SoapActionOutInterceptor extends AbstractSoapInterceptor { public SoapActionOutInterceptor() { super(Phase.PREPARE_SEND_ENDING); //<----------Right phase } public void handleMessage(SoapMessage message) throws Fault { try { CacheAndWriteOutputStream out = (CacheAndWriteOutputStream)(Message)message) .getContent(OutputStream.class);` The "prepare-send" phase does some magic *getConduit(message).prepare(message);* that allows the above. This solution is weird and frankly sucks. Let me know – Sergio Aug 29 '22 at 16:30
  • The right sequence is as follows: `CacheAndWriteOutputStream out = (CacheAndWriteOutputStream)((Message)message).getContent(OutputStream.class); byte payload[] = IOUtils.readBytesFromStream(out.getInputStream()); ByteArrayInputStream bin = new ByteArrayInputStream(payload);` Definitely a weird solution – Sergio Aug 29 '22 at 16:38

0 Answers0