Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / files / api / client / CustomHttpClient.java @ 378fe36a

History | View | Annotate | Download (4.1 kB)

1
package com.rackspace.cloud.files.api.client;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.net.Socket;
6
import java.net.UnknownHostException;
7
import java.security.KeyManagementException;
8
import java.security.KeyStore;
9
import java.security.KeyStoreException;
10
import java.security.NoSuchAlgorithmException;
11
import java.security.UnrecoverableKeyException;
12
import java.security.cert.CertificateException;
13
import java.security.cert.X509Certificate;
14

    
15
import javax.net.ssl.HostnameVerifier;
16
import javax.net.ssl.SSLContext;
17
import javax.net.ssl.SSLSession;
18
import javax.net.ssl.TrustManager;
19
import javax.net.ssl.X509TrustManager;
20

    
21
import org.apache.http.conn.ClientConnectionManager;
22
import org.apache.http.conn.scheme.PlainSocketFactory;
23
import org.apache.http.conn.scheme.Scheme;
24
import org.apache.http.conn.scheme.SchemeRegistry;
25
import org.apache.http.conn.ssl.SSLSocketFactory;
26
import org.apache.http.impl.client.DefaultHttpClient;
27
import org.apache.http.impl.conn.SingleClientConnManager;
28

    
29
import android.content.Context;
30

    
31
import com.rackspace.cloud.android.R;
32

    
33
/**
34
 * 
35
 * @author Chmouel Boudjnah <chmouel.boudjnah@rackspace.co.uk>
36
 * 
37
 *         Custom implementation of HTTPClient using the keystore (in bks
38
 *         format) from android 2.3.1 which allow us to connect to London
39
 *         CloudFiles SSL host which doesn't work with the default keystore in
40
 *         other version than 2.3.1.
41
 */
42
public class CustomHttpClient extends DefaultHttpClient {
43
        public static final String X_AUTH_TOKEN = "X-Auth-Token";
44
        public static final String X_STORAGE_TOKEN = "X-Auth-Token";
45
        private static KeyStore trusted;
46
        final Context context;
47

    
48
        public CustomHttpClient(Context context) {
49
                super();
50
                this.context = context;
51
        }
52

    
53
        @Override
54
        protected ClientConnectionManager createClientConnectionManager() {
55
                SchemeRegistry registry = new SchemeRegistry();
56
                registry.register(new Scheme("http", PlainSocketFactory
57
                                .getSocketFactory(), 80));
58
                try {
59
                        KeyStore trustStore = KeyStore.getInstance(KeyStore
60
                                        .getDefaultType());
61
                        trustStore.load(null, null);
62
                        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
63
                        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
64
                        registry.register(new Scheme("https", sf,// newSslSocketFactory(),
65
                                        443));
66
                        ClientConnectionManager m = new SingleClientConnManager(
67
                                        getParams(), registry);
68
                        return m;
69
                } catch (Exception e) {
70
                        throw new IllegalStateException(e);
71
                }
72
        }
73

    
74
        private SSLSocketFactory newSslSocketFactory() {
75
                try {
76
                        if (trusted == null) {
77
                                trusted = KeyStore.getInstance("BKS");
78
                                InputStream in = context.getResources().openRawResource(
79
                                                R.raw.mystore);
80
                                try {
81
                                        trusted.load(in, "ez24get".toCharArray());
82
                                } finally {
83
                                        in.close();
84
                                }
85

    
86
                        }
87
                        return new SSLSocketFactory(trusted);
88
                } catch (Exception e) {
89
                        throw new AssertionError(e);
90
                }
91
        }
92

    
93
        final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
94
                public boolean verify(String hostname, SSLSession session) {
95
                        return true;
96
                }
97
        };
98

    
99
        class MySSLSocketFactory extends SSLSocketFactory {
100
                SSLContext sslContext = SSLContext.getInstance("TLS");
101

    
102
                public MySSLSocketFactory(KeyStore truststore)
103
                                throws NoSuchAlgorithmException, KeyManagementException,
104
                                KeyStoreException, UnrecoverableKeyException {
105
                        super(truststore);
106

    
107
                        TrustManager tm = new X509TrustManager() {
108
                                public void checkClientTrusted(X509Certificate[] chain,
109
                                                String authType) throws CertificateException {
110
                                }
111

    
112
                                public void checkServerTrusted(X509Certificate[] chain,
113
                                                String authType) throws CertificateException {
114
                                }
115

    
116
                                public X509Certificate[] getAcceptedIssuers() {
117
                                        return null;
118
                                }
119
                        };
120

    
121
                        sslContext.init(null, new TrustManager[] { tm }, null);
122
                }
123

    
124
                @Override
125
                public Socket createSocket(Socket socket, String host, int port,
126
                                boolean autoClose) throws IOException, UnknownHostException {
127
                        return sslContext.getSocketFactory().createSocket(socket, host,
128
                                        port, autoClose);
129
                }
130

    
131
                @Override
132
                public Socket createSocket() throws IOException {
133
                        return sslContext.getSocketFactory().createSocket();
134
                }
135
        }
136
}