1 package org.apache.maven.continuum.project.builder;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.net.Socket;
26 import java.net.UnknownHostException;
27
28 import javax.net.ssl.SSLContext;
29 import javax.net.ssl.SSLSocket;
30 import javax.net.ssl.TrustManager;
31
32 import org.apache.http.conn.ConnectTimeoutException;
33 import org.apache.http.conn.scheme.LayeredSocketFactory;
34 import org.apache.http.conn.scheme.SocketFactory;
35 import org.apache.http.params.HttpConnectionParams;
36 import org.apache.http.params.HttpParams;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46
47 public class EasySSLSocketFactory
48 implements SocketFactory, LayeredSocketFactory
49 {
50 private static final Logger log = LoggerFactory.getLogger( EasySSLSocketFactory.class );
51
52 private SSLContext sslcontext = null;
53
54 private static SSLContext createEasySSLContext()
55 throws IOException
56 {
57 try
58 {
59 SSLContext context = SSLContext.getInstance( "SSL" );
60 context.init( null, new TrustManager[]{new EasyX509TrustManager( null )}, null );
61 return context;
62 }
63 catch ( Exception e )
64 {
65 LoggerFactory.getLogger( EasySSLSocketFactory.class ).error( e.getMessage(), e );
66 throw new IOException( e.getMessage() );
67 }
68 }
69
70 private SSLContext getSSLContext()
71 throws IOException
72 {
73 if ( this.sslcontext == null )
74 {
75 this.sslcontext = createEasySSLContext();
76 }
77 return this.sslcontext;
78 }
79
80
81
82
83 public Socket connectSocket( Socket sock, String host, int port, InetAddress localAddress, int localPort,
84 HttpParams params )
85 throws IOException, UnknownHostException, ConnectTimeoutException
86 {
87 int connTimeout = HttpConnectionParams.getConnectionTimeout( params );
88 int soTimeout = HttpConnectionParams.getSoTimeout( params );
89
90 InetSocketAddress remoteAddress = new InetSocketAddress( host, port );
91 SSLSocket sslsock = (SSLSocket) ( ( sock != null ) ? sock : createSocket() );
92
93 if ( ( localAddress != null ) || ( localPort > 0 ) )
94 {
95
96 if ( localPort < 0 )
97 {
98 localPort = 0;
99 }
100 InetSocketAddress isa = new InetSocketAddress( localAddress, localPort );
101 sslsock.bind( isa );
102 }
103
104 sslsock.connect( remoteAddress, connTimeout );
105 sslsock.setSoTimeout( soTimeout );
106 return sslsock;
107
108 }
109
110
111
112
113 public Socket createSocket()
114 throws IOException
115 {
116 if ( log.isDebugEnabled() )
117 {
118 log.debug( "create socket" );
119 }
120 return getSSLContext().getSocketFactory().createSocket();
121 }
122
123
124
125
126 public boolean isSecure( Socket socket )
127 throws IllegalArgumentException
128 {
129 return true;
130 }
131
132
133
134
135 public Socket createSocket( Socket socket, String host, int port, boolean autoClose )
136 throws IOException, UnknownHostException
137 {
138 if ( log.isDebugEnabled() )
139 {
140 log.debug( "create socket host " + host + ", port " + port );
141 }
142 return getSSLContext().getSocketFactory().createSocket();
143 }
144
145
146
147
148
149
150
151 public boolean equals( Object obj )
152 {
153 return ( ( obj != null ) && obj.getClass().equals( EasySSLSocketFactory.class ) );
154 }
155
156 public int hashCode()
157 {
158 return EasySSLSocketFactory.class.hashCode();
159 }
160
161
162 }