1 package eu
.alefzero
.owncloud
.authenticator
;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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
;
28 import javax
.net
.ssl
.SSLContext
;
29 import javax
.net
.ssl
.SSLSocket
;
30 import javax
.net
.ssl
.TrustManager
;
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
;
39 * This socket factory will create ssl socket that accepts self signed
43 * @version $Id: EasySSLSocketFactory.java 765355 2009-04-15 20:59:07Z evenisse
47 public class EasySSLSocketFactory
implements SocketFactory
,
48 LayeredSocketFactory
{
50 private SSLContext sslcontext
= null
;
52 private static SSLContext
createEasySSLContext() throws IOException
{
54 SSLContext context
= SSLContext
.getInstance("TLS");
55 context
.init(null
, new TrustManager
[] { new EasyX509TrustManager(
58 } catch (Exception e
) {
59 throw new IOException(e
.getMessage());
63 private SSLContext
getSSLContext() throws IOException
{
64 if (this.sslcontext
== null
) {
65 this.sslcontext
= createEasySSLContext();
67 return this.sslcontext
;
71 * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket,
72 * java.lang.String, int, java.net.InetAddress, int,
73 * org.apache.http.params.HttpParams)
75 public Socket
connectSocket(Socket sock
, String host
, int port
,
76 InetAddress localAddress
, int localPort
, HttpParams params
)
77 throws IOException
, UnknownHostException
, ConnectTimeoutException
{
78 int connTimeout
= HttpConnectionParams
.getConnectionTimeout(params
);
79 int soTimeout
= HttpConnectionParams
.getSoTimeout(params
);
81 InetSocketAddress remoteAddress
= new InetSocketAddress(host
, port
);
82 SSLSocket sslsock
= (SSLSocket
) ((sock
!= null
) ? sock
: createSocket());
84 if ((localAddress
!= null
) || (localPort
> 0)) {
85 // we need to bind explicitly
87 localPort
= 0; // indicates "any"
89 InetSocketAddress isa
= new InetSocketAddress(localAddress
,
94 sslsock
.connect(remoteAddress
, connTimeout
);
95 sslsock
.setSoTimeout(soTimeout
);
101 * @see org.apache.http.conn.scheme.SocketFactory#createSocket()
103 public Socket
createSocket() throws IOException
{
104 return getSSLContext().getSocketFactory().createSocket();
108 * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket)
110 public boolean isSecure(Socket socket
) throws IllegalArgumentException
{
115 * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket,
116 * java.lang.String, int, boolean)
118 public Socket
createSocket(Socket socket
, String host
, int port
,
119 boolean autoClose
) throws IOException
, UnknownHostException
{
120 return getSSLContext().getSocketFactory().createSocket(socket
, host
, port
, autoClose
);
123 // -------------------------------------------------------------------
124 // javadoc in org.apache.http.conn.scheme.SocketFactory says :
125 // Both Object.equals() and Object.hashCode() must be overridden
126 // for the correct operation of some connection managers
127 // -------------------------------------------------------------------
129 public boolean equals(Object obj
) {
130 return ((obj
!= null
) && obj
.getClass().equals(
131 EasySSLSocketFactory
.class));
134 public int hashCode() {
135 return EasySSLSocketFactory
.class.hashCode();