Example usage for javax.xml.soap SOAPEnvelope getNamespaceURI

List of usage examples for javax.xml.soap SOAPEnvelope getNamespaceURI

Introduction

In this page you can find the example usage for javax.xml.soap SOAPEnvelope getNamespaceURI.

Prototype

public String getNamespaceURI(String prefix);

Source Link

Document

Returns the URI of the namespace that has the given prefix.

Usage

From source file:Main.java

private static HashMap<String, String> getNamespaceDeclarations(SOAPEnvelope env, SOAPBodyElement body) {
    HashMap<String, String> namespaceDeclarations = new HashMap<String, String>();
    @SuppressWarnings("rawtypes")
    Iterator nss = env.getNamespacePrefixes();
    // Retrieve all namespace declarations from SOAP-ENV node
    while (nss.hasNext()) {
        String prefix = (String) nss.next();
        String uri = env.getNamespaceURI(prefix);
        // filter out SOAP-ENV namespace, since it is not interesting to us
        if (!uri.startsWith("http://schemas.xmlsoap.org/soap/envelope")) {
            namespaceDeclarations.put(prefix, uri);
        }//from w w  w  .  j  a  v  a2  s.co m
    }
    // Retrieve all namespace declarations from SOAP-BODY node
    nss = body.getNamespacePrefixes();
    while (nss.hasNext()) {
        String prefix = (String) nss.next();
        String uri = env.getNamespaceURI(prefix);
        namespaceDeclarations.put(prefix, uri);
    }
    return namespaceDeclarations;
}

From source file:ee.ria.xroad.common.message.SoapUtils.java

/**
        /*  w  ww  . ja v  a  2 s .  c  o  m*/
 * Returns namespace URIs from a SOAPMessage.
 * @param soap soap message from which to retrieve namespace URIs
 * @return a List of namespace URI Strings
 * @throws Exception if there is a SOAP error
 */
public static List<String> getNamespaceURIs(SOAPMessage soap) throws Exception {
    List<String> nsURIs = new ArrayList<>();

    SOAPEnvelope envelope = soap.getSOAPPart().getEnvelope();
    Iterator<?> it = envelope.getNamespacePrefixes();
    while (it.hasNext()) {
        nsURIs.add(envelope.getNamespaceURI((String) it.next()));
    }

    return nsURIs;
}

From source file:com.nortal.jroad.endpoint.AbstractXTeeBaseEndpoint.java

@SuppressWarnings("unchecked")
private XRoadProtocolVersion parseProtocolVersion(SOAPMessage requestMessage) throws SOAPException {
    XRoadProtocolVersion version = null;
    // Extract protocol version by headers
    if (requestMessage.getSOAPHeader() != null) {
        NodeList reqHeaders = requestMessage.getSOAPHeader().getChildNodes();
        for (int i = 0; i < reqHeaders.getLength(); i++) {
            Node reqHeader = reqHeaders.item(i);
            if (reqHeader.getNodeType() != Node.ELEMENT_NODE
                    || !reqHeader.getLocalName().equals(XTeeHeader.PROTOCOL_VERSION.getLocalPart())) {
                continue;
            }/*from   w  w  w .  j av  a  2  s  .  c  om*/

            if ((version = XRoadProtocolVersion
                    .getValueByVersionCode(SOAPUtil.getTextContent(reqHeader))) != null) {
                return version;
            }
        }
    }

    // Extract protocol version by namespaces
    SOAPEnvelope soapEnv = requestMessage.getSOAPPart().getEnvelope();
    Iterator<String> prefixes = soapEnv.getNamespacePrefixes();
    while (prefixes.hasNext()) {
        String nsPrefix = (String) prefixes.next();
        String nsURI = soapEnv.getNamespaceURI(nsPrefix).toLowerCase();
        if ((version = XRoadProtocolVersion.getValueByNamespaceURI(nsURI)) != null) {
            return version;
        }
    }
    throw new IllegalStateException("Unsupported protocol version");
}

