soapspring-wsws-addressing

Route WS-Addressing response with Spring-WS


I haven't much experience with SOAP Web Services and in particular with WS-Addressing. I'm trying to create example where according to WS-Addressing spec a response from server routes to another server instead of client. But I faced some issues which I explain below.

Thereby, there are 3 services: Client, Server, and Response-Receiver.

Here it is Client's configurations:

ClientConfiguration.java

@Configuration
public class ClientConfiguration {

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("my.rinat.country.gen");
        return marshaller;
    }

    @Bean
    public CountryWsClient countryWsClient(Jaxb2Marshaller marshaller) {
        CountryWsClient client = new CountryWsClient();
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }
}

CountryWsClient.java

public class CountryWsClient extends WebServiceGatewaySupport {
    public GetCountryResponse getCountry(String name) throws URISyntaxException {
        GetCountryRequest request = new GetCountryRequest();
        request.setName(name);
        var callback = new ActionCallback(
                new URI("http://www.rinat.my/country/getCountry"),
                new Addressing10(),
                new URI("http://localhost:8282/ws")
        );
        callback.setReplyTo(new EndpointReference(new URI("http://localhost:8282/ws")));
        return (GetCountryResponse) getWebServiceTemplate().marshalSendAndReceive("http://localhost:8181/ws", request, callback);
    }
}

Server's configurations:

WsConfiguration.java

@Configuration
public class WsConfiguration extends WsConfigurationSupport {

    public static final String ACTION = "http://www.rinat.my/country/getCountry";

    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<>(servlet, "/ws/*");
    }

    @Bean(name = "country-ws")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountryPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.rinat.my/country/gen");
        wsdl11Definition.setSchema(schema);

        Properties actions = new Properties();
        actions.setProperty("getCountry", ACTION);
        wsdl11Definition.setSoapActions(actions);

        return wsdl11Definition;
    }

    @Bean
    @Override
    public AnnotationActionEndpointMapping annotationActionEndpointMapping() {
        var mapping = super.annotationActionEndpointMapping();
        mapping.setMessageSender(new HttpComponentsMessageSender());
        return mapping;
    }

    @Bean
    public XsdSchema schema() {
        return new SimpleXsdSchema(new ClassPathResource("xsd/countries.xsd"));
    }
}

CountryEndpoint.java

@Endpoint
public class CountryEndpoint {

    private final Countries countries;

    public CountryEndpoint(Countries countries) {
        this.countries = countries;
    }

    @Action(WsConfiguration.ACTION)
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload final GetCountryRequest request) {
        GetCountryResponse response = new GetCountryResponse();
        response.setCountry(this.countries.findCountry(request.getName()));
        return response;
    }
}

And Response-Receiver's configurations almost identical to the Server's becuase they use identical xsd-schemas:

WsConfiguration.java

@Configuration
public class WsConfiguration extends WsConfigurationSupport {

    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<>(servlet, "/ws/*");
    }

    @Bean(name = "country-ws")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountryPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.rinat.my/country/gen");
        wsdl11Definition.setSchema(schema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema schema() {
        return new SimpleXsdSchema(new ClassPathResource("xsd/countries.xsd"));
    }
}

CountryEndpoint.java

@Slf4j
@Endpoint
public class CountryEndpoint {
    @PayloadRoot(namespace = "http://www.rinat.my/country/gen", localPart = "getCountryResponse")
    @ResponsePayload
    public void getCountry(@RequestPayload final GetCountryResponse response) {
        var country = response.getCountry();
        log.info("Country {} with the capital {}, population {}, and currency {}", country.getName(),
                country.getCapital(), country.getPopulation(), country.getCurrency());
    }
}

Full example you can see at github

When I initiate Client's WS-Addressing request (via /kick REST controller) Response-Receiver receives response from the Server but it can't handle it becuase of some "mustUnderstand headers" issue (I don't understand enough what is it). Here it is Response-Receiver's log:

2019-12-12 10:45:01.433 TRACE 17188 --- [nio-8282-exec-1] o.s.ws.server.MessageTracing.received    : Received request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:To SOAP-ENV:mustUnderstand="1">http://localhost:8282/ws</wsa:To><wsa:Action>http://www.rinat.my/country/getCountryResponse</wsa:Action><wsa:MessageID>urn:uuid:340c83d7-6470-444e-b845-cea844e57400</wsa:MessageID><wsa:RelatesTo>urn:uuid:25e481a1-887b-4b28-a921-8615e1a08d83</wsa:RelatesTo></SOAP-ENV:Header><SOAP-ENV:Body><ns2:getCountryResponse xmlns:ns2="http://www.rinat.my/country/gen"><ns2:country><ns2:name>Poland</ns2:name><ns2:population>38186860</ns2:population><ns2:capital>Warsaw</ns2:capital><ns2:currency>PLN</ns2:currency></ns2:country></ns2:getCountryResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>]
2019-12-12 10:45:01.441  WARN 17188 --- [nio-8282-exec-1] o.s.w.soap.server.SoapMessageDispatcher  : Could not handle mustUnderstand headers: {http://www.w3.org/2005/08/addressing}To. Returning fault
2019-12-12 10:45:01.450 TRACE 17188 --- [nio-8282-exec-1] o.s.ws.server.MessageTracing.sent        : Sent response [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:MustUnderstand</faultcode><faultstring xml:lang="en">One or more mandatory SOAP header blocks not understood</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>] for request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:To SOAP-ENV:mustUnderstand="1">http://localhost:8282/ws</wsa:To><wsa:Action>http://www.rinat.my/country/getCountryResponse</wsa:Action><wsa:MessageID>urn:uuid:340c83d7-6470-444e-b845-cea844e57400</wsa:MessageID><wsa:RelatesTo>urn:uuid:25e481a1-887b-4b28-a921-8615e1a08d83</wsa:RelatesTo></SOAP-ENV:Header><SOAP-ENV:Body><ns2:getCountryResponse xmlns:ns2="http://www.rinat.my/country/gen"><ns2:country><ns2:name>Poland</ns2:name><ns2:population>38186860</ns2:population><ns2:capital>Warsaw</ns2:capital><ns2:currency>PLN</ns2:currency></ns2:country></ns2:getCountryResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>]

I would be grateful for help.


Solution

  • Receiver of routed message should implement @Action endpoint and router must send action header with value that equals to value of @Action annotation of receiver's endpoint.

    Full example you can find here.