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.common.binding.artifact;
18  
19  import java.util.Arrays;
20  
21  import org.bouncycastle.util.encoders.Hex;
22  import org.opensaml.xml.util.Base64;
23  
24  /**
25   * Base class for SAML artifacts.
26   */
27  public abstract class AbstractSAMLArtifact {
28  
29      /** 2 byte artifact type code. */
30      private byte[] typeCode;
31  
32      /**
33       * Constructor.
34       * 
35       * @param code the artifact type code
36       * 
37       * @throws IllegalArgumentException thrown if the given type code is not two bytes in length
38       */
39      protected AbstractSAMLArtifact(byte[] code) {
40          if(code.length != 2){
41              throw new IllegalArgumentException("Type code was not 2-bytes in size");
42          }
43          typeCode = code;
44      }
45  
46      /**
47       * Gets the bytes for the artifact.
48       * 
49       * @return the bytes for the artifact
50       */
51      public byte[] getArtifactBytes() {
52          byte[] remainingArtifact = getRemainingArtifact();
53          byte[] artifact = new byte[2 + remainingArtifact.length];
54  
55          System.arraycopy(getTypeCode(), 0, artifact, 0, 2);
56          System.arraycopy(remainingArtifact, 0, artifact, 2, remainingArtifact.length);
57  
58          return artifact;
59      }
60  
61      /**
62       * Gets the 2 byte type code for this artifact.
63       * 
64       * @return the type code for this artifact
65       */
66      public byte[] getTypeCode() {
67          return typeCode;
68      }
69  
70      /**
71       * Sets the 2 byte type code for this artifact.
72       * 
73       * @param newTypeCode 2 byte type code for this artifact
74       * 
75       * @throws IllegalArgumentException thrown if the given type code is not two bytes
76       */
77      protected void setTypeCode(byte[] newTypeCode) {
78          typeCode = newTypeCode;
79      }
80  
81      /**
82       * Gets the artifact bytes minus the type code.
83       * 
84       * @return artifact bytes minus the type code
85       */
86      public abstract byte[] getRemainingArtifact();
87  
88      /**
89       * Gets the Base64 encoded artifact.
90       * 
91       * @return Base64 encoded artifact.
92       */
93      public String base64Encode() {
94          return new String(Base64.encodeBytes(getArtifactBytes()));
95      }
96  
97      /**
98       * Gets the hex encoded artifact.
99       * 
100      * @return hex encoded artifact
101      */
102     public String hexEncode() {
103         return new String(Hex.encode(getArtifactBytes()));
104     }
105 
106     /** {@inheritDoc} */
107     public boolean equals(Object o) {
108         if(o == this){
109             return true;
110         }
111         
112         if (o instanceof AbstractSAMLArtifact) {
113             AbstractSAMLArtifact otherArtifact = (AbstractSAMLArtifact) o;
114             return Arrays.equals(getArtifactBytes(), otherArtifact.getArtifactBytes());
115         }
116 
117         return false;
118     }
119 
120     /** {@inheritDoc} */
121     public int hashCode() {
122         return Arrays.hashCode(getArtifactBytes());
123     }
124     
125     /** {@inheritDoc} */
126     public String toString(){
127         return base64Encode();
128     }
129 }