View Javadoc

1   /*
2    * Copyright [2005] [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.common.impl;
18  
19  import org.joda.time.DateTime;
20  import org.joda.time.DateTimeZone;
21  import org.opensaml.common.SignableSAMLObject;
22  import org.opensaml.xml.AbstractValidatingSignableXMLObject;
23  import org.opensaml.xml.signature.Signature;
24  
25  /**
26   * Abstract SAMLObject implementation that also implements {@link org.opensaml.xml.signature.SignableXMLObject}.
27   */
28  public abstract class AbstractSignableSAMLObject extends AbstractValidatingSignableXMLObject implements
29          SignableSAMLObject {
30  
31      /**
32       * Constructor.
33       * 
34       * @param namespaceURI the namespace the element is in
35       * @param elementLocalName the local name of the XML element this Object represents
36       * @param namespacePrefix the prefix for the given namespace
37       */
38      protected AbstractSignableSAMLObject(String namespaceURI, String elementLocalName, String namespacePrefix) {
39          super(namespaceURI, elementLocalName, namespacePrefix);
40      }
41  
42      /** {@inheritDoc} */
43      public final boolean equals(Object obj) {
44          if(obj == this){
45              return true;
46          }
47          
48          return super.equals(obj);
49      }
50  
51      /**
52       * {@inheritDoc}
53       * 
54       * When a signature is added, a default content reference that uses the ID of this object will be
55       * created and added to the signature at the time of signing. See {@link SAMLObjectContentReference} 
56       * for the default digest algorithm and transforms that will be used.  These default values may be 
57       * changed prior to marshalling this object.
58       */
59      public void setSignature(Signature newSignature) {
60          if(newSignature != null){
61              newSignature.getContentReferences().add(new SAMLObjectContentReference(this));
62          }
63          super.setSignature(newSignature);
64      }
65  
66      /**
67       * A helper function for derived classes that checks to see if the old and new value are equal and if so releases
68       * the cached dom. Derived classes are expected to use this thus: <code>
69       *   this.foo = prepareForAssignment(this.foo, foo);
70       *   </code>
71       * 
72       * This method will do a (null) safe compare of the objects and will also invalidate the DOM if appropriate
73       * 
74       * @param oldValue - current value
75       * @param newValue - proposed new value
76       * 
77       * @return The value to assign to the saved Object
78       */
79      protected DateTime prepareForAssignment(DateTime oldValue, DateTime newValue) {
80          DateTime utcValue = null;
81          if (newValue != null) {
82              utcValue = newValue.withZone(DateTimeZone.UTC);
83          }
84  
85          return super.prepareForAssignment(oldValue, utcValue);
86      }
87  }