Skip to content

Commit

Permalink
send if-modified-since, if-none-match headers to insights api (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeping-h authored Jul 9, 2024
1 parent 4a8dee1 commit 68d8e1a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/main/java/io/kontur/disasterninja/config/WebConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import io.kontur.disasterninja.client.InsightsApiClientDummy;
import io.kontur.disasterninja.client.InsightsApiGraphqlClient;
import io.kontur.disasterninja.client.InsightsApiGraphqlClientDummy;
import io.kontur.disasterninja.config.interceptor.UserLanguageInterceptor;
import io.kontur.disasterninja.config.interceptor.HeaderInterceptor;
import io.kontur.disasterninja.config.metrics.ParamLessRestTemplateExchangeTagsProvider;
import io.kontur.disasterninja.controller.exception.WebApplicationException;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down Expand Up @@ -49,6 +49,7 @@
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
import java.util.List;

@Configuration
public class WebConfiguration {
Expand Down Expand Up @@ -111,12 +112,14 @@ public RestTemplate layersApiRestTemplate(RestTemplateBuilder builder, HttpClien
public RestTemplate insightsApiRestTemplate(RestTemplateBuilder builder, HttpClient httpClient,
@Value("${kontur.platform.insightsApi.url}") String insightsApiUrl,
@Value("${kontur.platform.insightsApi.connectionTimeout}") Integer connectionTimeout,
@Value("${kontur.platform.insightsApi.readTimeout}") Integer readTimeout) {
@Value("${kontur.platform.insightsApi.readTimeout}") Integer readTimeout,
ClientHttpRequestInterceptor insightsHeadersInterceptor) {
return builder
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(httpClient))
.rootUri(insightsApiUrl)
.setConnectTimeout(Duration.of(connectionTimeout, ChronoUnit.SECONDS))
.setReadTimeout(Duration.of(readTimeout, ChronoUnit.SECONDS))
.additionalInterceptors(insightsHeadersInterceptor)
.build();
}

Expand Down Expand Up @@ -225,9 +228,14 @@ public InsightsApiClient insightsApiClientDummy() {
return new InsightsApiClientDummy();
}

@Bean
public ClientHttpRequestInterceptor insightsHeadersInterceptor() {
return new HeaderInterceptor(List.of("If-None-Match", "If-Modified-Since"));
}

@Bean
public ClientHttpRequestInterceptor userLanguageInterceptor() {
return new UserLanguageInterceptor();
return new HeaderInterceptor(List.of("User-Language"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

public class UserLanguageInterceptor implements ClientHttpRequestInterceptor {
public class HeaderInterceptor implements ClientHttpRequestInterceptor {

private List<String> headers;

public HeaderInterceptor(List<String> headers) {
// what request headers should be passed from DN-BE further?
this.headers = headers;
}

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpServletRequest currentRequest = getCurrentHttpRequest();
if (currentRequest != null) {
String userLanguage = currentRequest.getHeader("User-Language");
if (userLanguage != null) {
request.getHeaders().add("User-Language", userLanguage);
for (String header : headers) {
String headerValue = currentRequest.getHeader(header);
if (headerValue != null) {
request.getHeaders().add(header, headerValue);
}
}
}
return execution.execute(request, body);
Expand All @@ -28,4 +38,4 @@ private HttpServletRequest getCurrentHttpRequest() {
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return sra != null ? sra.getRequest() : null;
}
}
}

0 comments on commit 68d8e1a

Please sign in to comment.