1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | 0 | super(); |
30 | 0 | socketFactory = factory; |
31 | 0 | } |
32 | |
|
33 | |
|
34 | |
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) |
35 | |
throws IOException, UnknownHostException |
36 | |
{ |
37 | 0 | return socketFactory.createSocket(socket, host, port, autoClose); |
38 | |
} |
39 | |
|
40 | |
public Socket createSocket(String host, int port) throws IOException, UnknownHostException |
41 | |
{ |
42 | 0 | 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 | 0 | 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 | 0 | int timeout = params.getConnectionTimeout(); |
55 | 0 | if (timeout == 0) |
56 | |
{ |
57 | 0 | return createSocket(host, port, localAddress, localPort); |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 0 | return createSocketWithTimeout(host, port, localAddress, localPort, timeout); |
62 | |
} |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
protected Socket createSocketWithTimeout(String host, int port, InetAddress localAddress, |
69 | |
int localPort, int timeout) throws IOException |
70 | |
{ |
71 | 0 | Socket socket = socketFactory.createSocket(); |
72 | 0 | SocketAddress local = new InetSocketAddress(localAddress, localPort); |
73 | 0 | SocketAddress remote = new InetSocketAddress(host, port); |
74 | |
|
75 | 0 | socket.bind(local); |
76 | 0 | socket.connect(remote, timeout); |
77 | 0 | return socket; |
78 | |
} |
79 | |
} |