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          * @see #setSocketTimeout(Timeout)
144          */
145         public Builder setSocketTimeout(final int soTimeout, final TimeUnit timeUnit) {
146             this.socketTimeout = Timeout.of(soTimeout, timeUnit);
147             return this;
148         }
149 
150         /**
151          * Determines the default socket timeout value for I/O operations.
152          * <p>
153          * Default: {@code null} (undefined)
154          * </p>
155          *
156          * @return the default socket timeout value for I/O operations.
157          */
158         public Builder setSocketTimeout(final Timeout soTimeout) {
159             this.socketTimeout = soTimeout;
160             return this;
161         }
162 
163         /**
164          * Determines the timeout until a new connection is fully established.
165          * <p>
166          * A timeout value of zero is interpreted as an infinite timeout.
167          * </p>
168          * <p>
169          * Default: 3 minutes
170          * </p>
171          */
172         public Builder setConnectTimeout(final Timeout connectTimeout) {
173             this.connectTimeout = connectTimeout;
174             return this;
175         }
176 
177         /**
178          * @see #setConnectTimeout(Timeout)
179          */
180         public Builder setConnectTimeout(final long connectTimeout, final TimeUnit timeUnit) {
181             this.connectTimeout = Timeout.of(connectTimeout, timeUnit);
182             return this;
183         }
184 
185         /**
186          * Defines period of inactivity after which persistent connections must
187          * be re-validated prior to being leased to the consumer. Negative values passed
188          * to this method disable connection validation.
189          * <p>
190          * Default: {@code null} (undefined)
191          * </p>
192          */
193         public Builder setValidateAfterInactivity(final TimeValue validateAfterInactivity) {
194             this.validateAfterInactivity = validateAfterInactivity;
195             return this;
196         }
197 
198         /**
199          * @see #setValidateAfterInactivity(TimeValue)
200          */
201         public Builder setValidateAfterInactivity(final long validateAfterInactivity, final TimeUnit timeUnit) {
202             this.validateAfterInactivity = TimeValue.of(validateAfterInactivity, timeUnit);
203             return this;
204         }
205 
206         /**
207          * Defines the total span of time connections can be kept alive or execute requests.
208          * <p>
209          * Default: {@code null} (undefined)
210          * </p>
211          */
212         public Builder setTimeToLive(final TimeValue timeToLive) {
213             this.timeToLive = timeToLive;
214             return this;
215         }
216 
217         /**
218          * @see #setTimeToLive(TimeValue)
219          */
220         public Builder setTimeToLive(final long timeToLive, final TimeUnit timeUnit) {
221             this.timeToLive = TimeValue.of(timeToLive, timeUnit);
222             return this;
223         }
224 
225         public ConnectionConfig build() {
226             return new ConnectionConfig(
227                     connectTimeout != null ? connectTimeout : DEFAULT_CONNECT_TIMEOUT,
228                     socketTimeout,
229                     validateAfterInactivity,
230                     timeToLive);
231         }
232 
233     }
234 
235 }