You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SignatureMarshalDefaultListener.java 3.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.crypt.dsig;
  16. import static org.apache.poi.poifs.crypt.dsig.facets.SignatureFacet.XML_DIGSIG_NS;
  17. import static org.apache.poi.poifs.crypt.dsig.facets.SignatureFacet.XML_NS;
  18. import java.util.Arrays;
  19. import java.util.HashMap;
  20. import java.util.HashSet;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.Element;
  25. import org.w3c.dom.NamedNodeMap;
  26. import org.w3c.dom.Node;
  27. import org.w3c.dom.NodeList;
  28. import org.w3c.dom.events.EventListener;
  29. import org.w3c.dom.events.EventTarget;
  30. import org.w3c.dom.traversal.DocumentTraversal;
  31. import org.w3c.dom.traversal.NodeFilter;
  32. import org.w3c.dom.traversal.NodeIterator;
  33. /**
  34. * This listener class is used, to modify the to be digested xml document,
  35. * e.g. to register id attributes or set prefixes for registered namespaces
  36. */
  37. public class SignatureMarshalDefaultListener implements SignatureMarshalListener {
  38. private final Set<String> IGNORE_NS = new HashSet<>(Arrays.asList(null, XML_NS, XML_DIGSIG_NS));
  39. private final String OBJECT_TAG = "Object";
  40. @Override
  41. public void handleElement(SignatureInfo signatureInfo, Document doc, EventTarget target, EventListener parentListener) {
  42. // see POI #63712 : because of Santuario change r1853805 in XmlSec 2.1.3,
  43. // we have to deal with the whole document now
  44. final DocumentTraversal traversal = (DocumentTraversal) doc;
  45. final Map<String, String> prefixCfg = signatureInfo.getSignatureConfig().getNamespacePrefixes();
  46. final Map<String, String> prefixUsed = new HashMap<>();
  47. NodeList nl = doc.getElementsByTagName(OBJECT_TAG);
  48. final int objLen = nl.getLength();
  49. for (int i=0; i<objLen; i++) {
  50. final Element objNode = (Element)nl.item(i);
  51. getAllNamespaces(traversal, objNode, prefixCfg, prefixUsed);
  52. prefixUsed.forEach((ns, prefix) -> objNode.setAttributeNS(XML_NS, "xmlns:"+prefix, ns));
  53. }
  54. }
  55. private void getAllNamespaces(DocumentTraversal traversal, Element objNode, Map<String, String> prefixCfg, Map<String, String> prefixUsed) {
  56. prefixUsed.clear();
  57. final NodeIterator iter = traversal.createNodeIterator(objNode, NodeFilter.SHOW_ELEMENT, null, false);
  58. try {
  59. for (Element node; (node = (Element)iter.nextNode()) != null; ) {
  60. setPrefix(node, prefixCfg, prefixUsed);
  61. NamedNodeMap nnm = node.getAttributes();
  62. final int nnmLen = nnm.getLength();
  63. for (int j=0; j<nnmLen; j++) {
  64. setPrefix(nnm.item(j), prefixCfg, prefixUsed);
  65. }
  66. }
  67. } finally {
  68. iter.detach();
  69. }
  70. }
  71. private void setPrefix(Node node, Map<String,String> prefixCfg, Map<String,String> prefixUsed) {
  72. String ns = node.getNamespaceURI();
  73. String prefix = prefixCfg.get(ns);
  74. if (!IGNORE_NS.contains(prefix)) {
  75. node.setPrefix(prefix);
  76. prefixUsed.put(ns, prefix);
  77. }
  78. }
  79. }