4
4
import java .io .IOException ;
5
5
import java .io .InputStream ;
6
6
import java .io .UnsupportedEncodingException ;
7
+ import java .net .URLDecoder ;
8
+ import java .net .URLEncoder ;
7
9
import java .util .ArrayList ;
8
10
import java .util .Arrays ;
9
11
import java .util .Collection ;
@@ -111,13 +113,105 @@ protected WSAsyncRequest(String url, String encoding) {
111
113
super (url , encoding );
112
114
}
113
115
116
+ /**
117
+ * Returns the url but removed the queryString-part of it
118
+ * The QueryString-info is later added with addQueryString()
119
+ */
120
+ protected String getUrlWithoutQueryString () {
121
+ int i = url .indexOf ('?' );
122
+ if ( i > 0 ) {
123
+ return url .substring (0 ,i );
124
+ } else {
125
+ return url ;
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Adds the queryString-part of the url to the BoundRequestBuilder
131
+ * @return the same BoundRequestBuilder as passed in
132
+ */
133
+ protected BoundRequestBuilder addQueryString (BoundRequestBuilder requestBuilder ) {
134
+
135
+ // AsyncHttpClient is by default encoding everything in utf-8 so for us to be able to use
136
+ // different encoding we have configured AHC to use raw urls. When using raw urls,
137
+ // AHC does not encode url and QueryParam with utf-8 - but there is another problem:
138
+ // If we send raw (none-encoded) url (with queryString) to AHC, it does not url-encode it,
139
+ // but transform all illegal chars to '?'.
140
+ // If we pre-encoded the url with QueryString before sending it to AHC, ahc will decode it, and then
141
+ // later break it with '?'.
142
+
143
+ // This method basically does the same as RequestBuilderBase.buildUrl() except from destroying the
144
+ // pre-encoding
145
+
146
+ // does url contain query_string?
147
+ int i = url .indexOf ('?' );
148
+ if ( i > 0 ) {
149
+
150
+ try {
151
+ // extract query-string-part
152
+ String queryPart = url .substring (i +1 );
153
+
154
+ // parse queryPart - and decode it... (it is going to be re-encoded later)
155
+ for ( String param : queryPart .split ("&" )) {
156
+
157
+ i = param .indexOf ('=' );
158
+ String name ;
159
+ String value = null ;
160
+ if ( i <=0 ) {
161
+ // only a flag
162
+ name = URLDecoder .decode (param , encoding );
163
+ } else {
164
+ name = URLDecoder .decode (param .substring (0 ,i ), encoding );
165
+ value = URLDecoder .decode (param .substring (i +1 ), encoding );
166
+ }
167
+
168
+ if (value == null ) {
169
+ requestBuilder .addQueryParameter (URLEncoder .encode (name , encoding ), null );
170
+ } else {
171
+ requestBuilder .addQueryParameter (URLEncoder .encode (name , encoding ), URLEncoder .encode (value , encoding ));
172
+ }
173
+
174
+ }
175
+ } catch (UnsupportedEncodingException e ) {
176
+ throw new RuntimeException ("Error parsing query-part of url" ,e );
177
+ }
178
+ }
179
+
180
+ return requestBuilder ;
181
+ }
182
+
183
+
184
+ public BoundRequestBuilder prepareGet () {
185
+ return addQueryString (httpClient .prepareGet (getUrlWithoutQueryString ()));
186
+ }
187
+
188
+ public BoundRequestBuilder prepareOptions () {
189
+ return addQueryString (httpClient .prepareOptions (getUrlWithoutQueryString ()));
190
+ }
191
+
192
+ public BoundRequestBuilder prepareHead () {
193
+ return addQueryString (httpClient .prepareHead (getUrlWithoutQueryString ()));
194
+ }
195
+
196
+ public BoundRequestBuilder preparePost () {
197
+ return addQueryString (httpClient .preparePost (getUrlWithoutQueryString ()));
198
+ }
199
+
200
+ public BoundRequestBuilder preparePut () {
201
+ return addQueryString (httpClient .preparePut (getUrlWithoutQueryString ()));
202
+ }
203
+
204
+ public BoundRequestBuilder prepareDelete () {
205
+ return addQueryString (httpClient .prepareDelete (getUrlWithoutQueryString ()));
206
+ }
207
+
114
208
/** Execute a GET request synchronously. */
115
209
@ Override
116
210
public HttpResponse get () {
117
211
this .type = "GET" ;
118
212
sign ();
119
213
try {
120
- return new HttpAsyncResponse (prepare (httpClient . prepareGet (url )).execute ().get ());
214
+ return new HttpAsyncResponse (prepare (prepareGet ()).execute ().get ());
121
215
} catch (Exception e ) {
122
216
Logger .error (e .toString ());
123
217
throw new RuntimeException (e );
@@ -129,16 +223,17 @@ public HttpResponse get() {
129
223
public Promise <HttpResponse > getAsync () {
130
224
this .type = "GET" ;
131
225
sign ();
132
- return execute (httpClient . prepareGet (url ));
226
+ return execute (prepareGet ());
133
227
}
134
228
229
+
135
230
/** Execute a POST request.*/
136
231
@ Override
137
232
public HttpResponse post () {
138
233
this .type = "POST" ;
139
234
sign ();
140
235
try {
141
- return new HttpAsyncResponse (prepare (httpClient . preparePost (url )).execute ().get ());
236
+ return new HttpAsyncResponse (prepare (preparePost ()).execute ().get ());
142
237
} catch (Exception e ) {
143
238
throw new RuntimeException (e );
144
239
}
@@ -149,15 +244,15 @@ public HttpResponse post() {
149
244
public Promise <HttpResponse > postAsync () {
150
245
this .type = "POST" ;
151
246
sign ();
152
- return execute (httpClient . preparePost (url ));
247
+ return execute (preparePost ());
153
248
}
154
249
155
250
/** Execute a PUT request.*/
156
251
@ Override
157
252
public HttpResponse put () {
158
253
this .type = "PUT" ;
159
254
try {
160
- return new HttpAsyncResponse (prepare (httpClient . preparePut (url )).execute ().get ());
255
+ return new HttpAsyncResponse (prepare (preparePut ()).execute ().get ());
161
256
} catch (Exception e ) {
162
257
throw new RuntimeException (e );
163
258
}
@@ -167,15 +262,15 @@ public HttpResponse put() {
167
262
@ Override
168
263
public Promise <HttpResponse > putAsync () {
169
264
this .type = "PUT" ;
170
- return execute (httpClient . preparePut (url ));
265
+ return execute (preparePut ());
171
266
}
172
267
173
268
/** Execute a DELETE request.*/
174
269
@ Override
175
270
public HttpResponse delete () {
176
271
this .type = "DELETE" ;
177
272
try {
178
- return new HttpAsyncResponse (prepare (httpClient . prepareDelete (url )).execute ().get ());
273
+ return new HttpAsyncResponse (prepare (prepareDelete ()).execute ().get ());
179
274
} catch (Exception e ) {
180
275
throw new RuntimeException (e );
181
276
}
@@ -185,15 +280,15 @@ public HttpResponse delete() {
185
280
@ Override
186
281
public Promise <HttpResponse > deleteAsync () {
187
282
this .type = "DELETE" ;
188
- return execute (httpClient . prepareDelete (url ));
283
+ return execute (prepareDelete ());
189
284
}
190
285
191
286
/** Execute a OPTIONS request.*/
192
287
@ Override
193
288
public HttpResponse options () {
194
289
this .type = "OPTIONS" ;
195
290
try {
196
- return new HttpAsyncResponse (prepare (httpClient . prepareOptions (url )).execute ().get ());
291
+ return new HttpAsyncResponse (prepare (prepareOptions ()).execute ().get ());
197
292
} catch (Exception e ) {
198
293
throw new RuntimeException (e );
199
294
}
@@ -203,15 +298,15 @@ public HttpResponse options() {
203
298
@ Override
204
299
public Promise <HttpResponse > optionsAsync () {
205
300
this .type = "OPTIONS" ;
206
- return execute (httpClient . prepareOptions (url ));
301
+ return execute (prepareOptions ());
207
302
}
208
303
209
304
/** Execute a HEAD request.*/
210
305
@ Override
211
306
public HttpResponse head () {
212
307
this .type = "HEAD" ;
213
308
try {
214
- return new HttpAsyncResponse (prepare (httpClient . prepareHead (url )).execute ().get ());
309
+ return new HttpAsyncResponse (prepare (prepareHead ()).execute ().get ());
215
310
} catch (Exception e ) {
216
311
throw new RuntimeException (e );
217
312
}
@@ -221,7 +316,7 @@ public HttpResponse head() {
221
316
@ Override
222
317
public Promise <HttpResponse > headAsync () {
223
318
this .type = "HEAD" ;
224
- return execute (httpClient . prepareHead (url ));
319
+ return execute (prepareHead ());
225
320
}
226
321
227
322
/** Execute a TRACE request.*/
0 commit comments