001/* 002 * Copyright 2002-2011 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.support; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.springframework.http.client.ClientHttpRequestFactory; 023import org.springframework.http.client.ClientHttpRequestInterceptor; 024import org.springframework.http.client.InterceptingClientHttpRequestFactory; 025import org.springframework.util.CollectionUtils; 026 027/** 028 * Base class for {@link org.springframework.web.client.RestTemplate} and other HTTP accessing gateway helpers, adding 029 * interceptor-related properties to {@link HttpAccessor}'s common properties. 030 * 031 * <p>Not intended to be used directly. See {@link org.springframework.web.client.RestTemplate}. 032 * 033 * @author Arjen Poutsma 034 */ 035public abstract class InterceptingHttpAccessor extends HttpAccessor { 036 037 private List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(); 038 039 /** 040 * Sets the request interceptors that this accessor should use. 041 */ 042 public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) { 043 this.interceptors = interceptors; 044 } 045 046 /** 047 * Return the request interceptor that this accessor uses. 048 */ 049 public List<ClientHttpRequestInterceptor> getInterceptors() { 050 return interceptors; 051 } 052 053 @Override 054 public ClientHttpRequestFactory getRequestFactory() { 055 ClientHttpRequestFactory delegate = super.getRequestFactory(); 056 if (!CollectionUtils.isEmpty(getInterceptors())) { 057 return new InterceptingClientHttpRequestFactory(delegate, getInterceptors()); 058 } 059 else { 060 return delegate; 061 } 062 } 063 064}