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.saml2.binding.artifact;
18  
19  import java.util.Arrays;
20  
21  /**
22   * SAML 2 Type 0x004 Artifact. SAML 2, type 4, artifacts contains a 2 byte type code with a value of 4 follwed by a 2
23   * byte endpoint index followed by a 20 byte source ID followed by a 20 byte message handle.
24   */
25  public class SAML2ArtifactType0004 extends AbstractSAML2Artifact {
26  
27      /** SAML 2 artifact type code (0x0004). */
28      public static final byte[] TYPE_CODE = { 0, 4 };
29  
30      /** 20 byte artifact source ID. */
31      private byte[] sourceID;
32  
33      /** 20 byte message handle. */
34      private byte[] messageHandle;
35  
36      /** Constructor. */
37      public SAML2ArtifactType0004() {
38          super(TYPE_CODE);
39      }
40  
41      /**
42       * Constructor.
43       * 
44       * @param endpointIndex 2 byte endpoint index of the artifact
45       * @param source 20 byte source ID of the artifact
46       * @param handle 20 byte message handle of the artifact
47       * 
48       * @throws IllegalArgumentException thrown if the endpoint index, source ID, or message handle arrays are not of the
49       *             right size
50       */
51      public SAML2ArtifactType0004(byte[] endpointIndex, byte[] source, byte[] handle) {
52          super(TYPE_CODE, endpointIndex);
53          setSourceID(source);
54          setMessageHandle(handle);
55      }
56  
57      /**
58       * Constructs a SAML 2 artifact from its byte array representation.
59       * 
60       * @param artifact the byte array representing the artifact
61       * 
62       * @return the type 0x0004 artifact created from the byte array
63       * 
64       * @throws IllegalArgumentException thrown if the artifact is not the right type or lenght (44 bytes)
65       */
66      public static SAML2ArtifactType0004 parseArtifact(byte[] artifact) {
67          if (artifact.length != 44) {
68              throw new IllegalArgumentException("Artifact length must be 44 bytes it was " + artifact.length + "bytes");
69          }
70  
71          byte[] typeCode = { artifact[0], artifact[1] };
72          if (!Arrays.equals(typeCode, TYPE_CODE)) {
73              throw new IllegalArgumentException("Illegal artifact type code");
74          }
75  
76          byte[] endpointIndex = { artifact[2], artifact[3] };
77  
78          byte[] sourceID = new byte[20];
79          System.arraycopy(artifact, 4, sourceID, 0, 20);
80  
81          byte[] messageHandle = new byte[20];
82          System.arraycopy(artifact, 24, messageHandle, 0, 20);
83  
84          return new SAML2ArtifactType0004(endpointIndex, sourceID, messageHandle);
85      }
86  
87      /**
88       * Gets the 20 byte source ID of the artifact.
89       * 
90       * @return the source ID of the artifact
91       */
92      public byte[] getSourceID() {
93          return sourceID;
94      }
95  
96      /**
97       * Sets the 20 byte source ID of the artifact.
98       * 
99       * @param newSourceID 20 byte source ID of the artifact
100      * 
101      * @throws IllegalArgumentException thrown if the given source ID is not 20 bytes
102      */
103     public void setSourceID(byte[] newSourceID) {
104         if (newSourceID.length != 20) {
105             throw new IllegalArgumentException("Artifact source ID must be 20 bytes long");
106         }
107         sourceID = newSourceID;
108     }
109 
110     /**
111      * Gets the 20 byte message handle of the artifact.
112      * 
113      * @return 20 byte message handle of the artifact
114      */
115     public byte[] getMessageHandle() {
116         return messageHandle;
117     }
118 
119     /**
120      * Sets the 20 byte message handle of the artifact.
121      * 
122      * @param handle 20 byte message handle of the artifact
123      */
124     public void setMessageHandle(byte[] handle) {
125         if (handle.length != 20) {
126             throw new IllegalArgumentException("Artifact message handle must be 20 bytes long");
127         }
128         messageHandle = handle;
129     }
130 
131     /** {@inheritDoc} */
132     public byte[] getRemainingArtifact() {
133         byte[] remainingArtifact = new byte[40];
134 
135         System.arraycopy(getSourceID(), 0, remainingArtifact, 0, 20);
136         System.arraycopy(getMessageHandle(), 0, remainingArtifact, 20, 20);
137 
138         return remainingArtifact;
139     }
140 }