View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.conn.scheme;
28  
29  import java.util.Locale;
30  
31  import org.apache.http.annotation.Immutable;
32  import org.apache.http.util.Args;
33  import org.apache.http.util.LangUtils;
34  
35  /**
36   * Encapsulates specifics of a protocol scheme such as "http" or "https". Schemes are identified
37   * by lowercase names. Supported schemes are typically collected in a {@link SchemeRegistry
38   * SchemeRegistry}.
39   * <p>
40   * For example, to configure support for "https://" URLs, you could write code like the following:
41   * </p>
42   * <pre>
43   * Scheme https = new Scheme("https", 443, new MySecureSocketFactory());
44   * SchemeRegistry registry = new SchemeRegistry();
45   * registry.register(https);
46   * </pre>
47   *
48   * @since 4.0
49   *
50   * @deprecated (4.3) use {@link org.apache.http.conn.SchemePortResolver} for default port
51   * resolution and {@link org.apache.http.config.Registry} for socket factory lookups.
52   */
53  @Immutable
54  @Deprecated
55  public final class Scheme {
56  
57      /** The name of this scheme, in lowercase. (e.g. http, https) */
58      private final String name;
59  
60      /** The socket factory for this scheme */
61      private final SchemeSocketFactory socketFactory;
62  
63      /** The default port for this scheme */
64      private final int defaultPort;
65  
66      /** Indicates whether this scheme allows for layered connections */
67      private final boolean layered;
68  
69      /** A string representation, for {@link #toString toString}. */
70      private String stringRep;
71      /*
72       *  This is used to cache the result of the toString() method
73       *  Since the method always generates the same value, there's no
74       *  need to synchronize, and it does not affect immutability.
75      */
76  
77      /**
78       * Creates a new scheme.
79       * Whether the created scheme allows for layered connections
80       * depends on the class of {@code factory}.
81       *
82       * @param name      the scheme name, for example "http".
83       *                  The name will be converted to lowercase.
84       * @param port      the default port for this scheme
85       * @param factory   the factory for creating sockets for communication
86       *                  with this scheme
87       *
88       * @since 4.1
89       */
90      public Scheme(final String name, final int port, final SchemeSocketFactory factory) {
91          Args.notNull(name, "Scheme name");
92          Args.check(port > 0 && port <= 0xffff, "Port is invalid");
93          Args.notNull(factory, "Socket factory");
94          this.name = name.toLowerCase(Locale.ENGLISH);
95          this.defaultPort = port;
96          if (factory instanceof SchemeLayeredSocketFactory) {
97              this.layered = true;
98              this.socketFactory = factory;
99          } else if (factory instanceof LayeredSchemeSocketFactory) {
100             this.layered = true;
101             this.socketFactory = new SchemeLayeredSocketFactoryAdaptor2((LayeredSchemeSocketFactory) factory);
102         } else {
103             this.layered = false;
104             this.socketFactory = factory;
105         }
106     }
107 
108     /**
109      * Creates a new scheme.
110      * Whether the created scheme allows for layered connections
111      * depends on the class of {@code factory}.
112      *
113      * @param name      the scheme name, for example "http".
114      *                  The name will be converted to lowercase.
115      * @param factory   the factory for creating sockets for communication
116      *                  with this scheme
117      * @param port      the default port for this scheme
118      *
119      * @deprecated (4.1)  Use {@link #Scheme(String, int, SchemeSocketFactory)}
120      */
121     @Deprecated
122     public Scheme(final String name,
123                   final SocketFactory factory,
124                   final int port) {
125 
126         Args.notNull(name, "Scheme name");
127         Args.notNull(factory, "Socket factory");
128         Args.check(port > 0 && port <= 0xffff, "Port is invalid");
129 
130         this.name = name.toLowerCase(Locale.ENGLISH);
131         if (factory instanceof LayeredSocketFactory) {
132             this.socketFactory = new SchemeLayeredSocketFactoryAdaptor(
133                     (LayeredSocketFactory) factory);
134             this.layered = true;
135         } else {
136             this.socketFactory = new SchemeSocketFactoryAdaptor(factory);
137             this.layered = false;
138         }
139         this.defaultPort = port;
140     }
141 
142     /**
143      * Obtains the default port.
144      *
145      * @return  the default port for this scheme
146      */
147     public final int getDefaultPort() {
148         return defaultPort;
149     }
150 
151 
152     /**
153      * Obtains the socket factory.
154      * If this scheme is {@link #isLayered layered}, the factory implements
155      * {@link LayeredSocketFactory LayeredSocketFactory}.
156      *
157      * @return  the socket factory for this scheme
158      *
159      * @deprecated (4.1)  Use {@link #getSchemeSocketFactory()}
160      */
161     @Deprecated
162     public final SocketFactory getSocketFactory() {
163         if (this.socketFactory instanceof SchemeSocketFactoryAdaptor) {
164             return ((SchemeSocketFactoryAdaptor) this.socketFactory).getFactory();
165         } else {
166             if (this.layered) {
167                 return new LayeredSocketFactoryAdaptor(
168                         (LayeredSchemeSocketFactory) this.socketFactory);
169             } else {
170                 return new SocketFactoryAdaptor(this.socketFactory);
171             }
172         }
173     }
174 
175     /**
176      * Obtains the socket factory.
177      * If this scheme is {@link #isLayered layered}, the factory implements
178      * {@link LayeredSocketFactory LayeredSchemeSocketFactory}.
179      *
180      * @return  the socket factory for this scheme
181      *
182      * @since 4.1
183      */
184     public final SchemeSocketFactory getSchemeSocketFactory() {
185         return this.socketFactory;
186     }
187 
188     /**
189      * Obtains the scheme name.
190      *
191      * @return  the name of this scheme, in lowercase
192      */
193     public final String getName() {
194         return name;
195     }
196 
197     /**
198      * Indicates whether this scheme allows for layered connections.
199      *
200      * @return {@code true} if layered connections are possible,
201      *         {@code false} otherwise
202      */
203     public final boolean isLayered() {
204         return layered;
205     }
206 
207     /**
208      * Resolves the correct port for this scheme.
209      * Returns the given port if it is valid, the default port otherwise.
210      *
211      * @param port      the port to be resolved,
212      *                  a negative number to obtain the default port
213      *
214      * @return the given port or the defaultPort
215      */
216     public final int resolvePort(final int port) {
217         return port <= 0 ? defaultPort : port;
218     }
219 
220     /**
221      * Return a string representation of this object.
222      *
223      * @return  a human-readable string description of this scheme
224      */
225     @Override
226     public final String toString() {
227         if (stringRep == null) {
228             final StringBuilder buffer = new StringBuilder();
229             buffer.append(this.name);
230             buffer.append(':');
231             buffer.append(Integer.toString(this.defaultPort));
232             stringRep = buffer.toString();
233         }
234         return stringRep;
235     }
236 
237     @Override
238     public final boolean equals(final Object obj) {
239         if (this == obj) {
240             return true;
241         }
242         if (obj instanceof Scheme) {
243             final Scheme that = (Scheme) obj;
244             return this.name.equals(that.name)
245                 && this.defaultPort == that.defaultPort
246                 && this.layered == that.layered;
247         } else {
248             return false;
249         }
250     }
251 
252     @Override
253     public int hashCode() {
254         int hash = LangUtils.HASH_SEED;
255         hash = LangUtils.hashCode(hash, this.defaultPort);
256         hash = LangUtils.hashCode(hash, this.name);
257         hash = LangUtils.hashCode(hash, this.layered);
258         return hash;
259     }
260 
261 }