28
28
import java .util .logging .Level ;
29
29
import java .util .logging .Logger ;
30
30
31
- import javax . net . ssl . HttpsURLConnection ;
31
+
32
32
33
33
import com .viglet .turing .client .utils .TurClientUtils ;
34
34
import org .json .JSONException ;
54
54
import com .viglet .turing .client .sn .spotlight .TurSNSpotlightDocument ;
55
55
import com .viglet .turing .client .sn .utils .TurSNClientUtils ;
56
56
57
+ import javax .net .ssl .HttpsURLConnection ;
58
+
57
59
/**
58
60
* Connect to Turing AI Server.
59
61
*
@@ -276,12 +278,12 @@ public List<String> autoComplete(TurSNAutoCompleteQuery autoCompleteQuery) {
276
278
return executeAutoCompleteRequest (prepareAutoCompleteRequest (autoCompleteQuery ));
277
279
}
278
280
279
- private List <TurSNLocale > executeLocalesRequest (HttpsURLConnection httpsURLConnection ) {
281
+ private List <TurSNLocale > executeLocalesRequest (URLConnection urlConnection ) {
280
282
281
283
try {
282
284
283
- int responseCode = httpsURLConnection .getResponseCode ();
284
- String result = TurClientUtils .getTurResponseBody (httpsURLConnection , responseCode );
285
+ int responseCode = (( HttpsURLConnection ) urlConnection ) .getResponseCode ();
286
+ String result = TurClientUtils .getTurResponseBody (urlConnection , responseCode );
285
287
return new ObjectMapper ().readValue (result , new TypeReference <List <TurSNLocale >>() {
286
288
});
287
289
} catch (IOException e ) {
@@ -290,11 +292,11 @@ private List<TurSNLocale> executeLocalesRequest(HttpsURLConnection httpsURLConne
290
292
return null ;
291
293
}
292
294
293
- private List <String > executeAutoCompleteRequest (HttpsURLConnection httpsURLConnection ) {
295
+ private List <String > executeAutoCompleteRequest (URLConnection urlConnection ) {
294
296
String result ;
295
297
try {
296
- int responseCode = httpsURLConnection .getResponseCode ();
297
- result = TurClientUtils .getTurResponseBody (httpsURLConnection , responseCode );
298
+ int responseCode = (( HttpURLConnection ) urlConnection ) .getResponseCode ();
299
+ result = TurClientUtils .getTurResponseBody (urlConnection , responseCode );
298
300
return new ObjectMapper ().readValue (result , new TypeReference <List <String >>() {
299
301
});
300
302
} catch (IOException e ) {
@@ -304,7 +306,7 @@ private List<String> executeAutoCompleteRequest(HttpsURLConnection httpsURLConne
304
306
305
307
}
306
308
307
- private HttpsURLConnection prepareLocalesRequest () {
309
+ private URLConnection prepareLocalesRequest () {
308
310
try {
309
311
return prepareGetRequest (new URL (turSNServer .concat (LOCALES_CONTEXT )));
310
312
} catch (MalformedURLException e ) {
@@ -313,7 +315,7 @@ private HttpsURLConnection prepareLocalesRequest() {
313
315
return null ;
314
316
}
315
317
316
- private HttpsURLConnection prepareAutoCompleteRequest (TurSNAutoCompleteQuery autoCompleteQuery ) {
318
+ private URLConnection prepareAutoCompleteRequest (TurSNAutoCompleteQuery autoCompleteQuery ) {
317
319
318
320
try {
319
321
URL turingURL = new URL (turSNServer .concat (AUTO_COMPLETE_CONTEXT ));
@@ -343,7 +345,7 @@ public QueryTurSNResponse query(TurSNQuery turSNQuery) {
343
345
344
346
345
347
346
- private HttpsURLConnection prepareQueryRequest () {
348
+ private URLConnection prepareQueryRequest () {
347
349
348
350
try {
349
351
URL turingURL = new URL (turSNServer .concat (SEARCH_CONTEXT ));
@@ -409,25 +411,26 @@ private TurSNDocumentList setResultsResponse(TurSNSiteSearchBean turSNSiteSearch
409
411
return turSNDocumentList ;
410
412
}
411
413
412
- private HttpsURLConnection preparePostRequest (URL turingURL ) {
413
- HttpsURLConnection httpsURLConnection = null ;
414
+ private URLConnection preparePostRequest (URL turingURL ) {
415
+ URLConnection urlConnection = null ;
414
416
try {
415
417
URL url = TurClientUtils .getURL (turingURL .toString ());
416
- httpsURLConnection = (HttpsURLConnection ) url .openConnection ();
417
-
418
- httpsURLConnection .setSSLSocketFactory (new TLSSocketConnectionFactory ());
419
- httpsURLConnection .setRequestProperty (ACCEPT_HEADER , APPLICATION_JSON );
420
- httpsURLConnection .setRequestProperty (CONTENT_TYPE_HEADER , APPLICATION_JSON );
421
- httpsURLConnection .setRequestProperty (ACCEPT_ENCODING_HEADER , UTF_8 );
418
+ urlConnection = url .openConnection ();
419
+ if (turingURL .toString ().toLowerCase ().startsWith (HTTPS )) {
420
+ ((HttpsURLConnection ) urlConnection ).setSSLSocketFactory (new TLSSocketConnectionFactory ());
421
+ }
422
422
423
- httpsURLConnection .setRequestMethod (POST );
424
- httpsURLConnection .setDoOutput (true );
423
+ urlConnection .setRequestProperty (ACCEPT_HEADER , APPLICATION_JSON );
424
+ urlConnection .setRequestProperty (CONTENT_TYPE_HEADER , APPLICATION_JSON );
425
+ urlConnection .setRequestProperty (ACCEPT_ENCODING_HEADER , UTF_8 );
426
+ ((HttpURLConnection ) urlConnection ).setRequestMethod (POST );
427
+ urlConnection .setDoOutput (true );
425
428
426
- TurSNClientUtils .basicAuth (httpsURLConnection , this .getCredentials ());
429
+ TurSNClientUtils .basicAuth (urlConnection , this .getCredentials ());
427
430
428
431
String jsonResult = new ObjectMapper ().writeValueAsString (this .getTurSNSitePostParams ());
429
432
430
- OutputStream os = httpsURLConnection .getOutputStream ();
433
+ OutputStream os = urlConnection .getOutputStream ();
431
434
byte [] input = jsonResult .getBytes (UTF_8 );
432
435
os .write (input , 0 , input .length );
433
436
@@ -442,23 +445,24 @@ private HttpsURLConnection preparePostRequest(URL turingURL) {
442
445
logger .log (Level .SEVERE , e .getMessage (), e );
443
446
}
444
447
445
- return httpsURLConnection ;
448
+ return urlConnection ;
446
449
447
450
}
448
451
449
- private HttpsURLConnection prepareGetRequest (URL turingURL ) {
450
- HttpsURLConnection httpsURLConnection = null ;
452
+ private URLConnection prepareGetRequest (URL turingURL ) {
453
+ URLConnection urlConnection = null ;
451
454
try {
452
455
URL url = new URL (null , turingURL .toString (), new sun .net .www .protocol .https .Handler ());
453
- httpsURLConnection = (HttpsURLConnection ) url .openConnection ();
454
-
455
- httpsURLConnection .setSSLSocketFactory (new TLSSocketConnectionFactory ());
456
- httpsURLConnection .setRequestProperty (ACCEPT_HEADER , APPLICATION_JSON );
457
- httpsURLConnection .setRequestProperty (CONTENT_TYPE_HEADER , APPLICATION_JSON );
458
- httpsURLConnection .setRequestProperty (ACCEPT_ENCODING_HEADER , UTF_8 );
456
+ urlConnection = url .openConnection ();
457
+ if (turingURL .toString ().toLowerCase ().startsWith (HTTPS )) {
458
+ ((HttpsURLConnection ) urlConnection ).setSSLSocketFactory (new TLSSocketConnectionFactory ());
459
+ }
460
+ urlConnection .setRequestProperty (ACCEPT_HEADER , APPLICATION_JSON );
461
+ urlConnection .setRequestProperty (CONTENT_TYPE_HEADER , APPLICATION_JSON );
462
+ urlConnection .setRequestProperty (ACCEPT_ENCODING_HEADER , UTF_8 );
459
463
460
- httpsURLConnection .setRequestMethod ("GET" );
461
- httpsURLConnection .setDoOutput (true );
464
+ (( HttpURLConnection ) urlConnection ) .setRequestMethod ("GET" );
465
+ urlConnection .setDoOutput (true );
462
466
463
467
} catch (MalformedURLException e ) {
464
468
logger .log (Level .SEVERE , e .getMessage (), e );
@@ -468,7 +472,7 @@ private HttpsURLConnection prepareGetRequest(URL turingURL) {
468
472
logger .log (Level .SEVERE , e .getMessage (), e );
469
473
}
470
474
471
- return httpsURLConnection ;
475
+ return urlConnection ;
472
476
}
473
477
474
478
private void pageNumberRequest (URL turingURL ) {
0 commit comments