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 org.opensaml.common.binding.artifact.AbstractSAMLArtifact;
20  
21  /**
22   * SAML 2 Artifact base class. SAML 2 artifacts contains a 2 byte type code followed by a 2 byte endpoint index followed
23   * by remaining artifact data.
24   */
25  public abstract class AbstractSAML2Artifact extends AbstractSAMLArtifact {
26  
27      /** 2 byte artifact endpoint index. */
28      private byte[] endpointIndex;
29  
30      /**
31       * Constructor.
32       * 
33       * @param artifactType artifact type code
34       */
35      protected AbstractSAML2Artifact(byte[] artifactType) {
36          super(artifactType);
37      }
38  
39      /**
40       * Constructor.
41       * 
42       * @param artifactType artifact type code
43       * @param index 2 byte endpoint index of the artifact
44       * 
45       * @throws IllegalArgumentException thrown if the endpoint index, source ID, or message handle arrays are not of the
46       *             right size
47       */
48      public AbstractSAML2Artifact(byte[] artifactType, byte[] index) {
49          super(artifactType);
50          setEndpointIndex(index);
51      }
52  
53      /**
54       * Gets the bytes for the artifact.
55       * 
56       * @return the bytes for the artifact
57       */
58      public byte[] getArtifactBytes() {
59          byte[] remainingArtifact = getRemainingArtifact();
60          byte[] artifact = new byte[4 + remainingArtifact.length];
61  
62          System.arraycopy(getTypeCode(), 0, artifact, 0, 2);
63          System.arraycopy(getEndpointIndex(), 0, artifact, 2, 2);
64          System.arraycopy(remainingArtifact, 0, artifact, 4, remainingArtifact.length);
65  
66          return artifact;
67      }
68  
69      /**
70       * Gets the 2 byte endpoint index for this artifact.
71       * 
72       * @return 2 byte endpoint index for this artifact
73       */
74      public byte[] getEndpointIndex() {
75          return endpointIndex;
76      }
77  
78      /**
79       * Sets the 2 byte endpoint index for this artifact.
80       * 
81       * @param newIndex 2 byte endpoint index for this artifact
82       * 
83       * @throws IllegalArgumentException thrown if the given index is not 2 bytes
84       */
85      public void setEndpointIndex(byte[] newIndex) {
86          if (newIndex.length != 2) {
87              throw new IllegalArgumentException("Artifact endpoint index must be two bytes long");
88          }
89  
90          endpointIndex = newIndex;
91      }
92  }