From 1f81256b136c71a47249a4c26438d0045c07b16a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mich=C3=A8le=20BARRE?= Date: Wed, 1 Mar 2023 10:01:38 +1100 Subject: [PATCH] feat(proxy): set configuration ref: #55 --- .github/workflows/maven.yml | 18 +++----- pom.xml | 2 +- .../java/nc/opt/tempsattente/Agences.java | 28 ++++++++++++ .../java/nc/opt/tempsattente/AgencesTest.java | 44 ++++++++++++++----- 4 files changed, 69 insertions(+), 23 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index bc881dc..aca5b83 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -10,20 +10,14 @@ jobs: permissions: checks: write pull-requests: write - strategy: - fail-fast: false - matrix: - jdk: [8, 17] - runs-on: ubuntu-latest - steps: - uses: actions/checkout@v3 - - name: Set up JDK ${{ matrix.jdk }} + - name: Set up JDK 17 uses: actions/setup-java@v3 with: - java-version: ${{ matrix.jdk }} - distribution: 'adopt' + java-version: 17 + distribution: 'temurin' cache: 'maven' - name: Build, Test @@ -42,11 +36,11 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - name: Set up JDK 8 + - name: Set up JDK 17 uses: actions/setup-java@v3 with: - java-version: 8 - distribution: 'adopt' + java-version: 17 + distribution: 'temurin' cache: 'maven' - name: Install NodeJS uses: actions/setup-node@v3 diff --git a/pom.xml b/pom.xml index b58ee21..fddd8ba 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,6 @@ 3.12.0 5.9.2 3.11.12 - 0.8.8 @@ -41,6 +40,7 @@ ${junit-jupiter.version} test + org.slf4j slf4j-simple diff --git a/src/main/java/nc/opt/tempsattente/Agences.java b/src/main/java/nc/opt/tempsattente/Agences.java index fe04eb5..cbaae1f 100644 --- a/src/main/java/nc/opt/tempsattente/Agences.java +++ b/src/main/java/nc/opt/tempsattente/Agences.java @@ -6,6 +6,8 @@ package nc.opt.tempsattente; import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -321,6 +323,11 @@ public static List getAgencesByDistance(double lon, double lat, long dis private static List load(URL url) throws IOException { ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + Proxy proxy = getProxy(); + if(proxy != null){ + url.openConnection(proxy); + } JsonNode jsonNode = mapper.readValue(url, JsonNode.class); int total = jsonNode.get("hits").get("total").asInt(); @@ -435,4 +442,25 @@ public static long[] getCoordonneesXYPrecises(JsonNode nodeCoordonnesXYPrecises) } } + + public static Proxy getProxy(){ + + String httpHost = System.getProperty("http.proxyHost"); + String httpPort = System.getProperty("http.proxyPort"); + String httpsHost = System.getProperty("https.proxyHost"); + String httpsPort = System.getProperty("https.proxyPort"); + + boolean httpsProxySet = httpsHost != null && httpsPort != null; + boolean httpProxySet = httpHost != null && httpPort != null; + + Proxy proxy = null; + if(httpProxySet || httpsProxySet) { + logger.info((httpsProxySet ? "HTTPS" : "HTTP") + " proxy configuration detected. Set it."); + proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpsProxySet ? httpsHost : httpHost, + httpsProxySet ? Integer.valueOf(httpsPort): Integer.valueOf(httpPort))); + } + + return proxy; + } + } diff --git a/src/test/java/nc/opt/tempsattente/AgencesTest.java b/src/test/java/nc/opt/tempsattente/AgencesTest.java index 4fc0946..c9e2f12 100644 --- a/src/test/java/nc/opt/tempsattente/AgencesTest.java +++ b/src/test/java/nc/opt/tempsattente/AgencesTest.java @@ -5,13 +5,13 @@ */ package nc.opt.tempsattente; -import static nc.opt.tempsattente.Agences.BASE_URL; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; import java.io.IOException; +import java.net.Proxy; import java.net.URL; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -19,11 +19,9 @@ import java.util.logging.Level; import java.util.logging.Logger; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import org.junit.jupiter.api.Test; +import static nc.opt.tempsattente.Agences.BASE_URL; +import static nc.opt.tempsattente.Agences.getProxy; +import static org.junit.jupiter.api.Assertions.*; /** * Test class of class Agences. @@ -199,4 +197,30 @@ public void testDemo() { } } + @Test + public void testHttpProxy(){ + System.setProperty("http.proxyHost","localhost"); + System.setProperty("http.proxyPort","9000"); + Proxy proxy = getProxy(); + assertNotNull(proxy); + } + + @Test + public void testHttpsProxy(){ + System.setProperty("https.proxyHost","localhost"); + System.setProperty("https.proxyPort","8443"); + Proxy proxy = getProxy(); + assertNotNull(proxy); + } + + @Test + public void testNoProxy(){ + System.getProperties().remove("http.proxyHost"); + System.getProperties().remove("https.proxyHost"); + System.getProperties().remove("https.proxyPort"); + System.getProperties().remove("http.proxyPort"); + Proxy proxy = getProxy(); + assertNull(proxy); + } + }