All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.web.client.config.RestTemplateJdkHttpClientConfig Maven / Gradle / Ivy

The newest version!
package org.springframework.web.client.config;

import java.net.http.HttpClient;
import java.time.Duration;
import java.util.Objects;

import org.springframework.web.client.logger.HttpLogLevel;

public class RestTemplateJdkHttpClientConfig {

    public static final int MIN_PRIORITY = 1;
    public static final int MAX_PRIORITY = 256;

    private boolean enabled = false;

    private HttpLogLevel level = HttpLogLevel.BASIC;
    private Duration connectionTimeout = Duration.ofMillis(2000);
    private Duration readTimeout = Duration.ofMillis(2000);
    private Integer http2Priority;
    private HttpClient.Redirect redirect = HttpClient.Redirect.ALWAYS;
    private HttpClient.Version httpVersion = HttpClient.Version.HTTP_1_1;

    public RestTemplateJdkHttpClientConfig() {
        super();
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public HttpLogLevel getLevel() {
        return level;
    }

    public RestTemplateJdkHttpClientConfig setLevel(HttpLogLevel level) {
        this.level = level;
        return this;
    }

    public Duration getConnectionTimeout() {
        return connectionTimeout;
    }

    public RestTemplateJdkHttpClientConfig setConnectionTimeout(Duration connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
        return this;
    }

    public Duration getReadTimeout() {
        return readTimeout;
    }

    public RestTemplateJdkHttpClientConfig setReadTimeout(Duration readTimeout) {
        this.readTimeout = readTimeout;
        return this;
    }

    public Integer getHttp2Priority() {
        return http2Priority;
    }

    public RestTemplateJdkHttpClientConfig setHttp2Priority(Integer http2Priority) {
        if (isValidHttp2Priority(http2Priority)) {
            this.http2Priority = http2Priority;
        }
        return this;
    }

    public HttpClient.Redirect getRedirect() {
        return redirect;
    }

    public RestTemplateJdkHttpClientConfig setRedirect(HttpClient.Redirect redirect) {
        this.redirect = redirect;
        return this;
    }

    public HttpClient.Version getHttpVersion() {
        return httpVersion;
    }

    public RestTemplateJdkHttpClientConfig setHttpVersion(HttpClient.Version httpVersion) {
        this.httpVersion = httpVersion;
        return this;
    }

    @Override
    public int hashCode() {
        return Objects.hash(connectionTimeout, enabled, http2Priority, httpVersion, level, readTimeout, redirect);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RestTemplateJdkHttpClientConfig other = (RestTemplateJdkHttpClientConfig)obj;
        return Objects.equals(connectionTimeout, other.connectionTimeout) && enabled == other.enabled
            && http2Priority == other.http2Priority && httpVersion == other.httpVersion && level == other.level
            && Objects.equals(readTimeout, other.readTimeout) && redirect == other.redirect;
    }

    @Override
    public String toString() {
        return "RestTemplateJdkHttpClientConfig [enabled=" + enabled + ", level=" + level + ", connectionTimeout="
            + connectionTimeout + ", readTimeout=" + readTimeout + ", http2Priority=" + http2Priority + ", redirect="
            + redirect + ", httpVersion=" + httpVersion + "]";
    }

    public static boolean isValidHttp2Priority(Integer http2Priority) {
        if (null == http2Priority)
            return false;
        int priority = http2Priority.intValue();
        return priority >= MIN_PRIORITY && priority <= MAX_PRIORITY;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy