7
7
import static com .google .common .collect .Lists .newArrayList ;
8
8
import static javax .ws .rs .core .HttpHeaders .AUTHORIZATION ;
9
9
import static javax .xml .bind .DatatypeConverter .printBase64Binary ;
10
+ import static org .apache .http .HttpVersion .HTTP_1_0 ;
11
+ import static org .apache .http .HttpVersion .HTTP_1_1 ;
10
12
import static org .slf4j .LoggerFactory .getLogger ;
11
13
import static se .bjurr .prnfb .http .UrlInvoker .HTTP_METHOD .GET ;
12
14
import static se .bjurr .prnfb .http .UrlInvoker .HTTP_METHOD .POST ;
24
26
25
27
import org .apache .http .HttpEntity ;
26
28
import org .apache .http .HttpHost ;
29
+ import org .apache .http .HttpVersion ;
30
+ import org .apache .http .ProtocolVersion ;
27
31
import org .apache .http .auth .AuthScope ;
28
32
import org .apache .http .auth .UsernamePasswordCredentials ;
29
33
import org .apache .http .client .CredentialsProvider ;
@@ -73,7 +77,7 @@ public enum HTTP_METHOD {
73
77
private static final Logger LOG = getLogger (UrlInvoker .class );
74
78
75
79
@ VisibleForTesting
76
- public static String getHeaderValue (PrnfbHeader header ) {
80
+ public static String getHeaderValue (final PrnfbHeader header ) {
77
81
return header .getValue ();
78
82
}
79
83
@@ -95,10 +99,11 @@ public static UrlInvoker urlInvoker() {
95
99
private boolean shouldAcceptAnyCertificate ;
96
100
97
101
private String urlParam ;
102
+ private ProtocolVersion httpVersion = HttpVersion .HTTP_1_0 ;
98
103
99
104
UrlInvoker () {}
100
105
101
- public UrlInvoker appendBasicAuth (PrnfbNotification notification ) {
106
+ public UrlInvoker appendBasicAuth (final PrnfbNotification notification ) {
102
107
if (notification .getUser ().isPresent () && notification .getPassword ().isPresent ()) {
103
108
final String userpass = notification .getUser ().get () + ":" + notification .getPassword ().get ();
104
109
final String basicAuth = "Basic " + new String (printBase64Binary (userpass .getBytes (UTF_8 )));
@@ -155,14 +160,26 @@ public String getUrlParam() {
155
160
return this .urlParam ;
156
161
}
157
162
163
+ public UrlInvoker setHttpVersion (final String httpVersion ) {
164
+ if (httpVersion == null || httpVersion .equals ("HTTP_1_0" )) {
165
+ this .httpVersion = HTTP_1_0 ;
166
+ } else if (httpVersion .equals ("HTTP_1_1" )) {
167
+ this .httpVersion = HTTP_1_1 ;
168
+ } else {
169
+ this .httpVersion = HTTP_1_0 ;
170
+ }
171
+ return this ;
172
+ }
173
+
158
174
public HttpResponse invoke () {
159
175
LOG .info ("Url: \" " + this .urlParam + "\" " );
160
176
161
- HttpRequestBase httpRequestBase = newHttpRequestBase ();
177
+ final HttpRequestBase httpRequestBase = newHttpRequestBase ();
162
178
configureUrl (httpRequestBase );
163
179
addHeaders (httpRequestBase );
180
+ httpRequestBase .setProtocolVersion (httpVersion );
164
181
165
- HttpClientBuilder builder = HttpClientBuilder .create ();
182
+ final HttpClientBuilder builder = HttpClientBuilder .create ();
166
183
configureSsl (builder );
167
184
configureProxy (builder );
168
185
@@ -176,7 +193,7 @@ public HttpResponse invoke() {
176
193
return this .response ;
177
194
}
178
195
179
- public void setResponse (HttpResponse response ) {
196
+ public void setResponse (final HttpResponse response ) {
180
197
this .response = response ;
181
198
}
182
199
@@ -185,7 +202,7 @@ public boolean shouldAcceptAnyCertificate() {
185
202
return this .shouldAcceptAnyCertificate ;
186
203
}
187
204
188
- public UrlInvoker shouldAcceptAnyCertificate (boolean shouldAcceptAnyCertificate ) {
205
+ public UrlInvoker shouldAcceptAnyCertificate (final boolean shouldAcceptAnyCertificate ) {
189
206
this .shouldAcceptAnyCertificate = shouldAcceptAnyCertificate ;
190
207
return this ;
191
208
}
@@ -205,58 +222,58 @@ public boolean shouldUseProxy() {
205
222
return getProxyHost ().isPresent () && getProxyPort ().isPresent () && getProxyPort ().get () > 0 ;
206
223
}
207
224
208
- public UrlInvoker withClientKeyStore (ClientKeyStore clientKeyStore ) {
225
+ public UrlInvoker withClientKeyStore (final ClientKeyStore clientKeyStore ) {
209
226
this .clientKeyStore = clientKeyStore ;
210
227
return this ;
211
228
}
212
229
213
- public UrlInvoker withHeader (String name , String value ) {
230
+ public UrlInvoker withHeader (final String name , final String value ) {
214
231
this .headers .add (new PrnfbHeader (name , value ));
215
232
return this ;
216
233
}
217
234
218
- public UrlInvoker withMethod (HTTP_METHOD method ) {
235
+ public UrlInvoker withMethod (final HTTP_METHOD method ) {
219
236
this .method = method ;
220
237
return this ;
221
238
}
222
239
223
- public UrlInvoker withPostContent (Optional <String > postContent ) {
240
+ public UrlInvoker withPostContent (final Optional <String > postContent ) {
224
241
this .postContent = postContent ;
225
242
return this ;
226
243
}
227
244
228
- public UrlInvoker withProxyPassword (Optional <String > proxyPassword ) {
245
+ public UrlInvoker withProxyPassword (final Optional <String > proxyPassword ) {
229
246
this .proxyPassword = proxyPassword ;
230
247
return this ;
231
248
}
232
249
233
- public UrlInvoker withProxyPort (Integer proxyPort ) {
250
+ public UrlInvoker withProxyPort (final Integer proxyPort ) {
234
251
this .proxyPort = fromNullable (proxyPort );
235
252
return this ;
236
253
}
237
254
238
- public UrlInvoker withProxySchema (Optional <String > proxySchema ) {
255
+ public UrlInvoker withProxySchema (final Optional <String > proxySchema ) {
239
256
this .proxySchema = proxySchema ;
240
257
return this ;
241
258
}
242
259
243
- public UrlInvoker withProxyServer (Optional <String > proxyHost ) {
260
+ public UrlInvoker withProxyServer (final Optional <String > proxyHost ) {
244
261
this .proxyHost = proxyHost ;
245
262
return this ;
246
263
}
247
264
248
- public UrlInvoker withProxyUser (Optional <String > proxyUser ) {
265
+ public UrlInvoker withProxyUser (final Optional <String > proxyUser ) {
249
266
this .proxyUser = proxyUser ;
250
267
return this ;
251
268
}
252
269
253
- public UrlInvoker withUrlParam (String urlParam ) {
270
+ public UrlInvoker withUrlParam (final String urlParam ) {
254
271
this .urlParam = urlParam .replaceAll ("\\ s" , "%20" );
255
272
return this ;
256
273
}
257
274
258
- private void addHeaders (HttpRequestBase httpRequestBase ) {
259
- for (PrnfbHeader header : this .headers ) {
275
+ private void addHeaders (final HttpRequestBase httpRequestBase ) {
276
+ for (final PrnfbHeader header : this .headers ) {
260
277
261
278
if (header .getName ().equals (AUTHORIZATION )) {
262
279
LOG .debug ("header: \" " + header .getName () + "\" value: \" **********\" " );
@@ -268,20 +285,20 @@ private void addHeaders(HttpRequestBase httpRequestBase) {
268
285
}
269
286
}
270
287
271
- private void configureUrl (HttpRequestBase httpRequestBase ) {
288
+ private void configureUrl (final HttpRequestBase httpRequestBase ) {
272
289
try {
273
290
httpRequestBase .setURI (new URI (this .urlParam ));
274
- } catch (URISyntaxException e ) {
291
+ } catch (final URISyntaxException e ) {
275
292
propagate (e );
276
293
}
277
294
}
278
295
279
296
private SSLContextBuilder doAcceptAnyCertificate (SSLContextBuilder customContext )
280
297
throws Exception {
281
- TrustStrategy easyStrategy =
298
+ final TrustStrategy easyStrategy =
282
299
new TrustStrategy () {
283
300
@ Override
284
- public boolean isTrusted (X509Certificate [] chain , String authType ) {
301
+ public boolean isTrusted (final X509Certificate [] chain , final String authType ) {
285
302
return true ;
286
303
}
287
304
};
@@ -291,7 +308,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) {
291
308
}
292
309
293
310
private SSLContext newSslContext () throws Exception {
294
- SSLContextBuilder sslContextBuilder = SSLContexts .custom ();
311
+ final SSLContextBuilder sslContextBuilder = SSLContexts .custom ();
295
312
if (this .shouldAcceptAnyCertificate ) {
296
313
doAcceptAnyCertificate (sslContextBuilder );
297
314
if (this .clientKeyStore .getKeyStore ().isPresent ()) {
@@ -308,16 +325,16 @@ private boolean shouldUseSsl() {
308
325
}
309
326
310
327
@ VisibleForTesting
311
- HttpClientBuilder configureProxy (HttpClientBuilder builder ) {
328
+ HttpClientBuilder configureProxy (final HttpClientBuilder builder ) {
312
329
if (!shouldUseProxy ()) {
313
330
return builder ;
314
331
}
315
332
316
333
if (this .proxyUser .isPresent () && this .proxyPassword .isPresent ()) {
317
- String username = this .proxyUser .get ();
318
- String password = this .proxyPassword .get ();
319
- UsernamePasswordCredentials creds = new UsernamePasswordCredentials (username , password );
320
- CredentialsProvider credsProvider = new BasicCredentialsProvider ();
334
+ final String username = this .proxyUser .get ();
335
+ final String password = this .proxyPassword .get ();
336
+ final UsernamePasswordCredentials creds = new UsernamePasswordCredentials (username , password );
337
+ final CredentialsProvider credsProvider = new BasicCredentialsProvider ();
321
338
credsProvider .setCredentials (
322
339
new AuthScope (this .proxyHost .get (), this .proxyPort .get ()), creds );
323
340
builder .setDefaultCredentialsProvider (credsProvider );
@@ -331,44 +348,44 @@ HttpClientBuilder configureProxy(HttpClientBuilder builder) {
331
348
}
332
349
333
350
@ VisibleForTesting
334
- HttpClientBuilder configureSsl (HttpClientBuilder builder ) {
351
+ HttpClientBuilder configureSsl (final HttpClientBuilder builder ) {
335
352
if (shouldUseSsl ()) {
336
353
try {
337
- SSLContext s = newSslContext ();
338
- SSLConnectionSocketFactory sslConnSocketFactory = new SSLConnectionSocketFactory (s );
354
+ final SSLContext s = newSslContext ();
355
+ final SSLConnectionSocketFactory sslConnSocketFactory = new SSLConnectionSocketFactory (s );
339
356
builder .setSSLSocketFactory (sslConnSocketFactory );
340
357
341
- Registry <ConnectionSocketFactory > registry =
358
+ final Registry <ConnectionSocketFactory > registry =
342
359
RegistryBuilder .<ConnectionSocketFactory >create ()
343
360
.register ("https" , sslConnSocketFactory )
344
361
.build ();
345
362
346
- HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager (registry );
363
+ final HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager (registry );
347
364
348
365
builder .setConnectionManager (ccm );
349
- } catch (Exception e ) {
366
+ } catch (final Exception e ) {
350
367
propagate (e );
351
368
}
352
369
}
353
370
return builder ;
354
371
}
355
372
356
373
@ VisibleForTesting
357
- HttpResponse doInvoke (HttpRequestBase httpRequestBase , HttpClientBuilder builder ) {
374
+ HttpResponse doInvoke (final HttpRequestBase httpRequestBase , final HttpClientBuilder builder ) {
358
375
CloseableHttpResponse httpResponse = null ;
359
376
try {
360
377
httpResponse =
361
378
builder //
362
379
.build () //
363
380
.execute (httpRequestBase );
364
381
365
- HttpEntity entity = httpResponse .getEntity ();
382
+ final HttpEntity entity = httpResponse .getEntity ();
366
383
String entityString = "" ;
367
384
if (entity != null ) {
368
385
entityString = EntityUtils .toString (entity , UTF_8 );
369
386
}
370
- URI uri = httpRequestBase .getURI ();
371
- int statusCode = httpResponse .getStatusLine ().getStatusCode ();
387
+ final URI uri = httpRequestBase .getURI ();
388
+ final int statusCode = httpResponse .getStatusLine ().getStatusCode ();
372
389
return new HttpResponse (uri , statusCode , entityString );
373
390
} catch (final Exception e ) {
374
391
LOG .error ("" , e );
@@ -377,7 +394,7 @@ HttpResponse doInvoke(HttpRequestBase httpRequestBase, HttpClientBuilder builder
377
394
if (httpResponse != null ) {
378
395
httpResponse .close ();
379
396
}
380
- } catch (IOException e ) {
397
+ } catch (final IOException e ) {
381
398
propagate (e );
382
399
}
383
400
}
@@ -386,8 +403,8 @@ HttpResponse doInvoke(HttpRequestBase httpRequestBase, HttpClientBuilder builder
386
403
387
404
@ VisibleForTesting
388
405
HttpEntityEnclosingRequestBase newHttpEntityEnclosingRequestBase (
389
- HTTP_METHOD method , String entity ) {
390
- HttpEntityEnclosingRequestBase entityEnclosing =
406
+ final HTTP_METHOD method , final String entity ) {
407
+ final HttpEntityEnclosingRequestBase entityEnclosing =
391
408
new HttpEntityEnclosingRequestBase () {
392
409
@ Override
393
410
public String getMethod () {
@@ -409,7 +426,7 @@ HttpRequestBase newHttpRequestBase() {
409
426
}
410
427
411
428
@ VisibleForTesting
412
- HttpRequestBase newHttpRequestBase (HTTP_METHOD method ) {
429
+ HttpRequestBase newHttpRequestBase (final HTTP_METHOD method ) {
413
430
return new HttpRequestBase () {
414
431
@ Override
415
432
public String getMethod () {
0 commit comments