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