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