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