13bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath/* 23bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/SocketFactory.java $ 33bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * $Revision: 645850 $ 43bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * $Date: 2008-04-08 04:08:52 -0700 (Tue, 08 Apr 2008) $ 53bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 63bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * ==================================================================== 73bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * Licensed to the Apache Software Foundation (ASF) under one 83bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * or more contributor license agreements. See the NOTICE file 93bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * distributed with this work for additional information 103bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * regarding copyright ownership. The ASF licenses this file 113bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * to you under the Apache License, Version 2.0 (the 123bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * "License"); you may not use this file except in compliance 133bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * with the License. You may obtain a copy of the License at 143bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 153bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * http://www.apache.org/licenses/LICENSE-2.0 163bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 173bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * Unless required by applicable law or agreed to in writing, 183bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * software distributed under the License is distributed on an 193bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 203bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * KIND, either express or implied. See the License for the 213bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * specific language governing permissions and limitations 223bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * under the License. 233bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * ==================================================================== 243bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 253bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * This software consists of voluntary contributions made by many 263bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * individuals on behalf of the Apache Software Foundation. For more 273bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * information on the Apache Software Foundation, please see 283bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * <http://www.apache.org/>. 293bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 303bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath */ 313bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 323bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamathpackage org.apache.http.conn.scheme; 333bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 343bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamathimport java.io.IOException; 353bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamathimport java.net.InetAddress; 363bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamathimport java.net.Socket; 373bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamathimport java.net.UnknownHostException; 383bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 393bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamathimport org.apache.http.conn.ConnectTimeoutException; 403bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamathimport org.apache.http.params.HttpParams; 413bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 423bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath/** 433bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * A factory for creating and connecting sockets. 443bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * The factory encapsulates the logic for establishing a socket connection. 453bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * <br/> 463bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * Both {@link java.lang.Object#equals(java.lang.Object) Object.equals()} 473bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * and {@link java.lang.Object#hashCode() Object.hashCode()} 483bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * must be overridden for the correct operation of some connection managers. 493bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 503bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @author <a href="mailto:rolandw at apache.org">Roland Weber</a> 513bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @author Michael Becke 523bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 533bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 543bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @deprecated Please use {@link java.net.URL#openConnection} instead. 553bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 563bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * for further details. 573bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath */ 583bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath@Deprecated 593bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamathpublic interface SocketFactory { 603bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 613bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath /** 623bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * Creates a new, unconnected socket. 633bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * The socket should subsequently be passed to 643bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * {@link #connectSocket connectSocket}. 653bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 663bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @return a new socket 673bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 683bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @throws IOException if an I/O error occurs while creating the socket 693bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath */ 703bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath Socket createSocket() 713bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath throws IOException 723bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath ; 733bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 743bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 753bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath /** 763bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * Connects a socket to the given host. 773bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 783bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @param sock the socket to connect, as obtained from 793bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * {@link #createSocket createSocket}. 803bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * <code>null</code> indicates that a new socket 813bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * should be created and connected. 823bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @param host the host to connect to 833bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @param port the port to connect to on the host 843bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @param localAddress the local address to bind the socket to, or 853bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * <code>null</code> for any 863bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @param localPort the port on the local machine, 873bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 0 or a negative number for any 883bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @param params additional {@link HttpParams parameters} for connecting 893bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 903bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @return the connected socket. The returned object may be different 913bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * from the <code>sock</code> argument if this factory supports 923bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * a layered protocol. 933bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 943bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @throws IOException if an I/O error occurs 953bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @throws UnknownHostException if the IP address of the target host 963bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * can not be determined 973bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @throws ConnectTimeoutException if the socket cannot be connected 983bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * within the time limit defined in the <code>params</code> 993bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath */ 1003bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath Socket connectSocket( 1013bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath Socket sock, 1023bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath String host, 1033bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath int port, 1043bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath InetAddress localAddress, 1053bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath int localPort, 1063bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath HttpParams params 1073bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath ) throws IOException, UnknownHostException, ConnectTimeoutException; 1083bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 1093bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 1103bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath /** 1113bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * Checks whether a socket provides a secure connection. 1123bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * The socket must be {@link #connectSocket connected} 1133bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * by this factory. 1143bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * The factory will <i>not</i> perform I/O operations 1153bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * in this method. 1163bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * <br/> 1173bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * As a rule of thumb, plain sockets are not secure and 1183bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * TLS/SSL sockets are secure. However, there may be 1193bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * application specific deviations. For example, a plain 1203bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * socket to a host in the same intranet ("trusted zone") 1213bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * could be considered secure. On the other hand, a 1223bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * TLS/SSL socket could be considered insecure based on 1233bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * the cypher suite chosen for the connection. 1243bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 1253bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @param sock the connected socket to check 1263bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 1273bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @return <code>true</code> if the connection of the socket 1283bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * should be considered secure, or 1293bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * <code>false</code> if it should not 1303bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * 1313bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * @throws IllegalArgumentException 1323bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * if the argument is invalid, for example because it is 1333bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * not a connected socket or was created by a different 1343bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * socket factory. 1353bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * Note that socket factories are <i>not</i> required to 1363bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * check these conditions, they may simply return a default 1373bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath * value when called with an invalid socket argument. 1383bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath */ 1393bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath boolean isSecure(Socket sock) 1403bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath throws IllegalArgumentException 1413bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath ; 1423bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath 1433bdd327f8532a79b83f575cc62e8eb09a1f93f3dNarayan Kamath} 144