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  
28  package org.apache.http.conn.scheme;
29  
30  import java.io.IOException;
31  import java.net.InetAddress;
32  import java.net.InetSocketAddress;
33  import java.net.Socket;
34  import java.net.SocketTimeoutException;
35  import java.net.UnknownHostException;
36  
37  import org.apache.http.annotation.Immutable;
38  
39  import org.apache.http.conn.ConnectTimeoutException;
40  import org.apache.http.conn.DnsResolver;
41  import org.apache.http.params.HttpConnectionParams;
42  import org.apache.http.params.HttpParams;
43  
44  /**
45   * The default class for creating plain (unencrypted) sockets.
46   * <p>
47   * The following parameters can be used to customize the behavior of this
48   * class:
49   * <ul>
50   *  <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
51   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_REUSEADDR}</li>
52   * </ul>
53   *
54   * @since 4.0
55   */
56  @SuppressWarnings("deprecation")
57  @Immutable
58  public class PlainSocketFactory implements SocketFactory, SchemeSocketFactory {
59  
60      private final HostNameResolver nameResolver;
61  
62      /**
63       * Gets the default factory.
64       *
65       * @return the default factory
66       */
67      public static PlainSocketFactory getSocketFactory() {
68          return new PlainSocketFactory();
69      }
70  
71      /**
72       * @deprecated (4.1) use {@link DnsResolver}
73       */
74      @Deprecated
75      public PlainSocketFactory(final HostNameResolver nameResolver) {
76          super();
77          this.nameResolver = nameResolver;
78      }
79  
80      public PlainSocketFactory() {
81          super();
82          this.nameResolver = null;
83      }
84  
85      /**
86       * @param params Optional parameters. Parameters passed to this method will have no effect.
87       *               This method will create a unconnected instance of {@link Socket} class
88       *               using default constructor.
89       *
90       * @since 4.1
91       */
92      public Socket createSocket(final HttpParams params) {
93          return new Socket();
94      }
95  
96      public Socket createSocket() {
97          return new Socket();
98      }
99  
100     /**
101      * @since 4.1
102      */
103     public Socket connectSocket(
104             final Socket socket,
105             final InetSocketAddress remoteAddress,
106             final InetSocketAddress localAddress,
107             final HttpParams params) throws IOException, ConnectTimeoutException {
108         if (remoteAddress == null) {
109             throw new IllegalArgumentException("Remote address may not be null");
110         }
111         if (params == null) {
112             throw new IllegalArgumentException("HTTP parameters may not be null");
113         }
114         Socket sock = socket;
115         if (sock == null) {
116             sock = createSocket();
117         }
118         if (localAddress != null) {
119             sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
120             sock.bind(localAddress);
121         }
122         int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
123         int soTimeout = HttpConnectionParams.getSoTimeout(params);
124 
125         try {
126             sock.setSoTimeout(soTimeout);
127             sock.connect(remoteAddress, connTimeout);
128         } catch (SocketTimeoutException ex) {
129             throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
130         }
131         return sock;
132     }
133 
134     /**
135      * Checks whether a socket connection is secure.
136      * This factory creates plain socket connections
137      * which are not considered secure.
138      *
139      * @param sock      the connected socket
140      *
141      * @return  <code>false</code>
142      *
143      * @throws IllegalArgumentException if the argument is invalid
144      */
145     public final boolean isSecure(Socket sock)
146         throws IllegalArgumentException {
147 
148         if (sock == null) {
149             throw new IllegalArgumentException("Socket may not be null.");
150         }
151         // This check is performed last since it calls a method implemented
152         // by the argument object. getClass() is final in java.lang.Object.
153         if (sock.isClosed()) {
154             throw new IllegalArgumentException("Socket is closed.");
155         }
156         return false;
157     }
158 
159     /**
160      * @deprecated (4.1)  Use {@link #connectSocket(Socket, InetSocketAddress, InetSocketAddress, HttpParams)}
161      */
162     @Deprecated
163     public Socket connectSocket(
164             final Socket socket,
165             final String host, int port,
166             final InetAddress localAddress, int localPort,
167             final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
168         InetSocketAddress local = null;
169         if (localAddress != null || localPort > 0) {
170             // we need to bind explicitly
171             if (localPort < 0) {
172                 localPort = 0; // indicates "any"
173             }
174             local = new InetSocketAddress(localAddress, localPort);
175         }
176         InetAddress remoteAddress;
177         if (this.nameResolver != null) {
178             remoteAddress = this.nameResolver.resolve(host);
179         } else {
180             remoteAddress = InetAddress.getByName(host);
181         }
182         InetSocketAddress remote = new InetSocketAddress(remoteAddress, port);
183         return connectSocket(socket, remote, local, params);
184     }
185 
186 }