From source file:be.agiv.security.handler.WSAddressingHandler.java

private void handleOutboundMessage(SOAPMessageContext context) throws SOAPException {
    LOG.debug("adding WS-Addressing headers");
    SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
    SOAPHeader header = envelope.getHeader();
    if (null == header) {
        header = envelope.addHeader();//w w w.j  a  v  a 2s  . co m
    }

    String wsuPrefix = null;
    String wsAddrPrefix = null;
    Iterator namespacePrefixesIter = envelope.getNamespacePrefixes();
    while (namespacePrefixesIter.hasNext()) {
        String namespacePrefix = (String) namespacePrefixesIter.next();
        String namespace = envelope.getNamespaceURI(namespacePrefix);
        if (WSConstants.WS_ADDR_NAMESPACE.equals(namespace)) {
            wsAddrPrefix = namespacePrefix;
        } else if (WSConstants.WS_SECURITY_UTILITY_NAMESPACE.equals(namespace)) {
            wsuPrefix = namespacePrefix;
        }
    }
    if (null == wsAddrPrefix) {
        wsAddrPrefix = getUniquePrefix("a", envelope);
        envelope.addNamespaceDeclaration(wsAddrPrefix, WSConstants.WS_ADDR_NAMESPACE);
    }
    if (null == wsuPrefix) {
        /*
         * Using "wsu" is very important for the IP-STS X509 credential.
         * Apparently the STS refuses when the namespace prefix of the
         * wsu:Id on the WS-Addressing To element is different from the
         * wsu:Id prefix on the WS-Security timestamp.
         */
        wsuPrefix = "wsu";
        envelope.addNamespaceDeclaration(wsuPrefix, WSConstants.WS_SECURITY_UTILITY_NAMESPACE);
    }

    SOAPFactory factory = SOAPFactory.newInstance();

    SOAPHeaderElement actionHeaderElement = header
            .addHeaderElement(new QName(WSConstants.WS_ADDR_NAMESPACE, "Action", wsAddrPrefix));
    actionHeaderElement.setMustUnderstand(true);
    actionHeaderElement.addTextNode(this.action);

    SOAPHeaderElement messageIdElement = header
            .addHeaderElement(new QName(WSConstants.WS_ADDR_NAMESPACE, "MessageID", wsAddrPrefix));
    String messageId = "urn:uuid:" + UUID.randomUUID().toString();
    context.put(MESSAGE_ID_CONTEXT_ATTRIBUTE, messageId);
    messageIdElement.addTextNode(messageId);

    SOAPHeaderElement replyToElement = header
            .addHeaderElement(new QName(WSConstants.WS_ADDR_NAMESPACE, "ReplyTo", wsAddrPrefix));
    SOAPElement addressElement = factory.createElement("Address", wsAddrPrefix, WSConstants.WS_ADDR_NAMESPACE);
    addressElement.addTextNode("http://www.w3.org/2005/08/addressing/anonymous");
    replyToElement.addChildElement(addressElement);

    SOAPHeaderElement toElement = header
            .addHeaderElement(new QName(WSConstants.WS_ADDR_NAMESPACE, "To", wsAddrPrefix));
    toElement.setMustUnderstand(true);

    toElement.addTextNode(this.to);

    String toIdentifier = "to-id-" + UUID.randomUUID().toString();
    toElement.addAttribute(new QName(WSConstants.WS_SECURITY_UTILITY_NAMESPACE, "Id", wsuPrefix), toIdentifier);
    try {
        toElement.setIdAttributeNS(WSConstants.WS_SECURITY_UTILITY_NAMESPACE, "Id", true);
    } catch (UnsupportedOperationException e) {
        // Axis2 has missing implementation of setIdAttributeNS
        LOG.error("error setting Id attribute: " + e.getMessage());
    }
    context.put(TO_ID_CONTEXT_ATTRIBUTE, toIdentifier);
}