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