001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.http.client; 018 019import java.io.IOException; 020 021import org.springframework.http.HttpRequest; 022import org.springframework.http.client.support.InterceptingAsyncHttpAccessor; 023import org.springframework.util.concurrent.ListenableFuture; 024 025/** 026 * Intercepts client-side HTTP requests. Implementations of this interface can be 027 * {@linkplain org.springframework.web.client.AsyncRestTemplate#setInterceptors registered} 028 * with the {@link org.springframework.web.client.AsyncRestTemplate} as to modify 029 * the outgoing {@link HttpRequest} and/or register to modify the incoming 030 * {@link ClientHttpResponse} with help of a 031 * {@link org.springframework.util.concurrent.ListenableFutureAdapter}. 032 * 033 * <p>The main entry point for interceptors is {@link #intercept}. 034 * 035 * @author Jakub Narloch 036 * @author Rossen Stoyanchev 037 * @since 4.3 038 * @see org.springframework.web.client.AsyncRestTemplate 039 * @see InterceptingAsyncHttpAccessor 040 */ 041public interface AsyncClientHttpRequestInterceptor { 042 043 /** 044 * Intercept the given request, and return a response future. The given 045 * {@link AsyncClientHttpRequestExecution} allows the interceptor to pass on 046 * the request to the next entity in the chain. 047 * <p>An implementation might follow this pattern: 048 * <ol> 049 * <li>Examine the {@linkplain HttpRequest request} and body</li> 050 * <li>Optionally {@linkplain org.springframework.http.client.support.HttpRequestWrapper 051 * wrap} the request to filter HTTP attributes.</li> 052 * <li>Optionally modify the body of the request.</li> 053 * <li>One of the following: 054 * <ul> 055 * <li>execute the request through {@link ClientHttpRequestExecution}</li> 056 * <li>don't execute the request to block the execution altogether</li> 057 * </ul> 058 * <li>Optionally adapt the response to filter HTTP attributes with the help of 059 * {@link org.springframework.util.concurrent.ListenableFutureAdapter 060 * ListenableFutureAdapter}.</li> 061 * </ol> 062 * @param request the request, containing method, URI, and headers 063 * @param body the body of the request 064 * @param execution the request execution 065 * @return the response future 066 * @throws IOException in case of I/O errors 067 */ 068 ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, 069 AsyncClientHttpRequestExecution execution) throws IOException; 070 071}