001package io.freefair.spring.okhttp.autoconfigure;
002
003import org.springframework.boot.context.properties.ConfigurationProperties;
004import org.springframework.boot.context.properties.NestedConfigurationProperty;
005import org.springframework.util.unit.DataSize;
006import java.io.File;
007import java.time.Duration;
008
009/**
010 * @author Lars Grefer
011 */
012@ConfigurationProperties(prefix = "okhttp")
013public class OkHttpProperties {
014    /**
015     * The default connect timeout for new connections.
016     */
017    private Duration connectTimeout = Duration.ofSeconds(10);
018    /**
019     * The default read timeout for new connections.
020     */
021    private Duration readTimeout = Duration.ofSeconds(10);
022    /**
023     * The default write timeout for new connections.
024     */
025    private Duration writeTimeout = Duration.ofSeconds(10);
026    /**
027     * The interval between web socket pings initiated by this client. Use this to
028     * automatically send web socket ping frames until either the web socket fails or it is closed.
029     * This keeps the connection alive and may detect connectivity failures early. No timeouts are
030     * enforced on the acknowledging pongs.
031     *
032     * <p>The default value of 0 disables client-initiated pings.
033     */
034    private Duration pingInterval = Duration.ZERO;
035    @NestedConfigurationProperty
036    private CacheProperties cache = new CacheProperties();
037    /**
038     * Whether to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.
039     */
040    private boolean followSslRedirects = true;
041    /**
042     * Whether to follow redirects.
043     */
044    private boolean followRedirects = true;
045    /**
046     * Whether to retry or not when a connectivity problem is encountered.
047     */
048    private boolean retryOnConnectionFailure = true;
049    @NestedConfigurationProperty
050    private final ConnectionPoolProperties connectionPool = new ConnectionPoolProperties();
051
052
053    /**
054     * @author Lars Grefer
055     * @see okhttp3.Cache
056     */
057    public static class CacheProperties {
058        private boolean enabled;
059        /**
060         * The maximum number of bytes this cache should use to store.
061         */
062        private DataSize maxSize = DataSize.ofMegabytes(10);
063        /**
064         * The path of the directory where the cache should be stored.
065         */
066        private File directory;
067
068        public CacheProperties() {
069        }
070
071        public boolean isEnabled() {
072            return this.enabled;
073        }
074
075        /**
076         * The maximum number of bytes this cache should use to store.
077         */
078        public DataSize getMaxSize() {
079            return this.maxSize;
080        }
081
082        /**
083         * The path of the directory where the cache should be stored.
084         */
085        public File getDirectory() {
086            return this.directory;
087        }
088
089        public void setEnabled(final boolean enabled) {
090            this.enabled = enabled;
091        }
092
093        /**
094         * The maximum number of bytes this cache should use to store.
095         */
096        public void setMaxSize(final DataSize maxSize) {
097            this.maxSize = maxSize;
098        }
099
100        /**
101         * The path of the directory where the cache should be stored.
102         */
103        public void setDirectory(final File directory) {
104            this.directory = directory;
105        }
106
107        @Override
108        public boolean equals(final Object o) {
109            if (o == this) return true;
110            if (!(o instanceof OkHttpProperties.CacheProperties)) return false;
111            final OkHttpProperties.CacheProperties other = (OkHttpProperties.CacheProperties) o;
112            if (!other.canEqual((Object) this)) return false;
113            if (this.isEnabled() != other.isEnabled()) return false;
114            final Object this$maxSize = this.getMaxSize();
115            final Object other$maxSize = other.getMaxSize();
116            if (this$maxSize == null ? other$maxSize != null : !this$maxSize.equals(other$maxSize)) return false;
117            final Object this$directory = this.getDirectory();
118            final Object other$directory = other.getDirectory();
119            if (this$directory == null ? other$directory != null : !this$directory.equals(other$directory)) return false;
120            return true;
121        }
122
123        protected boolean canEqual(final Object other) {
124            return other instanceof OkHttpProperties.CacheProperties;
125        }
126
127        @Override
128        public int hashCode() {
129            final int PRIME = 59;
130            int result = 1;
131            result = result * PRIME + (this.isEnabled() ? 79 : 97);
132            final Object $maxSize = this.getMaxSize();
133            result = result * PRIME + ($maxSize == null ? 43 : $maxSize.hashCode());
134            final Object $directory = this.getDirectory();
135            result = result * PRIME + ($directory == null ? 43 : $directory.hashCode());
136            return result;
137        }
138
139        @Override
140        public String toString() {
141            return "OkHttpProperties.CacheProperties(enabled=" + this.isEnabled() + ", maxSize=" + this.getMaxSize() + ", directory=" + this.getDirectory() + ")";
142        }
143    }
144
145
146    /**
147     * @see okhttp3.ConnectionPool
148     */
149    public static class ConnectionPoolProperties {
150        /**
151         * The maximum number of idle connections for each address.
152         */
153        private int maxIdleConnections = 5;
154        private Duration keepAliveDuration = Duration.ofMinutes(5);
155
156        public ConnectionPoolProperties() {
157        }
158
159        /**
160         * The maximum number of idle connections for each address.
161         */
162        public int getMaxIdleConnections() {
163            return this.maxIdleConnections;
164        }
165
166        public Duration getKeepAliveDuration() {
167            return this.keepAliveDuration;
168        }
169
170        /**
171         * The maximum number of idle connections for each address.
172         */
173        public void setMaxIdleConnections(final int maxIdleConnections) {
174            this.maxIdleConnections = maxIdleConnections;
175        }
176
177        public void setKeepAliveDuration(final Duration keepAliveDuration) {
178            this.keepAliveDuration = keepAliveDuration;
179        }
180
181        @Override
182        public boolean equals(final Object o) {
183            if (o == this) return true;
184            if (!(o instanceof OkHttpProperties.ConnectionPoolProperties)) return false;
185            final OkHttpProperties.ConnectionPoolProperties other = (OkHttpProperties.ConnectionPoolProperties) o;
186            if (!other.canEqual((Object) this)) return false;
187            if (this.getMaxIdleConnections() != other.getMaxIdleConnections()) return false;
188            final Object this$keepAliveDuration = this.getKeepAliveDuration();
189            final Object other$keepAliveDuration = other.getKeepAliveDuration();
190            if (this$keepAliveDuration == null ? other$keepAliveDuration != null : !this$keepAliveDuration.equals(other$keepAliveDuration)) return false;
191            return true;
192        }
193
194        protected boolean canEqual(final Object other) {
195            return other instanceof OkHttpProperties.ConnectionPoolProperties;
196        }
197
198        @Override
199        public int hashCode() {
200            final int PRIME = 59;
201            int result = 1;
202            result = result * PRIME + this.getMaxIdleConnections();
203            final Object $keepAliveDuration = this.getKeepAliveDuration();
204            result = result * PRIME + ($keepAliveDuration == null ? 43 : $keepAliveDuration.hashCode());
205            return result;
206        }
207
208        @Override
209        public String toString() {
210            return "OkHttpProperties.ConnectionPoolProperties(maxIdleConnections=" + this.getMaxIdleConnections() + ", keepAliveDuration=" + this.getKeepAliveDuration() + ")";
211        }
212    }
213
214    public OkHttpProperties() {
215    }
216
217    /**
218     * The default connect timeout for new connections.
219     */
220    public Duration getConnectTimeout() {
221        return this.connectTimeout;
222    }
223
224    /**
225     * The default read timeout for new connections.
226     */
227    public Duration getReadTimeout() {
228        return this.readTimeout;
229    }
230
231    /**
232     * The default write timeout for new connections.
233     */
234    public Duration getWriteTimeout() {
235        return this.writeTimeout;
236    }
237
238    /**
239     * The interval between web socket pings initiated by this client. Use this to
240     * automatically send web socket ping frames until either the web socket fails or it is closed.
241     * This keeps the connection alive and may detect connectivity failures early. No timeouts are
242     * enforced on the acknowledging pongs.
243     *
244     * <p>The default value of 0 disables client-initiated pings.
245     */
246    public Duration getPingInterval() {
247        return this.pingInterval;
248    }
249
250    public CacheProperties getCache() {
251        return this.cache;
252    }
253
254    /**
255     * Whether to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.
256     */
257    public boolean isFollowSslRedirects() {
258        return this.followSslRedirects;
259    }
260
261    /**
262     * Whether to follow redirects.
263     */
264    public boolean isFollowRedirects() {
265        return this.followRedirects;
266    }
267
268    /**
269     * Whether to retry or not when a connectivity problem is encountered.
270     */
271    public boolean isRetryOnConnectionFailure() {
272        return this.retryOnConnectionFailure;
273    }
274
275    public ConnectionPoolProperties getConnectionPool() {
276        return this.connectionPool;
277    }
278
279    /**
280     * The default connect timeout for new connections.
281     */
282    public void setConnectTimeout(final Duration connectTimeout) {
283        this.connectTimeout = connectTimeout;
284    }
285
286    /**
287     * The default read timeout for new connections.
288     */
289    public void setReadTimeout(final Duration readTimeout) {
290        this.readTimeout = readTimeout;
291    }
292
293    /**
294     * The default write timeout for new connections.
295     */
296    public void setWriteTimeout(final Duration writeTimeout) {
297        this.writeTimeout = writeTimeout;
298    }
299
300    /**
301     * The interval between web socket pings initiated by this client. Use this to
302     * automatically send web socket ping frames until either the web socket fails or it is closed.
303     * This keeps the connection alive and may detect connectivity failures early. No timeouts are
304     * enforced on the acknowledging pongs.
305     *
306     * <p>The default value of 0 disables client-initiated pings.
307     */
308    public void setPingInterval(final Duration pingInterval) {
309        this.pingInterval = pingInterval;
310    }
311
312    public void setCache(final CacheProperties cache) {
313        this.cache = cache;
314    }
315
316    /**
317     * Whether to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.
318     */
319    public void setFollowSslRedirects(final boolean followSslRedirects) {
320        this.followSslRedirects = followSslRedirects;
321    }
322
323    /**
324     * Whether to follow redirects.
325     */
326    public void setFollowRedirects(final boolean followRedirects) {
327        this.followRedirects = followRedirects;
328    }
329
330    /**
331     * Whether to retry or not when a connectivity problem is encountered.
332     */
333    public void setRetryOnConnectionFailure(final boolean retryOnConnectionFailure) {
334        this.retryOnConnectionFailure = retryOnConnectionFailure;
335    }
336
337    @Override
338    public boolean equals(final Object o) {
339        if (o == this) return true;
340        if (!(o instanceof OkHttpProperties)) return false;
341        final OkHttpProperties other = (OkHttpProperties) o;
342        if (!other.canEqual((Object) this)) return false;
343        if (this.isFollowSslRedirects() != other.isFollowSslRedirects()) return false;
344        if (this.isFollowRedirects() != other.isFollowRedirects()) return false;
345        if (this.isRetryOnConnectionFailure() != other.isRetryOnConnectionFailure()) return false;
346        final Object this$connectTimeout = this.getConnectTimeout();
347        final Object other$connectTimeout = other.getConnectTimeout();
348        if (this$connectTimeout == null ? other$connectTimeout != null : !this$connectTimeout.equals(other$connectTimeout)) return false;
349        final Object this$readTimeout = this.getReadTimeout();
350        final Object other$readTimeout = other.getReadTimeout();
351        if (this$readTimeout == null ? other$readTimeout != null : !this$readTimeout.equals(other$readTimeout)) return false;
352        final Object this$writeTimeout = this.getWriteTimeout();
353        final Object other$writeTimeout = other.getWriteTimeout();
354        if (this$writeTimeout == null ? other$writeTimeout != null : !this$writeTimeout.equals(other$writeTimeout)) return false;
355        final Object this$pingInterval = this.getPingInterval();
356        final Object other$pingInterval = other.getPingInterval();
357        if (this$pingInterval == null ? other$pingInterval != null : !this$pingInterval.equals(other$pingInterval)) return false;
358        final Object this$cache = this.getCache();
359        final Object other$cache = other.getCache();
360        if (this$cache == null ? other$cache != null : !this$cache.equals(other$cache)) return false;
361        final Object this$connectionPool = this.getConnectionPool();
362        final Object other$connectionPool = other.getConnectionPool();
363        if (this$connectionPool == null ? other$connectionPool != null : !this$connectionPool.equals(other$connectionPool)) return false;
364        return true;
365    }
366
367    protected boolean canEqual(final Object other) {
368        return other instanceof OkHttpProperties;
369    }
370
371    @Override
372    public int hashCode() {
373        final int PRIME = 59;
374        int result = 1;
375        result = result * PRIME + (this.isFollowSslRedirects() ? 79 : 97);
376        result = result * PRIME + (this.isFollowRedirects() ? 79 : 97);
377        result = result * PRIME + (this.isRetryOnConnectionFailure() ? 79 : 97);
378        final Object $connectTimeout = this.getConnectTimeout();
379        result = result * PRIME + ($connectTimeout == null ? 43 : $connectTimeout.hashCode());
380        final Object $readTimeout = this.getReadTimeout();
381        result = result * PRIME + ($readTimeout == null ? 43 : $readTimeout.hashCode());
382        final Object $writeTimeout = this.getWriteTimeout();
383        result = result * PRIME + ($writeTimeout == null ? 43 : $writeTimeout.hashCode());
384        final Object $pingInterval = this.getPingInterval();
385        result = result * PRIME + ($pingInterval == null ? 43 : $pingInterval.hashCode());
386        final Object $cache = this.getCache();
387        result = result * PRIME + ($cache == null ? 43 : $cache.hashCode());
388        final Object $connectionPool = this.getConnectionPool();
389        result = result * PRIME + ($connectionPool == null ? 43 : $connectionPool.hashCode());
390        return result;
391    }
392
393    @Override
394    public String toString() {
395        return "OkHttpProperties(connectTimeout=" + this.getConnectTimeout() + ", readTimeout=" + this.getReadTimeout() + ", writeTimeout=" + this.getWriteTimeout() + ", pingInterval=" + this.getPingInterval() + ", cache=" + this.getCache() + ", followSslRedirects=" + this.isFollowSslRedirects() + ", followRedirects=" + this.isFollowRedirects() + ", retryOnConnectionFailure=" + this.isRetryOnConnectionFailure() + ", connectionPool=" + this.getConnectionPool() + ")";
396    }
397}