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 */
036public interface ClientHttpRequestInterceptor {
037
038        /**
039         * Intercept the given request, and return a response. The given
040         * {@link ClientHttpRequestExecution} allows the interceptor to pass on the
041         * request and response to the next entity in the chain.
042         * <p>A typical implementation of this method would follow the following pattern:
043         * <ol>
044         * <li>Examine the {@linkplain HttpRequest request} and body</li>
045         * <li>Optionally {@linkplain org.springframework.http.client.support.HttpRequestWrapper
046         * wrap} the request to filter HTTP attributes.</li>
047         * <li>Optionally modify the body of the request.</li>
048         * <li><strong>Either</strong>
049         * <ul>
050         * <li>execute the request using
051         * {@link ClientHttpRequestExecution#execute(org.springframework.http.HttpRequest, byte[])},</li>
052         * <strong>or</strong>
053         * <li>do not execute the request to block the execution altogether.</li>
054         * </ul>
055         * <li>Optionally wrap the response to filter HTTP attributes.</li>
056         * </ol>
057         * @param request the request, containing method, URI, and headers
058         * @param body the body of the request
059         * @param execution the request execution
060         * @return the response
061         * @throws IOException in case of I/O errors
062         */
063        ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
064                        throws IOException;
065
066}