3333import android .os .Looper ;
3434
3535/**
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).
4340 * <p>
4441 * For example:
4542 * <p>
4643 * <pre>
4744 * 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) {
4947 * @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) {
5649 * // Successfully got a response
5750 * }
58- *
59- * @Override
60- * public void onFailure(Throwable e, String response) {
61- * // Response failed :(
62- * }
6351 *
6452 * @Override
65- * public void onFinish( ) {
66- * // Completed the request (either success or failure)
53+ * public void onFailure(Throwable e, byte[] imageData ) {
54+ * // Response failed :(
6755 * }
6856 * });
6957 * </pre>
7058 */
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+ };
8165
8266 /**
83- * Creates a new AsyncHttpResponseHandler
67+ * Creates a new BinaryHttpResponseHandler
8468 */
85- public ImageHttpResponseHandler () {
69+ public BinaryHttpResponseHandler () {
8670 super ();
8771 }
88-
72+
8973 /**
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.
9276 */
93- public ImageHttpResponseHandler (String [] allowedContentTypes ) {
94- this ();
95- mAllowedContentTypes = allowedContentTypes ;
77+ public BinaryHttpResponseHandler (String [] allowedContentTypes ) {
78+ this ();
79+ mAllowedContentTypes = allowedContentTypes ;
9680 }
9781
9882
@@ -104,21 +88,14 @@ public ImageHttpResponseHandler(String[] allowedContentTypes) {
10488 * Fired when a request returns successfully, override to handle in your own code
10589 * @param content the body of the HTTP response from the server
10690 */
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 ) {}
11592
11693 /**
11794 * Fired when a request fails to complete, override to handle in your own code
11895 * @param error the underlying cause of the failure
11996 * @param content the response body, if any
12097 */
121- public void onFailure (Throwable error , byte [] imageData ) {
98+ public void onFailure (Throwable error , byte [] binaryData ) {
12299 // By default, call the deprecated onFailure(Throwable) for compatibility
123100 onFailure (error );
124101 }
@@ -127,7 +104,7 @@ public void onFailure(Throwable error, byte[] imageData) {
127104 //
128105 // Pre-processing of messages (executes in background threadpool thread)
129106 //
130-
107+
131108 protected void sendSuccessMessage (byte [] responseBody ) {
132109 sendMessage (obtainMessage (SUCCESS_MESSAGE , responseBody ));
133110 }
@@ -148,8 +125,6 @@ protected void handleFailureMessage(Throwable e, byte[] responseBody) {
148125 onFailure (e , responseBody );
149126 }
150127
151-
152-
153128 // Methods which emulate android's Handler and Message methods
154129 protected void handleMessage (Message msg ) {
155130 switch (msg .what ) {
@@ -160,11 +135,8 @@ protected void handleMessage(Message msg) {
160135 Object [] response = (Object [])msg .obj ;
161136 handleFailureMessage ((Throwable )response [0 ], (byte [])response [1 ]);
162137 break ;
163- case START_MESSAGE :
164- onStart ();
165- break ;
166- case FINISH_MESSAGE :
167- onFinish ();
138+ default :
139+ super .handleMessage (msg );
168140 break ;
169141 }
170142 }
@@ -175,21 +147,21 @@ void sendResponseMessage(HttpResponse response) {
175147 Header [] contentTypeHeaders = response .getHeaders ("Content-Type" );
176148 byte [] responseBody = null ;
177149 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 ;
181153 }
182154 Header contentTypeHeader = contentTypeHeaders [0 ];
183155 boolean foundAllowedContentType = false ;
184156 for (String anAllowedContentType : mAllowedContentTypes ) {
185- if (anAllowedContentType .equals (contentTypeHeader .getValue ())) {
186- foundAllowedContentType = true ;
187- }
157+ if (anAllowedContentType .equals (contentTypeHeader .getValue ())) {
158+ foundAllowedContentType = true ;
159+ }
188160 }
189161 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 ;
193165 }
194166 try {
195167 HttpEntity entity = null ;
0 commit comments