Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

fix: send request with TLSv1.2 #90

Merged
merged 3 commits into from Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions build.gradle
@@ -1,13 +1,13 @@
plugins {
id "java"
id "application"
id "org.beryx.runtime" version "1.11.1"
id "org.beryx.runtime" version "1.11.4"
id "edu.sc.seis.launch4j" version '2.4.6'
id "org.openjfx.javafxplugin" version "0.0.9"
}

group "blaise.img2latex"
version "0.7.6"
version "0.7.7"

compileJava {
sourceCompatibility = 14
Expand All @@ -19,7 +19,7 @@ repositories {
}

javafx {
version = "14.0.1"
version = "14.0.2.1"
}

application {
Expand All @@ -39,20 +39,20 @@ dependencies {
implementation "com.google.code.gson:gson:2.8.6"
implementation "org.scilab.forge:jlatexmath:1.0.7"
if (project.hasProperty("windows")) {
implementation "org.openjfx:javafx-base:14.0.1:win"
implementation "org.openjfx:javafx-swing:14.0.1:win"
implementation "org.openjfx:javafx-graphics:14.0.1:win"
implementation "org.openjfx:javafx-controls:14.0.1:win"
implementation "org.openjfx:javafx-base:14.0.2.1:win"
implementation "org.openjfx:javafx-swing:14.0.2.1:win"
implementation "org.openjfx:javafx-graphics:14.0.2.1:win"
implementation "org.openjfx:javafx-controls:14.0.2.1:win"
} else if (project.hasProperty("linux")) {
implementation "org.openjfx:javafx-base:14.0.1:linux"
implementation "org.openjfx:javafx-swing:14.0.1:linux"
implementation "org.openjfx:javafx-graphics:14.0.1:linux"
implementation "org.openjfx:javafx-controls:14.0.1:linux"
implementation "org.openjfx:javafx-base:14.0.2.1:linux"
implementation "org.openjfx:javafx-swing:14.0.2.1:linux"
implementation "org.openjfx:javafx-graphics:14.0.2.1:linux"
implementation "org.openjfx:javafx-controls:14.0.2.1:linux"
} else {
implementation "org.openjfx:javafx-base:14.0.1:mac"
implementation "org.openjfx:javafx-swing:14.0.1:mac"
implementation "org.openjfx:javafx-graphics:14.0.1:mac"
implementation "org.openjfx:javafx-controls:14.0.1:mac"
implementation "org.openjfx:javafx-base:14.0.2.1:mac"
implementation "org.openjfx:javafx-swing:14.0.2.1:mac"
implementation "org.openjfx:javafx-graphics:14.0.2.1:mac"
implementation "org.openjfx:javafx-controls:14.0.2.1:mac"
}
}

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,5 +1,4 @@
#Fri Jul 10 21:13:11 CST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Expand Up @@ -130,7 +130,7 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`

JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
Expand Down
21 changes: 3 additions & 18 deletions gradlew.bat
Expand Up @@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
if "%ERRORLEVEL%" == "0" goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand All @@ -54,7 +54,7 @@ goto fail
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init
if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
Expand All @@ -64,29 +64,14 @@ echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
Expand Down
6 changes: 3 additions & 3 deletions scripts/jdk_setup.sh
Expand Up @@ -16,9 +16,9 @@ WINDOWS_JRE_FILE=./windows_jre.zip

mkdir -p $JDK_PARENT_PATH/$LINUX_JDK_PATH $JDK_PARENT_PATH/$MACOS_JDK_PATH $JDK_PARENT_PATH/$WINDOWS_JRE_PATH

wget -nc -O $LINUX_JDK_FILE https://github.com/AdoptOpenJDK/openjdk14-binaries/releases/download/jdk-14.0.1%2B7/OpenJDK14U-jdk_x64_linux_hotspot_14.0.1_7.tar.gz
wget -nc -O $MACOS_JDK_FILE https://github.com/AdoptOpenJDK/openjdk14-binaries/releases/download/jdk-14.0.1%2B7/OpenJDK14U-jdk_x64_mac_hotspot_14.0.1_7.tar.gz
wget -nc -O $WINDOWS_JRE_FILE https://github.com/AdoptOpenJDK/openjdk14-binaries/releases/download/jdk-14.0.1%2B7.1/OpenJDK14U-jre_x64_windows_hotspot_14.0.1_7.zip
wget -nc -O $LINUX_JDK_FILE https://github.com/AdoptOpenJDK/openjdk14-binaries/releases/download/jdk-14.0.2%2B12/OpenJDK14U-jdk_x64_linux_hotspot_14.0.2_12.tar.gz
wget -nc -O $MACOS_JDK_FILE https://github.com/AdoptOpenJDK/openjdk14-binaries/releases/download/jdk-14.0.2%2B12/OpenJDK14U-jdk_x64_mac_hotspot_14.0.2_12.tar.gz
wget -nc -O $WINDOWS_JRE_FILE https://github.com/AdoptOpenJDK/openjdk14-binaries/releases/download/jdk-14.0.2%2B12/OpenJDK14U-jre_x64_windows_hotspot_14.0.2_12.zip

bsdtar -zxf $LINUX_JDK_FILE -C $JDK_PARENT_PATH/$LINUX_JDK_PATH --strip 1 && rm $LINUX_JDK_FILE
bsdtar -zxf $MACOS_JDK_FILE -C $JDK_PARENT_PATH/$MACOS_JDK_PATH --strip 1 && rm $MACOS_JDK_FILE
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/IOUtils.java
Expand Up @@ -3,7 +3,6 @@
import javafx.scene.image.Image;

import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -107,7 +106,7 @@ public static boolean isAPICredentialConfigValid() {
* @return current date formatted like ${year}-${month}.
*/
public static String getCurrentDate() {
LocalDate localDate = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
var localDate = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return localDate.getYear() + "-" + localDate.getMonthValue();
}

Expand Down
32 changes: 18 additions & 14 deletions src/main/java/io/OCRRequestHelper.java
Expand Up @@ -3,12 +3,14 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import javax.net.ssl.SSLContext;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.concurrent.ExecutionException;

Expand All @@ -27,7 +29,7 @@ public class OCRRequestHelper {
* @param parameters JsonObject to send as the request parameters.
* @return a IO.Response object.
*/
public static Response getResult(JsonObject parameters) {
public static Response getResult(JsonObject parameters) throws NoSuchAlgorithmException {

String appId;
String appKey;
Expand All @@ -42,35 +44,37 @@ public static Response getResult(JsonObject parameters) {
return new Response(IOUtils.INVALID_CREDENTIALS_ERROR);
}

// default protocols: [TLSv1.3, TLSv1.2]
HttpClient httpClient;
// TLSv1.2 only
var sslParameters = SSLContext.getDefault().getDefaultSSLParameters();
sslParameters.setProtocols(new String[]{"TLSv1.2"});
var httpClientBuilder = HttpClient.newBuilder().sslParameters(sslParameters);

// HTTP version 2 first, then HTTP version 1.1
if (PreferenceHelper.getProxyEnableOption()) {
// proxy enabled
var config = PreferenceHelper.getProxyConfig();
if (config.isValid()) {
httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).
proxy(ProxySelector.of(new InetSocketAddress(config.getHostname(), config.getPort()))).build();
var proxyConfig = PreferenceHelper.getProxyConfig();
if (proxyConfig.isValid()) {
var inetSocketAddress = new InetSocketAddress(proxyConfig.getHostname(), proxyConfig.getPort());
httpClientBuilder.proxy(ProxySelector.of(inetSocketAddress));
} else {
return new Response(IOUtils.INVALID_PROXY_CONFIG_ERROR);
}
} else {
httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).build();
}

var httpClient = httpClientBuilder.build();

// request body
var requestBody = HttpRequest.BodyPublishers.ofString(parameters.toString());
// wait up to 15 seconds
// wait up to 30 seconds
var httpRequest = HttpRequest.newBuilder().uri(URI.create(IOUtils.API_URL)).
headers("app_id", appId, "app_key", appKey, "Content-type", "application/json").
POST(requestBody).timeout(Duration.ofSeconds(15)).build();
POST(requestBody).timeout(Duration.ofSeconds(30)).build();

var completableFuture = httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body);
var completableFuture = httpClient.
sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body);

try {
return new Gson().fromJson(completableFuture.get(), Response.class);
} catch (InterruptedException | ExecutionException e) {
} catch (RuntimeException | InterruptedException | ExecutionException e) {
return new Response(e.getMessage());
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/Recognition.java
Expand Up @@ -8,6 +8,7 @@
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -89,7 +90,7 @@ public Boolean setSrcParameters(Image image) {
* @return OCR request result.
*/
@Override
public Response call() {
public Response call() throws NoSuchAlgorithmException {
return OCRRequestHelper.getResult(parameters);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/project.properties
@@ -1,2 +1,2 @@
applicationName=Image2LaTeX
version=0.7.6
version=0.7.7