1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.i18n.addressinput;
18
19import org.apache.http.client.HttpClient;
20import org.apache.http.client.methods.HttpGet;
21import org.apache.http.client.methods.HttpUriRequest;
22import org.apache.http.impl.client.BasicResponseHandler;
23import org.apache.http.impl.client.DefaultHttpClient;
24import org.apache.http.params.HttpConnectionParams;
25import org.apache.http.params.HttpParams;
26
27import java.io.UnsupportedEncodingException;
28import java.net.URLEncoder;
29
30/**
31 * Android implementation of those parts of the com.google.gwt.jsonp.client.JsonpRequestBuilder
32 * class that the CacheData class uses.
33 */
34class JsonpRequestBuilder {
35
36    /**
37     * Re-implementation of the com.google.gwt.user.client.rpc.AsyncCallback interface.
38     */
39    interface AsyncCallback<T> {
40
41        public void onFailure(Throwable caught);
42
43        public void onSuccess(T result);
44    }
45
46    /**
47     * @param timeout The expected timeout (ms) for this request.
48     */
49    void setTimeout(int timeout) {
50        HttpParams params = AsyncHttp.CLIENT.getParams();
51        HttpConnectionParams.setConnectionTimeout(params, timeout);
52        HttpConnectionParams.setSoTimeout(params, timeout);
53    }
54
55    /**
56     * Sends a JSONP request and expects a JsoMap object as a result.
57     */
58    void requestObject(String url, AsyncCallback<JsoMap> callback) {
59        HttpUriRequest request = new HttpGet(encodeUrl(url));
60        (new AsyncHttp(request, callback)).start();
61    }
62
63    /**
64     * Simple implementation of asynchronous HTTP GET.
65     */
66    private static class AsyncHttp extends Thread {
67
68        private static final HttpClient CLIENT = new DefaultHttpClient();
69
70        private HttpUriRequest mRequest;
71
72        private AsyncCallback<JsoMap> mCallback;
73
74        protected AsyncHttp(HttpUriRequest request, AsyncCallback<JsoMap> callback) {
75            mRequest = request;
76            mCallback = callback;
77        }
78
79        @Override
80        public void run() {
81            try {
82                final String response;
83                synchronized (CLIENT) {
84                    response = CLIENT.execute(mRequest, new BasicResponseHandler());
85                }
86                mCallback.onSuccess(JsoMap.buildJsoMap(response));
87            } catch (Exception e) {
88                mCallback.onFailure(e);
89            }
90        }
91    }
92
93    /**
94     * A quick hack to transform a string into an RFC 3986 compliant URL.
95     *
96     * TODO: Refactor the code to stop passing URLs around as strings, to eliminate the need for
97     * this hack.
98     */
99    private static String encodeUrl(String url) {
100        int length = url.length();
101        StringBuilder tmp = new StringBuilder(length);
102
103        try {
104            for (int i = 0; i < length; i++) {
105                int j = i;
106                char c = '\0';
107                for (; j < length; j++) {
108                    c = url.charAt(j);
109                    if (c == ':' || c == '/') {
110                        break;
111                    }
112                }
113                if (j == length) {
114                    tmp.append(URLEncoder.encode(url.substring(i), "UTF-8"));
115                    break;
116                } else if (j > i) {
117                    tmp.append(URLEncoder.encode(url.substring(i, j), "UTF-8"));
118                    tmp.append(c);
119                    i = j;
120                } else {
121                    tmp.append(c);
122                }
123            }
124        } catch (UnsupportedEncodingException e) {
125            throw new RuntimeException(e);  // Impossible.
126        }
127
128        return tmp.toString();
129    }
130}
131