View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2011 Michael Mimo Moratti.
3    *
4    * Michael Mimo Moratti licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at:
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
11   * License for the specific language governing permissions and limitations
12   * under the License.
13   *******************************************************************************/
14  package ch.mimo.netty.handler.codec.icap;
15  
16  import java.util.regex.Matcher;
17  import java.util.regex.Pattern;
18  
19  import org.jboss.netty.handler.codec.http.HttpVersion;
20  
21  /**
22   * Provides translation and handling for Icap version string.
23   * 
24   * @author Michael Mimo Moratti (mimo@mimo.ch)
25   *
26   */
27  public final class IcapVersion {
28  
29      private static final Pattern VERSION_PATTERN = Pattern.compile("(\\S+)/(\\d+)\\.(\\d+)");
30  	
31  	private String protocolName;
32  	private int major;
33  	private int minor;
34  	private String text;
35  	
36  	public static final IcapVersion ICAP_1_0 = new IcapVersion("ICAP", 1, 0);
37  
38  	/**
39  	 * @param protocolName ICAP or different
40  	 * @param major the major version
41  	 * @param minor the minor version
42  	 */
43      private IcapVersion(String protocolName, int major, int minor) {
44      	this.protocolName = protocolName;
45      	this.major = major;
46      	this.minor = minor;
47      	this.text = protocolName + '/' + major + '.' + minor;
48      }
49      
50      /**
51       * parses a valid icap protocol version string.
52       * @param text the version (ICAP/1.0)
53       */
54      private IcapVersion(String text) {
55      	if(text == null) {
56      		throw new NullPointerException("text");
57      	}
58          Matcher m = VERSION_PATTERN.matcher(text.trim().toUpperCase());
59          if (!m.matches()) {
60              throw new IllegalArgumentException("invalid version format: [" + text + "]");
61          }
62          protocolName = m.group(1);
63          major = Integer.parseInt(m.group(2));
64          minor = Integer.parseInt(m.group(3));
65          this.text = text;
66      }
67      
68      /**
69       * Protocol name
70       * @return ICAP or different.
71       */
72      public String getProtocolName() {
73      	return protocolName;
74      }
75      
76      /**
77       * Major version
78       * @return 1
79       */
80      public int getMajorVersion() {
81      	return major;
82      }
83      
84      /**
85       * Minor version
86       * @return 0
87       */
88      public int getMinorVersion() {
89      	return minor;
90      }
91      
92      /**
93       * The text representation of this version.
94       * @return ICAP/1.0
95       */
96      public String getText() {
97      	return text;
98      }
99  	
100     /**
101      * Returns an existing or new {@link HttpVersion} instance which matches to
102      * the specified RTSP version string.  If the specified {@code text} is
103      * equal to {@code "ICAP/1.0"}, {@link #ICAP_1_0} will be returned.
104      * Otherwise, a new {@link HttpVersion} instance will be returned.
105      */
106     public static IcapVersion valueOf(String text) {
107         if (text == null) {
108             throw new NullPointerException("text");
109         }
110         if (text.trim().toUpperCase().equals("ICAP/1.0")) {
111             return ICAP_1_0;
112         }
113 
114         return new IcapVersion(text);
115     }
116     
117     @Override
118     public String toString() {
119     	return text;
120     }
121 }