001/* 002 * Copyright 2002-2018 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; 022 023/** 024 * Intercepts client-side HTTP requests. Implementations of this interface can be 025 * {@linkplain org.springframework.web.client.RestTemplate#setInterceptors registered} 026 * with the {@link org.springframework.web.client.RestTemplate RestTemplate}, 027 * as to modify the outgoing {@link ClientHttpRequest} and/or the incoming 028 * {@link ClientHttpResponse}. 029 * 030 * <p>The main entry point for interceptors is 031 * {@link #intercept(HttpRequest, byte[], ClientHttpRequestExecution)}. 032 * 033 * @author Arjen Poutsma 034 * @since 3.1 035 */ 036@FunctionalInterface 037public interface ClientHttpRequestInterceptor { 038 039 /** 040 * Intercept the given request, and return a response. The given 041 * {@link ClientHttpRequestExecution} allows the interceptor to pass on the 042 * request and response to the next entity in the chain. 043 * <p>A typical implementation of this method would follow the following pattern: 044 * <ol> 045 * <li>Examine the {@linkplain HttpRequest request} and body</li> 046 * <li>Optionally {@linkplain org.springframework.http.client.support.HttpRequestWrapper 047 * wrap} the request to filter HTTP attributes.</li> 048 * <li>Optionally modify the body of the request.</li> 049 * <li><strong>Either</strong> 050 * <ul> 051 * <li>execute the request using 052 * {@link ClientHttpRequestExecution#execute(org.springframework.http.HttpRequest, byte[])},</li> 053 * <strong>or</strong> 054 * <li>do not execute the request to block the execution altogether.</li> 055 * </ul> 056 * <li>Optionally wrap the response to filter HTTP attributes.</li> 057 * </ol> 058 * @param request the request, containing method, URI, and headers 059 * @param body the body of the request 060 * @param execution the request execution 061 * @return the response 062 * @throws IOException in case of I/O errors 063 */ 064 ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) 065 throws IOException; 066 067}