View Javadoc

1   /*
2    * Copyright [2007] [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.HashMap;
20  import java.util.Map;
21  
22  import org.opensaml.xml.util.Base64;
23  
24  /**
25   * Factory used to construct SAML 1 artifact builders.
26   */
27  public class SAML1ArtifactBuilderFactory {
28  
29      /** Registered artifact builders. */
30      private Map<String, SAML1ArtifactBuilder> artifactBuilders;
31  
32      /** Constructor. */
33      public SAML1ArtifactBuilderFactory() {
34          artifactBuilders = new HashMap<String, SAML1ArtifactBuilder>(2);
35          artifactBuilders.put(new String(SAML1ArtifactType0001.TYPE_CODE), new SAML1ArtifactType0001Builder());
36          artifactBuilders.put(new String(SAML1ArtifactType0002.TYPE_CODE), new SAML1ArtifactType0002Builder());
37      }
38  
39      /**
40       * Gets the currently registered artifact builders.
41       * 
42       * @return currently registered artifact builders
43       */
44      public Map<String, SAML1ArtifactBuilder> getArtifactBuilders() {
45          return artifactBuilders;
46      }
47  
48      /**
49       * Gets the artifact builder for the given type.
50       * 
51       * @param type type of artifact to be built
52       * 
53       * @return artifact builder for the given type
54       */
55      public SAML1ArtifactBuilder getArtifactBuilder(byte[] type) {
56          return artifactBuilders.get(new String(type));
57      }
58      
59      /**
60       * Convenience method for getting an artifact builder and parsing the given Base64 encoded artifact with it.
61       * 
62       * @param base64Artifact Base64 encoded artifact to parse
63       * 
64       * @return constructed artifact
65       */
66      public AbstractSAML1Artifact buildArtifact(String base64Artifact){
67          return buildArtifact(Base64.decode(base64Artifact));
68      }
69  
70      /**
71       * Convenience method for getting an artifact builder and parsing the given artifact with it.
72       * 
73       * @param artifact artifact to parse
74       * 
75       * @return constructed artifact
76       */
77      public AbstractSAML1Artifact buildArtifact(byte[] artifact) {
78          if(artifact == null){
79              return null;
80          }
81          
82          byte[] type = new byte[2];
83          type[0] = artifact[0];
84          type[1] = artifact[1];
85  
86          SAML1ArtifactBuilder<?> artifactBuilder = getArtifactBuilder(type);
87          return artifactBuilder.buildArtifact(artifact);
88      }
89  }