Skip to content

Commit ac66e58

Browse files
committed
- JDK6: Replaced HttpsURLConnection per URLConnection
1 parent d3494a4 commit ac66e58

File tree

3 files changed

+139
-136
lines changed

3 files changed

+139
-136
lines changed

turing-jdk6/turing-java-sdk-jdk6/src/main/java/com/viglet/turing/client/ocr/TurOcr.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ private TurFileAttributes getTurFileAttributes(TurSNServer turServer, File file,
8989
String endpoint) {
9090
String attachmentFileName = file.getName();
9191
try {
92-
URLConnection httpsURLConnection = getURLConnection(turServer, endpoint);
93-
httpsURLConnection.setRequestProperty(
92+
URLConnection urlConnection = getURLConnection(turServer, endpoint);
93+
urlConnection.setRequestProperty(
9494
CONTENT_TYPE_HEADER, MULTIPART + ";boundary=" + BOUNDARY);
9595
DataOutputStream request = new DataOutputStream(
96-
httpsURLConnection.getOutputStream());
96+
urlConnection.getOutputStream());
9797
request.writeBytes(TWO_HYPHENS + BOUNDARY + CRLF);
9898
request.writeBytes("Content-Disposition: form-data; name=\"" +
9999
FILE + "\";filename=\"" +
@@ -111,7 +111,7 @@ private TurFileAttributes getTurFileAttributes(TurSNServer turServer, File file,
111111
request.flush();
112112
request.close();
113113

114-
return new ObjectMapper().readValue(TurClientUtils.openConnectionAndRequest(httpsURLConnection),
114+
return new ObjectMapper().readValue(TurClientUtils.openConnectionAndRequest(urlConnection),
115115
TurFileAttributes.class);
116116
} catch (IOException e) {
117117
logger.log(Level.SEVERE, e.getMessage(), e);

turing-jdk6/turing-java-sdk-jdk6/src/main/java/com/viglet/turing/client/sn/TurSNServer.java

+38-34
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.logging.Level;
2929
import java.util.logging.Logger;
3030

31-
import javax.net.ssl.HttpsURLConnection;
31+
3232

3333
import com.viglet.turing.client.utils.TurClientUtils;
3434
import org.json.JSONException;
@@ -54,6 +54,8 @@
5454
import com.viglet.turing.client.sn.spotlight.TurSNSpotlightDocument;
5555
import com.viglet.turing.client.sn.utils.TurSNClientUtils;
5656

57+
import javax.net.ssl.HttpsURLConnection;
58+
5759
/**
5860
* Connect to Turing AI Server.
5961
*
@@ -276,12 +278,12 @@ public List<String> autoComplete(TurSNAutoCompleteQuery autoCompleteQuery) {
276278
return executeAutoCompleteRequest(prepareAutoCompleteRequest(autoCompleteQuery));
277279
}
278280

279-
private List<TurSNLocale> executeLocalesRequest(HttpsURLConnection httpsURLConnection) {
281+
private List<TurSNLocale> executeLocalesRequest(URLConnection urlConnection) {
280282

281283
try {
282284

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);
285287
return new ObjectMapper().readValue(result, new TypeReference<List<TurSNLocale>>() {
286288
});
287289
} catch (IOException e) {
@@ -290,11 +292,11 @@ private List<TurSNLocale> executeLocalesRequest(HttpsURLConnection httpsURLConne
290292
return null;
291293
}
292294

293-
private List<String> executeAutoCompleteRequest(HttpsURLConnection httpsURLConnection) {
295+
private List<String> executeAutoCompleteRequest(URLConnection urlConnection) {
294296
String result;
295297
try {
296-
int responseCode = httpsURLConnection.getResponseCode();
297-
result = TurClientUtils.getTurResponseBody(httpsURLConnection, responseCode);
298+
int responseCode = ((HttpURLConnection) urlConnection).getResponseCode();
299+
result = TurClientUtils.getTurResponseBody(urlConnection, responseCode);
298300
return new ObjectMapper().readValue(result, new TypeReference<List<String>>() {
299301
});
300302
} catch (IOException e) {
@@ -304,7 +306,7 @@ private List<String> executeAutoCompleteRequest(HttpsURLConnection httpsURLConne
304306

305307
}
306308

307-
private HttpsURLConnection prepareLocalesRequest() {
309+
private URLConnection prepareLocalesRequest() {
308310
try {
309311
return prepareGetRequest(new URL(turSNServer.concat(LOCALES_CONTEXT)));
310312
} catch (MalformedURLException e) {
@@ -313,7 +315,7 @@ private HttpsURLConnection prepareLocalesRequest() {
313315
return null;
314316
}
315317

316-
private HttpsURLConnection prepareAutoCompleteRequest(TurSNAutoCompleteQuery autoCompleteQuery) {
318+
private URLConnection prepareAutoCompleteRequest(TurSNAutoCompleteQuery autoCompleteQuery) {
317319

318320
try {
319321
URL turingURL = new URL(turSNServer.concat(AUTO_COMPLETE_CONTEXT));
@@ -343,7 +345,7 @@ public QueryTurSNResponse query(TurSNQuery turSNQuery) {
343345

344346

345347

346-
private HttpsURLConnection prepareQueryRequest() {
348+
private URLConnection prepareQueryRequest() {
347349

348350
try {
349351
URL turingURL = new URL(turSNServer.concat(SEARCH_CONTEXT));
@@ -409,25 +411,26 @@ private TurSNDocumentList setResultsResponse(TurSNSiteSearchBean turSNSiteSearch
409411
return turSNDocumentList;
410412
}
411413

412-
private HttpsURLConnection preparePostRequest(URL turingURL) {
413-
HttpsURLConnection httpsURLConnection = null;
414+
private URLConnection preparePostRequest(URL turingURL) {
415+
URLConnection urlConnection = null;
414416
try {
415417
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+
}
422422

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);
425428

426-
TurSNClientUtils.basicAuth(httpsURLConnection, this.getCredentials());
429+
TurSNClientUtils.basicAuth(urlConnection, this.getCredentials());
427430

428431
String jsonResult = new ObjectMapper().writeValueAsString(this.getTurSNSitePostParams());
429432

430-
OutputStream os = httpsURLConnection.getOutputStream();
433+
OutputStream os = urlConnection.getOutputStream();
431434
byte[] input = jsonResult.getBytes(UTF_8);
432435
os.write(input, 0, input.length);
433436

@@ -442,23 +445,24 @@ private HttpsURLConnection preparePostRequest(URL turingURL) {
442445
logger.log(Level.SEVERE, e.getMessage(), e);
443446
}
444447

445-
return httpsURLConnection;
448+
return urlConnection;
446449

447450
}
448451

449-
private HttpsURLConnection prepareGetRequest(URL turingURL) {
450-
HttpsURLConnection httpsURLConnection = null;
452+
private URLConnection prepareGetRequest(URL turingURL) {
453+
URLConnection urlConnection = null;
451454
try {
452455
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);
459463

460-
httpsURLConnection.setRequestMethod("GET");
461-
httpsURLConnection.setDoOutput(true);
464+
((HttpURLConnection) urlConnection).setRequestMethod("GET");
465+
urlConnection.setDoOutput(true);
462466

463467
} catch (MalformedURLException e) {
464468
logger.log(Level.SEVERE, e.getMessage(), e);
@@ -468,7 +472,7 @@ private HttpsURLConnection prepareGetRequest(URL turingURL) {
468472
logger.log(Level.SEVERE, e.getMessage(), e);
469473
}
470474

471-
return httpsURLConnection;
475+
return urlConnection;
472476
}
473477

474478
private void pageNumberRequest(URL turingURL) {

0 commit comments

Comments
 (0)