001package io.freefair.spring.okhttp;
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        public int getMaxIdleConnections() {
160            return this.maxIdleConnections;
161        }
162
163        public Duration getKeepAliveDuration() {
164            return this.keepAliveDuration;
165        }
166
167        public void setMaxIdleConnections(final int maxIdleConnections) {
168            this.maxIdleConnections = maxIdleConnections;
169        }
170
171        public void setKeepAliveDuration(final Duration keepAliveDuration) {
172            this.keepAliveDuration = keepAliveDuration;
173        }
174
175        @Override
176        public boolean equals(final Object o) {
177            if (o == this) return true;
178            if (!(o instanceof OkHttpProperties.ConnectionPoolProperties)) return false;
179            final OkHttpProperties.ConnectionPoolProperties other = (OkHttpProperties.ConnectionPoolProperties) o;
180            if (!other.canEqual((Object) this)) return false;
181            if (this.getMaxIdleConnections() != other.getMaxIdleConnections()) return false;
182            final Object this$keepAliveDuration = this.getKeepAliveDuration();
183            final Object other$keepAliveDuration = other.getKeepAliveDuration();
184            if (this$keepAliveDuration == null ? other$keepAliveDuration != null : !this$keepAliveDuration.equals(other$keepAliveDuration)) return false;
185            return true;
186        }
187
188        protected boolean canEqual(final Object other) {
189            return other instanceof OkHttpProperties.ConnectionPoolProperties;
190        }
191
192        @Override
193        public int hashCode() {
194            final int PRIME = 59;
195            int result = 1;
196            result = result * PRIME + this.getMaxIdleConnections();
197            final Object $keepAliveDuration = this.getKeepAliveDuration();
198            result = result * PRIME + ($keepAliveDuration == null ? 43 : $keepAliveDuration.hashCode());
199            return result;
200        }
201
202        @Override
203        public String toString() {
204            return "OkHttpProperties.ConnectionPoolProperties(maxIdleConnections=" + this.getMaxIdleConnections() + ", keepAliveDuration=" + this.getKeepAliveDuration() + ")";
205        }
206    }
207
208    public OkHttpProperties() {
209    }
210
211    /**
212     * The default connect timeout for new connections.
213     */
214    public Duration getConnectTimeout() {
215        return this.connectTimeout;
216    }
217
218    /**
219     * The default read timeout for new connections.
220     */
221    public Duration getReadTimeout() {
222        return this.readTimeout;
223    }
224
225    /**
226     * The default write timeout for new connections.
227     */
228    public Duration getWriteTimeout() {
229        return this.writeTimeout;
230    }
231
232    /**
233     * The interval between web socket pings initiated by this client. Use this to
234     * automatically send web socket ping frames until either the web socket fails or it is closed.
235     * This keeps the connection alive and may detect connectivity failures early. No timeouts are
236     * enforced on the acknowledging pongs.
237     *
238     * <p>The default value of 0 disables client-initiated pings.
239     */
240    public Duration getPingInterval() {
241        return this.pingInterval;
242    }
243
244    public CacheProperties getCache() {
245        return this.cache;
246    }
247
248    /**
249     * Whether to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.
250     */
251    public boolean isFollowSslRedirects() {
252        return this.followSslRedirects;
253    }
254
255    /**
256     * Whether to follow redirects.
257     */
258    public boolean isFollowRedirects() {
259        return this.followRedirects;
260    }
261
262    /**
263     * Whether to retry or not when a connectivity problem is encountered.
264     */
265    public boolean isRetryOnConnectionFailure() {
266        return this.retryOnConnectionFailure;
267    }
268
269    public ConnectionPoolProperties getConnectionPool() {
270        return this.connectionPool;
271    }
272
273    /**
274     * The default connect timeout for new connections.
275     */
276    public void setConnectTimeout(final Duration connectTimeout) {
277        this.connectTimeout = connectTimeout;
278    }
279
280    /**
281     * The default read timeout for new connections.
282     */
283    public void setReadTimeout(final Duration readTimeout) {
284        this.readTimeout = readTimeout;
285    }
286
287    /**
288     * The default write timeout for new connections.
289     */
290    public void setWriteTimeout(final Duration writeTimeout) {
291        this.writeTimeout = writeTimeout;
292    }
293
294    /**
295     * The interval between web socket pings initiated by this client. Use this to
296     * automatically send web socket ping frames until either the web socket fails or it is closed.
297     * This keeps the connection alive and may detect connectivity failures early. No timeouts are
298     * enforced on the acknowledging pongs.
299     *
300     * <p>The default value of 0 disables client-initiated pings.
301     */
302    public void setPingInterval(final Duration pingInterval) {
303        this.pingInterval = pingInterval;
304    }
305
306    public void setCache(final CacheProperties cache) {
307        this.cache = cache;
308    }
309
310    /**
311     * Whether to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.
312     */
313    public void setFollowSslRedirects(final boolean followSslRedirects) {
314        this.followSslRedirects = followSslRedirects;
315    }
316
317    /**
318     * Whether to follow redirects.
319     */
320    public void setFollowRedirects(final boolean followRedirects) {
321        this.followRedirects = followRedirects;
322    }
323
324    /**
325     * Whether to retry or not when a connectivity problem is encountered.
326     */
327    public void setRetryOnConnectionFailure(final boolean retryOnConnectionFailure) {
328        this.retryOnConnectionFailure = retryOnConnectionFailure;
329    }
330
331    @Override
332    public boolean equals(final Object o) {
333        if (o == this) return true;
334        if (!(o instanceof OkHttpProperties)) return false;
335        final OkHttpProperties other = (OkHttpProperties) o;
336        if (!other.canEqual((Object) this)) return false;
337        if (this.isFollowSslRedirects() != other.isFollowSslRedirects()) return false;
338        if (this.isFollowRedirects() != other.isFollowRedirects()) return false;
339        if (this.isRetryOnConnectionFailure() != other.isRetryOnConnectionFailure()) return false;
340        final Object this$connectTimeout = this.getConnectTimeout();
341        final Object other$connectTimeout = other.getConnectTimeout();
342        if (this$connectTimeout == null ? other$connectTimeout != null : !this$connectTimeout.equals(other$connectTimeout)) return false;
343        final Object this$readTimeout = this.getReadTimeout();
344        final Object other$readTimeout = other.getReadTimeout();
345        if (this$readTimeout == null ? other$readTimeout != null : !this$readTimeout.equals(other$readTimeout)) return false;
346        final Object this$writeTimeout = this.getWriteTimeout();
347        final Object other$writeTimeout = other.getWriteTimeout();
348        if (this$writeTimeout == null ? other$writeTimeout != null : !this$writeTimeout.equals(other$writeTimeout)) return false;
349        final Object this$pingInterval = this.getPingInterval();
350        final Object other$pingInterval = other.getPingInterval();
351        if (this$pingInterval == null ? other$pingInterval != null : !this$pingInterval.equals(other$pingInterval)) return false;
352        final Object this$cache = this.getCache();
353        final Object other$cache = other.getCache();
354        if (this$cache == null ? other$cache != null : !this$cache.equals(other$cache)) return false;
355        final Object this$connectionPool = this.getConnectionPool();
356        final Object other$connectionPool = other.getConnectionPool();
357        if (this$connectionPool == null ? other$connectionPool != null : !this$connectionPool.equals(other$connectionPool)) return false;
358        return true;
359    }
360
361    protected boolean canEqual(final Object other) {
362        return other instanceof OkHttpProperties;
363    }
364
365    @Override
366    public int hashCode() {
367        final int PRIME = 59;
368        int result = 1;
369        result = result * PRIME + (this.isFollowSslRedirects() ? 79 : 97);
370        result = result * PRIME + (this.isFollowRedirects() ? 79 : 97);
371        result = result * PRIME + (this.isRetryOnConnectionFailure() ? 79 : 97);
372        final Object $connectTimeout = this.getConnectTimeout();
373        result = result * PRIME + ($connectTimeout == null ? 43 : $connectTimeout.hashCode());
374        final Object $readTimeout = this.getReadTimeout();
375        result = result * PRIME + ($readTimeout == null ? 43 : $readTimeout.hashCode());
376        final Object $writeTimeout = this.getWriteTimeout();
377        result = result * PRIME + ($writeTimeout == null ? 43 : $writeTimeout.hashCode());
378        final Object $pingInterval = this.getPingInterval();
379        result = result * PRIME + ($pingInterval == null ? 43 : $pingInterval.hashCode());
380        final Object $cache = this.getCache();
381        result = result * PRIME + ($cache == null ? 43 : $cache.hashCode());
382        final Object $connectionPool = this.getConnectionPool();
383        result = result * PRIME + ($connectionPool == null ? 43 : $connectionPool.hashCode());
384        return result;
385    }
386
387    @Override
388    public String toString() {
389        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() + ")";
390    }
391}