001/*
002 * Copyright 2002-2016 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.AsyncClientHttpRequestFactory;
023import org.springframework.http.client.AsyncClientHttpRequestInterceptor;
024import org.springframework.http.client.InterceptingAsyncClientHttpRequestFactory;
025import org.springframework.util.CollectionUtils;
026
027/**
028 * The HTTP accessor that extends the base {@link AsyncHttpAccessor} with
029 * request intercepting functionality.
030 *
031 * @author Jakub Narloch
032 * @author Rossen Stoyanchev
033 * @since 4.3
034 */
035public abstract class InterceptingAsyncHttpAccessor extends AsyncHttpAccessor {
036
037    private List<AsyncClientHttpRequestInterceptor> interceptors =
038            new ArrayList<AsyncClientHttpRequestInterceptor>();
039
040
041    /**
042     * Set the request interceptors that this accessor should use.
043     * @param interceptors the list of interceptors
044     */
045    public void setInterceptors(List<AsyncClientHttpRequestInterceptor> interceptors) {
046        this.interceptors = interceptors;
047    }
048
049    /**
050     * Return the request interceptor that this accessor uses.
051     */
052    public List<AsyncClientHttpRequestInterceptor> getInterceptors() {
053        return this.interceptors;
054    }
055
056
057    @Override
058    public AsyncClientHttpRequestFactory getAsyncRequestFactory() {
059        AsyncClientHttpRequestFactory delegate = super.getAsyncRequestFactory();
060        if (!CollectionUtils.isEmpty(getInterceptors())) {
061            return new InterceptingAsyncClientHttpRequestFactory(delegate, getInterceptors());
062        }
063        else {
064            return delegate;
065        }
066    }
067
068}