1
1
package com .viglet .turing .client .ocr ;
2
2
3
- import com .fasterxml .jackson .core .type .TypeReference ;
4
3
import com .fasterxml .jackson .databind .ObjectMapper ;
5
4
import com .viglet .turing .client .sn .TurSNServer ;
6
5
import com .viglet .turing .client .sn .utils .TurSNClientUtils ;
10
9
11
10
import javax .net .ssl .HttpsURLConnection ;
12
11
import java .io .*;
12
+ import java .net .HttpURLConnection ;
13
13
import java .net .URI ;
14
14
import java .net .URL ;
15
- import java .util . List ;
15
+ import java .net . URLConnection ;
16
16
import java .util .logging .Level ;
17
17
import java .util .logging .Logger ;
18
18
@@ -36,6 +36,7 @@ public class TurOcr {
36
36
public static final String TWO_HYPHENS = "--" ;
37
37
public static final String BOUNDARY = "*****" ;
38
38
public static final String MULTIPART = "multipart/form-data" ;
39
+ public static final String HTTPS = "https" ;
39
40
40
41
public TurFileAttributes processUrl (TurSNServer turServer , URI url ) {
41
42
return getTurFileAttributes (turServer ,
@@ -52,42 +53,43 @@ public TurFileAttributes processFile(TurSNServer turServer, File file) {
52
53
private TurFileAttributes getTurFileAttributes (TurSNServer turServer , JSONObject jsonObject ,
53
54
String endpoint ) {
54
55
try {
55
- HttpsURLConnection httpsURLConnection = getHttpsURLConnection (turServer , endpoint );
56
- httpsURLConnection .setRequestProperty (CONTENT_TYPE_HEADER , APPLICATION_JSON );
57
- OutputStream os = httpsURLConnection .getOutputStream ();
56
+ URLConnection urlConnection = getURLConnection (turServer , endpoint );
57
+ urlConnection .setRequestProperty (CONTENT_TYPE_HEADER , APPLICATION_JSON );
58
+ OutputStream os = urlConnection .getOutputStream ();
58
59
byte [] input = jsonObject .toString ().getBytes (UTF_8 );
59
60
os .write (input , 0 , input .length );
60
61
61
- new ObjectMapper ().readValue (TurClientUtils .openConnectionAndRequest (httpsURLConnection ),
62
- new TypeReference <List <String >>() {
63
- });
62
+ return new ObjectMapper ().readValue (TurClientUtils .openConnectionAndRequest (urlConnection ),
63
+ TurFileAttributes .class );
64
64
} catch (IOException e ) {
65
65
logger .log (Level .SEVERE , e .getMessage (), e );
66
66
}
67
67
return null ;
68
68
}
69
69
70
- private static HttpsURLConnection getHttpsURLConnection (TurSNServer turServer , String endpoint ) throws IOException {
71
- URL url = new URL (null , endpoint , new sun .net .www .protocol .https .Handler ());
72
- HttpsURLConnection httpsURLConnection = (HttpsURLConnection ) url .openConnection ();
73
- httpsURLConnection .setSSLSocketFactory (new TLSSocketConnectionFactory ());
74
- httpsURLConnection .setRequestProperty (ACCEPT_HEADER , APPLICATION_JSON );
75
- httpsURLConnection .setRequestProperty (ACCEPT_ENCODING_HEADER , UTF_8 );
76
- httpsURLConnection .setRequestProperty (CONNECTION , KEEP_ALIVE );
77
- httpsURLConnection .setRequestProperty (CACHE_CONTROL , NO_CACHE );
78
- httpsURLConnection .setRequestMethod (POST );
79
- httpsURLConnection .setUseCaches (false );
80
- httpsURLConnection .setDoOutput (true );
81
- TurSNClientUtils .basicAuth (httpsURLConnection , turServer .getCredentials ());
70
+ private static URLConnection getURLConnection (TurSNServer turServer , String endpoint ) throws IOException {
71
+ URL url = TurClientUtils .getURL (endpoint );
72
+ URLConnection urlConnection = url .openConnection ();
73
+ if (endpoint .toLowerCase ().startsWith (HTTPS )) {
74
+ ((HttpsURLConnection ) urlConnection ).setSSLSocketFactory (new TLSSocketConnectionFactory ());
75
+ }
76
+ urlConnection .setRequestProperty (ACCEPT_HEADER , APPLICATION_JSON );
77
+ urlConnection .setRequestProperty (ACCEPT_ENCODING_HEADER , UTF_8 );
78
+ urlConnection .setRequestProperty (CONNECTION , KEEP_ALIVE );
79
+ urlConnection .setRequestProperty (CACHE_CONTROL , NO_CACHE );
80
+ ((HttpURLConnection ) urlConnection ).setRequestMethod (POST );
81
+ urlConnection .setUseCaches (false );
82
+ urlConnection .setDoOutput (true );
83
+ TurSNClientUtils .basicAuth (urlConnection , turServer .getCredentials ());
82
84
83
- return httpsURLConnection ;
85
+ return urlConnection ;
84
86
}
85
87
86
88
private TurFileAttributes getTurFileAttributes (TurSNServer turServer , File file ,
87
89
String endpoint ) {
88
90
String attachmentFileName = file .getName ();
89
91
try {
90
- HttpsURLConnection httpsURLConnection = getHttpsURLConnection (turServer , endpoint );
92
+ URLConnection httpsURLConnection = getURLConnection (turServer , endpoint );
91
93
httpsURLConnection .setRequestProperty (
92
94
CONTENT_TYPE_HEADER , MULTIPART + ";boundary=" + BOUNDARY );
93
95
DataOutputStream request = new DataOutputStream (
0 commit comments