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);
}
}
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");
}
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;
}
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;
}
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;
}
Aggregations