src/org/gege/caldavsyncadapter/caldav/CopyOfEasySSLSocketFactory.java

changeset 0
fb9019fb1bf7
equal deleted inserted replaced
-1:000000000000 0:828b81e76142
1 /**
2 * Copyright (c) 2012-2013, Gerald Garcia
3 *
4 * This file is part of Andoid Caldav Sync Adapter Free.
5 *
6 * Andoid Caldav Sync Adapter Free is free software: you can redistribute
7 * it and/or modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation, either version 3 of the
9 * License, or at your option any later version.
10 *
11 * Andoid Caldav Sync Adapter Free is distributed in the hope that
12 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Andoid Caldav Sync Adapter Free.
18 * If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package org.gege.caldavsyncadapter.caldav;
23
24 /*
25 * Licensed to the Apache Software Foundation (ASF) under one
26 * or more contributor license agreements. See the NOTICE file
27 * distributed with this work for additional information
28 * regarding copyright ownership. The ASF licenses this file
29 * to you under the Apache License, Version 2.0 (the
30 * "License"); you may not use this file except in compliance
31 * with the License. You may obtain a copy of the License at
32 *
33 * http://www.apache.org/licenses/LICENSE-2.0
34 *
35 * Unless required by applicable law or agreed to in writing,
36 * software distributed under the License is distributed on an
37 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
38 * KIND, either express or implied. See the License for the
39 * specific language governing permissions and limitations
40 * under the License.
41 */
42
43 import java.io.IOException;
44 import java.net.InetAddress;
45 import java.net.InetSocketAddress;
46 import java.net.Socket;
47 import java.net.UnknownHostException;
48
49 import javax.net.ssl.SSLContext;
50 import javax.net.ssl.SSLSocket;
51 import javax.net.ssl.TrustManager;
52
53 import org.apache.http.conn.ConnectTimeoutException;
54 import org.apache.http.conn.scheme.LayeredSocketFactory;
55 import org.apache.http.conn.scheme.SocketFactory;
56 import org.apache.http.params.HttpConnectionParams;
57 import org.apache.http.params.HttpParams;
58
59 /**
60 * This socket factory will create ssl socket that accepts self signed
61 * certificate
62 *
63 * @author olamy
64 * @version $Id: EasySSLSocketFactory.java 765355 2009-04-15 20:59:07Z evenisse
65 * $
66 * @since 1.2.3
67 */
68 public class CopyOfEasySSLSocketFactory implements SocketFactory,
69 LayeredSocketFactory {
70
71 private SSLContext sslcontext = null;
72
73 private static SSLContext createEasySSLContext() throws IOException {
74 try {
75 SSLContext context = SSLContext.getInstance("TLS");
76 context.init(null, new TrustManager[] { new EasyX509TrustManager(
77 null) }, null);
78 return context;
79 } catch (Exception e) {
80 throw new IOException(e.getMessage());
81 }
82 }
83
84 private SSLContext getSSLContext() throws IOException {
85 if (this.sslcontext == null) {
86 this.sslcontext = createEasySSLContext();
87 }
88 return this.sslcontext;
89 }
90
91 /**
92 * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket,
93 * java.lang.String, int, java.net.InetAddress, int,
94 * org.apache.http.params.HttpParams)
95 */
96 public Socket connectSocket(Socket sock, String host, int port,
97 InetAddress localAddress, int localPort, HttpParams params)
98 throws IOException, UnknownHostException, ConnectTimeoutException {
99 int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
100 int soTimeout = HttpConnectionParams.getSoTimeout(params);
101
102 InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
103 SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());
104
105 if ((localAddress != null) || (localPort > 0)) {
106 // we need to bind explicitly
107 if (localPort < 0) {
108 localPort = 0; // indicates "any"
109 }
110 InetSocketAddress isa = new InetSocketAddress(localAddress,
111 localPort);
112 sslsock.bind(isa);
113 }
114
115 sslsock.connect(remoteAddress, connTimeout);
116 sslsock.setSoTimeout(soTimeout);
117 return sslsock;
118
119 }
120
121 /**
122 * @see org.apache.http.conn.scheme.SocketFactory#createSocket()
123 */
124 public Socket createSocket() throws IOException {
125 return getSSLContext().getSocketFactory().createSocket();
126 }
127
128 /**
129 * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket)
130 */
131 public boolean isSecure(Socket socket) throws IllegalArgumentException {
132 return true;
133 }
134
135 /**
136 * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket,
137 * java.lang.String, int, boolean)
138 */
139 public Socket createSocket(Socket socket, String host, int port,
140 boolean autoClose) throws IOException, UnknownHostException {
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 return ((obj != null) && obj.getClass().equals(
152 CopyOfEasySSLSocketFactory.class));
153 }
154
155 public int hashCode() {
156 return CopyOfEasySSLSocketFactory.class.hashCode();
157 }
158
159 }

mercurial