Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/impl/io/SSLSocketFactory.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/io/SSLSocketFactory.java $
3    * $Revision: 376961 $
4    * $Date: 2006-02-11 11:32:50 +0100 (Sat, 11 Feb 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2002-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http.impl.io;
31  
32  import java.io.IOException;
33  import java.net.InetAddress;
34  import java.net.Socket;
35  import java.net.UnknownHostException;
36  
37  import org.apache.http.ConnectTimeoutException;
38  import org.apache.http.io.SecureSocketFactory;
39  import org.apache.http.params.HttpConnectionParams;
40  import org.apache.http.params.HttpParams;
41  
42  /**
43   * .A wrapper class for the standard JSSE SSLSocketFactory.
44   *   
45   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
46   */
47  
48  public class SSLSocketFactory implements SecureSocketFactory {
49  
50      /**
51       * The factory singleton.
52       */
53      private static final SSLSocketFactory DEFAULT_FACTORY = new SSLSocketFactory();
54      
55      /**
56       * Gets an singleton instance of the SSLProtocolSocketFactory.
57       * @return a SSLProtocolSocketFactory
58       */
59      public static SSLSocketFactory getSocketFactory() {
60          return DEFAULT_FACTORY;
61      }
62      
63      /**
64       * Attempts to get a new socket connection to the given host within the given time limit.
65       *  
66       * @param host the host name/IP
67       * @param port the port on the host
68       * @param localAddress the local host name/IP to bind the socket to
69       * @param localPort the port on the local machine
70       * @param params {@link HttpConnectionParams Http connection parameters}
71       * 
72       * @return Socket a new socket
73       * 
74       * @throws IOException if an I/O error occurs while creating the socket
75       * @throws UnknownHostException if the IP address of the host cannot be
76       * determined
77       * @throws ConnectTimeoutException if socket cannot be connected within the
78       *  given time limit
79       * 
80       * @since 3.0
81       */
82      public Socket createSocket(
83          final String host,
84          final int port,
85          final InetAddress localAddress,
86          final int localPort,
87          final HttpParams params
88      ) throws IOException, UnknownHostException, ConnectTimeoutException {
89          if (params == null) {
90              throw new IllegalArgumentException("Parameters may not be null");
91          }
92          int timeout = HttpConnectionParams.getConnectionTimeout(params);
93          if (timeout != 0) {
94              throw new IllegalStateException("Connection timeout is not supported in old IO mode");
95          }
96          javax.net.ssl.SSLSocketFactory socketfactory = 
97              (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault();
98          if (localAddress != null) {
99              return socketfactory.createSocket(host, port, localAddress, localPort);
100         } else {
101             return socketfactory.createSocket(host, port);
102         }
103     }
104 
105     // non-javadoc, see interface SocketFactory
106     public Socket createSocket(
107         Socket socket,
108         String host,
109         int port,
110         boolean autoClose)
111         throws IOException, UnknownHostException
112     {
113         javax.net.ssl.SSLSocketFactory socketfactory = 
114             (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault();
115         return socketfactory.createSocket(socket, host, port, autoClose);
116     }
117     
118 }