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  import org.apache.http.conn.ConnectTimeoutException;
39  import org.apache.http.conn.DnsResolver;
40  import org.apache.http.params.HttpConnectionParams;
41  import org.apache.http.params.HttpParams;
42  import org.apache.http.util.Args;
43  
44  /**
45   * The default class for creating plain (unencrypted) sockets.
46   *
47   * @since 4.0
48   *
49   * @deprecated (4.3) use {@link org.apache.http.conn.socket.PlainSocketFactory}
50   */
51  @Immutable
52  @Deprecated
53  public class PlainSocketFactory implements SocketFactory, SchemeSocketFactory {
54  
55      private final HostNameResolver nameResolver;
56  
57      /**
58       * Gets the default factory.
59       *
60       * @return the default factory
61       */
62      public static PlainSocketFactory getSocketFactory() {
63          return new PlainSocketFactory();
64      }
65  
66      /**
67       * @deprecated (4.1) use {@link DnsResolver}
68       */
69      @Deprecated
70      public PlainSocketFactory(final HostNameResolver nameResolver) {
71          super();
72          this.nameResolver = nameResolver;
73      }
74  
75      public PlainSocketFactory() {
76          super();
77          this.nameResolver = null;
78      }
79  
80      /**
81       * @param params Optional parameters. Parameters passed to this method will have no effect.
82       *               This method will create a unconnected instance of {@link Socket} class
83       *               using default constructor.
84       *
85       * @since 4.1
86       */
87      public Socket createSocket(final HttpParams params) {
88          return new Socket();
89      }
90  
91      public Socket createSocket() {
92          return new Socket();
93      }
94  
95      /**
96       * @since 4.1
97       */
98      public Socket connectSocket(
99              final Socket socket,
100             final InetSocketAddress remoteAddress,
101             final InetSocketAddress localAddress,
102             final HttpParams params) throws IOException, ConnectTimeoutException {
103         Args.notNull(remoteAddress, "Remote address");
104         Args.notNull(params, "HTTP parameters");
105         Socket sock = socket;
106         if (sock == null) {
107             sock = createSocket();
108         }
109         if (localAddress != null) {
110             sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
111             sock.bind(localAddress);
112         }
113         final int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
114         final int soTimeout = HttpConnectionParams.getSoTimeout(params);
115 
116         try {
117             sock.setSoTimeout(soTimeout);
118             sock.connect(remoteAddress, connTimeout);
119         } catch (final SocketTimeoutException ex) {
120             throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
121         }
122         return sock;
123     }
124 
125     /**
126      * Checks whether a socket connection is secure.
127      * This factory creates plain socket connections
128      * which are not considered secure.
129      *
130      * @param sock      the connected socket
131      *
132      * @return  <code>false</code>
133      */
134     public final boolean isSecure(final Socket sock) {
135         return false;
136     }
137 
138     /**
139      * @deprecated (4.1)  Use {@link #connectSocket(Socket, InetSocketAddress, InetSocketAddress, HttpParams)}
140      */
141     @Deprecated
142     public Socket connectSocket(
143             final Socket socket,
144             final String host, final int port,
145             final InetAddress localAddress, int localPort,
146             final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
147         InetSocketAddress local = null;
148         if (localAddress != null || localPort > 0) {
149             // we need to bind explicitly
150             if (localPort < 0) {
151                 localPort = 0; // indicates "any"
152             }
153             local = new InetSocketAddress(localAddress, localPort);
154         }
155         InetAddress remoteAddress;
156         if (this.nameResolver != null) {
157             remoteAddress = this.nameResolver.resolve(host);
158         } else {
159             remoteAddress = InetAddress.getByName(host);
160         }
161         final InetSocketAddress remote = new InetSocketAddress(remoteAddress, port);
162         return connectSocket(socket, remote, local, params);
163     }
164 
165 }