001/*
002 * Copyright 2002-2017 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.net.URI;
020import java.util.Collections;
021import java.util.List;
022
023import org.springframework.http.HttpMethod;
024
025/**
026 * {@link ClientHttpRequestFactory} wrapper with support for {@link ClientHttpRequestInterceptor}s.
027 *
028 * @author Arjen Poutsma
029 * @since 3.1
030 * @see ClientHttpRequestFactory
031 * @see ClientHttpRequestInterceptor
032 */
033public class InterceptingClientHttpRequestFactory extends AbstractClientHttpRequestFactoryWrapper {
034
035        private final List<ClientHttpRequestInterceptor> interceptors;
036
037
038        /**
039         * Create a new instance of the {@code InterceptingClientHttpRequestFactory} with the given parameters.
040         * @param requestFactory the request factory to wrap
041         * @param interceptors the interceptors that are to be applied (can be {@code null})
042         */
043        public InterceptingClientHttpRequestFactory(ClientHttpRequestFactory requestFactory,
044                        List<ClientHttpRequestInterceptor> interceptors) {
045
046                super(requestFactory);
047                this.interceptors = (interceptors != null ? interceptors : Collections.<ClientHttpRequestInterceptor>emptyList());
048        }
049
050
051        @Override
052        protected ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod, ClientHttpRequestFactory requestFactory) {
053                return new InterceptingClientHttpRequest(requestFactory, this.interceptors, uri, httpMethod);
054        }
055
056}