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.saml1.binding.artifact;
18  
19  import java.util.Arrays;
20  
21  import org.opensaml.xml.util.DatatypeHelper;
22  
23  /**
24   * SAML 1 Type 0x0002 Artifact. SAML 1, type 2, artifacts contains a 2 byte type code with a value of 1 followed by a 20
25   * byte assertion handle followed by an unspecified number of bytes which are a UTF-8 encoded source location string.
26   */
27  public class SAML1ArtifactType0002 extends AbstractSAML1Artifact {
28  
29      /** Artifact type code (0x0002). */
30      public static final byte[] TYPE_CODE = { 0, 2 };
31  
32      /** 20 byte assertion handle. */
33      private byte[] assertionHandle;
34  
35      /** Artifact source location component. */
36      private String sourceLocation;
37  
38      /** Constructor. */
39      public SAML1ArtifactType0002() {
40          super(TYPE_CODE);
41      }
42  
43      /**
44       * Constructor.
45       * 
46       * @param handle 20 byte assertion handle artifact component
47       * @param location source location artifact component
48       * 
49       * @throws IllegalArgumentException thrown if the given assertion handle is not 20 bytes or the source location is
50       *             null or empty
51       */
52      public SAML1ArtifactType0002(byte[] handle, String location) {
53          super(TYPE_CODE);
54  
55          setAssertionHandle(handle);
56          setSourceLocation(location);
57      }
58  
59      /**
60       * Constructs a SAML 1 artifact from its byte representation.
61       * 
62       * @param artifact the byte array representing the artifact
63       * @return the artifact parsed from the byte representation
64       * 
65       * @throws IllegalArgumentException thrown if the artifact type is not 0x0002
66       */
67      public static SAML1ArtifactType0002 parseArtifact(byte[] artifact) {
68          byte[] typeCode = { artifact[0], artifact[1] };
69          if (!Arrays.equals(typeCode, TYPE_CODE)) {
70              throw new IllegalArgumentException("Artifact is not of appropriate type.");
71          }
72  
73          byte[] assertionHandle = new byte[20];
74          System.arraycopy(artifact, 2, assertionHandle, 0, 20);
75  
76          int locationLength = artifact.length - 22;
77          byte[] sourceLocation = new byte[locationLength];
78          System.arraycopy(artifact, 22, sourceLocation, 0, locationLength);
79  
80          return new SAML1ArtifactType0002(assertionHandle, new String(sourceLocation));
81      }
82  
83      /**
84       * Gets the artifiact's 20 byte assertion handle.
85       * 
86       * @return artifiact's 20 byte assertion handle
87       */
88      public byte[] getAssertionHandle() {
89          return assertionHandle;
90      }
91  
92      /**
93       * Sets the artifiact's 20 byte assertion handle.
94       * 
95       * @param handle artifiact's 20 byte assertion handle
96       */
97      public void setAssertionHandle(byte[] handle) {
98          if (handle.length != 20) {
99              throw new IllegalArgumentException("Artifact assertion handle must be 20 bytes long");
100         }
101         assertionHandle = handle;
102     }
103 
104     /**
105      * Gets the source location component of this artifact.
106      * 
107      * @return source location component of this artifact
108      */
109     public String getSourceLocation() {
110         return sourceLocation;
111     }
112 
113     /**
114      * Sets source location component of this artifact.
115      * 
116      * @param newLocation source location component of this artifact
117      * 
118      * @throws IllegalArgumentException thrown if the given location is empty or null
119      */
120     protected void setSourceLocation(String newLocation) {
121         String location = DatatypeHelper.safeTrimOrNullString(newLocation);
122         if (location == null) {
123             throw new IllegalArgumentException("Artifact source location may not be a null or empty string");
124         }
125 
126         sourceLocation = location;
127     }
128 
129     /** {@inheritDoc} */
130     public byte[] getRemainingArtifact() {
131         byte[] location = getSourceLocation().getBytes();
132         byte[] remainingArtifact = new byte[20 + location.length];
133 
134         System.arraycopy(getAssertionHandle(), 0, remainingArtifact, 0, 20);
135         System.arraycopy(location, 0, remainingArtifact, 20, location.length);
136 
137         return remainingArtifact;
138     }
139 }