View Javadoc

1   /*
2    * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.xml.signature;
18  
19  import java.util.LinkedList;
20  import java.util.List;
21  
22  import org.apache.xml.security.signature.XMLSignature;
23  import org.apache.xml.security.transforms.Transforms;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * A generic content reference that uses a URI to reference the content to be signed.
29   */
30  public class URIContentReference implements ContentReference {
31  
32      /** Logger. */
33      private final Logger log = LoggerFactory.getLogger(URIContentReference.class);
34  
35      /** Element reference ID. */
36      private String referenceID;
37  
38      /** Algorithm used to digest the content . */
39      private String digestAlgorithm;
40  
41      /** Transforms applied to the content. */
42      private List<String> transforms;
43  
44      /**
45       * Constructor. The anchor designator (#) must not be included in the ID.
46       * 
47       * @param referenceID the reference ID of the element to be signed
48       */
49      public URIContentReference(String referenceID) {
50          this.referenceID = referenceID;
51          transforms = new LinkedList<String>();
52      }
53  
54      /**
55       * Gets the transforms applied to the content prior to digest generation.
56       * 
57       * @return the transforms applied to the content prior to digest generation
58       */
59      public List<String> getTransforms() {
60          return transforms;
61      }
62  
63      /**
64       * Gets the algorithm used to digest the content.
65       * 
66       * @return the algorithm used to digest the content
67       */
68      public String getDigestAlgorithm() {
69          return digestAlgorithm;
70      }
71  
72      /**
73       * Sets the algorithm used to digest the content.
74       * 
75       * @param newAlgorithm the algorithm used to digest the content
76       */
77      public void setDigestAlgorithm(String newAlgorithm) {
78          digestAlgorithm = newAlgorithm;
79      }
80  
81      /** {@inheritDoc} */
82      public void createReference(XMLSignature signature) {
83          try {
84              Transforms dsigTransforms = new Transforms(signature.getDocument());
85              for (String transform : transforms) {
86                  dsigTransforms.addTransform(transform);
87              }
88  
89              signature.addDocument(referenceID, dsigTransforms, digestAlgorithm);
90          } catch (Exception e) {
91              log.error("Error while adding content reference", e);
92          }
93      }
94  }