1

I am trying consume a SOAP web Service and I have written my SOAP client using Spring-ws. This also included a message signing. However when I am trying to trigger a request I am getting the exception. From the server standpoint, the server is sending a successful response but I am not able to unmarshall it.

I followed quite a few suggestion including one from this UnmarshallingFailureException, unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault")

Following the above link however removed the exception but in turn I am getting a null response object and I could not trace out where the response went missing.

This is my Config class methods:

    public WebServiceTemplate accountsInquiryWebServiceTemplate() throws Exception {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        Jaxb2Marshaller accountsInquiryMarshaller = accountsInquiryMarshaller();
        webServiceTemplate.setMarshaller(accountsInquiryMarshaller);
        webServiceTemplate.setUnmarshaller(accountsInquiryMarshaller);
        ClientInterceptor[] clientInterceptors = new ClientInterceptor[1];
        clientInterceptors[0] = wsClientSecurityInterceptor();
        webServiceTemplate.setInterceptors(clientInterceptors);
        webServiceTemplate.setCheckConnectionForFault(true);
        webServiceTemplate.setDefaultUri(serviceURL);

        return webServiceTemplate;
    }
private Jaxb2Marshaller accountsInquiryMarshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPaths(new String[]{
             "com.accounting.integrationservice"
});
        return jaxb2Marshaller;
    }
private Wss4jSecurityInterceptor wsClientSecurityInterceptor() throws Exception {
        Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
        wss4jSecurityInterceptor.setSecurementActions("Signature");
        wss4jSecurityInterceptor.setSecurementUsername(username);
        wss4jSecurityInterceptor.setSecurementPassword(password);
        wss4jSecurityInterceptor.setSecurementSignatureCrypto(clientCrypto());
        wss4jSecurityInterceptor.setValidationActions("Signature");

        return wss4jSecurityInterceptor;
    }
private Crypto clientCrypto() throws Exception {
        CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
        cryptoFactoryBean.setKeyStoreLocation(resourceLoader.getResource("classpath:accountsInquiry/keystore/" + keyStoreLocation));
        cryptoFactoryBean.setKeyStorePassword(keyStorePassword);
        cryptoFactoryBean.afterPropertiesSet();
        return cryptoFactoryBean.getObject();
    }

The client calls as below:

Response response = accountsWebServiceTemplate.marshalSendAndReceive(request);

The exception stack that I am getting is as below : Its similar to the referred link above, so not putting the entire exception stack

org.springframework.ws.soap.security.AbstractWsSecurityInterceptor.handleResponse(AbstractWsSecurityInterceptor.java:247)
 - Could not validate request: No WS-Security header found
org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault"). Expected elements are http://www.qualityaccounts.com/IntegrationService/schemas/ProductInfo/v1}
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:911)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:784)
    at org.springframework.ws.support.MarshallingUtils.unmarshal(MarshallingUtils.java:62)
    at org.springframework.ws.client.core.WebServiceTemplate$3.extractData(WebServiceTemplate.java:413)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:619)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:383)
Nirmalya
  • 195
  • 4
  • 16

1 Answers1

1

Ok So, I was trying to use "Signature" to validate the Security Header of the response even though there was no header. But thing is if I didn't specify the ValidationAction it was throwing NullPointerException so it was expecting at least one ValidationAction.

To counter all this I told the Spring security not to validate the response by this piece of code.

wss4jSecurityInterceptor.setValidateResponse(false);

Thanks to this article Spring Security - Wss4jSecurityInterceptor - Nullpointer

Nirmalya
  • 195
  • 4
  • 16