1 | package org.apache.maven.continuum.project.builder; |
2 | |
3 | /* |
4 | * Licensed to the Apache Software Foundation (ASF) under one |
5 | * or more contributor license agreements. See the NOTICE file |
6 | * distributed with this work for additional information |
7 | * regarding copyright ownership. The ASF licenses this file |
8 | * to you under the Apache License, Version 2.0 (the |
9 | * "License"); you may not use this file except in compliance |
10 | * with the License. You may obtain a copy of the License at |
11 | * |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | * |
14 | * Unless required by applicable law or agreed to in writing, |
15 | * software distributed under the License is distributed on an |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
17 | * KIND, either express or implied. See the License for the |
18 | * specific language governing permissions and limitations |
19 | * under the License. |
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 | * This socket factory will create ssl socket that accepts self signed certificate |
42 | * |
43 | * @author olamy |
44 | * @version $Id: EasySSLSocketFactory.java 764863 2009-04-14 16:28:12Z evenisse $ |
45 | * @since 1.2.3 |
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 | * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, java.lang.String, int, java.net.InetAddress, int, org.apache.http.params.HttpParams) |
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 | // we need to bind explicitly |
96 | if ( localPort < 0 ) |
97 | { |
98 | localPort = 0; // indicates "any" |
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 | * @see org.apache.http.conn.scheme.SocketFactory#createSocket() |
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 | * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket) |
125 | */ |
126 | public boolean isSecure( Socket socket ) |
127 | throws IllegalArgumentException |
128 | { |
129 | return true; |
130 | } |
131 | |
132 | /** |
133 | * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket, java.lang.String, int, boolean) |
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 | // javadoc in org.apache.http.conn.scheme.SocketFactory says : |
147 | // Both Object.equals() and Object.hashCode() must be overridden |
148 | // for the correct operation of some connection managers |
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 | } |