0

Using SoapUI I am able to send a request with a custom SOAP header like this:

<soap:Header>
    <To xmlns="http://www.w3.org/2005/08/addressing">ws://xxx.com/PP/QM/GPMService/Vx</To>
   <Action xmlns="http://www.w3.org/2005/08/addressing">http://xmldefs.xxx.com/PP/QM/GPMService/Vx/AbcService/GetServiceInfoRequest</Action>
    <MessageID xmlns="http://www.w3.org/2005/08/addressing">ITEST-2018-04-16-0001</MessageID>
    <Stage xmlns="http://xmldefs.xxx.com/Technical/Addressing/V1">ProdX</Stage>
</soap:Header>

and get a reasonable response. I can't achieve this in my SpringBoot application. I have a service extending WebServiceGatewaySupport:

@Service
public class AbcService extends WebServiceGatewaySupport{
    private AbcConfiguration abcConfiguration;

    @Autowired
    public void setAbcConfiguration(final AbcConfiguration abcConfiguration) {
        this.abcConfiguration = abcConfiguration;
    }

    public GetServiceInfoResponse GetServiceInfo() {
        final String actionStr = "GetServiceInfo";
        final ObjectFactory factory = new ObjectFactory();

        GetServiceInfo getServiceInfo = factory.createGetServiceInfo();
        JAXBElement<GetServiceInfo> gsiRequest = factory.createGetServiceInfo(getServiceInfo);
        WebServiceTemplate wst = this.getWebServiceTemplate();
        @SuppressWarnings("unchecked")
        JAXBElement<GetServiceInfoResponse> gsiResponse = (JAXBElement<GetServiceInfoResponse>)wst
            .marshalSendAndReceive("https://ws-gateway-cert.xxx.com/services/", gsiRequest, new WebServiceMessageCallback() {
                    @Override
                    public void doWithMessage(WebServiceMessage message) {
                        try {
                            SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
                            SoapHeaderElement toElem = soapHeader.addHeaderElement(new QName("http://www.w3.org/2005/08/addressing", "To"));
                            toElem.setText("ws://xxx.com/PP/QM/GPMService/Vx");
                            ...
                        } catch (Exception e) {
                            logger.error("Error during marshalling of the SOAP headers", e);
                        }
                    }
            });

        return gsiResponse.getValue();
    }
}

What am I doing wrong? Can anybody tell me how I can do this?


Okay. I got it working so far and the SOAP XML looks as demanded and running the request (being generated form my SpringBoot app) in SoapUI I get the demanded result.

    public GetServiceInfoResponse GetServiceInfo() {
    final String actionStr = "GetServiceInfo";
    final ObjectFactory factory = new ObjectFactory();

    GetServiceInfo getServiceInfo = factory.createGetServiceInfo();
    JAXBElement<GetServiceInfo> gsiRequest = factory.createGetServiceInfo(getServiceInfo);
    WebServiceTemplate wst = this.getWebServiceTemplate();
    @SuppressWarnings("unchecked")
    JAXBElement<GetServiceInfoResponse> gsiResponse = (JAXBElement<GetServiceInfoResponse>)wst
        .marshalSendAndReceive(kpmConfiguration.getEndpoint(), gsiRequest, new WebServiceMessageCallback() {
                @Override
                public void doWithMessage(WebServiceMessage message) {
                    System.out.println(message.toString());
                    try {
                        // get the header from the SOAP message
                        final SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
                        final SaajSoapMessage ssMessage = (SaajSoapMessage)message;
                        final SOAPEnvelope envelope = ssMessage.getSaajMessage().getSOAPPart().getEnvelope();
                System.out.println("envelope.getPrefix(): " + envelope.getPrefix());
                        envelope.removeNamespaceDeclaration("SOAP-ENV");
                        envelope.setPrefix(NAMESPACE_PREFIX_SOAP);

                System.out.println("envelope.getPrefix(): " + envelope.getPrefix());
                        envelope.getBody().setPrefix(NAMESPACE_PREFIX_SOAP);
                        envelope.getHeader().setPrefix(NAMESPACE_PREFIX_SOAP);
                        envelope.addNamespaceDeclaration(NAMESPACE_PREFIX_SOAP, NAMESPACE_PREFIX_SOAP_DEF);
                        envelope.addNamespaceDeclaration(NAMESPACE_PREFIX_V2, NAMESPACE_PREFIX_V2_DEF);
                        envelope.addNamespaceDeclaration(NAMESPACE_PREFIX_WSSE, NAMESPACE_PREFIX_WSSE_DEF);

                        final SoapHeaderElement toElem = soapHeader.addHeaderElement(new QName(NAMESPACE_PREFIX_ADDRESSING, "To"));
                        toElem.setText(TO_VALUE);

                        final SoapHeaderElement actionElem = soapHeader.addHeaderElement(new QName(NAMESPACE_PREFIX_ADDRESSING, "Action"));
                        actionElem.setText(NAMESPACE_PREFIX_V2_DEF + "/AbcService/" + actionStr + "Request");

                        final SoapHeaderElement messageIdElem = soapHeader.addHeaderElement(new QName(NAMESPACE_PREFIX_ADDRESSING, "MessageID"));
                        messageIdElem.setText(MESSAGE_ID_VALUE + UUID.randomUUID());

                        final SoapHeaderElement stageElem = soapHeader.addHeaderElement(new QName(NAMESPACE_PREFIX_VWA, "Stage"));
                        stageElem.setText("Production");

                        final NodeList nl = ssMessage.getSaajMessage().getSOAPPart().getEnvelope().getBody().getChildNodes();

                        ssMessage.getSaajMessage().getSOAPPart().getEnvelope().getBody().removeChild(nl.item(0));
                        final SOAPElement se = ssMessage.getSaajMessage().getSOAPPart().getEnvelope().getBody().addBodyElement(new QName(actionStr));
                        se.setPrefix(NAMESPACE_PREFIX_V2);
                        final SOAPElement userAuthElem = se.addChildElement(new QName("UserAuthentification"));
                        final SOAPElement userIdElem = userAuthElem.addChildElement("UserId");
                        userIdElem.setTextContent(kpmConfiguration.getCredentials().getUsername());
                    System.out.println(userIdElem.getTextContent());
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    transformer.transform(ssMessage.getPayloadSource(), soapHeader.getResult());
                    } catch (Exception e) {
                        logger.error("Error during marshalling of the SOAP headers", e);
                    }
                }
            });
    return gsiResponse.getValue();
}

However, when I submit the request from my SpringBoot app I always get an exception:

java.net.SocketException: Unexpected end of file from server

Am I missing something in the code?

du-it
  • 2,561
  • 8
  • 42
  • 80

1 Answers1

0

See the answer to the original question above in the edited question. Concerning the java.net.SocketException: Unexpected end of file from server it seemed to come from redirecting the request through Eclipse's TCP/IP Monitor. When sending the request directly to the server I get a meaningful response with:

INFO_001 Method compelted successfully

:-)

du-it
  • 2,561
  • 8
  • 42
  • 80