View Javadoc

1   /*
2    * $Id: MuleSecureProtocolSocketFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.http;
12  
13  import java.io.IOException;
14  import java.net.InetAddress;
15  import java.net.InetSocketAddress;
16  import java.net.Socket;
17  import java.net.SocketAddress;
18  import java.net.UnknownHostException;
19  
20  import javax.net.ssl.SSLSocketFactory;
21  
22  import org.apache.commons.httpclient.ConnectTimeoutException;
23  import org.apache.commons.httpclient.params.HttpConnectionParams;
24  import org.apache.commons.httpclient.protocol.ReflectionSocketFactory;
25  import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
26  
27  public class MuleSecureProtocolSocketFactory implements SecureProtocolSocketFactory
28  {
29      private SSLSocketFactory socketFactory;
30  
31      public MuleSecureProtocolSocketFactory(SSLSocketFactory factory)
32      {
33          super();
34          socketFactory = factory;
35      }
36  
37  
38      public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
39          throws IOException, UnknownHostException
40      {
41          return socketFactory.createSocket(socket, host, port, autoClose);
42      }
43  
44      public Socket createSocket(String host, int port) throws IOException, UnknownHostException
45      {
46          return socketFactory.createSocket(host, port);
47      }
48  
49      public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
50          throws IOException, UnknownHostException
51      {
52          return socketFactory.createSocket(host, port, localAddress, localPort);
53      }
54  
55      public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
56          HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException
57      {
58          int timeout = params.getConnectionTimeout();
59          if (timeout == 0) 
60          {
61              return createSocket(host, port, localAddress, localPort);
62          } 
63          else 
64          {
65              return createSocketWithTimeout(host, port, localAddress, localPort, timeout);
66          }
67      }
68  
69      /**
70       * This is a direct version of code in {@link ReflectionSocketFactory}.
71       */
72      protected Socket createSocketWithTimeout(String host, int port, InetAddress localAddress,
73          int localPort, int timeout) throws IOException
74      {
75          Socket socket = socketFactory.createSocket();
76          SocketAddress local = new InetSocketAddress(localAddress, localPort);
77          SocketAddress remote = new InetSocketAddress(host, port);
78          
79          socket.bind(local);
80          socket.connect(remote, timeout);
81          return socket;
82      }
83  }