Search in sources :

Example 11 with XPathFilterParameterSpec

use of javax.xml.crypto.dsig.spec.XPathFilterParameterSpec in project jdk8u_jdk by JetBrains.

the class DOMXPathTransform method marshalParams.

public void marshalParams(XMLStructure parent, XMLCryptoContext context) throws MarshalException {
    super.marshalParams(parent, context);
    XPathFilterParameterSpec xp = (XPathFilterParameterSpec) getParameterSpec();
    Element xpathElem = DOMUtils.createElement(ownerDoc, "XPath", XMLSignature.XMLNS, DOMUtils.getSignaturePrefix(context));
    xpathElem.appendChild(ownerDoc.createTextNode(xp.getXPath()));
    // add namespace attributes, if necessary
    @SuppressWarnings("unchecked") Set<Map.Entry<String, String>> entries = xp.getNamespaceMap().entrySet();
    for (Map.Entry<String, String> entry : entries) {
        xpathElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + entry.getKey(), entry.getValue());
    }
    transformElem.appendChild(xpathElem);
}
Also used : Element(org.w3c.dom.Element) XPathFilterParameterSpec(javax.xml.crypto.dsig.spec.XPathFilterParameterSpec) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 12 with XPathFilterParameterSpec

use of javax.xml.crypto.dsig.spec.XPathFilterParameterSpec in project camel by apache.

the class XmlSignerProcessor method getParentForEnvelopedCase.

protected Element getParentForEnvelopedCase(Document doc, Message inMessage) throws Exception {
    //NOPMD
    if (getConfiguration().getParentXpath() != null) {
        XPathFilterParameterSpec xp = getConfiguration().getParentXpath();
        XPathExpression exp;
        try {
            exp = XmlSignatureHelper.getXPathExpression(xp);
        } catch (XPathExpressionException e) {
            throw new XmlSignatureException("The parent XPath " + getConfiguration().getParentXpath().getXPath() + " is wrongly configured: The XPath " + xp.getXPath() + " is invalid.", e);
        }
        NodeList list = (NodeList) exp.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
        if (list == null || list.getLength() == 0) {
            throw new XmlSignatureException("The parent XPath " + xp.getXPath() + " returned no result. Check the configuration of the XML signer component.");
        }
        int length = list.getLength();
        for (int i = 0; i < length; i++) {
            Node node = list.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                // return the first element
                return (Element) node;
            }
        }
        throw new XmlSignatureException("The parent XPath " + xp.getXPath() + " returned no element. Check the configuration of the XML signer component.");
    } else {
        // parent local name is not null!
        NodeList parents = doc.getElementsByTagNameNS(getConfiguration().getParentNamespace(), getConfiguration().getParentLocalName());
        if (parents == null || parents.getLength() == 0) {
            throw new XmlSignatureFormatException(String.format("Incoming message has wrong format: The parent element with the local name %s and the namespace %s was not found in the message to build an enveloped XML signature.", getConfiguration().getParentLocalName(), getConfiguration().getParentNamespace()));
        }
        // return the first element
        return (Element) parents.item(0);
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XmlSignatureException(org.apache.camel.component.xmlsecurity.api.XmlSignatureException) XmlSignatureFormatException(org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) XPathFilterParameterSpec(javax.xml.crypto.dsig.spec.XPathFilterParameterSpec)

Example 13 with XPathFilterParameterSpec

use of javax.xml.crypto.dsig.spec.XPathFilterParameterSpec in project camel by apache.

the class XmlSignatureHelper method getXPathTransform.

/**
     * Returns a configuration for an XPATH transformation which needs a
     * namespace map.
     * 
     * @param xpath
     *            XPATH expression
     * @param namespaceMap
     *            namespace map, key is the prefix, value is the namespace, can
     *            be <code>null</code>
     * @throws IllegalArgumentException
     *             if <tt>xpath</tt> is <code>null</code>
     * @return XPATH transformation
     */
public static AlgorithmMethod getXPathTransform(String xpath, Map<String, String> namespaceMap) {
    if (xpath == null) {
        throw new IllegalArgumentException("xpath is null");
    }
    XmlSignatureTransform transformXPath = new XmlSignatureTransform();
    transformXPath.setAlgorithm(Transform.XPATH);
    XPathFilterParameterSpec params = getXpathFilter(xpath, namespaceMap);
    transformXPath.setParameterSpec(params);
    return transformXPath;
}
Also used : XPathFilterParameterSpec(javax.xml.crypto.dsig.spec.XPathFilterParameterSpec)

Example 14 with XPathFilterParameterSpec

use of javax.xml.crypto.dsig.spec.XPathFilterParameterSpec in project jdk8u_jdk by JetBrains.

the class DOMXPathTransform method unmarshalParams.

private void unmarshalParams(Element paramsElem) {
    String xPath = paramsElem.getFirstChild().getNodeValue();
    // create a Map of namespace prefixes
    NamedNodeMap attributes = paramsElem.getAttributes();
    if (attributes != null) {
        int length = attributes.getLength();
        Map<String, String> namespaceMap = new HashMap<String, String>(length);
        for (int i = 0; i < length; i++) {
            Attr attr = (Attr) attributes.item(i);
            String prefix = attr.getPrefix();
            if (prefix != null && prefix.equals("xmlns")) {
                namespaceMap.put(attr.getLocalName(), attr.getValue());
            }
        }
        this.params = new XPathFilterParameterSpec(xPath, namespaceMap);
    } else {
        this.params = new XPathFilterParameterSpec(xPath);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) XPathFilterParameterSpec(javax.xml.crypto.dsig.spec.XPathFilterParameterSpec) Attr(org.w3c.dom.Attr)

Aggregations

XPathFilterParameterSpec (javax.xml.crypto.dsig.spec.XPathFilterParameterSpec)14 JndiRegistry (org.apache.camel.impl.JndiRegistry)4 XPathExpression (javax.xml.xpath.XPathExpression)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 Element (org.w3c.dom.Element)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 XmlSignatureException (org.apache.camel.component.xmlsecurity.api.XmlSignatureException)2 Attr (org.w3c.dom.Attr)2 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 Key (java.security.Key)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 SecretKey (javax.crypto.SecretKey)1 XmlSignatureFormatException (org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException)1