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  package org.apache.hc.client5.http.impl;
28  
29  import java.util.Iterator;
30  
31  import org.apache.hc.client5.http.ConnectionKeepAliveStrategy;
32  import org.apache.hc.client5.http.config.RequestConfig;
33  import org.apache.hc.client5.http.protocol.HttpClientContext;
34  import org.apache.hc.core5.annotation.Contract;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  import org.apache.hc.core5.http.HeaderElement;
37  import org.apache.hc.core5.http.HeaderElements;
38  import org.apache.hc.core5.http.HttpResponse;
39  import org.apache.hc.core5.http.message.MessageSupport;
40  import org.apache.hc.core5.http.protocol.HttpContext;
41  import org.apache.hc.core5.util.Args;
42  import org.apache.hc.core5.util.TimeValue;
43  
44  /**
45   * Default implementation of a strategy deciding duration
46   * that a connection can remain idle.
47   * <p>
48   * The default implementation looks solely at the 'Keep-Alive'
49   * header's timeout token.
50   * </p>
51   *
52   * @since 4.0
53   */
54  @Contract(threading = ThreadingBehavior.STATELESS)
55  public class DefaultConnectionKeepAliveStrategy implements ConnectionKeepAliveStrategy {
56  
57      public static final DefaultConnectionKeepAliveStrategy INSTANCE = new DefaultConnectionKeepAliveStrategy();
58  
59      @Override
60      public TimeValue getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
61          Args.notNull(response, "HTTP response");
62          final Iterator<HeaderElement> it = MessageSupport.iterate(response, HeaderElements.KEEP_ALIVE);
63          while (it.hasNext()) {
64              final HeaderElement he = it.next();
65              final String param = he.getName();
66              final String value = he.getValue();
67              if (value != null && param.equalsIgnoreCase("timeout")) {
68                  try {
69                      return TimeValue.ofSeconds(Long.parseLong(value));
70                  } catch(final NumberFormatException ignore) {
71                  }
72              }
73          }
74          final HttpClientContext clientContext = HttpClientContext.adapt(context);
75          final RequestConfig requestConfig = clientContext.getRequestConfig();
76          return requestConfig.getConnectionKeepAlive();
77      }
78  
79  }