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