Search in sources :

Example 56 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XMLSecurityDataFormat method marshal.

public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
    // Retrieve the message body as input stream
    InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, graph);
    // and covert that to XML
    Document document = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, is);
    if (null != keyCipherAlgorithm && (keyCipherAlgorithm.equals(XMLCipher.RSA_v1dot5) || keyCipherAlgorithm.equals(XMLCipher.RSA_OAEP) || keyCipherAlgorithm.equals(XMLCipher.RSA_OAEP_11))) {
        encryptAsymmetric(exchange, document, stream);
    } else if (null != recipientKeyAlias) {
        encryptAsymmetric(exchange, document, stream);
    } else {
        encryptSymmetric(exchange, document, stream);
    }
}
Also used : InputStream(java.io.InputStream) Document(org.w3c.dom.Document)

Example 57 with Document

use of org.w3c.dom.Document in project camel by apache.

the class SpringXmlSignatureTest method xades.

@Test
public void xades() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    sendBody("direct:xades", payload);
    assertMockEndpointsSatisfied();
    Message message = getMessage(mock);
    byte[] body = message.getBody(byte[].class);
    Document doc = XmlSignatureHelper.newDocumentBuilder(true).parse(new ByteArrayInputStream(body));
    Map<String, String> prefix2Ns = XAdESSignaturePropertiesTest.getPrefix2NamespaceMap();
    prefix2Ns.put("t", "http://test.com/");
    XAdESSignaturePropertiesTest.checkXpath(doc, "/ds:Signature/ds:Object/etsi:QualifyingProperties/etsi:SignedProperties/etsi:SignedSignatureProperties/etsi:SignerRole/etsi:ClaimedRoles/etsi:ClaimedRole/t:test", prefix2Ns, "test");
}
Also used : Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 58 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XAdESSignatureProperties method createChildFromXmlFragmentOrText.

protected Element createChildFromXmlFragmentOrText(Document doc, Input input, String localElementName, String errorMessage, String elementOrText) throws IOException, ParserConfigurationException, XmlSignatureException {
    String ending = localElementName + ">";
    Element child;
    if (elementOrText.startsWith("<") && elementOrText.endsWith(ending)) {
        try {
            // assume xml
            InputSource source = new InputSource(new StringReader(elementOrText));
            source.setEncoding("UTF-8");
            Document parsedDoc = XmlSignatureHelper.newDocumentBuilder(Boolean.TRUE).parse(source);
            replacePrefixes(parsedDoc, input);
            child = (Element) doc.adoptNode(parsedDoc.getDocumentElement());
            // check for correct namespace
            String ns = findNamespace(input.getMessage());
            if (!ns.equals(child.getNamespaceURI())) {
                throw new XmlSignatureException(String.format("The XAdES confguration is invalid. The root element '%s' of the provided XML fragment '%s' has the invalid namespace '%s'. The correct namespace is '%s'.", child.getLocalName(), elementOrText, child.getNamespaceURI(), ns));
            }
        } catch (SAXException e) {
            throw new XmlSignatureException(String.format(errorMessage, elementOrText, localElementName, namespace), e);
        }
    } else {
        child = createElement(localElementName, doc, input);
        child.setTextContent(elementOrText);
    }
    return child;
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 59 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XAdESSignatureProperties method get.

@Override
public Output get(Input input) throws Exception {
    //NOPMD
    XmlSignatureProperties.Output result = new Output();
    if (!isAddSignedSignatureProperties() && !isAddSignedDataObjectPropeties()) {
        LOG.debug("XAdES signature properties are empty. Therefore no XAdES element will be added to the signature.");
        return result;
    }
    String signedPropertiesId = "_" + UUID.randomUUID().toString();
    List<Transform> transforms = Collections.emptyList();
    Reference ref = input.getSignatureFactory().newReference("#" + signedPropertiesId, input.getSignatureFactory().newDigestMethod(input.getContentDigestAlgorithm(), null), transforms, "http://uri.etsi.org/01903#SignedProperties", null);
    Node parent = input.getParent();
    Document doc;
    if (Node.DOCUMENT_NODE == parent.getNodeType()) {
        // enveloping
        doc = (Document) parent;
    } else {
        // enveloped
        doc = parent.getOwnerDocument();
    }
    Element qualifyingProperties = createElement("QualifyingProperties", doc, input);
    setIdAttributeFromHeader(XmlSignatureConstants.HEADER_XADES_QUALIFYING_PROPERTIES_ID, qualifyingProperties, input);
    String signatureId = input.getSignatureId();
    if (signatureId == null || signatureId.isEmpty()) {
        LOG.debug("No signature Id configured. Therefore a value is generated.");
        // generate one
        signatureId = "_" + UUID.randomUUID().toString();
        // and set to output
        result.setSignatureId(signatureId);
    }
    setAttribute(qualifyingProperties, "Target", "#" + signatureId);
    Element signedProperties = createElement("SignedProperties", doc, input);
    qualifyingProperties.appendChild(signedProperties);
    setAttribute(signedProperties, "Id", signedPropertiesId);
    signedProperties.setIdAttribute("Id", true);
    addSignedSignatureProperties(doc, signedProperties, input);
    String contentReferenceId = addSignedDataObjectProperties(doc, signedProperties, input);
    result.setContentReferenceId(contentReferenceId);
    DOMStructure structure = new DOMStructure(qualifyingProperties);
    XMLObject propertiesObject = input.getSignatureFactory().newXMLObject(Collections.singletonList(structure), null, null, null);
    result.setReferences(Collections.singletonList(ref));
    result.setObjects(Collections.singletonList(propertiesObject));
    return result;
}
Also used : Reference(javax.xml.crypto.dsig.Reference) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) DOMStructure(javax.xml.crypto.dom.DOMStructure) XMLObject(javax.xml.crypto.dsig.XMLObject) Transform(javax.xml.crypto.dsig.Transform) Document(org.w3c.dom.Document)

