Skip to content

Commit ce9f0c4

Browse files
author
Noor Dawod
committed
Add pre-processing for requests and responses.
1 parent 4717ae8 commit ce9f0c4

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ 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";
125128

126129
public static final int DEFAULT_MAX_CONNECTIONS = 10;
127130
public static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
@@ -138,6 +141,7 @@ public class AsyncHttpClient {
138141
private ExecutorService threadPool;
139142
private final Map<Context, List<RequestHandle>> requestMap;
140143
private final Map<String, String> clientHeaderMap;
144+
private PreProcessingInterface preProcessor;
141145
private boolean isUrlEncodingEnabled = true;
142146

143147
/**
@@ -272,6 +276,7 @@ public void process(HttpResponse response, HttpContext context) {
272276
if (entity == null) {
273277
return;
274278
}
279+
275280
final Header encoding = entity.getContentEncoding();
276281
if (encoding != null) {
277282
for (HeaderElement element : encoding.getElements()) {
@@ -281,6 +286,18 @@ public void process(HttpResponse response, HttpContext context) {
281286
}
282287
}
283288
}
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+
}
284301
}
285302
});
286303

@@ -300,6 +317,18 @@ public void process(final HttpRequest request, final HttpContext context) throws
300317
authState.setCredentials(creds);
301318
}
302319
}
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+
}
303332
}
304333
}, 0);
305334

@@ -370,6 +399,25 @@ public ExecutorService getThreadPool() {
370399
return threadPool;
371400
}
372401

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+
373421
/**
374422
* Get the default threading pool to be used for this HTTP client.
375423
*
@@ -519,7 +567,7 @@ public int getResponseTimeout() {
519567
* Set response timeout limit (milliseconds). By default, this is set to
520568
* 10 seconds.
521569
*
522-
* @param value Response timeout in milliseconds, minimal value is 1000 (1 second).
570+
* @param value Response timeout in milliseconds, minimal value is 1000 (1 second).
523571
*/
524572
public void setResponseTimeout(int value) {
525573
responseTimeout = value < 1000 ? DEFAULT_SOCKET_TIMEOUT : value;

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 = "true".getBytes();
60-
private static final byte[] JSON_FALSE = "false".getBytes();
59+
private static final byte[] JSON_TRUE = AsyncHttpClient.TRUE.getBytes();
60+
private static final byte[] JSON_FALSE = AsyncHttpClient.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");

0 commit comments

Comments
 (0)