001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.protocol;
029
030import org.apache.http.HttpRequestInterceptor;
031import org.apache.http.HttpResponseInterceptor;
032
033/**
034 * Builder for {@link HttpProcessor} instances.
035 *
036 * @since 4.3
037 */
038public class HttpProcessorBuilder {
039
040    private ChainBuilder<HttpRequestInterceptor> requestChainBuilder;
041    private ChainBuilder<HttpResponseInterceptor> responseChainBuilder;
042
043    public static HttpProcessorBuilder create() {
044        return new HttpProcessorBuilder();
045    }
046
047    HttpProcessorBuilder() {
048        super();
049    }
050
051    private ChainBuilder<HttpRequestInterceptor> getRequestChainBuilder() {
052        if (requestChainBuilder == null) {
053            requestChainBuilder = new ChainBuilder<HttpRequestInterceptor>();
054        }
055        return requestChainBuilder;
056    }
057
058    private ChainBuilder<HttpResponseInterceptor> getResponseChainBuilder() {
059        if (responseChainBuilder == null) {
060            responseChainBuilder = new ChainBuilder<HttpResponseInterceptor>();
061        }
062        return responseChainBuilder;
063    }
064
065    public HttpProcessorBuilder addFirst(final HttpRequestInterceptor e) {
066        if (e == null) {
067            return this;
068        }
069        getRequestChainBuilder().addFirst(e);
070        return this;
071    }
072
073    public HttpProcessorBuilder addLast(final HttpRequestInterceptor e) {
074        if (e == null) {
075            return this;
076        }
077        getRequestChainBuilder().addLast(e);
078        return this;
079    }
080
081    public HttpProcessorBuilder add(final HttpRequestInterceptor e) {
082        return addLast(e);
083    }
084
085    public HttpProcessorBuilder addAllFirst(final HttpRequestInterceptor... e) {
086        if (e == null) {
087            return this;
088        }
089        getRequestChainBuilder().addAllFirst(e);
090        return this;
091    }
092
093    public HttpProcessorBuilder addAllLast(final HttpRequestInterceptor... e) {
094        if (e == null) {
095            return this;
096        }
097        getRequestChainBuilder().addAllLast(e);
098        return this;
099    }
100
101    public HttpProcessorBuilder addAll(final HttpRequestInterceptor... e) {
102        return addAllLast(e);
103    }
104
105    public HttpProcessorBuilder addFirst(final HttpResponseInterceptor e) {
106        if (e == null) {
107            return this;
108        }
109        getResponseChainBuilder().addFirst(e);
110        return this;
111    }
112
113    public HttpProcessorBuilder addLast(final HttpResponseInterceptor e) {
114        if (e == null) {
115            return this;
116        }
117        getResponseChainBuilder().addLast(e);
118        return this;
119    }
120
121    public HttpProcessorBuilder add(final HttpResponseInterceptor e) {
122        return addLast(e);
123    }
124
125    public HttpProcessorBuilder addAllFirst(final HttpResponseInterceptor... e) {
126        if (e == null) {
127            return this;
128        }
129        getResponseChainBuilder().addAllFirst(e);
130        return this;
131    }
132
133    public HttpProcessorBuilder addAllLast(final HttpResponseInterceptor... e) {
134        if (e == null) {
135            return this;
136        }
137        getResponseChainBuilder().addAllLast(e);
138        return this;
139    }
140
141    public HttpProcessorBuilder addAll(final HttpResponseInterceptor... e) {
142        return addAllLast(e);
143    }
144
145    public HttpProcessor build() {
146        return new ImmutableHttpProcessor(
147                requestChainBuilder != null ? requestChainBuilder.build() : null,
148                responseChainBuilder != null ? responseChainBuilder.build() : null);
149    }
150
151}