Skip to content

Commit

Permalink
Merge pull request #57 from opt-nc/develop
Browse files Browse the repository at this point in the history
feat(proxy): set configuration
  • Loading branch information
mbarre authored Mar 1, 2023
2 parents d2c0c2a + 1f81256 commit ba1f8bd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 23 deletions.
18 changes: 6 additions & 12 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<commons-lang3.version>3.12.0</commons-lang3.version>
<junit-jupiter.version>5.9.2</junit-jupiter.version>
<unirest.version>3.11.12</unirest.version>

<!-- Plugins versions -->
<jacoco-maven-plugin.version>0.8.8</jacoco-maven-plugin.version>
</properties>
Expand All @@ -41,6 +40,7 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/nc/opt/tempsattente/Agences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -321,6 +323,11 @@ public static List<Agence> getAgencesByDistance(double lon, double lat, long dis

private static List<Agence> 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();
Expand Down Expand Up @@ -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;
}

}
44 changes: 34 additions & 10 deletions src/test/java/nc/opt/tempsattente/AgencesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@
*/
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;
import java.time.temporal.ChronoUnit;
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.
Expand Down Expand Up @@ -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);
}

}

0 comments on commit ba1f8bd

Please sign in to comment.