Skip to content

Commit

Permalink
add config masking headers value
Browse files Browse the repository at this point in the history
  • Loading branch information
asep.rojali committed Jun 13, 2023
1 parent 0a7986c commit 2112be4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion 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.6</version>
<version>1.0.7</version>
<packaging>jar</packaging>
<description>Custom retrofit with masking log</description>

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/github/avew/CustomHttpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
Expand All @@ -31,6 +32,9 @@ public class CustomHttpConfig {
private boolean masking = true;
@Builder.Default
private boolean retryConnectionFailure = false;
@Builder.Default
private List<String> excludeHeaders = new ArrayList<>();


@Override
public String toString() {
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/io/github/avew/VewHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public Retrofit.Builder builder(Consumer<OkHttpClient.Builder> 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());
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/java/io/github/avew/OkHttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2112be4

Please sign in to comment.