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.http;
29  
30  import org.apache.http.protocol.HttpContext;
31  
32  /**
33   * Interface for deciding whether a connection can be re-used for
34   * subsequent requests and should be kept alive.
35   * <p>
36   * Implementations of this interface must be thread-safe. Access to shared
37   * data must be synchronized as methods of this interface may be executed
38   * from multiple threads.
39   *
40   * @since 4.0
41   */
42  public interface ConnectionReuseStrategy {
43  
44      /**
45       * Decides whether a connection can be kept open after a request.
46       * If this method returns {@code false}, the caller MUST
47       * close the connection to correctly comply with the HTTP protocol.
48       * If it returns {@code true}, the caller SHOULD attempt to
49       * keep the connection open for reuse with another request.
50       * <p>
51       * One can use the HTTP context to retrieve additional objects that
52       * may be relevant for the keep-alive strategy: the actual HTTP
53       * connection, the original HTTP request, target host if known,
54       * number of times the connection has been reused already and so on.
55       * </p>
56       * <p>
57       * If the connection is already closed, {@code false} is returned.
58       * The stale connection check MUST NOT be triggered by a
59       * connection reuse strategy.
60       * </p>
61       *
62       * @param response
63       *          The last response received over that connection.
64       * @param context   the context in which the connection is being
65       *          used.
66       *
67       * @return {@code true} if the connection is allowed to be reused, or
68       *         {@code false} if it MUST NOT be reused
69       */
70      boolean keepAlive(HttpResponse response, HttpContext context);
71  
72  }