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

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

     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  */
    22 package org.gege.caldavsyncadapter.caldav;
    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  */
    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;
    49 import javax.net.ssl.SSLContext;
    50 import javax.net.ssl.SSLSocket;
    51 import javax.net.ssl.TrustManager;
    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;
    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 {
    71 	private SSLContext sslcontext = null;
    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 	}
    84 	private SSLContext getSSLContext() throws IOException {
    85 		if (this.sslcontext == null) {
    86 			this.sslcontext = createEasySSLContext();
    87 		}
    88 		return this.sslcontext;
    89 	}
    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);
   102 		InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
   103 		SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());
   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 		}
   115 		sslsock.connect(remoteAddress, connTimeout);
   116 		sslsock.setSoTimeout(soTimeout);
   117 		return sslsock;
   119 	}
   121 	/**
   122 	 * @see org.apache.http.conn.scheme.SocketFactory#createSocket()
   123 	 */
   124 	public Socket createSocket() throws IOException {
   125 		return getSSLContext().getSocketFactory().createSocket();
   126 	}
   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 	}
   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 	}
   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 	// -------------------------------------------------------------------
   150 	public boolean equals(Object obj) {
   151 		return ((obj != null) && obj.getClass().equals(
   152 				CopyOfEasySSLSocketFactory.class));
   153 	}
   155 	public int hashCode() {
   156 		return CopyOfEasySSLSocketFactory.class.hashCode();
   157 	}
   159 }

mercurial