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 */ 027package org.apache.http.protocol; 028 029import java.io.IOException; 030import java.util.List; 031 032import org.apache.http.HttpException; 033import org.apache.http.HttpRequest; 034import org.apache.http.HttpRequestInterceptor; 035import org.apache.http.HttpResponse; 036import org.apache.http.HttpResponseInterceptor; 037import org.apache.http.annotation.ThreadingBehavior; 038import org.apache.http.annotation.Contract; 039 040/** 041 * Immutable {@link HttpProcessor}. 042 * 043 * @since 4.1 044 */ 045@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL) 046public final class ImmutableHttpProcessor implements HttpProcessor { 047 048 private final HttpRequestInterceptor[] requestInterceptors; 049 private final HttpResponseInterceptor[] responseInterceptors; 050 051 public ImmutableHttpProcessor( 052 final HttpRequestInterceptor[] requestInterceptors, 053 final HttpResponseInterceptor[] responseInterceptors) { 054 super(); 055 if (requestInterceptors != null) { 056 final int l = requestInterceptors.length; 057 this.requestInterceptors = new HttpRequestInterceptor[l]; 058 System.arraycopy(requestInterceptors, 0, this.requestInterceptors, 0, l); 059 } else { 060 this.requestInterceptors = new HttpRequestInterceptor[0]; 061 } 062 if (responseInterceptors != null) { 063 final int l = responseInterceptors.length; 064 this.responseInterceptors = new HttpResponseInterceptor[l]; 065 System.arraycopy(responseInterceptors, 0, this.responseInterceptors, 0, l); 066 } else { 067 this.responseInterceptors = new HttpResponseInterceptor[0]; 068 } 069 } 070 071 /** 072 * @since 4.3 073 */ 074 public ImmutableHttpProcessor( 075 final List<HttpRequestInterceptor> requestInterceptors, 076 final List<HttpResponseInterceptor> responseInterceptors) { 077 super(); 078 if (requestInterceptors != null) { 079 final int l = requestInterceptors.size(); 080 this.requestInterceptors = requestInterceptors.toArray(new HttpRequestInterceptor[l]); 081 } else { 082 this.requestInterceptors = new HttpRequestInterceptor[0]; 083 } 084 if (responseInterceptors != null) { 085 final int l = responseInterceptors.size(); 086 this.responseInterceptors = responseInterceptors.toArray(new HttpResponseInterceptor[l]); 087 } else { 088 this.responseInterceptors = new HttpResponseInterceptor[0]; 089 } 090 } 091 092 /** 093 * @deprecated (4.3) do not use. 094 */ 095 @Deprecated 096 public ImmutableHttpProcessor( 097 final HttpRequestInterceptorList requestInterceptors, 098 final HttpResponseInterceptorList responseInterceptors) { 099 super(); 100 if (requestInterceptors != null) { 101 final int count = requestInterceptors.getRequestInterceptorCount(); 102 this.requestInterceptors = new HttpRequestInterceptor[count]; 103 for (int i = 0; i < count; i++) { 104 this.requestInterceptors[i] = requestInterceptors.getRequestInterceptor(i); 105 } 106 } else { 107 this.requestInterceptors = new HttpRequestInterceptor[0]; 108 } 109 if (responseInterceptors != null) { 110 final int count = responseInterceptors.getResponseInterceptorCount(); 111 this.responseInterceptors = new HttpResponseInterceptor[count]; 112 for (int i = 0; i < count; i++) { 113 this.responseInterceptors[i] = responseInterceptors.getResponseInterceptor(i); 114 } 115 } else { 116 this.responseInterceptors = new HttpResponseInterceptor[0]; 117 } 118 } 119 120 public ImmutableHttpProcessor(final HttpRequestInterceptor... requestInterceptors) { 121 this(requestInterceptors, null); 122 } 123 124 public ImmutableHttpProcessor(final HttpResponseInterceptor... responseInterceptors) { 125 this(null, responseInterceptors); 126 } 127 128 @Override 129 public void process( 130 final HttpRequest request, 131 final HttpContext context) throws IOException, HttpException { 132 for (final HttpRequestInterceptor requestInterceptor : this.requestInterceptors) { 133 requestInterceptor.process(request, context); 134 } 135 } 136 137 @Override 138 public void process( 139 final HttpResponse response, 140 final HttpContext context) throws IOException, HttpException { 141 for (final HttpResponseInterceptor responseInterceptor : this.responseInterceptors) { 142 responseInterceptor.process(response, context); 143 } 144 } 145 146}