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.client.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.annotation.Contract; 036import org.apache.http.annotation.ThreadingBehavior; 037import org.apache.http.client.config.RequestConfig; 038import org.apache.http.protocol.HttpContext; 039 040/** 041 * Class responsible for handling Content Encoding requests in HTTP. 042 * <p> 043 * Instances of this class are stateless, therefore they're thread-safe and immutable. 044 * 045 * @see "http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5" 046 * 047 * @since 4.1 048 */ 049@Contract(threading = ThreadingBehavior.IMMUTABLE) 050public class RequestAcceptEncoding implements HttpRequestInterceptor { 051 052 private final String acceptEncoding; 053 054 /** 055 * @since 4.4 056 */ 057 public RequestAcceptEncoding(final List<String> encodings) { 058 if (encodings != null && !encodings.isEmpty()) { 059 final StringBuilder buf = new StringBuilder(); 060 for (int i = 0; i < encodings.size(); i++) { 061 if (i > 0) { 062 buf.append(","); 063 } 064 buf.append(encodings.get(i)); 065 } 066 this.acceptEncoding = buf.toString(); 067 } else { 068 this.acceptEncoding = "gzip,deflate"; 069 } 070 } 071 072 public RequestAcceptEncoding() { 073 this(null); 074 } 075 076 @Override 077 public void process( 078 final HttpRequest request, 079 final HttpContext context) throws HttpException, IOException { 080 081 final HttpClientContext clientContext = HttpClientContext.adapt(context); 082 final RequestConfig requestConfig = clientContext.getRequestConfig(); 083 084 /* Signal support for Accept-Encoding transfer encodings. */ 085 if (!request.containsHeader("Accept-Encoding") && requestConfig.isContentCompressionEnabled()) { 086 request.addHeader("Accept-Encoding", acceptEncoding); 087 } 088 } 089 090}