Example usage for javax.xml.soap SOAPHeader getChildElements

List of usage examples for javax.xml.soap SOAPHeader getChildElements

Introduction

In this page you can find the example usage for javax.xml.soap SOAPHeader getChildElements.

Prototype

public Iterator<Node> getChildElements(Name name);

Source Link

Document

Returns an Iterator over all the immediate child Node s of this element with the specified name.

Usage

From source file:com.qubit.solution.fenixedu.bennu.webservices.services.server.BennuWebServiceHandler.java

@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    //for response message only, true for outbound messages, false for inbound
    if (!isRequest) {
        try {//from   w ww.j a  v a  2  s  . co m

            WebServiceServerConfiguration configuration = getWebServiceServerConfiguration(
                    ((com.sun.xml.ws.api.server.WSEndpoint) context.get("com.sun.xml.ws.api.server.WSEndpoint"))
                            .getImplementationClass().getName());

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

            if (!configuration.isActive()) {
                generateSOAPErrorMessage(soapMsg, "Sorry webservice is disabled at application level!");
            }

            if (configuration.isAuthenticatioNeeded()) {

                if (configuration.isUsingWSSecurity()) {
                    if (soapHeader == null) {
                        generateSOAPErrorMessage(soapMsg,
                                "No header in message, unabled to validate security credentials");
                    }

                    String username = null;
                    String password = null;
                    String nonce = null;
                    String created = null;

                    Iterator<SOAPElement> childElements = soapHeader.getChildElements(QNAME_WSSE_SECURITY);
                    if (childElements.hasNext()) {
                        SOAPElement securityElement = childElements.next();
                        Iterator<SOAPElement> usernameTokens = securityElement
                                .getChildElements(QNAME_WSSE_USERNAME_TOKEN);
                        if (usernameTokens.hasNext()) {
                            SOAPElement usernameToken = usernameTokens.next();
                            username = ((SOAPElement) usernameToken.getChildElements(QNAME_WSSE_USERNAME)
                                    .next()).getValue();
                            password = ((SOAPElement) usernameToken.getChildElements(QNAME_WSSE_PASSWORD)
                                    .next()).getValue();
                            nonce = ((SOAPElement) usernameToken.getChildElements(QNAME_WSSE_NONCE).next())
                                    .getValue();
                            created = ((SOAPElement) usernameToken.getChildElements(QNAME_WSSE_CREATED).next())
                                    .getValue();
                        }
                    }
                    if (username == null || password == null || nonce == null || created == null) {
                        generateSOAPErrorMessage(soapMsg,
                                "Missing information, unabled to validate security credentials");
                    }

                    SecurityHeader securityHeader = new SecurityHeader(configuration, username, password, nonce,
                            created);
                    if (!securityHeader.isValid()) {
                        generateSOAPErrorMessage(soapMsg, "Invalid credentials");
                    } else {
                        context.put(BennuWebService.SECURITY_HEADER, securityHeader);
                        context.setScope(BennuWebService.SECURITY_HEADER, Scope.APPLICATION);
                    }
                } else {
                    com.sun.xml.ws.transport.Headers httpHeader = (Headers) context
                            .get(MessageContext.HTTP_REQUEST_HEADERS);
                    String username = null;
                    String password = null;
                    List<String> list = httpHeader.get("authorization");
                    if (list != null) {
                        for (String value : list) {
                            if (value.startsWith("Basic")) {
                                String[] split = value.split(" ");
                                try {
                                    String decoded = new String(Base64.decodeBase64(split[1]), "UTF-8");
                                    String[] split2 = decoded.split(":");
                                    if (split2.length == 2) {
                                        username = split2[0];
                                        password = split2[1];
                                    }
                                } catch (UnsupportedEncodingException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }

                    if (username == null || password == null) {
                        generateSOAPErrorMessage(soapMsg,
                                "Missing information, unabled to validate security credentials");
                    }

                    if (!configuration.validate(username, password)) {
                        generateSOAPErrorMessage(soapMsg, "Invalid credentials");
                    }
                }
            }

        } catch (SOAPException e) {
            System.err.println(e);
        }
    }

    //continue other handler chain
    return true;
}