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