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