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); } }
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); }
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 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); }
@Override 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); }
@Test public void testExceptionXpathsToIdAttributesNameAndXPathSet() throws Exception { XmlSignerEndpoint endpoint = getSignatureEncpointForSignException(); MockEndpoint mock = setupExceptionMock(); try { endpoint.setParentXpath(getNodeSerachXPath()); List<XPathFilterParameterSpec> xpaths = Collections.singletonList(XmlSignatureHelper.getXpathFilter("/ns:root/a/@ID", null)); endpoint.setXpathsToIdAttributes(xpaths); sendBody("direct:signexceptions", payload); assertMockEndpointsSatisfied(); checkThrownException( mock, XmlSignatureException.class, "The configuration of the XML signer component is wrong. " + // "The parent XPath //pre:root for an enveloped signature and the XPATHs to ID attributes for a detached signature are specified. You must not specify both parameters.", null); } finally { endpoint.setParentXpath(null); endpoint.setXpathsToIdAttributes(null); } }
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 namespaceMap = new HashMap(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); } }
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 Iterator i = xp.getNamespaceMap().entrySet().iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); xpathElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + (String) entry.getKey(), (String) entry.getValue()); } transformElem.appendChild(xpathElem); }
public void init(TransformParameterSpec params) throws InvalidAlgorithmParameterException { if (params == null) { throw new InvalidAlgorithmParameterException("params are required"); } else if (!(params instanceof XPathFilterParameterSpec)) { throw new InvalidAlgorithmParameterException ("params must be of type XPathFilterParameterSpec"); } this.params = params; }
@Override public void init(TransformParameterSpec params) throws InvalidAlgorithmParameterException { if (params == null) { throw new InvalidAlgorithmParameterException("params are required"); } else if (!(params instanceof XPathFilterParameterSpec)) { throw new InvalidAlgorithmParameterException ("params must be of type XPathFilterParameterSpec"); } this.params = params; }
protected Node getOutputNodeViaXPath(Input input) throws Exception { //NOPMD checkSearchValueNotNull(input); checkSearchValueOfType(XPathFilterParameterSpec.class, input); XPathFilterParameterSpec xpathFilter = (XPathFilterParameterSpec) input.getOutputNodeSearch(); XPathExpression expr = XmlSignatureHelper.getXPathExpression(xpathFilter); NodeList nodes = (NodeList) expr.evaluate(input.getMessageBodyDocument(), XPathConstants.NODESET); if (nodes == null || nodes.getLength() == 0) { throw new XmlSignatureException( String.format( "Cannot extract root node for the output document from the XML signature document. No node found for XPATH %s as specified in the output node search.", xpathFilter.getXPath())); } if (nodes.getLength() > 1) { throw new XmlSignatureException( String.format( "Cannot extract root node for the output document from the XML signature document. XPATH %s as specified in the output node search results into more than one child.", xpathFilter.getXPath())); } Node result = nodes.item(0); if (Node.ELEMENT_NODE == result.getNodeType() || Node.TEXT_NODE == result.getNodeType() || Node.DOCUMENT_NODE == result.getNodeType()) { return result; } throw new XmlSignatureException(String.format("Cannot extract root node for the output document from the XML signature document. " + "XPATH %s as specified in the output node search results into a node which has the wrong type.", xpathFilter.getXPath())); }
@SuppressWarnings("unchecked") public static XPathExpression getXPathExpression(XPathFilterParameterSpec xpathFilter) throws XPathExpressionException { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); if (xpathFilter.getNamespaceMap() != null) { xpath.setNamespaceContext(new XPathNamespaceContext(xpathFilter.getNamespaceMap())); } return xpath.compile(xpathFilter.getXPath()); }
protected List<XPathFilterParameterSpec> getXpathToIdAttributes(Message message) { @SuppressWarnings("unchecked") List<XPathFilterParameterSpec> result = (List<XPathFilterParameterSpec>) message .getHeader(XmlSignatureConstants.HEADER_XPATHS_TO_ID_ATTRIBUTES); if (result == null) { result = getConfiguration().getXpathsToIdAttributes(); } return result; }
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); } }