33
33
/**
34
34
* Internal class, representing the HttpRequest, done in asynchronous manner
35
35
*/
36
- public class AsyncHttpRequest implements PreProcessInterface , Runnable {
36
+ public class AsyncHttpRequest implements Runnable {
37
37
private final AbstractHttpClient client ;
38
38
private final HttpContext context ;
39
39
private final HttpUriRequest request ;
@@ -51,8 +51,26 @@ public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriR
51
51
this .responseHandler = responseHandler ;
52
52
}
53
53
54
- @ Override
55
- public void onPreProcess () {
54
+ /**
55
+ * This method is called once by the system when the request is about to be
56
+ * processed by the system. The library makes sure that a single request
57
+ * is pre-processed only once.
58
+ *
59
+ * @param request The request to pre-process
60
+ */
61
+ public void onPreProcessRequest (AsyncHttpRequest request ) {
62
+ // default action is to do nothing...
63
+ }
64
+
65
+ /**
66
+ * This method is called once by the system when the request has been fully
67
+ * sent, handled and finished. The library makes sure that a single request
68
+ * is post-processed only once.
69
+ *
70
+ * @param request The request to post-process
71
+ */
72
+ public void onPostProcessRequest (AsyncHttpRequest request ) {
73
+ // default action is to do nothing...
56
74
}
57
75
58
76
@ Override
@@ -64,7 +82,7 @@ public void run() {
64
82
// Carry out pre-processing for this request only once.
65
83
if (!isRequestPreProcessed ) {
66
84
isRequestPreProcessed = true ;
67
- onPreProcess ( );
85
+ onPreProcessRequest ( this );
68
86
}
69
87
70
88
if (isCancelled ()) {
@@ -97,13 +115,21 @@ public void run() {
97
115
responseHandler .sendFinishMessage ();
98
116
}
99
117
118
+ if (isCancelled ()) {
119
+ return ;
120
+ }
121
+
122
+ // Carry out post-processing for this request.
123
+ onPostProcessRequest (this );
124
+
100
125
isFinished = true ;
101
126
}
102
127
103
128
private void makeRequest () throws IOException {
104
129
if (isCancelled ()) {
105
130
return ;
106
131
}
132
+
107
133
// Fixes #115
108
134
if (request .getURI ().getScheme () == null ) {
109
135
// subclass of IOException so processed in the caller
@@ -112,12 +138,27 @@ private void makeRequest() throws IOException {
112
138
113
139
HttpResponse response = client .execute (request , context );
114
140
115
- if (!isCancelled () && responseHandler != null ) {
141
+ if (isCancelled ()) {
142
+ return ;
143
+ }
144
+
145
+ if (responseHandler != null ) {
116
146
// Carry out pre-processing for this response.
117
- responseHandler .onPreProcess ();
147
+ responseHandler .onPreProcessResponse (responseHandler , response );
148
+
149
+ if (isCancelled ()) {
150
+ return ;
151
+ }
118
152
119
153
// The response is ready, handle it.
120
154
responseHandler .sendResponseMessage (response );
155
+
156
+ if (isCancelled ()) {
157
+ return ;
158
+ }
159
+
160
+ // Carry out post-processing for this response.
161
+ responseHandler .onPostProcessResponse (responseHandler , response );
121
162
}
122
163
}
123
164
0 commit comments