Search in sources :

Example 1 with XmlSignatureFormatException

use of org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException in project camel by apache.

the class XmlVerifierProcessor method parseInput.

protected Document parseInput(InputStream is, Message message) throws Exception {
    //NOPMD
    try {
        ValidatorErrorHandler errorHandler = new DefaultValidationErrorHandler();
        Schema schema = getSchema(message);
        DocumentBuilder db = XmlSignatureHelper.newDocumentBuilder(getConfiguration().getDisallowDoctypeDecl(), schema);
        db.setErrorHandler(errorHandler);
        Document doc = db.parse(is);
        // throws ValidationException
        errorHandler.handleErrors(message.getExchange(), schema, null);
        return doc;
    } catch (SAXException e) {
        throw new XmlSignatureFormatException("Message has wrong format, it is not a XML signature document. Check the sent message.", e);
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) XmlSignatureFormatException(org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException) Schema(javax.xml.validation.Schema) ValidatorErrorHandler(org.apache.camel.processor.validation.ValidatorErrorHandler) DefaultValidationErrorHandler(org.apache.camel.processor.validation.DefaultValidationErrorHandler) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 2 with XmlSignatureFormatException

use of org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException in project camel by apache.

the class XmlVerifierProcessor method getSignatureNodes.

private NodeList getSignatureNodes(Document doc) throws IOException, ParserConfigurationException, XmlSignatureFormatException {
    // Find Signature element
    NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
    if (nl.getLength() == 0) {
        throw new XmlSignatureFormatException("Message is not a correct XML signature document: 'Signature' element is missing. Check the sent message.");
    }
    LOG.debug("{} signature elements found", nl.getLength());
    return nl;
}
Also used : XmlSignatureFormatException(org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException) NodeList(org.w3c.dom.NodeList)

Example 3 with XmlSignatureFormatException

use of org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException 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 4 with XmlSignatureFormatException

use of org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException in project camel by apache.

the class XmlSignerProcessor method parseInput.

protected Document parseInput(InputStream is, Boolean disallowDoctypeDecl, Schema schema, ErrorHandler errorHandler) throws ParserConfigurationException, IOException, XmlSignatureFormatException {
    try {
        DocumentBuilder db = XmlSignatureHelper.newDocumentBuilder(disallowDoctypeDecl, schema);
        db.setErrorHandler(errorHandler);
        return db.parse(is);
    } catch (SAXException e) {
        throw new XmlSignatureFormatException("XML signature generation not possible. Sent message is not an XML document. Check the sent message.", e);
    } finally {
        IOHelper.close(is, "input stream");
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) XmlSignatureFormatException(org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException) SAXException(org.xml.sax.SAXException)

Aggregations

XmlSignatureFormatException (org.apache.camel.component.xmlsecurity.api.XmlSignatureFormatException)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 NodeList (org.w3c.dom.NodeList)2 SAXException (org.xml.sax.SAXException)2 XPathFilterParameterSpec (javax.xml.crypto.dsig.spec.XPathFilterParameterSpec)1 Schema (javax.xml.validation.Schema)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 XmlSignatureException (org.apache.camel.component.xmlsecurity.api.XmlSignatureException)1 DefaultValidationErrorHandler (org.apache.camel.processor.validation.DefaultValidationErrorHandler)1 ValidatorErrorHandler (org.apache.camel.processor.validation.ValidatorErrorHandler)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1