diff --git a/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/google/GoogleTranslateService.java b/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/google/GoogleTranslateService.java index fa0b2bc..c82727f 100644 --- a/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/google/GoogleTranslateService.java +++ b/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/google/GoogleTranslateService.java @@ -1,19 +1,13 @@ package io.gitlab.jfronny.libjf.translate.impl.google; +import io.gitlab.jfronny.commons.http.client.HttpClient; import io.gitlab.jfronny.libjf.translate.api.TranslateException; import io.gitlab.jfronny.libjf.translate.impl.AbstractTranslateService; import org.apache.commons.lang3.StringEscapeUtils; -import javax.net.ssl.HttpsURLConnection; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -103,7 +97,7 @@ public class GoogleTranslateService extends AbstractTranslateService response = client.send(HttpRequest.newBuilder().uri(new URI(url)).build(), HttpResponse.BodyHandlers.ofString()); - if (response.statusCode() / 100 != 2) { - throw new IOException("Could not get page: " + response.statusCode()); - } - return response.body(); - } catch (URISyntaxException | IOException | InterruptedException e) { - throw new RuntimeException(e); - } + private static String get(String url) throws URISyntaxException, IOException { + return HttpClient.get(url).sendString(); } - private static String get(String url) { - // Technically, we should be using HttpClient, but Google Translate doesn't like it for some reason and this mess bypasses that - // based on https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/helper/HttpConnection.java - try { - HttpsURLConnection connection = (HttpsURLConnection) URI.create(url).toURL().openConnection(); - connection.setRequestMethod("GET"); - connection.setInstanceFollowRedirects(false); - connection.setConnectTimeout(5000); - connection.setReadTimeout(2500); - connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"); - connection.connect(); - if (connection.getResponseCode() / 100 != 2) { - String loc = connection.getHeaderField("Location"); - if (loc == null) loc = ""; - else loc = " (redirected to " + loc + ")"; - throw new IOException("Could not get page: " + connection.getResponseCode() + loc); - } - connection.getRequestMethod(); - connection.getURL(); - connection.getResponseCode(); - connection.getResponseMessage(); - connection.getContentType(); - try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { - StringBuilder sb = new StringBuilder(); - String line; - while ((line = br.readLine()) != null) { - sb.append(line).append('\n'); - } - return sb.toString(); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } + // Alternative methods to get the page source (kept for reference) + // The HttpClient here is not the same as the one in libjf-commons, so adjust imports if you want to use them again +// private static String get2(String url) { +// // An attempt to use the stuff wrapped by HttpClient. Will need to test and (if it works) adjust commons, so it can be used again +// try { +// HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build(); +// HttpResponse response = client.send(HttpRequest.newBuilder().uri(new URI(url)).header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36").build(), HttpResponse.BodyHandlers.ofString()); +// if (response.statusCode() / 100 != 2) { +// throw new IOException("Could not get page: " + response.statusCode()); +// } +// return response.body(); +// } catch (URISyntaxException | IOException | InterruptedException e) { +// throw new RuntimeException(e); +// } +// } + +// private static String get3(String url) { +// // Technically, we should be using HttpClient, but Google Translate doesn't like it for some reason and this mess bypasses that +// // based on https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/helper/HttpConnection.java +// try { +// HttpsURLConnection connection = (HttpsURLConnection) URI.create(url).toURL().openConnection(); +// connection.setRequestMethod("GET"); +// connection.setInstanceFollowRedirects(false); +// connection.setConnectTimeout(5000); +// connection.setReadTimeout(2500); +// connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"); +// connection.connect(); +// if (connection.getResponseCode() / 100 != 2) { +// String loc = connection.getHeaderField("Location"); +// if (loc == null) loc = ""; +// else loc = " (redirected to " + loc + ")"; +// throw new IOException("Could not get page: " + connection.getResponseCode() + loc); +// } +// connection.getRequestMethod(); +// connection.getURL(); +// connection.getResponseCode(); +// connection.getResponseMessage(); +// connection.getContentType(); +// try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { +// StringBuilder sb = new StringBuilder(); +// String line; +// while ((line = br.readLine()) != null) { +// sb.append(line).append('\n'); +// } +// return sb.toString(); +// } +// } catch (IOException e) { +// throw new RuntimeException(e); +// } +// } }