javaweb-servicessoapuisaaj

String to SOAPMessage not working in java


I have below message in a String,

"<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:def="http://DefaultNamespace">
   <soapenv:Header/>
   <soapenv:Body>
      <def:XXX soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <in0 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">someEncryptedTextGoesHere</in0>
      </def:XXX>
   </soapenv:Body>
</soapenv:Envelope>"

I'm trying to convert it in SOAPMessage and access its body in order to get the EncryptedText, but for some reason envelop gets null, I don't understand I tried with,

1) Basically I have HttpEntity, and body of a entity is a soaprequest

public String getEncryptedCodeFromSOAPRequest(HttpEntity<byte[]> requestEntity) throws SOAPException, IOException {
    InputStream is = new ByteArrayInputStream(requestEntity.getBody());
    log.info(requestEntity.getBody().toString());
    String encryptedCode = "";
    if (is.available() > 0) {
        //SOAPMessage request= MessageFactory.newInstance().createMessage(null, is);
        SOAPMessage request = MessageFactory.newInstance().createMessage(null,is);
        encryptedCode = new String(request.getSOAPBody().getFirstChild().getFirstChild().getNodeValue().getBytes(), StandardCharsets.UTF_8);
    }
    return encryptedCode;
}

2) Tried using different kinds of SOAPConstants like,

SOAPMessage request = MessageFactory.newInstance(SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE).createMessage(null,is);

URI_NS_SOAP_1_1_ENVELOPE using this because xmlns:soapenv in soap msg seems to be like this dont know it this login correct or not.

Please let me know if you have suggestion on this

enter image description here

enter image description here

enter image description here


Solution

  • Try replacing this:

    if (is.available() > 0) {
        //SOAPMessage request= MessageFactory.newInstance().createMessage(null, is);
        SOAPMessage request = MessageFactory.newInstance().createMessage(null,is);
        encryptedCode = new String(request.getSOAPBody().getFirstChild().getFirstChild().getNodeValue().getBytes(), StandardCharsets.UTF_8);
    }
    

    With This:

        if (is.available() > 0) {
            SOAPMessage message = MessageFactory.newInstance("SOAP 1.2 Protocol").createMessage(null, is);
            Document requestDocument = message.getSOAPBody().extractContentAsDocument();
            NodeList nodes = requestDocument.getChildNodes();
            //get parent node 1
            Node parentNode = nodes.item(0);
            //get child nodes, of parent node
            NodeList childNodes = parentNode.getChildNodes();
            //get first child node since theres only one in the example xml
            Node childNode = childNodes.item(0);
            //print type, value, etc
            System.out.println(childNode.getNodeType() + " = " + childNode.getNodeName() + "/" + childNode.getNodeValue());
            encryptedCode = childNode.getNodeValue();
        }