1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.apache.commons.httpclient.protocol;
33
34 import java.io.IOException;
35 import java.net.InetAddress;
36 import java.net.Socket;
37 import java.net.UnknownHostException;
38
39 import javax.net.ssl.SSLSocketFactory;
40
41 /***
42 * A SecureProtocolSocketFactory that uses JSSE to create sockets.
43 *
44 * @author Michael Becke
45 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
46 *
47 * @since 2.0
48 */
49 public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
50
51 /***
52 * The factory singleton.
53 */
54 private static final SSLProtocolSocketFactory factory = new SSLProtocolSocketFactory();
55
56 /***
57 * Gets an singleton instance of the SSLProtocolSocketFactory.
58 * @return a SSLProtocolSocketFactory
59 */
60 static SSLProtocolSocketFactory getSocketFactory() {
61 return factory;
62 }
63
64 /***
65 * Constructor for SSLProtocolSocketFactory.
66 */
67 public SSLProtocolSocketFactory() {
68 super();
69 }
70
71 /***
72 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
73 */
74 public Socket createSocket(
75 String host,
76 int port,
77 InetAddress clientHost,
78 int clientPort)
79 throws IOException, UnknownHostException {
80 return SSLSocketFactory.getDefault().createSocket(
81 host,
82 port,
83 clientHost,
84 clientPort
85 );
86 }
87
88 /***
89 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
90 */
91 public Socket createSocket(String host, int port)
92 throws IOException, UnknownHostException {
93 return SSLSocketFactory.getDefault().createSocket(
94 host,
95 port
96 );
97 }
98
99 /***
100 * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
101 */
102 public Socket createSocket(
103 Socket socket,
104 String host,
105 int port,
106 boolean autoClose)
107 throws IOException, UnknownHostException {
108 return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(
109 socket,
110 host,
111 port,
112 autoClose
113 );
114 }
115
116 /***
117 * All instances of SSLProtocolSocketFactory are the same.
118 */
119 public boolean equals(Object obj) {
120 return ((obj != null) && obj.getClass().equals(SSLProtocolSocketFactory.class));
121 }
122
123 /***
124 * All instances of SSLProtocolSocketFactory have the same hash code.
125 */
126 public int hashCode() {
127 return SSLProtocolSocketFactory.class.hashCode();
128 }
129
130 }