Example 60 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XmlSignatureHelper method getXslTranform.

/**
     * Returns a configuration for an XSL transformation.
     * 
     * @param is
     *            input stream of the XSL
     * @return XSL transform
     * @throws IllegalArgumentException
     *             if <tt>is</tt> is <code>null</code>
     * @throws Exception
     *             if an error during the reading of the XSL file occurs
     */
public static AlgorithmMethod getXslTranform(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    if (is == null) {
        throw new IllegalArgumentException("is must not be null");
    }
    Document doc = parseInput(is);
    DOMStructure stylesheet = new DOMStructure(doc.getDocumentElement());
    XSLTTransformParameterSpec spec = new XSLTTransformParameterSpec(stylesheet);
    XmlSignatureTransform transformXslt = new XmlSignatureTransform();
    transformXslt.setAlgorithm(Transform.XSLT);
    transformXslt.setParameterSpec(spec);
    return transformXslt;
}
Also used : XSLTTransformParameterSpec(javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec) DOMStructure(javax.xml.crypto.dom.DOMStructure) Document(org.w3c.dom.Document)

Aggregations

Document (org.w3c.dom.Document)3242 Element (org.w3c.dom.Element)1380 DocumentBuilder (javax.xml.parsers.DocumentBuilder)835 NodeList (org.w3c.dom.NodeList)718 Node (org.w3c.dom.Node)644 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)632 IOException (java.io.IOException)530 Test (org.junit.Test)485 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)384 SAXException (org.xml.sax.SAXException)370 InputSource (org.xml.sax.InputSource)292 File (java.io.File)268 InputStream (java.io.InputStream)249 ArrayList (java.util.ArrayList)244 StringReader (java.io.StringReader)233 DOMSource (javax.xml.transform.dom.DOMSource)230 ByteArrayInputStream (java.io.ByteArrayInputStream)202 Attr (org.w3c.dom.Attr)147 HashMap (java.util.HashMap)139 DOMException (org.w3c.dom.DOMException)136