1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 /* 26 * $Id: DOMSignContext.java,v 1.9 2005/05/10 16:31:14 mullan Exp $ 27 */ 28 package javax.xml.crypto.dsig.dom; 29 30 import javax.xml.crypto.KeySelector; 31 import javax.xml.crypto.dom.DOMCryptoContext; 32 import javax.xml.crypto.dsig.XMLSignContext; 33 import javax.xml.crypto.dsig.XMLSignature; 34 import java.security.Key; 35 import org.w3c.dom.Node; 36 37 /** 38 * A DOM-specific {@link XMLSignContext}. This class contains additional methods 39 * to specify the location in a DOM tree where an {@link XMLSignature} 40 * object is to be marshalled when generating the signature. 41 * 42 * <p>Note that <code>DOMSignContext</code> instances can contain 43 * information and state specific to the XML signature structure it is 44 * used with. The results are unpredictable if a 45 * <code>DOMSignContext</code> is used with different signature structures 46 * (for example, you should not use the same <code>DOMSignContext</code> 47 * instance to sign two different {@link XMLSignature} objects). 48 * 49 * @author Sean Mullan 50 * @author JSR 105 Expert Group 51 * @since 1.6 52 */ 53 public class DOMSignContext extends DOMCryptoContext implements XMLSignContext { 54 55 private Node parent; 56 private Node nextSibling; 57 58 /** 59 * Creates a <code>DOMSignContext</code> with the specified signing key 60 * and parent node. The signing key is stored in a 61 * {@link KeySelector#singletonKeySelector singleton KeySelector} that is 62 * returned by the {@link #getKeySelector getKeySelector} method. 63 * The marshalled <code>XMLSignature</code> will be added as the last 64 * child element of the specified parent node unless a next sibling node is 65 * specified by invoking the {@link #setNextSibling setNextSibling} method. 66 * 67 * @param signingKey the signing key 68 * @param parent the parent node 69 * @throws NullPointerException if <code>signingKey</code> or 70 * <code>parent</code> is <code>null</code> 71 */ DOMSignContext(Key signingKey, Node parent)72 public DOMSignContext(Key signingKey, Node parent) { 73 if (signingKey == null) { 74 throw new NullPointerException("signingKey cannot be null"); 75 } 76 if (parent == null) { 77 throw new NullPointerException("parent cannot be null"); 78 } 79 setKeySelector(KeySelector.singletonKeySelector(signingKey)); 80 this.parent = parent; 81 } 82 83 /** 84 * Creates a <code>DOMSignContext</code> with the specified signing key, 85 * parent and next sibling nodes. The signing key is stored in a 86 * {@link KeySelector#singletonKeySelector singleton KeySelector} that is 87 * returned by the {@link #getKeySelector getKeySelector} method. 88 * The marshalled <code>XMLSignature</code> will be inserted as a child 89 * element of the specified parent node and immediately before the 90 * specified next sibling node. 91 * 92 * @param signingKey the signing key 93 * @param parent the parent node 94 * @param nextSibling the next sibling node 95 * @throws NullPointerException if <code>signingKey</code>, 96 * <code>parent</code> or <code>nextSibling</code> is <code>null</code> 97 */ DOMSignContext(Key signingKey, Node parent, Node nextSibling)98 public DOMSignContext(Key signingKey, Node parent, Node nextSibling) { 99 if (signingKey == null) { 100 throw new NullPointerException("signingKey cannot be null"); 101 } 102 if (parent == null) { 103 throw new NullPointerException("parent cannot be null"); 104 } 105 if (nextSibling == null) { 106 throw new NullPointerException("nextSibling cannot be null"); 107 } 108 setKeySelector(KeySelector.singletonKeySelector(signingKey)); 109 this.parent = parent; 110 this.nextSibling = nextSibling; 111 } 112 113 /** 114 * Creates a <code>DOMSignContext</code> with the specified key selector 115 * and parent node. The marshalled <code>XMLSignature</code> will be added 116 * as the last child element of the specified parent node unless a next 117 * sibling node is specified by invoking the 118 * {@link #setNextSibling setNextSibling} method. 119 * 120 * @param ks the key selector 121 * @param parent the parent node 122 * @throws NullPointerException if <code>ks</code> or <code>parent</code> 123 * is <code>null</code> 124 */ DOMSignContext(KeySelector ks, Node parent)125 public DOMSignContext(KeySelector ks, Node parent) { 126 if (ks == null) { 127 throw new NullPointerException("key selector cannot be null"); 128 } 129 if (parent == null) { 130 throw new NullPointerException("parent cannot be null"); 131 } 132 setKeySelector(ks); 133 this.parent = parent; 134 } 135 136 /** 137 * Creates a <code>DOMSignContext</code> with the specified key selector, 138 * parent and next sibling nodes. The marshalled <code>XMLSignature</code> 139 * will be inserted as a child element of the specified parent node and 140 * immediately before the specified next sibling node. 141 * 142 * @param ks the key selector 143 * @param parent the parent node 144 * @param nextSibling the next sibling node 145 * @throws NullPointerException if <code>ks</code>, <code>parent</code> or 146 * <code>nextSibling</code> is <code>null</code> 147 */ DOMSignContext(KeySelector ks, Node parent, Node nextSibling)148 public DOMSignContext(KeySelector ks, Node parent, Node nextSibling) { 149 if (ks == null) { 150 throw new NullPointerException("key selector cannot be null"); 151 } 152 if (parent == null) { 153 throw new NullPointerException("parent cannot be null"); 154 } 155 if (nextSibling == null) { 156 throw new NullPointerException("nextSibling cannot be null"); 157 } 158 setKeySelector(ks); 159 this.parent = parent; 160 this.nextSibling = nextSibling; 161 } 162 163 /** 164 * Sets the parent node. 165 * 166 * @param parent the parent node. The marshalled <code>XMLSignature</code> 167 * will be added as a child element of this node. 168 * @throws NullPointerException if <code>parent</code> is <code>null</code> 169 * @see #getParent 170 */ setParent(Node parent)171 public void setParent(Node parent) { 172 if (parent == null) { 173 throw new NullPointerException("parent is null"); 174 } 175 this.parent = parent; 176 } 177 178 /** 179 * Sets the next sibling node. 180 * 181 * @param nextSibling the next sibling node. The marshalled 182 * <code>XMLSignature</code> will be inserted immediately before this 183 * node. Specify <code>null</code> to remove the current setting. 184 * @see #getNextSibling 185 */ setNextSibling(Node nextSibling)186 public void setNextSibling(Node nextSibling) { 187 this.nextSibling = nextSibling; 188 } 189 190 /** 191 * Returns the parent node. 192 * 193 * @return the parent node (never <code>null</code>) 194 * @see #setParent(Node) 195 */ getParent()196 public Node getParent() { 197 return parent; 198 } 199 200 /** 201 * Returns the nextSibling node. 202 * 203 * @return the nextSibling node, or <code>null</code> if not specified. 204 * @see #setNextSibling(Node) 205 */ getNextSibling()206 public Node getNextSibling() { 207 return nextSibling; 208 } 209 } 210