001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http;
029
030import org.apache.http.protocol.HttpContext;
031
032/**
033 * Interface for deciding whether a connection can be re-used for
034 * subsequent requests and should be kept alive.
035 * <p>
036 * Implementations of this interface must be thread-safe. Access to shared
037 * data must be synchronized as methods of this interface may be executed
038 * from multiple threads.
039 *
040 * @since 4.0
041 */
042public interface ConnectionReuseStrategy {
043
044    /**
045     * Decides whether a connection can be kept open after a request.
046     * If this method returns {@code false}, the caller MUST
047     * close the connection to correctly comply with the HTTP protocol.
048     * If it returns {@code true}, the caller SHOULD attempt to
049     * keep the connection open for reuse with another request.
050     * <p>
051     * One can use the HTTP context to retrieve additional objects that
052     * may be relevant for the keep-alive strategy: the actual HTTP
053     * connection, the original HTTP request, target host if known,
054     * number of times the connection has been reused already and so on.
055     * </p>
056     * <p>
057     * If the connection is already closed, {@code false} is returned.
058     * The stale connection check MUST NOT be triggered by a
059     * connection reuse strategy.
060     * </p>
061     *
062     * @param response
063     *          The last response received over that connection.
064     * @param context   the context in which the connection is being
065     *          used.
066     *
067     * @return {@code true} if the connection is allowed to be reused, or
068     *         {@code false} if it MUST NOT be reused
069     */
070    boolean keepAlive(HttpResponse response, HttpContext context);
071
072}