Example usage for org.w3c.dom Element getOwnerDocument

List of usage examples for org.w3c.dom Element getOwnerDocument

Introduction

In this page you can find the example usage for org.w3c.dom Element getOwnerDocument.

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:org.apache.ws.security.util.WSSecurityUtil.java

/**
 * find a ws-security header block for a given actor <p/>
 * /*from   w w w. j a  v a 2  s  .  co m*/
 * @param doc the DOM document (SOAP request)
 * @param envelope the SOAP envelope
 * @param actor the actor (role) name of the WSS header
 * @param doCreate if true create a new WSS header block if none exists
 * @return the WSS header or null if none found and doCreate is false
 */
public static Element findWsseSecurityHeaderBlock(Document doc, Element envelope, String actor,
        boolean doCreate) {
    SOAPConstants sc = getSOAPConstants(envelope);
    Element wsseSecurity = getSecurityHeader(doc, actor, sc);
    if (wsseSecurity != null) {
        return wsseSecurity;
    }
    Element header = findChildElement(envelope, sc.getEnvelopeURI(), sc.getHeaderQName().getLocalPart());
    if (header == null && doCreate) {
        header = createElementInSameNamespace(envelope, sc.getHeaderQName().getLocalPart());
        header = prependChildElement(envelope, header);
    }
    if (doCreate) {
        wsseSecurity = header.getOwnerDocument().createElementNS(WSConstants.WSSE_NS, "wsse:Security");
        wsseSecurity.setAttributeNS(WSConstants.XMLNS_NS, "xmlns:wsse", WSConstants.WSSE_NS);
        return prependChildElement(header, wsseSecurity);
    }
    return null;
}

From source file:org.apache.ws.security.util.WSSecurityUtil.java

public static SOAPConstants getSOAPConstants(Element startElement) {
    Document doc = startElement.getOwnerDocument();
    String ns = doc.getDocumentElement().getNamespaceURI();
    if (WSConstants.URI_SOAP12_ENV.equals(ns)) {
        return new SOAP12Constants();
    }// w ww  . jav  a 2s.co  m
    return new SOAP11Constants();
}

From source file:org.apache.ws.security.WSSecurityEngine.java

