root / src / com / rackspace / cloud / files / api / client / CustomHttpClient.java @ b722cab3
History | View | Annotate | Download (1.8 kB)
1 |
package com.rackspace.cloud.files.api.client; |
---|---|
2 |
|
3 |
import android.content.Context; |
4 |
|
5 |
import org.apache.http.conn.ClientConnectionManager; |
6 |
import org.apache.http.conn.scheme.PlainSocketFactory; |
7 |
import org.apache.http.conn.scheme.Scheme; |
8 |
import org.apache.http.conn.scheme.SchemeRegistry; |
9 |
import org.apache.http.conn.ssl.SSLSocketFactory; |
10 |
import org.apache.http.impl.client.DefaultHttpClient; |
11 |
import org.apache.http.impl.conn.SingleClientConnManager; |
12 |
|
13 |
import com.rackspacecloud.android.R; |
14 |
|
15 |
import java.io.InputStream; |
16 |
import java.security.KeyStore; |
17 |
|
18 |
|
19 |
/**
|
20 |
*
|
21 |
* @author Chmouel Boudjnah <chmouel.boudjnah@rackspace.co.uk>
|
22 |
*
|
23 |
* Custom implementation of HTTPClient using the keystore (in bks
|
24 |
* format) from android 2.3.1 which allow us to connect to London
|
25 |
* CloudFiles SSL host which doesn't work with the default keystore in
|
26 |
* other version than 2.3.1.
|
27 |
*/
|
28 |
public class CustomHttpClient extends DefaultHttpClient { |
29 |
|
30 |
final Context context; |
31 |
static KeyStore trusted; |
32 |
|
33 |
public CustomHttpClient(Context context) { |
34 |
super();
|
35 |
this.context = context;
|
36 |
} |
37 |
|
38 |
@Override
|
39 |
protected ClientConnectionManager createClientConnectionManager() {
|
40 |
SchemeRegistry registry = new SchemeRegistry();
|
41 |
registry.register(new Scheme("http", PlainSocketFactory |
42 |
.getSocketFactory(), 80));
|
43 |
registry.register(new Scheme("https", newSslSocketFactory(), 443)); |
44 |
return new SingleClientConnManager(getParams(), registry); |
45 |
} |
46 |
|
47 |
private SSLSocketFactory newSslSocketFactory() { |
48 |
try {
|
49 |
if(trusted == null){ |
50 |
trusted = KeyStore.getInstance("BKS"); |
51 |
InputStream in = context.getResources().openRawResource(
|
52 |
R.raw.android231); |
53 |
try {
|
54 |
trusted.load(in, "changeit".toCharArray());
|
55 |
} finally {
|
56 |
in.close(); |
57 |
} |
58 |
} |
59 |
return new SSLSocketFactory(trusted); |
60 |
} catch (Exception e) { |
61 |
throw new AssertionError(e); |
62 |
} |
63 |
} |
64 |
} |