View Javadoc

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 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   * This socket factory will create ssl socket that accepts self signed certificate
41   *
42   * @author olamy
43   * @version $Id: EasySSLSocketFactory.java 1372260 2012-08-13 04:29:09Z brett $
44   * @since 1.2.3
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       * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, java.lang.String, int, java.net.InetAddress, int, org.apache.http.params.HttpParams)
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              // we need to bind explicitly
95              if ( localPort < 0 )
96              {
97                  localPort = 0; // indicates "any"
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      * @see org.apache.http.conn.scheme.SocketFactory#createSocket()
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      * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket)
124      */
125     public boolean isSecure( Socket socket )
126         throws IllegalArgumentException
127     {
128         return true;
129     }
130 
131     /**
132      * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket, java.lang.String, int, boolean)
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     //  javadoc in org.apache.http.conn.scheme.SocketFactory says :
146     //  Both Object.equals() and Object.hashCode() must be overridden 
147     //  for the correct operation of some connection managers
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 }