diff --git a/pom.xml b/pom.xml index 43cd532..3a6a157 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ avew.github.io retrofit-mask - 1.0.6 + 1.0.7 jar Custom retrofit with masking log diff --git a/src/main/java/io/github/avew/CustomHttpConfig.java b/src/main/java/io/github/avew/CustomHttpConfig.java index 9b7f248..09473ba 100644 --- a/src/main/java/io/github/avew/CustomHttpConfig.java +++ b/src/main/java/io/github/avew/CustomHttpConfig.java @@ -5,6 +5,7 @@ import lombok.Data; import lombok.NoArgsConstructor; +import java.util.ArrayList; import java.util.List; @Data @@ -31,6 +32,9 @@ public class CustomHttpConfig { private boolean masking = true; @Builder.Default private boolean retryConnectionFailure = false; + @Builder.Default + private List excludeHeaders = new ArrayList<>(); + @Override public String toString() { diff --git a/src/main/java/io/github/avew/VewHttp.java b/src/main/java/io/github/avew/VewHttp.java index 0181404..0479800 100644 --- a/src/main/java/io/github/avew/VewHttp.java +++ b/src/main/java/io/github/avew/VewHttp.java @@ -58,10 +58,10 @@ public Retrofit.Builder builder(Consumer configureClient) httpClient.retryOnConnectionFailure(config.isRetryConnectionFailure()); httpClient.addInterceptor(chain -> { Request original = chain.request(); - Request request = original.newBuilder() - .header("User-Agent", config.getAgent()) - .build(); - return chain.proceed(request); + Request.Builder request = original.newBuilder(); + request.header("User-Agent", config.getAgent()); + if (config.isRetryConnectionFailure()) request.header("Connection", "close"); + return chain.proceed(request.build()); }); if (config.isMasking()) { httpClient.addNetworkInterceptor(customNetworkInterceptors()); @@ -184,8 +184,11 @@ private CustomizableHttpLoggingInterceptor customNetworkInterceptors() { }, debug, debug); - logging.redactHeader("Authorization"); - logging.redactHeader("Cookie"); + if (config.getExcludeHeaders().isEmpty()) { + logging.redactHeader("Authorization"); + logging.redactHeader("Cookie"); + } else config.getExcludeHeaders().forEach(logging::redactHeader); + return logging; } diff --git a/src/test/java/io/github/avew/OkHttpTest.java b/src/test/java/io/github/avew/OkHttpTest.java index d54914a..9ff7c89 100644 --- a/src/test/java/io/github/avew/OkHttpTest.java +++ b/src/test/java/io/github/avew/OkHttpTest.java @@ -42,9 +42,11 @@ public void init() { CustomHttpConfig customHttpConfig = CustomHttpConfig.builder() .url("https://reqres.in") .agent("OkHttp/4.1.0") - .masking(false) + .masking(true) + .excludeHeaders(Arrays.asList("X-Api-Key","User-Agent","Host")) .build(); log.debug("CONFIG {}", customHttpConfig.getCustomTimeout()); + log.debug("CONFIG {}", customHttpConfig.getExcludeHeaders()); ObjectMapper mapper = new ObjectMapper(); mapper.writerWithDefaultPrettyPrinter();