1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.mobileNotification.androidUtils;
38 import java.io.IOException;
39 import java.security.cert.CertificateException;
40 import java.security.cert.X509Certificate;
41 import javax.net.ssl.SSLContext;
42 import javax.net.ssl.SSLException;
43 import javax.net.ssl.SSLSession;
44 import javax.net.ssl.SSLSocket;
45 import javax.net.ssl.TrustManager;
46 import javax.net.ssl.X509TrustManager;
47 import org.apache.http.client.HttpClient;
48 import org.apache.http.conn.ClientConnectionManager;
49 import org.apache.http.conn.scheme.Scheme;
50 import org.apache.http.conn.scheme.SchemeRegistry;
51 import org.apache.http.conn.ssl.SSLSocketFactory;
52 import org.apache.http.conn.ssl.X509HostnameVerifier;
53 import org.apache.http.impl.client.DefaultHttpClient;
55 public class WebClientWrapper {
57 public static HttpClient wrapClient(HttpClient base) {
59 SSLContext ctx = SSLContext.getInstance("TLS");
60 X509TrustManager tm = new X509TrustManager() {
62 public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
65 public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
68 public X509Certificate[] getAcceptedIssuers() {
73 X509HostnameVerifier verifier = new X509HostnameVerifier() {
75 public void verify(String string, SSLSocket ssls) throws IOException {
78 public void verify(String string, X509Certificate xc) throws SSLException {
81 public void verify(String string, String[] strings, String[] strings1) throws SSLException {
84 public boolean verify(String arg0, SSLSession arg1) {
85 // TODO Auto-generated method stub
90 ctx.init(null, new TrustManager[]{tm}, null);
91 SSLSocketFactory ssf = new SSLSocketFactory(ctx);
92 ssf.setHostnameVerifier(verifier);
93 ClientConnectionManager ccm = base.getConnectionManager();
94 SchemeRegistry sr = ccm.getSchemeRegistry();
95 sr.register(new Scheme("https", ssf, 443));
96 return new DefaultHttpClient(ccm, base.getParams());
97 } catch (Exception ex) {