Commit eff4f9e2 authored by oleg.pahhomov's avatar oleg.pahhomov

SA0140-578 | add timeouts to http clients

parent d92c3bdf
package ee.stat.dashboard;
import ee.stat.dashboard.config.logging.data.ApacheHttpClientInterceptor;
import ee.stat.dashboard.config.logging.meta.RestTemplateInterceptor;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.client.RestTemplate;
import javax.servlet.Filter;
......@@ -27,27 +17,11 @@ public class DashboardApplication {
SpringApplication.run(DashboardApplication.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.interceptors(new RestTemplateInterceptor())
.requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))
.build();
}
@Bean
public HttpClient httpClient() {
return HttpClientBuilder.create()
.addInterceptorFirst((HttpRequestInterceptor) new ApacheHttpClientInterceptor())
.addInterceptorFirst((HttpResponseInterceptor) new ApacheHttpClientInterceptor())
.build();
}
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
@Profile("logging")
@Bean(name = "TeeFilter")
public Filter teeFilter() {
......
package ee.stat.dashboard.config;
import ee.stat.dashboard.config.logging.data.ApacheHttpClientInterceptor;
import ee.stat.dashboard.config.logging.meta.RestTemplateInterceptor;
import ee.stat.dashboard.config.props.HttpClientPropsConfig;
import lombok.AllArgsConstructor;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
@AllArgsConstructor
public class HttpClientConfig {
private HttpClientPropsConfig httpClientPropsConfig;
@Bean
public HttpClient httpClient() {
if (httpClientPropsConfig.invalidConfig()) throw new RuntimeException("invalid http client config");
RequestConfig config = RequestConfig.custom()
.setSocketTimeout(httpClientPropsConfig.getReadTimeout())
.setConnectTimeout(httpClientPropsConfig.getConnectionTimeout())
.setConnectionRequestTimeout(httpClientPropsConfig.getConnectionRequestTimeout())
.build();
return HttpClientBuilder.create()
.addInterceptorFirst((HttpRequestInterceptor) new ApacheHttpClientInterceptor())
.addInterceptorFirst((HttpResponseInterceptor) new ApacheHttpClientInterceptor())
.setDefaultRequestConfig(config)
.build();
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
if (httpClientPropsConfig.invalidConfig()) throw new RuntimeException("invalid rest template config");
HttpComponentsClientHttpRequestFactory config = new HttpComponentsClientHttpRequestFactory();
config.setReadTimeout(httpClientPropsConfig.getReadTimeout());
config.setConnectTimeout(httpClientPropsConfig.getConnectionTimeout());
config.setConnectionRequestTimeout(httpClientPropsConfig.getConnectionRequestTimeout());
return builder.interceptors(new RestTemplateInterceptor())
.requestFactory(() -> new BufferingClientHttpRequestFactory(config))
.build();
}
}
package ee.stat.dashboard.config.props;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "app.http")
public class HttpClientPropsConfig {
private Integer readTimeout;
private Integer connectionTimeout;
private Integer connectionRequestTimeout;
public boolean invalidConfig() {
return readTimeout == null || connectionTimeout == null || connectionRequestTimeout == null;
}
}
......@@ -93,4 +93,8 @@ app:
timeEt: Aeg
timeEn: Time
referenceEt: Vaatlusperiood
referenceEn: Reference period
\ No newline at end of file
referenceEn: Reference period
http:
readTimeout: 30000
connectionTimeout: 30000
connectionRequestTimeout: 30000
......@@ -75,3 +75,7 @@ app:
timeEn: Time
referenceEt: Vaatlusperiood
referenceEn: Reference period
http:
readTimeout: 30000
connectionTimeout: 30000
connectionRequestTimeout: 30000
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment