-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebHelper.java
More file actions
413 lines (380 loc) · 12.6 KB
/
WebHelper.java
File metadata and controls
413 lines (380 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// $Id: WebHelper.java,v 1.3 2012-11-27 04:24:16 falk Exp $
/**
* This module contains basic web communication utilities.
*
* TODO: examine this code for ways that bad actors could abuse it.
*/
package com.android.recovery;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.NameValuePair;
import android.util.Log;
/**
* This class represents an open connection to a web site.
* Key methods:
*
* get(String url) GET a url, return HttpResponse
* get(HttpRequestBase req) GET a url, return HttpResponse
* getStream(String url) GET a url, return InputStream
* getStream(HttpRequestBase) GET a url, return InputStream
* getStream(HttpResponse) Exctract the stream from a response
* getString(String url) GET a url, return String
* getString(HttpRequestBase) GET a url, return String
* getString(HttpResponse) Extract a string from a response
* getArgs(String url, args) Append args to url and get()
* getArgs(String args) Same, use default url
* head(String url) HEAD request, return HttpResponse
* post(url, String data) POST request with data, return HttpResponse
* post(url, List data) POST request with list of name/value pairs
* post(url, Map data) POST request with map of name/value pairs
* post(url, name, value, ...) POST request with name/value pairs in line
* post(url, from,file,name) POST multipart, one file
* post(url, HttpEntity) POST arbitrary request, arbitrary data entity
* post(HttpPost, HttpEntity) POST arbitrary request, arbitrary data entity
*
* Utilities:
*
* entity(HttpResponse resp) Convert HttpResponse to HttpEntity
* inputStream(HttpEntity entity) Convert HttpEntity to InputStream
* getString(InputStream) Convert InputStream to String
*/
class WebHelper {
static private final String TAG = "WebHelper";
private String mDefUrl = "http://www.example.com/cgi-bin/api";
private HttpClient mClient = null;
private Random mRand = null;
public WebHelper() {
mClient = new DefaultHttpClient();
}
public WebHelper(final String url) {
mDefUrl = url;
mClient = new DefaultHttpClient();
}
// Any http return other than SC_OK will throw this exception.
public static class WebError extends Exception {
private static final long serialVersionUID = 1L;
int status;
WebError(final int status, final String reason) {
super(reason);
this.status = status;
}
WebError(final HttpResponse resp) {
super(resp.getStatusLine().getReasonPhrase());
this.status = resp.getStatusLine().getStatusCode();
}
public String toString() {
return ""+status+": "+getMessage();
}
}
/**
* GET the given url, return an HttpResponse
*/
public HttpResponse get(String url) throws WebError {
HttpGet req = new HttpGet(url);
req.setHeader("Cache-Control", "no-cache");
return get(req);
}
/**
* Fetch the given request, return an HttpResponse
*/
public HttpResponse get(HttpRequestBase req) throws WebError {
HttpResponse resp;
try {
resp = mClient.execute(req);
int status = resp.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK)
throw new WebError(resp);
return resp;
} catch (ClientProtocolException e) {
Log.e(TAG, "Protocol exception: " + e);
throw new WebError(503, "Exception: " + e);
} catch (UnknownHostException e) {
throw new WebError(HttpStatus.SC_SERVICE_UNAVAILABLE,
"Exception: " + e);
} catch (IOException e) {
Log.e(TAG, "IO exception: " + e);
throw new WebError(HttpStatus.SC_SERVICE_UNAVAILABLE,
"Exception: " + e);
}
}
/**
* GET the given url, return an InputStream
*/
public InputStream getStream(String url) throws WebError {
return getStream(get(url));
}
/**
* GET the given url, return an InputStream
*/
public InputStream getStream(HttpRequestBase req) throws WebError {
return getStream(get(req));
}
/**
* Utility: convert an HttpResponse to a stream
* @throws WebError on failure.
*/
public static InputStream getStream(HttpResponse resp) throws WebError {
return inputStream(entity(resp));
}
/**
* GET the given url, return as a string
*/
public String getString(final String url) throws WebError {
return getString(getStream(url));
}
/**
* GET the given url, return as a string
*/
public String getString(final HttpRequestBase req) throws WebError {
return getString(getStream(req));
}
/**
* Utility: convert an HttpResponse to a string.
* @throws WebError on failure.
*/
public static String getString(final HttpResponse resp) throws WebError {
return getString(inputStream(entity(resp)));
}
/**
* Utility: get the string contained in the given input stream
*/
public static String getString(final InputStream is) throws WebError {
if (is == null) return null;
try {
return Utils.inputStreamAsString(is);
} catch (IOException e) {
Log.e(TAG, "IO Exception in getString: " + e);
Utils.where(e);
throw new WebError(503, "Exception: " + e);
}
}
/**
* Append the given arguments to the given URL and return the results
* as an InputStream
*/
public InputStream getArgs(String url, String args) throws WebError {
return getStream(url + args);
}
/**
* Append the given arguments to the default URL and return the results
* as an InputStream
*/
public InputStream getArgs(String args) throws WebError {
return getArgs(mDefUrl, args);
}
/**
* Fetch the headers for the given url, return as an HttpResponse
*/
public HttpResponse head(String url) throws WebError {
return get(new HttpHead(url));
}
/**
* POST the given url, providing the given input string as data.
* @param url destination url
* @param data post data, as a single string
* Caller is responsible for creating an encoded form within the string.
*/
public HttpResponse post(String url, String data) throws WebError {
HttpPost req = new HttpPost(url);
req.setHeader("Content-Type", "application/x-www-form-urlencoded");
StringEntity e;
try {
e = new StringEntity(data, "UTF-8");
} catch (UnsupportedEncodingException e1) {
Log.e(TAG, "Unsupported encoding: " + e1);
Utils.where(e1);
throw new WebError(503, "Exception: " + e1);
}
return post(req, e);
}
/**
* POST the given url, providing the given as a list of NameValuePairs
* @param url destination url
* @param data post data, as a list of name/value pairs
*/
public HttpResponse post(String url, List<NameValuePair> data)
throws WebError
{
HttpPost req = new HttpPost(url);
UrlEncodedFormEntity e;
try {
e = new UrlEncodedFormEntity(data, "UTF-8");
} catch (UnsupportedEncodingException e1) {
Log.e(TAG, "Unsupported encoding: " + e1);
Utils.where(e1);
throw new WebError(503, "Exception: " + e1);
}
return post(req, e);
}
/**
* POST the given url, providing a Map of Name,Value
* @param url destination url
* @param data post data as a map. All values must support toString()
*/
public HttpResponse post(String url, Map<String,Object> data)
throws WebError
{
List<NameValuePair> pairs = new ArrayList<NameValuePair>(data.size());
NameValuePair pair;
for (Map.Entry<String,Object> e : data.entrySet()) {
pair = new BasicNameValuePair(e.getKey(), e.getValue().toString());
pairs.add(pair);
}
return post(url, pairs);
}
/**
* POST the given url, providing names and values.
* @param url destination url
* @param name,value, ... All must be strings.
*/
public HttpResponse post(String url, String... args) throws WebError {
List<NameValuePair> pairs = new ArrayList<NameValuePair>(args.length/2);
NameValuePair pair;
for (int i = 0; i < args.length; i += 2) {
pair = new BasicNameValuePair(args[i], args[i+1]);
pairs.add(pair);
}
return post(url, pairs);
}
/**
* Post a file using multipart/form-data.
* @param url Url for the POST
* @param from Value for "From" field of form
* @param file File to upload in the "file" field.
* @param name Content-disposition value for file; may be null
* @return HttpResponse from server
* @throws WebError
*/
public HttpResponse post(String url, String from, File file, String name)
throws WebError
{
HttpPost req = new HttpPost(url);
String sep = genSep();
req.setHeader("Content-Type", "multipart/form-data; boundary=" + sep);
List<MultiPartProducer.MultiPartPair> parts =
new ArrayList<MultiPartProducer.MultiPartPair>(2);
parts.add(new MultiPartProducer.MultiPartPair("from", from));
parts.add(new MultiPartProducer.MultiPartPair("file", file, name));
EntityTemplate e =
new EntityTemplate(new MultiPartProducer(sep, parts));
return post(req, e);
}
/**
* Post an arbitrary entity.
* @param url Url for the POST
* @param data Any HttpEntity subclass
* @return HttpResponse from server
* @throws WebError
*/
public HttpResponse post(String url, HttpEntity data) throws WebError {
HttpPost req = new HttpPost(url);
String sep = genSep();
req.setHeader("Content-Type", "multipart/form-data; boundary=" + sep);
return post(req, data);
}
/**
* Post an arbitrary entity.
* @param req HttpPost
* @param data Any HttpEntity subclass
* @return HttpResponse from server
* @throws WebError
*/
public HttpResponse post(HttpPost req, HttpEntity data) throws WebError {
try {
req.setEntity(data);
HttpResponse resp = mClient.execute(req);
int status = resp.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK) {
//Log.w(TAG,
// "http error: " + resp.getStatusLine().getReasonPhrase());
throw new WebError(resp);
}
return resp;
} catch (ClientProtocolException e) {
Log.e(TAG, "Protocol exception: " + e);
throw new WebError(HttpStatus.SC_SERVICE_UNAVAILABLE,
"Exception: " + e);
} catch (UnknownHostException e) {
throw new WebError(HttpStatus.SC_SERVICE_UNAVAILABLE,
"Exception: " + e);
} catch (IOException e) {
Log.e(TAG, "IO exception: " + e);
throw new WebError(HttpStatus.SC_SERVICE_UNAVAILABLE,
"Exception: " + e);
} catch (WebError e) {
throw e;
} catch (Exception e) {
// Catch-all
Log.e(TAG, "Unknown exception: " + e);
throw new WebError(503, "Exception: " + e);
}
}
/**
* Utility: convert HttpResponse to HttpEntity
* @throws WebError
*/
public static HttpEntity entity(HttpResponse resp) throws WebError {
HttpEntity entity;
if ((entity = resp.getEntity()) == null ) {
Log.w(TAG, "http error: empty response");
throw new WebError(204, "http error: empty response");
}
return entity;
}
/**
* Utility: convert HttpEntity to InputStream
* @throws WebError
* Tip: use Utils.inputStreamAsString() to convert InputStream to String
*/
public static InputStream inputStream(HttpEntity entity) throws WebError {
InputStream input;
try {
input = entity.getContent();
} catch (IOException e) {
Log.e(TAG, "IO exception: " + e);
throw new WebError(503, "Exception: " + e);
}
if( input == null ) {
Log.w(TAG, "http error: empty response");
throw new WebError(204, "http error: empty response");
}
return input;
}
// Free resources.
public void close() {
mClient = null;
}
/**
* Utility: generate multipart seperator string.
*/
public String genSep() {
final String ochars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
StringBuilder sb = new StringBuilder("___WebHelper");
if (mRand == null) mRand = new Random();
for (int i=0; i<30; ++i)
sb.append(ochars.charAt(mRand.nextInt(64)));
return sb.toString();
}
}