michael@0: /** michael@0: * Copyright (c) 2012-2013, Gerald Garcia michael@0: * michael@0: * This file is part of Andoid Caldav Sync Adapter Free. michael@0: * michael@0: * Andoid Caldav Sync Adapter Free is free software: you can redistribute michael@0: * it and/or modify it under the terms of the GNU General Public License michael@0: * as published by the Free Software Foundation, either version 3 of the michael@0: * License, or at your option any later version. michael@0: * michael@0: * Andoid Caldav Sync Adapter Free is distributed in the hope that michael@0: * it will be useful, but WITHOUT ANY WARRANTY; without even the implied michael@0: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the michael@0: * GNU General Public License for more details. michael@0: * michael@0: * You should have received a copy of the GNU General Public License michael@0: * along with Andoid Caldav Sync Adapter Free. michael@0: * If not, see . michael@0: * michael@0: */ michael@0: michael@0: package org.gege.caldavsyncadapter.caldav; michael@0: michael@0: /* michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: */ michael@0: michael@0: import java.io.IOException; michael@0: import java.net.InetAddress; michael@0: import java.net.InetSocketAddress; michael@0: import java.net.Socket; michael@0: import java.net.UnknownHostException; michael@0: michael@0: import javax.net.ssl.SSLContext; michael@0: import javax.net.ssl.SSLSocket; michael@0: import javax.net.ssl.TrustManager; michael@0: michael@0: import org.apache.http.conn.ConnectTimeoutException; michael@0: import org.apache.http.conn.scheme.LayeredSocketFactory; michael@0: import org.apache.http.conn.scheme.SocketFactory; michael@0: import org.apache.http.params.HttpConnectionParams; michael@0: import org.apache.http.params.HttpParams; michael@0: michael@0: /** michael@0: * This socket factory will create ssl socket that accepts self signed michael@0: * certificate michael@0: * michael@0: * @author olamy michael@0: * @version $Id: EasySSLSocketFactory.java 765355 2009-04-15 20:59:07Z evenisse michael@0: * $ michael@0: * @since 1.2.3 michael@0: */ michael@0: public class CopyOfEasySSLSocketFactory implements SocketFactory, michael@0: LayeredSocketFactory { michael@0: michael@0: private SSLContext sslcontext = null; michael@0: michael@0: private static SSLContext createEasySSLContext() throws IOException { michael@0: try { michael@0: SSLContext context = SSLContext.getInstance("TLS"); michael@0: context.init(null, new TrustManager[] { new EasyX509TrustManager( michael@0: null) }, null); michael@0: return context; michael@0: } catch (Exception e) { michael@0: throw new IOException(e.getMessage()); michael@0: } michael@0: } michael@0: michael@0: private SSLContext getSSLContext() throws IOException { michael@0: if (this.sslcontext == null) { michael@0: this.sslcontext = createEasySSLContext(); michael@0: } michael@0: return this.sslcontext; michael@0: } michael@0: michael@0: /** michael@0: * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, michael@0: * java.lang.String, int, java.net.InetAddress, int, michael@0: * org.apache.http.params.HttpParams) michael@0: */ michael@0: public Socket connectSocket(Socket sock, String host, int port, michael@0: InetAddress localAddress, int localPort, HttpParams params) michael@0: throws IOException, UnknownHostException, ConnectTimeoutException { michael@0: int connTimeout = HttpConnectionParams.getConnectionTimeout(params); michael@0: int soTimeout = HttpConnectionParams.getSoTimeout(params); michael@0: michael@0: InetSocketAddress remoteAddress = new InetSocketAddress(host, port); michael@0: SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket()); michael@0: michael@0: if ((localAddress != null) || (localPort > 0)) { michael@0: // we need to bind explicitly michael@0: if (localPort < 0) { michael@0: localPort = 0; // indicates "any" michael@0: } michael@0: InetSocketAddress isa = new InetSocketAddress(localAddress, michael@0: localPort); michael@0: sslsock.bind(isa); michael@0: } michael@0: michael@0: sslsock.connect(remoteAddress, connTimeout); michael@0: sslsock.setSoTimeout(soTimeout); michael@0: return sslsock; michael@0: michael@0: } michael@0: michael@0: /** michael@0: * @see org.apache.http.conn.scheme.SocketFactory#createSocket() michael@0: */ michael@0: public Socket createSocket() throws IOException { michael@0: return getSSLContext().getSocketFactory().createSocket(); michael@0: } michael@0: michael@0: /** michael@0: * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket) michael@0: */ michael@0: public boolean isSecure(Socket socket) throws IllegalArgumentException { michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket, michael@0: * java.lang.String, int, boolean) michael@0: */ michael@0: public Socket createSocket(Socket socket, String host, int port, michael@0: boolean autoClose) throws IOException, UnknownHostException { michael@0: return getSSLContext().getSocketFactory().createSocket(); michael@0: } michael@0: michael@0: // ------------------------------------------------------------------- michael@0: // javadoc in org.apache.http.conn.scheme.SocketFactory says : michael@0: // Both Object.equals() and Object.hashCode() must be overridden michael@0: // for the correct operation of some connection managers michael@0: // ------------------------------------------------------------------- michael@0: michael@0: public boolean equals(Object obj) { michael@0: return ((obj != null) && obj.getClass().equals( michael@0: CopyOfEasySSLSocketFactory.class)); michael@0: } michael@0: michael@0: public int hashCode() { michael@0: return CopyOfEasySSLSocketFactory.class.hashCode(); michael@0: } michael@0: michael@0: }