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.impl;
18  
19  import java.math.BigInteger;
20  
21  import org.opensaml.xml.schema.impl.XSBase64BinaryImpl;
22  import org.opensaml.xml.security.keyinfo.KeyInfoHelper;
23  import org.opensaml.xml.signature.CryptoBinary;
24  import org.opensaml.xml.util.DatatypeHelper;
25  
26  /**
27   * Concrete implementation of {@link org.opensaml.xml.signature.CryptoBinary}.
28   */
29  public class CryptoBinaryImpl extends XSBase64BinaryImpl implements CryptoBinary {
30      
31      /** The cached BigInteger representation of the element's base64-encoded value. */
32      private BigInteger bigIntValue;
33  
34      /**
35       * Constructor.
36       *
37       * @param namespaceURI
38       * @param elementLocalName
39       * @param namespacePrefix
40       */
41      protected CryptoBinaryImpl(String namespaceURI, String elementLocalName, String namespacePrefix) {
42          super(namespaceURI, elementLocalName, namespacePrefix);
43      }
44  
45      /** {@inheritDoc} */
46      public BigInteger getValueBigInt() {
47          if (bigIntValue == null && !DatatypeHelper.isEmpty(getValue())) {
48              bigIntValue = KeyInfoHelper.decodeBigIntegerFromCryptoBinary(getValue());
49          }
50          return bigIntValue;
51      }
52  
53      /** {@inheritDoc} */
54      public void setValueBigInt(BigInteger bigInt) {
55          if (bigInt == null) {
56              setValue(null);
57          } else {
58              setValue(KeyInfoHelper.encodeCryptoBinaryFromBigInteger(bigInt));
59          }
60          bigIntValue = bigInt;
61      }
62      
63      /** {@inheritDoc} */
64      public void setValue(String newValue) {
65          if (bigIntValue != null 
66                  && (!DatatypeHelper.safeEquals(getValue(), newValue) || newValue == null)) {
67              // Just clear the cached value, my not be needed in big int form again,
68              // let it be lazily recreated if necessary
69              bigIntValue = null;
70          }
71          super.setValue(newValue);
72      }
73  
74  }