/**
 * Process the security header given the <code>wsse:Security</code> DOM
 * Element. /*  w ww .j  a  v  a2s  .c  o m*/
 * 
 * This function loops over all direct child elements of the
 * <code>wsse:Security</code> header. If it finds a known element, it
 * transfers control to the appropriate handling function. The method
 * processes the known child elements in the same order as they appear in
 * the <code>wsse:Security</code> element. This is in accordance to the WS
 * Security specification. <p/>
 * 
 * Currently the functions can handle the following child elements:
 * 
 * <ul>
 * <li>{@link #SIGNATURE <code>ds:Signature</code>}</li>
 * <li>{@link #ENCRYPTED_KEY <code>xenc:EncryptedKey</code>}</li>
 * <li>{@link #REFERENCE_LIST <code>xenc:ReferenceList</code>}</li>
 * <li>{@link #usernameToken <code>wsse:UsernameToken</code>}</li>
 * <li>{@link #timeStamp <code>wsu:Timestamp</code>}</li>
 * </ul>
 *
 * Note that additional child elements can be processed if appropriate
 * Processors have been registered with the WSSCondig instance set
 * on this class.
 *
 * @param securityHeader the <code>wsse:Security</code> header element
 * @param cb             a callback hander to the caller to resolve passwords during
 *                       encryption and {@link UsernameToken}handling
 * @param sigCrypto      the object that implements the access to the keystore and the
 *                       handling of certificates used for Signature
 * @param decCrypto      the object that implements the access to the keystore and the
 *                       handling of certificates used for Decryption
 * @return a Vector of {@link WSSecurityEngineResult}. Each element in the
 *         the Vector represents the result of a security action. The elements
 *         are ordered according to the sequence of the security actions in the
 *         wsse:Signature header. The Vector maybe empty if no security processing
 *         was performed.
 * @throws WSSecurityException
 */
protected Vector processSecurityHeader(Element securityHeader, CallbackHandler cb, Crypto sigCrypto,
        Crypto decCrypto) throws WSSecurityException {

    long t0 = 0, t1 = 0, t2 = 0;
    if (tlog.isDebugEnabled()) {
        t0 = System.currentTimeMillis();
    }
    /*
     * Gather some info about the document to process and store
     * it for retrieval. Store the implementation of signature crypto
     * (no need for encryption --- yet)
     */
    WSDocInfo wsDocInfo = new WSDocInfo(securityHeader.getOwnerDocument());
    wsDocInfo.setCrypto(sigCrypto);

    NodeList list = securityHeader.getChildNodes();
    int len = list.getLength();
    Node elem;
    if (tlog.isDebugEnabled()) {
        t1 = System.currentTimeMillis();
    }
    Vector returnResults = new Vector();

    for (int i = 0; i < len; i++) {
        elem = list.item(i);
        if (elem.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        QName el = new QName(elem.getNamespaceURI(), elem.getLocalName());
        final WSSConfig cfg = getWssConfig();
        Processor p = cfg.getProcessor(el);
        /*
         * Call the processor for this token. After the processor returns, 
         * store it for later retrieval. The token processor may store some
         * information about the processed token
         */
        if (p != null) {
            p.handleToken((Element) elem, sigCrypto, decCrypto, cb, wsDocInfo, returnResults, cfg);
            wsDocInfo.setProcessor(p);
        } else {
            /*
             * Add check for a BinarySecurityToken, add info to WSDocInfo. If BST is
             * found before a Signature token this would speed up (at least a little
             * bit) the processing of STR Transform.
             */
            if (doDebug) {
                log.debug("Unknown Element: " + elem.getLocalName() + " " + elem.getNamespaceURI());
            }
        }
    }
    if (tlog.isDebugEnabled()) {
        t2 = System.currentTimeMillis();
        tlog.debug("processHeader: total " + (t2 - t0) + ", prepare " + (t1 - t0) + ", handle " + (t2 - t1));
    }
    return returnResults;
}

From source file:org.apache.xml.security.algorithms.implementations.IntegrityHmac.java

/**
 * Method engineAddContextToElement//w ww.  ja va 2  s .c  om
 *
 * @param element
 */
public void engineAddContextToElement(Element element) {
    if (element == null) {
        throw new IllegalArgumentException("null element");
    }

    if (this.HMACOutputLengthSet) {
        Document doc = element.getOwnerDocument();
        Element HMElem = XMLUtils.createElementInSignatureSpace(doc, Constants._TAG_HMACOUTPUTLENGTH);
        Text HMText = doc.createTextNode(Integer.valueOf(this.HMACOutputLength).toString());

        HMElem.appendChild(HMText);
        XMLUtils.addReturnToElement(element);
        element.appendChild(HMElem);
        XMLUtils.addReturnToElement(element);
    }
}

From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java

protected void handleParent(Element e, NameSpaceSymbTable ns) {
    if (!e.hasAttributes() && e.getNamespaceURI() == null) {
        return;//from w w w .j  ava  2s.c o m
    }
    xmlattrStack.push(-1);
    NamedNodeMap attrs = e.getAttributes();
    int attrsLength = attrs.getLength();
    for (int i = 0; i < attrsLength; i++) {
        Attr attribute = (Attr) attrs.item(i);
        String NName = attribute.getLocalName();
        String NValue = attribute.getNodeValue();

        if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())) {
            if (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
                ns.addMapping(NName, NValue, attribute);
            }
        } else if (!"id".equals(NName) && XML_LANG_URI.equals(attribute.getNamespaceURI())) {
            xmlattrStack.addXmlnsAttr(attribute);
        }
    }
    if (e.getNamespaceURI() != null) {
        String NName = e.getPrefix();
        String NValue = e.getNamespaceURI();
        String Name;
        if (NName == null || NName.equals("")) {
            NName = "xmlns";
            Name = "xmlns";
        } else {
            Name = "xmlns:" + NName;
        }
        Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
        n.setValue(NValue);
        ns.addMapping(NName, NValue, n);
    }
}

From source file:org.apache.xml.security.encryption.XMLCipher.java

/**
 * Returns an <code>EncryptedKey</code> interface. Use this operation if
 * you want to load an <code>EncryptedKey</code> structure from a DOM 
 * structure and manipulate the contents.
 *
 * Assumes that the context document is the document that owns the element
 *
 * @param element the <code>Element</code> that will be loaded
 * @return the <code>EncryptedKey</code>
 * @throws XMLEncryptionException/*from   w  w w . ja  v  a 2  s .c  om*/
 */
public EncryptedKey loadEncryptedKey(Element element) throws XMLEncryptionException {
    return loadEncryptedKey(element.getOwnerDocument(), element);
}

From source file:org.apache.xml.security.keys.keyresolver.implementations.X509IssuerSerialResolver.java

/** @inheritDoc */
public X509Certificate engineLookupResolveX509Certificate(Element element, String BaseURI,
        StorageResolver storage) throws KeyResolverException {
    if (log.isDebugEnabled()) {
        log.debug("Can I resolve " + element.getTagName() + "?");
    }// w  w  w  .  j a va 2  s . com

    X509Data x509data = null;
    try {
        x509data = new X509Data(element, BaseURI);
    } catch (XMLSignatureException ex) {
        if (log.isDebugEnabled()) {
            log.debug("I can't");
        }
        return null;
    } catch (XMLSecurityException ex) {
        if (log.isDebugEnabled()) {
            log.debug("I can't");
        }
        return null;
    }

    if (x509data == null) {
        if (log.isDebugEnabled()) {
            log.debug("I can't");
        }
        return null;
    }

    if (!x509data.containsIssuerSerial()) {
        return null;
    }
    try {
        if (storage == null) {
            Object exArgs[] = { Constants._TAG_X509ISSUERSERIAL };
            KeyResolverException ex = new KeyResolverException("KeyResolver.needStorageResolver", exArgs);

            if (log.isDebugEnabled()) {
                log.debug("", ex);
            }
            throw ex;
        }

        int noOfISS = x509data.lengthIssuerSerial();

        Iterator<Certificate> storageIterator = storage.getIterator();
        while (storageIterator.hasNext()) {
            X509Certificate cert = (X509Certificate) storageIterator.next();
            XMLX509IssuerSerial certSerial = new XMLX509IssuerSerial(element.getOwnerDocument(), cert);

            if (log.isDebugEnabled()) {
                log.debug("Found Certificate Issuer: " + certSerial.getIssuerName());
                log.debug("Found Certificate Serial: " + certSerial.getSerialNumber().toString());
            }

            for (int i = 0; i < noOfISS; i++) {
                XMLX509IssuerSerial xmliss = x509data.itemIssuerSerial(i);

                if (log.isDebugEnabled()) {
                    log.debug("Found Element Issuer:     " + xmliss.getIssuerName());
                    log.debug("Found Element Serial:     " + xmliss.getSerialNumber().toString());
                }

                if (certSerial.equals(xmliss)) {
                    if (log.isDebugEnabled()) {
                        log.debug("match !!! ");
                    }
                    return cert;
                }
                if (log.isDebugEnabled()) {
                    log.debug("no match...");
                }
            }
        }

        return null;
    } catch (XMLSecurityException ex) {
        if (log.isDebugEnabled()) {
            log.debug("XMLSecurityException", ex);
        }

        throw new KeyResolverException("generic.EmptyMessage", ex);
    }
}

From source file:org.apache.xml.security.keys.keyresolver.implementations.X509SKIResolver.java

/**
 * Method engineResolveX509Certificate/*from  w  w  w  .  java 2  s  . com*/
 * @inheritDoc
 * @param element
 * @param BaseURI
 * @param storage
 *
 * @throws KeyResolverException
 */
public X509Certificate engineLookupResolveX509Certificate(Element element, String BaseURI,
        StorageResolver storage) throws KeyResolverException {
    if (log.isDebugEnabled()) {
        log.debug("Can I resolve " + element.getTagName() + "?");
    }
    if (!XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_X509DATA)) {
        if (log.isDebugEnabled()) {
            log.debug("I can't");
        }
        return null;
    }
    /** Field _x509childObject[] */
    XMLX509SKI x509childObject[] = null;

    Element x509childNodes[] = null;
    x509childNodes = XMLUtils.selectDsNodes(element.getFirstChild(), Constants._TAG_X509SKI);

    if (!((x509childNodes != null) && (x509childNodes.length > 0))) {
        if (log.isDebugEnabled()) {
            log.debug("I can't");
        }
        return null;
    }
    try {
        if (storage == null) {
            Object exArgs[] = { Constants._TAG_X509SKI };
            KeyResolverException ex = new KeyResolverException("KeyResolver.needStorageResolver", exArgs);

            if (log.isDebugEnabled()) {
                log.debug("", ex);
            }

            throw ex;
        }

        x509childObject = new XMLX509SKI[x509childNodes.length];

        for (int i = 0; i < x509childNodes.length; i++) {
            x509childObject[i] = new XMLX509SKI(x509childNodes[i], BaseURI);
        }

        Iterator<Certificate> storageIterator = storage.getIterator();
        while (storageIterator.hasNext()) {
            X509Certificate cert = (X509Certificate) storageIterator.next();
            XMLX509SKI certSKI = new XMLX509SKI(element.getOwnerDocument(), cert);

            for (int i = 0; i < x509childObject.length; i++) {
                if (certSKI.equals(x509childObject[i])) {
                    if (log.isDebugEnabled()) {
                        log.debug("Return PublicKey from " + cert.getSubjectDN().getName());
                    }

                    return cert;
                }
            }
        }
    } catch (XMLSecurityException ex) {
        throw new KeyResolverException("empty", ex);
    }

    return null;
}

From source file:org.apache.xml.security.keys.keyresolver.implementations.X509SubjectNameResolver.java

/**
 * Method engineResolveX509Certificate//from   ww  w.j av a  2 s.c  o m
 * @inheritDoc
 * @param element
 * @param BaseURI
 * @param storage
 *
 * @throws KeyResolverException
 */
public X509Certificate engineLookupResolveX509Certificate(Element element, String BaseURI,
        StorageResolver storage) throws KeyResolverException {
    if (log.isDebugEnabled()) {
        log.debug("Can I resolve " + element.getTagName() + "?");
    }
    Element[] x509childNodes = null;
    XMLX509SubjectName x509childObject[] = null;

    if (!XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_X509DATA)) {
        if (log.isDebugEnabled()) {
            log.debug("I can't");
        }
        return null;
    }
    x509childNodes = XMLUtils.selectDsNodes(element.getFirstChild(), Constants._TAG_X509SUBJECTNAME);

    if (!((x509childNodes != null) && (x509childNodes.length > 0))) {
        if (log.isDebugEnabled()) {
            log.debug("I can't");
        }
        return null;
    }

    try {
        if (storage == null) {
            Object exArgs[] = { Constants._TAG_X509SUBJECTNAME };
            KeyResolverException ex = new KeyResolverException("KeyResolver.needStorageResolver", exArgs);

            if (log.isDebugEnabled()) {
                log.debug("", ex);
            }

            throw ex;
        }

        x509childObject = new XMLX509SubjectName[x509childNodes.length];

        for (int i = 0; i < x509childNodes.length; i++) {
            x509childObject[i] = new XMLX509SubjectName(x509childNodes[i], BaseURI);
        }

        Iterator<Certificate> storageIterator = storage.getIterator();
        while (storageIterator.hasNext()) {
            X509Certificate cert = (X509Certificate) storageIterator.next();
            XMLX509SubjectName certSN = new XMLX509SubjectName(element.getOwnerDocument(), cert);

            if (log.isDebugEnabled()) {
                log.debug("Found Certificate SN: " + certSN.getSubjectName());
            }

            for (int i = 0; i < x509childObject.length; i++) {
                if (log.isDebugEnabled()) {
                    log.debug("Found Element SN:     " + x509childObject[i].getSubjectName());
                }

                if (certSN.equals(x509childObject[i])) {
                    if (log.isDebugEnabled()) {
                        log.debug("match !!! ");
                    }

                    return cert;
                }
                if (log.isDebugEnabled()) {
                    log.debug("no match...");
                }
            }
        }

        return null;
    } catch (XMLSecurityException ex) {
        if (log.isDebugEnabled()) {
            log.debug("XMLSecurityException", ex);
        }

        throw new KeyResolverException("generic.EmptyMessage", ex);
    }
}

From source file:org.apache.xml.security.utils.ElementProxy.java

/**
 * Constructor ElementProxy/*from   w ww . j av a2  s.  c o m*/
 *
 * @param element
 * @param BaseURI
 * @throws XMLSecurityException
 */
public ElementProxy(Element element, String BaseURI) throws XMLSecurityException {
    if (element == null) {
        throw new XMLSecurityException("ElementProxy.nullElement");
    }

    if (log.isDebugEnabled()) {
        log.debug("setElement(\"" + element.getTagName() + "\", \"" + BaseURI + "\")");
    }

    this.doc = element.getOwnerDocument();
    this.constructionElement = element;
    this.baseURI = BaseURI;

    this.guaranteeThatElementInCorrectSpace();
}