33
33
import android .os .Looper ;
34
34
35
35
/**
36
- * Used to intercept and handle the responses from requests made using
37
- * {@link AsyncHttpClient}, with automatic handling of png/jpg image data
38
- * (e.g. checks Content-Type against allowed list, Content-length). Receives response body as
39
- * byte array.
40
- * <p>
41
- * Additionally, you can override the {@link #onFailure(Throwable, String)},
42
- * {@link #onStart()}, and {@link #onFinish()} methods as required.
36
+ * Used to intercept and handle the responses from requests made using
37
+ * {@link AsyncHttpClient}. Receives response body as byte array with a
38
+ * content-type whitelist. (e.g. checks Content-Type against allowed list,
39
+ * Content-length).
43
40
* <p>
44
41
* For example:
45
42
* <p>
46
43
* <pre>
47
44
* AsyncHttpClient client = new AsyncHttpClient();
48
- * client.get("http://www.google.com", new AsyncHttpResponseHandler() {
45
+ * String[] allowedTypes = new String[] { "image/png" };
46
+ * client.get("http://www.example.com/image.png", new BinaryHttpResponseHandler(allowedTypes) {
49
47
* @Override
50
- * public void onStart() {
51
- * // Initiated the request
52
- * }
53
- *
54
- * @Override
55
- * public void onSuccess(String response) {
48
+ * public void onSuccess(byte[] imageData) {
56
49
* // Successfully got a response
57
50
* }
58
- *
59
- * @Override
60
- * public void onFailure(Throwable e, String response) {
61
- * // Response failed :(
62
- * }
63
51
*
64
52
* @Override
65
- * public void onFinish( ) {
66
- * // Completed the request (either success or failure)
53
+ * public void onFailure(Throwable e, byte[] imageData ) {
54
+ * // Response failed :(
67
55
* }
68
56
* });
69
57
* </pre>
70
58
*/
71
- public class ImageHttpResponseHandler extends AsyncHttpResponseHandler {
72
- private static final int SUCCESS_MESSAGE = 0 ;
73
- private static final int FAILURE_MESSAGE = 1 ;
74
- private static final int START_MESSAGE = 2 ;
75
- private static final int FINISH_MESSAGE = 3 ;
76
-
77
- private static String [] mAllowedContentTypes = new String [] { "image/jpeg" ,
78
- "image/png" };
79
-
80
- private Handler handler ;
59
+ public class BinaryHttpResponseHandler extends AsyncHttpResponseHandler {
60
+ // Allow images by default
61
+ private static String [] mAllowedContentTypes = new String [] {
62
+ "image/jpeg" ,
63
+ "image/png"
64
+ };
81
65
82
66
/**
83
- * Creates a new AsyncHttpResponseHandler
67
+ * Creates a new BinaryHttpResponseHandler
84
68
*/
85
- public ImageHttpResponseHandler () {
69
+ public BinaryHttpResponseHandler () {
86
70
super ();
87
71
}
88
-
72
+
89
73
/**
90
- * Creates a new AsyncHttpResponseHandler , and overrides the default allowed image
91
- * content types with passed String array (hopefully) of image content types.
74
+ * Creates a new BinaryHttpResponseHandler , and overrides the default allowed
75
+ * content types with passed String array (hopefully) of content types.
92
76
*/
93
- public ImageHttpResponseHandler (String [] allowedContentTypes ) {
94
- this ();
95
- mAllowedContentTypes = allowedContentTypes ;
77
+ public BinaryHttpResponseHandler (String [] allowedContentTypes ) {
78
+ this ();
79
+ mAllowedContentTypes = allowedContentTypes ;
96
80
}
97
81
98
82
@@ -104,21 +88,14 @@ public ImageHttpResponseHandler(String[] allowedContentTypes) {
104
88
* Fired when a request returns successfully, override to handle in your own code
105
89
* @param content the body of the HTTP response from the server
106
90
*/
107
- public void onSuccess (byte [] imageData ) {}
108
-
109
- /**
110
- * Fired when a request fails to complete, override to handle in your own code
111
- * @param error the underlying cause of the failure
112
- * @deprecated use {@link #onFailure(Throwable, String)}
113
- */
114
- public void onFailure (Throwable error ) {}
91
+ public void onSuccess (byte [] binaryData ) {}
115
92
116
93
/**
117
94
* Fired when a request fails to complete, override to handle in your own code
118
95
* @param error the underlying cause of the failure
119
96
* @param content the response body, if any
120
97
*/
121
- public void onFailure (Throwable error , byte [] imageData ) {
98
+ public void onFailure (Throwable error , byte [] binaryData ) {
122
99
// By default, call the deprecated onFailure(Throwable) for compatibility
123
100
onFailure (error );
124
101
}
@@ -127,7 +104,7 @@ public void onFailure(Throwable error, byte[] imageData) {
127
104
//
128
105
// Pre-processing of messages (executes in background threadpool thread)
129
106
//
130
-
107
+
131
108
protected void sendSuccessMessage (byte [] responseBody ) {
132
109
sendMessage (obtainMessage (SUCCESS_MESSAGE , responseBody ));
133
110
}
@@ -148,8 +125,6 @@ protected void handleFailureMessage(Throwable e, byte[] responseBody) {
148
125
onFailure (e , responseBody );
149
126
}
150
127
151
-
152
-
153
128
// Methods which emulate android's Handler and Message methods
154
129
protected void handleMessage (Message msg ) {
155
130
switch (msg .what ) {
@@ -160,11 +135,8 @@ protected void handleMessage(Message msg) {
160
135
Object [] response = (Object [])msg .obj ;
161
136
handleFailureMessage ((Throwable )response [0 ], (byte [])response [1 ]);
162
137
break ;
163
- case START_MESSAGE :
164
- onStart ();
165
- break ;
166
- case FINISH_MESSAGE :
167
- onFinish ();
138
+ default :
139
+ super .handleMessage (msg );
168
140
break ;
169
141
}
170
142
}
@@ -175,21 +147,21 @@ void sendResponseMessage(HttpResponse response) {
175
147
Header [] contentTypeHeaders = response .getHeaders ("Content-Type" );
176
148
byte [] responseBody = null ;
177
149
if (contentTypeHeaders .length != 1 ) {
178
- //malformed/ambiguous HTTP Header, ABORT!
179
- sendFailureMessage (new HttpResponseException (status .getStatusCode (), "None, or more than one, Content-Type Header found!" ), responseBody );
180
- return ;
150
+ //malformed/ambiguous HTTP Header, ABORT!
151
+ sendFailureMessage (new HttpResponseException (status .getStatusCode (), "None, or more than one, Content-Type Header found!" ), responseBody );
152
+ return ;
181
153
}
182
154
Header contentTypeHeader = contentTypeHeaders [0 ];
183
155
boolean foundAllowedContentType = false ;
184
156
for (String anAllowedContentType : mAllowedContentTypes ) {
185
- if (anAllowedContentType .equals (contentTypeHeader .getValue ())) {
186
- foundAllowedContentType = true ;
187
- }
157
+ if (anAllowedContentType .equals (contentTypeHeader .getValue ())) {
158
+ foundAllowedContentType = true ;
159
+ }
188
160
}
189
161
if (!foundAllowedContentType ) {
190
- //Content-Type not in allowed list, ABORT!
191
- sendFailureMessage (new HttpResponseException (status .getStatusCode (), "Content-Type not allowed!" ), responseBody );
192
- return ;
162
+ //Content-Type not in allowed list, ABORT!
163
+ sendFailureMessage (new HttpResponseException (status .getStatusCode (), "Content-Type not allowed!" ), responseBody );
164
+ return ;
193
165
}
194
166
try {
195
167
HttpEntity entity = null ;
0 commit comments