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   * <pre>
42   * Scheme https = new Scheme("https", 443, new MySecureSocketFactory());
43   * SchemeRegistry registry = new SchemeRegistry();
44   * registry.register(https);
45   * </pre>
46   *
47   * @since 4.0
48   *
49   * @deprecated (4.3) use {@link org.apache.http.conn.SchemePortResolver} for default port
50   * resolution and {@link org.apache.http.config.Registry} for socket factory lookups.
51   */
52  @Immutable
53  @Deprecated
54  public final class Scheme {
55  
56      /** The name of this scheme, in lowercase. (e.g. http, https) */
57      private final String name;
58  
59      /** The socket factory for this scheme */
60      private final SchemeSocketFactory socketFactory;
61  
62      /** The default port for this scheme */
63      private final int defaultPort;
64  
65      /** Indicates whether this scheme allows for layered connections */
66      private final boolean layered;
67  
68      /** A string representation, for {@link #toString toString}. */
69      private String stringRep;
70      /*
71       *  This is used to cache the result of the toString() method
72       *  Since the method always generates the same value, there's no
73       *  need to synchronize, and it does not affect immutability.
74      */
75  
76      /**
77       * Creates a new scheme.
78       * Whether the created scheme allows for layered connections
79       * depends on the class of <code>factory</code>.
80       *
81       * @param name      the scheme name, for example "http".
82       *                  The name will be converted to lowercase.
83       * @param port      the default port for this scheme
84       * @param factory   the factory for creating sockets for communication
85       *                  with this scheme
86       *
87       * @since 4.1
88       */
89      public Scheme(final String name, final int port, final SchemeSocketFactory factory) {
90          Args.notNull(name, "Scheme name");
91          Args.check(port > 0 && port <= 0xffff, "Port is invalid");
92          Args.notNull(factory, "Socket factory");
93          this.name = name.toLowerCase(Locale.ENGLISH);
94          this.defaultPort = port;
95          if (factory instanceof SchemeLayeredSocketFactory) {
96              this.layered = true;
97              this.socketFactory = factory;
98          } else if (factory instanceof LayeredSchemeSocketFactory) {
99              this.layered = true;
100             this.socketFactory = new SchemeLayeredSocketFactoryAdaptor2((LayeredSchemeSocketFactory) factory);
101         } else {
102             this.layered = false;
103             this.socketFactory = factory;
104         }
105     }
106 
107     /**
108      * Creates a new scheme.
109      * Whether the created scheme allows for layered connections
110      * depends on the class of <code>factory</code>.
111      *
112      * @param name      the scheme name, for example "http".
113      *                  The name will be converted to lowercase.
114      * @param factory   the factory for creating sockets for communication
115      *                  with this scheme
116      * @param port      the default port for this scheme
117      *
118      * @deprecated (4.1)  Use {@link #Scheme(String, int, SchemeSocketFactory)}
119      */
120     @Deprecated
121     public Scheme(final String name,
122                   final SocketFactory factory,
123                   final int port) {
124 
125         Args.notNull(name, "Scheme name");
126         Args.notNull(factory, "Socket factory");
127         Args.check(port > 0 && port <= 0xffff, "Port is invalid");
128 
129         this.name = name.toLowerCase(Locale.ENGLISH);
130         if (factory instanceof LayeredSocketFactory) {
131             this.socketFactory = new SchemeLayeredSocketFactoryAdaptor(
132                     (LayeredSocketFactory) factory);
133             this.layered = true;
134         } else {
135             this.socketFactory = new SchemeSocketFactoryAdaptor(factory);
136             this.layered = false;
137         }
138         this.defaultPort = port;
139     }
140 
141     /**
142      * Obtains the default port.
143      *
144      * @return  the default port for this scheme
145      */
146     public final int getDefaultPort() {
147         return defaultPort;
148     }
149 
150 
151     /**
152      * Obtains the socket factory.
153      * If this scheme is {@link #isLayered layered}, the factory implements
154      * {@link LayeredSocketFactory LayeredSocketFactory}.
155      *
156      * @return  the socket factory for this scheme
157      *
158      * @deprecated (4.1)  Use {@link #getSchemeSocketFactory()}
159      */
160     @Deprecated
161     public final SocketFactory getSocketFactory() {
162         if (this.socketFactory instanceof SchemeSocketFactoryAdaptor) {
163             return ((SchemeSocketFactoryAdaptor) this.socketFactory).getFactory();
164         } else {
165             if (this.layered) {
166                 return new LayeredSocketFactoryAdaptor(
167                         (LayeredSchemeSocketFactory) this.socketFactory);
168             } else {
169                 return new SocketFactoryAdaptor(this.socketFactory);
170             }
171         }
172     }
173 
174     /**
175      * Obtains the socket factory.
176      * If this scheme is {@link #isLayered layered}, the factory implements
177      * {@link LayeredSocketFactory LayeredSchemeSocketFactory}.
178      *
179      * @return  the socket factory for this scheme
180      *
181      * @since 4.1
182      */
183     public final SchemeSocketFactory getSchemeSocketFactory() {
184         return this.socketFactory;
185     }
186 
187     /**
188      * Obtains the scheme name.
189      *
190      * @return  the name of this scheme, in lowercase
191      */
192     public final String getName() {
193         return name;
194     }
195 
196     /**
197      * Indicates whether this scheme allows for layered connections.
198      *
199      * @return <code>true</code> if layered connections are possible,
200      *         <code>false</code> otherwise
201      */
202     public final boolean isLayered() {
203         return layered;
204     }
205 
206     /**
207      * Resolves the correct port for this scheme.
208      * Returns the given port if it is valid, the default port otherwise.
209      *
210      * @param port      the port to be resolved,
211      *                  a negative number to obtain the default port
212      *
213      * @return the given port or the defaultPort
214      */
215     public final int resolvePort(final int port) {
216         return port <= 0 ? defaultPort : port;
217     }
218 
219     /**
220      * Return a string representation of this object.
221      *
222      * @return  a human-readable string description of this scheme
223      */
224     @Override
225     public final String toString() {
226         if (stringRep == null) {
227             final StringBuilder buffer = new StringBuilder();
228             buffer.append(this.name);
229             buffer.append(':');
230             buffer.append(Integer.toString(this.defaultPort));
231             stringRep = buffer.toString();
232         }
233         return stringRep;
234     }
235 
236     @Override
237     public final boolean equals(final Object obj) {
238         if (this == obj) {
239             return true;
240         }
241         if (obj instanceof Scheme) {
242             final Scheme that = (Scheme) obj;
243             return this.name.equals(that.name)
244                 && this.defaultPort == that.defaultPort
245                 && this.layered == that.layered;
246         } else {
247             return false;
248         }
249     }
250 
251     @Override
252     public int hashCode() {
253         int hash = LangUtils.HASH_SEED;
254         hash = LangUtils.hashCode(hash, this.defaultPort);
255         hash = LangUtils.hashCode(hash, this.name);
256         hash = LangUtils.hashCode(hash, this.layered);
257         return hash;
258     }
259 
260 }