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