View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.config;
29  
30  import java.util.concurrent.TimeUnit;
31  
32  import org.apache.hc.core5.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.util.TimeValue;
35  import org.apache.hc.core5.util.Timeout;
36  
37  /**
38   * Immutable class encapsulating connection initialization and management settings.
39   *
40   * @since 5.2
41   */
42  @Contract(threading = ThreadingBehavior.IMMUTABLE)
43  public class ConnectionConfig implements Cloneable {
44  
45      private static final Timeout DEFAULT_CONNECT_TIMEOUT = Timeout.ofMinutes(3);
46  
47      public static final ConnectionConfig DEFAULT = new Builder().build();
48  
49      private final Timeout connectTimeout;
50      private final Timeout socketTimeout;
51      private final TimeValue validateAfterInactivity;
52      private final TimeValue timeToLive;
53  
54      /**
55       * Intended for CDI compatibility
56       */
57      protected ConnectionConfig() {
58          this(DEFAULT_CONNECT_TIMEOUT, null, null, null);
59      }
60  
61      ConnectionConfig(
62              final Timeout connectTimeout,
63              final Timeout socketTimeout,
64              final TimeValue validateAfterInactivity,
65              final TimeValue timeToLive) {
66          super();
67          this.connectTimeout = connectTimeout;
68          this.socketTimeout = socketTimeout;
69          this.validateAfterInactivity = validateAfterInactivity;
70          this.timeToLive = timeToLive;
71      }
72  
73      /**
74       * @see Builder#setSocketTimeout(Timeout)
75       */
76      public Timeout getSocketTimeout() {
77          return socketTimeout;
78      }
79  
80      /**
81       * @see Builder#setConnectTimeout(Timeout)
82       */
83      public Timeout getConnectTimeout() {
84          return connectTimeout;
85      }
86  
87      /**
88       * @see Builder#setValidateAfterInactivity(TimeValue)
89       */
90      public TimeValue getValidateAfterInactivity() {
91          return validateAfterInactivity;
92      }
93  
94      /**
95       * @see Builder#setTimeToLive(TimeValue) (TimeValue)
96       */
97      public TimeValue getTimeToLive() {
98          return timeToLive;
99      }
100 
101     @Override
102     protected ConnectionConfig clone() throws CloneNotSupportedException {
103         return (ConnectionConfig) super.clone();
104     }
105 
106     @Override
107     public String toString() {
108         final StringBuilder builder = new StringBuilder();
109         builder.append("[");
110         builder.append("connectTimeout=").append(connectTimeout);
111         builder.append(", socketTimeout=").append(socketTimeout);
112         builder.append(", validateAfterInactivity=").append(validateAfterInactivity);
113         builder.append(", timeToLive=").append(timeToLive);
114         builder.append("]");
115         return builder.toString();
116     }
117 
118     public static ConnectionConfig.Builder custom() {
119         return new Builder();
120     }
121 
122     public static ConnectionConfig.Builder copy(final ConnectionConfig config) {
123         return new Builder()
124                 .setConnectTimeout(config.getConnectTimeout())
125                 .setSocketTimeout(config.getSocketTimeout())
126                 .setValidateAfterInactivity(config.getValidateAfterInactivity())
127                 .setTimeToLive(config.getTimeToLive());
128     }
129 
130     public static class Builder {
131 
132         private Timeout socketTimeout;
133         private Timeout connectTimeout;
134         private TimeValue validateAfterInactivity;
135         private TimeValue timeToLive;
136 
137         Builder() {
138             super();
139             this.connectTimeout = DEFAULT_CONNECT_TIMEOUT;
140         }
141 
142         /**
143          * @return this instance.
144          * @see #setSocketTimeout(Timeout)
145          */
146         public Builder setSocketTimeout(final int soTimeout, final TimeUnit timeUnit) {
147             this.socketTimeout = Timeout.of(soTimeout, timeUnit);
148             return this;
149         }
150 
151         /**
152          * Determines the default socket timeout value for I/O operations.
153          * <p>
154          * Default: {@code null} (undefined)
155          * </p>
156          *
157          * @return this instance.
158          */
159         public Builder setSocketTimeout(final Timeout soTimeout) {
160             this.socketTimeout = soTimeout;
161             return this;
162         }
163 
164         /**
165          * Determines the timeout until a new connection is fully established.
166          * <p>
167          * A timeout value of zero is interpreted as an infinite timeout.
168          * </p>
169          * <p>
170          * Default: 3 minutes
171          * </p>
172          *
173          * @return this instance.
174          */
175         public Builder setConnectTimeout(final Timeout connectTimeout) {
176             this.connectTimeout = connectTimeout;
177             return this;
178         }
179 
180         /**
181          * @return this instance.
182          * @see #setConnectTimeout(Timeout)
183          */
184         public Builder setConnectTimeout(final long connectTimeout, final TimeUnit timeUnit) {
185             this.connectTimeout = Timeout.of(connectTimeout, timeUnit);
186             return this;
187         }
188 
189         /**
190          * Defines period of inactivity after which persistent connections must
191          * be re-validated prior to being leased to the consumer. Negative values passed
192          * to this method disable connection validation.
193          * <p>
194          * Default: {@code null} (undefined)
195          * </p>
196          *
197          * @return this instance.
198          */
199         public Builder setValidateAfterInactivity(final TimeValue validateAfterInactivity) {
200             this.validateAfterInactivity = validateAfterInactivity;
201             return this;
202         }
203 
204         /**
205          * @return this instance.
206          * @see #setValidateAfterInactivity(TimeValue)
207          */
208         public Builder setValidateAfterInactivity(final long validateAfterInactivity, final TimeUnit timeUnit) {
209             this.validateAfterInactivity = TimeValue.of(validateAfterInactivity, timeUnit);
210             return this;
211         }
212 
213         /**
214          * Defines the total span of time connections can be kept alive or execute requests.
215          * <p>
216          * Default: {@code null} (undefined)
217          * </p>
218          *
219          * @return this instance.
220          */
221         public Builder setTimeToLive(final TimeValue timeToLive) {
222             this.timeToLive = timeToLive;
223             return this;
224         }
225 
226         /**
227          * @return this instance.
228          * @see #setTimeToLive(TimeValue)
229          */
230         public Builder setTimeToLive(final long timeToLive, final TimeUnit timeUnit) {
231             this.timeToLive = TimeValue.of(timeToLive, timeUnit);
232             return this;
233         }
234 
235         public ConnectionConfig build() {
236             return new ConnectionConfig(
237                     connectTimeout != null ? connectTimeout : DEFAULT_CONNECT_TIMEOUT,
238                     socketTimeout,
239                     validateAfterInactivity,
240                     timeToLive);
241         }
242 
243     }
244 
245 }