Skip to content

Commit 638f126

Browse files
author
Noor Dawod
committed
Make pre-processing work per request and accompanying response.
1 parent 731b304 commit 638f126

File tree

8 files changed

+69
-56
lines changed

8 files changed

+69
-56
lines changed

library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ public class AsyncHttpClient {
122122
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
123123
public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
124124
public static final String ENCODING_GZIP = "gzip";
125-
public static final String ATTR_PRE_PROCESSED = "ahc.pre-processed";
126-
public static final String TRUE = "true";
127-
public static final String FALSE = "false";
128125

129126
public static final int DEFAULT_MAX_CONNECTIONS = 10;
130127
public static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
@@ -141,7 +138,6 @@ public class AsyncHttpClient {
141138
private ExecutorService threadPool;
142139
private final Map<Context, List<RequestHandle>> requestMap;
143140
private final Map<String, String> clientHeaderMap;
144-
private PreProcessingInterface preProcessor;
145141
private boolean isUrlEncodingEnabled = true;
146142

147143
/**
@@ -276,7 +272,6 @@ public void process(HttpResponse response, HttpContext context) {
276272
if (entity == null) {
277273
return;
278274
}
279-
280275
final Header encoding = entity.getContentEncoding();
281276
if (encoding != null) {
282277
for (HeaderElement element : encoding.getElements()) {
@@ -286,18 +281,6 @@ public void process(HttpResponse response, HttpContext context) {
286281
}
287282
}
288283
}
289-
290-
if (preProcessor != null) {
291-
// Handle pre-processing only once.
292-
Object isPreProcessed = context.getAttribute(ATTR_PRE_PROCESSED);
293-
if (isPreProcessed != null && isPreProcessed.equals(TRUE)) {
294-
// Pre-process this request.
295-
preProcessor.onPreProcessResponse(response);
296-
297-
// Indicate that this request has passed pre-processing.
298-
context.removeAttribute(ATTR_PRE_PROCESSED);
299-
}
300-
}
301284
}
302285
});
303286

@@ -317,18 +300,6 @@ public void process(final HttpRequest request, final HttpContext context) throws
317300
authState.setCredentials(creds);
318301
}
319302
}
320-
321-
if (preProcessor != null) {
322-
// Handle pre-processing only once.
323-
Object isPreProcessed = context.getAttribute(ATTR_PRE_PROCESSED);
324-
if (isPreProcessed == null || !isPreProcessed.equals(TRUE)) {
325-
// Pre-process this request.
326-
preProcessor.onPreProcessRequest(request);
327-
328-
// Indicate that this request has passed pre-processing.
329-
context.setAttribute(ATTR_PRE_PROCESSED, TRUE);
330-
}
331-
}
332303
}
333304
}, 0);
334305

@@ -399,25 +370,6 @@ public ExecutorService getThreadPool() {
399370
return threadPool;
400371
}
401372

402-
/**
403-
* Sets a handler to be used for pre-processing requests and responses.
404-
*
405-
* @param handler an instance of {@link PreProcessingInterface} to use for
406-
* pre-processing requests and responses
407-
*/
408-
public void setPreProcessor(PreProcessingInterface handler) {
409-
this.preProcessor = handler;
410-
}
411-
412-
/**
413-
* Returns the current handler for pre-processing requests and responses.
414-
*
415-
* @return current handler used, or null if none is defined
416-
*/
417-
public PreProcessingInterface getPreProcessor() {
418-
return preProcessor;
419-
}
420-
421373
/**
422374
* Get the default threading pool to be used for this HTTP client.
423375
*

library/src/main/java/com/loopj/android/http/AsyncHttpRequest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@
3333
/**
3434
* Internal class, representing the HttpRequest, done in asynchronous manner
3535
*/
36-
public class AsyncHttpRequest implements Runnable {
36+
public class AsyncHttpRequest implements PreProcessInterface, Runnable {
3737
private final AbstractHttpClient client;
3838
private final HttpContext context;
3939
private final HttpUriRequest request;
4040
private final ResponseHandlerInterface responseHandler;
4141
private int executionCount;
42-
private boolean isCancelled = false;
43-
private boolean cancelIsNotified = false;
44-
private boolean isFinished = false;
42+
private boolean isCancelled;
43+
private boolean cancelIsNotified;
44+
private boolean isFinished;
45+
private boolean isRequestPreProcessed;
4546

4647
public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriRequest request, ResponseHandlerInterface responseHandler) {
4748
this.client = client;
@@ -50,12 +51,26 @@ public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriR
5051
this.responseHandler = responseHandler;
5152
}
5253

54+
@Override
55+
public void onPreProcess() {
56+
}
57+
5358
@Override
5459
public void run() {
5560
if (isCancelled()) {
5661
return;
5762
}
5863

64+
// Carry out pre-processing for this request only once.
65+
if (!isRequestPreProcessed) {
66+
isRequestPreProcessed = true;
67+
onPreProcess();
68+
}
69+
70+
if (isCancelled()) {
71+
return;
72+
}
73+
5974
if (responseHandler != null) {
6075
responseHandler.sendStartMessage();
6176
}
@@ -98,6 +113,10 @@ private void makeRequest() throws IOException {
98113
HttpResponse response = client.execute(request, context);
99114

100115
if (!isCancelled() && responseHandler != null) {
116+
// Carry out pre-processing for this response.
117+
responseHandler.onPreProcess();
118+
119+
// The response is ready, handle it.
101120
responseHandler.sendResponseMessage(response);
102121
}
103122
}

library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
* </pre>
8080
*/
8181
public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterface {
82+
8283
private static final String LOG_TAG = "AsyncHttpResponseHandler";
8384

8485
protected static final int SUCCESS_MESSAGE = 0;
@@ -217,6 +218,11 @@ public void onFinish() {
217218
// default log warning is not necessary, because this method is just optional notification
218219
}
219220

221+
@Override
222+
public void onPreProcess() {
223+
// default action is to do nothing...
224+
}
225+
220226
/**
221227
* Fired when a request returns successfully, override to handle in your own code
222228
*

library/src/main/java/com/loopj/android/http/JsonHttpResponseHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* Additionally, you can override the other event methods from the parent class.
3737
*/
3838
public class JsonHttpResponseHandler extends TextHttpResponseHandler {
39+
3940
private static final String LOG_TAG = "JsonHttpResponseHandler";
4041

4142
/**

library/src/main/java/com/loopj/android/http/JsonStreamerEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public class JsonStreamerEntity implements HttpEntity {
5656
// automatically enlarge the buffer.
5757
private static final StringBuilder BUILDER = new StringBuilder(128);
5858

59-
private static final byte[] JSON_TRUE = AsyncHttpClient.TRUE.getBytes();
60-
private static final byte[] JSON_FALSE = AsyncHttpClient.FALSE.getBytes();
59+
private static final byte[] JSON_TRUE = "true".getBytes();
60+
private static final byte[] JSON_FALSE = "false".getBytes();
6161
private static final byte[] JSON_NULL = "null".getBytes();
6262
private static final byte[] STREAM_NAME = escape("name");
6363
private static final byte[] STREAM_TYPE = escape("type");
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Android Asynchronous Http Client
3+
Copyright (c) 2011 James Smith <[email protected]>
4+
http://loopj.com
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package com.loopj.android.http;
20+
21+
/**
22+
* This interface is used to define a pre-processing handler. The handler is
23+
* called once for a request and once for a response.
24+
*
25+
* @author Noor Dawod <[email protected]>
26+
*/
27+
public interface PreProcessInterface {
28+
29+
/**
30+
* This method is called once by the system when either a request or a response
31+
* needs pre-processing. The library makes sure that each request and each
32+
* response is pre-processed only once.
33+
*/
34+
void onPreProcess();
35+
}

library/src/main/java/com/loopj/android/http/ResponseHandlerInterface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* Interface to standardize implementations
2929
*/
30-
public interface ResponseHandlerInterface {
30+
public interface ResponseHandlerInterface extends PreProcessInterface {
3131

3232
/**
3333
* Returns data whether request completed successfully

library/src/main/java/com/loopj/android/http/TextHttpResponseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* </pre>
5757
*/
5858
public abstract class TextHttpResponseHandler extends AsyncHttpResponseHandler {
59+
5960
private static final String LOG_TAG = "TextHttpResponseHandler";
6061

6162
/**
@@ -119,5 +120,4 @@ public static String getResponseString(byte[] stringBytes, String charset) {
119120
return null;
120121
}
121122
}
122-
123123
}

0 commit comments

Comments
 (0)