@@ -122,6 +122,9 @@ public class AsyncHttpClient {
122
122
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition" ;
123
123
public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding" ;
124
124
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" ;
125
128
126
129
public static final int DEFAULT_MAX_CONNECTIONS = 10 ;
127
130
public static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000 ;
@@ -138,6 +141,7 @@ public class AsyncHttpClient {
138
141
private ExecutorService threadPool ;
139
142
private final Map <Context , List <RequestHandle >> requestMap ;
140
143
private final Map <String , String > clientHeaderMap ;
144
+ private PreProcessingInterface preProcessor ;
141
145
private boolean isUrlEncodingEnabled = true ;
142
146
143
147
/**
@@ -272,6 +276,7 @@ public void process(HttpResponse response, HttpContext context) {
272
276
if (entity == null ) {
273
277
return ;
274
278
}
279
+
275
280
final Header encoding = entity .getContentEncoding ();
276
281
if (encoding != null ) {
277
282
for (HeaderElement element : encoding .getElements ()) {
@@ -281,6 +286,18 @@ public void process(HttpResponse response, HttpContext context) {
281
286
}
282
287
}
283
288
}
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
+ }
284
301
}
285
302
});
286
303
@@ -300,6 +317,18 @@ public void process(final HttpRequest request, final HttpContext context) throws
300
317
authState .setCredentials (creds );
301
318
}
302
319
}
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
+ }
303
332
}
304
333
}, 0 );
305
334
@@ -370,6 +399,25 @@ public ExecutorService getThreadPool() {
370
399
return threadPool ;
371
400
}
372
401
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
+
373
421
/**
374
422
* Get the default threading pool to be used for this HTTP client.
375
423
*
@@ -519,7 +567,7 @@ public int getResponseTimeout() {
519
567
* Set response timeout limit (milliseconds). By default, this is set to
520
568
* 10 seconds.
521
569
*
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).
523
571
*/
524
572
public void setResponseTimeout (int value ) {
525
573
responseTimeout = value < 1000 ? DEFAULT_SOCKET_TIMEOUT : value ;
0 commit comments