Skip to content

Commit

Permalink
java 1.8 + add access token & client id
Browse files Browse the repository at this point in the history
  • Loading branch information
Heru Rusdianto committed Aug 1, 2023
1 parent 91c9e17 commit 4490fdb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>avew.github.io</groupId>
<artifactId>retrofit-mask</artifactId>
<version>1.0.10</version>
<version>0.0.10</version>
<packaging>jar</packaging>
<description>Custom retrofit with masking log</description>

Expand All @@ -27,8 +27,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<okhttp3.version>4.9.3</okhttp3.version>
<retrofit.version>2.6.4</retrofit.version>
Expand All @@ -37,7 +37,7 @@
<commons-lang.version>2.6</commons-lang.version>
<gson.version>2.10.1</gson.version>
<jackson-databind.version>2.15.2</jackson-databind.version>
<logback.version>1.4.7</logback.version>
<logback.version>1.3.8</logback.version>
<junit.version>4.13.2</junit.version>

<maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
Expand Down Expand Up @@ -148,8 +148,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration> <!-- Compile java 7 compatible bytecode -->
<source>11</source>
<target>11</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/github/avew/CustomHttpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class CustomHttpConfig {
@Builder.Default
private HttpTls TLS = HttpTls.TLS1_2;

@Builder.Default
private String accessToken = null;

@Builder.Default
private String clientId = null;

public static String mask(String value) {
if (value == null) {
return null;
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/io/github/avew/VewHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -73,6 +74,13 @@ public OkHttpClient setupClient() throws NoSuchAlgorithmException, KeyManagement
if (config.getAgent() != null)
request.header("User-Agent", config.getAgent());
if (config.isRetryConnectionFailure()) request.header("Connection", "close");

if (config.getAccessToken() != null)
request.header("Authorization", "Bearer " + config.getAccessToken());

if (config.getClientId() != null)
request.header("X-Client", config.getClientId());

return chain.proceed(request.build());
});
httpClient.addNetworkInterceptor(customNetworkInterceptors());
Expand All @@ -85,19 +93,19 @@ public OkHttpClient setupClient() throws NoSuchAlgorithmException, KeyManagement
.tlsVersions(TlsVersion.TLS_1_0, TlsVersion.TLS_1_1)
.build();

httpClient.connectionSpecs(List.of(tls11, connectionSpecClearText));
httpClient.connectionSpecs(Arrays.asList(tls11, connectionSpecClearText));
break;
case TLS1_2:
ConnectionSpec tls12 = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.build();
httpClient.connectionSpecs(List.of(tls12, connectionSpecClearText));
httpClient.connectionSpecs(Arrays.asList(tls12, connectionSpecClearText));
break;
case TLS1_3:
ConnectionSpec tls13 = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_3)
.build();
httpClient.connectionSpecs(List.of(tls13, connectionSpecClearText));
httpClient.connectionSpecs(Arrays.asList(tls13, connectionSpecClearText));
break;
}

Expand Down Expand Up @@ -146,15 +154,15 @@ public List<Proxy> select(URI uri) {

String noProxyHost = proxy.getUrlSkip().stream().collect(Collectors.joining(","));
if (StringUtils.contains(noProxyHost, host)) {
return List.of(Proxy.NO_PROXY);
return Arrays.asList(Proxy.NO_PROXY);
} else {
// Add Proxy
if (proxy.getType().equals(ProxyType.HTTP)) {
return List.of(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxy.getHost(), proxy.getPort())));
return Arrays.asList(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(proxy.getHost(), proxy.getPort())));
} else {
return List.of(new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(proxy.getHost(), proxy.getPort())));
return Arrays.asList(new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(proxy.getHost(), proxy.getPort())));
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/github/avew/OkHttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void init() {
CustomHttpConfig customHttpConfig = CustomHttpConfig.builder()
.url("https://reqres.in")
.masking(true)
.excludeHeaders(List.of("X-Api-Key"))
.excludeHeaders(Arrays.asList("X-Api-Key"))
.build();

retrofit = new VewHttp(customHttpConfig, maskingLog, true)
Expand Down

0 comments on commit 4490fdb

Please sign in to comment.