0

This a sample SOAP request I'm sending to a WS which is deployed in WAS 8.5

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://xmlns.scania.com/logistics/schema/transport/v1">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-37" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>username</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">
password
</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<v1:TransportInformationRequest>
         <!--1 or more repetitions:-->
         <v1:ChassisNumber>2105909</v1:ChassisNumber>
      </v1:TransportInformationRequest>
   </soapenv:Body>
</soapenv:Envelope>

What I want is to retrieve the username and password sent in the above request. I flipped through the Java EE api but couldn't find the correct way to do the same. I have written a painful code in the SOAPHandler as below :

@Override
    public boolean handleMessage(SOAPMessageContext context) {
        // TODO Auto-generated method stub

        Boolean isOutbound = (Boolean) context
                .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        log.info("In TransportHandler.handleMessage(...) " + isOutbound);

        if (!isOutbound) {

            SOAPMessage soapMsg = context.getMessage();
            SOAPEnvelope soapEnv;
            try {
                soapEnv = soapMsg.getSOAPPart().getEnvelope();
                SOAPHeader soapHeader = soapEnv.getHeader();

                Iterator<SOAPElement> securityHeaderElements = soapHeader
                        .getChildElements(new QName(
                                "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                                "Security", "wsse"));

                if (securityHeaderElements.hasNext()) {
                    SOAPElement securityHeaderElement = securityHeaderElements
                            .next();
                    log.info("*****securityHeaderElement : "
                            + securityHeaderElement + " type is "
                            + securityHeaderElement.getClass().getName());

                    Iterator<Node> securityHeaderChildren = securityHeaderElement
                            .getChildElements();

                    while (securityHeaderChildren.hasNext()) {

                        Node securityElement = securityHeaderChildren.next();

                        log.info("*****securityElement : "
                                + securityElement.getClass().getName());

                        if (securityElement instanceof com.ibm.ws.webservices.engine.xmlsoap.SOAPElement) {
                            com.ibm.ws.webservices.engine.xmlsoap.SOAPElement securitySOAPElement = (com.ibm.ws.webservices.engine.xmlsoap.SOAPElement) securityElement;

                            log.info("*****securitySOAPElement "
                                    + securitySOAPElement);

                            Iterator<Node> securitySOAPElementChildren = securitySOAPElement
                                    .getChildElements();

                            while (securitySOAPElementChildren.hasNext()) {
                                Node securitySOAPElementChild = securitySOAPElementChildren
                                        .next();

                                log.info("*****securitySOAPElementChild : "
                                        + securitySOAPElementChild);

                                if (securitySOAPElementChild instanceof com.ibm.ws.webservices.engine.xmlsoap.SOAPElement) {
                                    com.ibm.ws.webservices.engine.xmlsoap.SOAPElement securitySOAPChildElement = (com.ibm.ws.webservices.engine.xmlsoap.SOAPElement) securitySOAPElementChild;

                                    log.info("*****securitySOAPChildElement "
                                            + securitySOAPChildElement);
                                }
                            }

                        }
                    }
                }

            } catch (SOAPException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        log.info("Returning from TransportHandler.handleMessage(...)");

        return true;

    }

What is the efficient way to get the username and password ?

Kaliyug Antagonist
  • 3,512
  • 9
  • 51
  • 103
  • 1
    I believe this post might be helpful [http://stackoverflow.com/questions/10284486/how-to-get-soap-headers] – Rao Jan 26 '15 at 15:58
  • Dunno how efficient and reliable this code snippet is but worked well for me ;) NodeList userIdNode = header.getElementsByTagNameNS("*", "userId"); String userId = userIdNode.item(0).getChildNodes().item(0).getNodeValue(); thanks – Kaliyug Antagonist Jan 27 '15 at 08:15

0 Answers0