View Javadoc

1   /*
2    * Copyright [2005] [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;
18  
19  /**
20   * A type safe SAML version enumeration.
21   */
22  public final class SAMLVersion {
23  
24      /** SAML version 1.0. */
25      public static final SAMLVersion VERSION_10 = new SAMLVersion(1, 0);
26  
27      /** SAML Version 1.1. */
28      public static final SAMLVersion VERSION_11 = new SAMLVersion(1, 1);
29  
30      /** SAML Version 2.0. */
31      public static final SAMLVersion VERSION_20 = new SAMLVersion(2, 0);
32  
33      /** Major version number. */
34      private int majorVersion;
35  
36      /** Minor version number. */
37      private int minorVersion;
38  
39      /** String representation of the version. */
40      private String versionString;
41  
42      /**
43       * Constructor.
44       * 
45       * @param major SAML major version number
46       * @param minor SAML minor version number
47       */
48      private SAMLVersion(int major, int minor) {
49          majorVersion = major;
50          minorVersion = minor;
51  
52          versionString = majorVersion + "." + minorVersion;
53      }
54  
55      /**
56       * Gets the SAMLVersion given the major and minor version number.
57       * 
58       * @param majorVersion major version number
59       * @param minorVersion minor version number
60       * 
61       * @return the SAMLVersion
62       */
63      public static final SAMLVersion valueOf(int majorVersion, int minorVersion) {
64          if (majorVersion == 1) {
65              if (minorVersion == 0) {
66                  return SAMLVersion.VERSION_10;
67              } else if (minorVersion == 1) {
68                  return SAMLVersion.VERSION_11;
69              }
70          } else if (majorVersion == 2) {
71              if (minorVersion == 0) {
72                  return SAMLVersion.VERSION_20;
73              }
74          }
75  
76          return new SAMLVersion(majorVersion, minorVersion);
77      }
78  
79      /**
80       * Gets the SAMLVersion for a given version string, such as "2.0".
81       * 
82       * @param version SAML version string
83       * 
84       * @return SAMLVersion for the given string
85       */
86      public static final SAMLVersion valueOf(String version) {
87          String[] components = version.split("\\.");
88          return valueOf(Integer.valueOf(components[0]), Integer.valueOf(components[1]));
89      }
90  
91      /**
92       * Gets the major version of the SAML version.
93       * 
94       * @return the major version of the SAML version
95       */
96      public int getMajorVersion() {
97          return majorVersion;
98      }
99  
100     /**
101      * Gets the minor version of the SAML version.
102      * 
103      * @return the minor version of the SAML version
104      */
105     public int getMinorVersion() {
106         return minorVersion;
107     }
108 
109     /** {@inheritDoc} */
110     public String toString() {
111         return versionString;
112     }
113 }