View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.client.protocol;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.util.Locale;
32  import java.util.zip.GZIPInputStream;
33  
34  import org.apache.http.Header;
35  import org.apache.http.HeaderElement;
36  import org.apache.http.HttpEntity;
37  import org.apache.http.HttpException;
38  import org.apache.http.HttpResponse;
39  import org.apache.http.HttpResponseInterceptor;
40  import org.apache.http.annotation.Immutable;
41  import org.apache.http.client.config.RequestConfig;
42  import org.apache.http.client.entity.DecompressingEntity;
43  import org.apache.http.client.entity.DeflateInputStream;
44  import org.apache.http.client.entity.InputStreamFactory;
45  import org.apache.http.config.Lookup;
46  import org.apache.http.config.RegistryBuilder;
47  import org.apache.http.protocol.HttpContext;
48  
49  /**
50   * {@link HttpResponseInterceptor} responsible for processing Content-Encoding
51   * responses.
52   * <p>
53   * Instances of this class are stateless and immutable, therefore threadsafe.
54   *
55   * @since 4.1
56   *
57   */
58  @Immutable
59  public class ResponseContentEncoding implements HttpResponseInterceptor {
60  
61      public static final String UNCOMPRESSED = "http.client.response.uncompressed";
62  
63      private final static InputStreamFactory GZIP = new InputStreamFactory() {
64  
65          @Override
66          public InputStream create(final InputStream instream) throws IOException {
67              return new GZIPInputStream(instream);
68          }
69      };
70  
71      private final static InputStreamFactory DEFLATE = new InputStreamFactory() {
72  
73          @Override
74          public InputStream create(final InputStream instream) throws IOException {
75              return new DeflateInputStream(instream);
76          }
77  
78      };
79  
80      private final Lookup<InputStreamFactory> decoderRegistry;
81      private final boolean ignoreUnknown;
82  
83      /**
84       * @since 4.5
85       */
86      public ResponseContentEncoding(final Lookup<InputStreamFactory> decoderRegistry, final boolean ignoreUnknown) {
87          this.decoderRegistry = decoderRegistry != null ? decoderRegistry :
88              RegistryBuilder.<InputStreamFactory>create()
89                      .register("gzip", GZIP)
90                      .register("x-gzip", GZIP)
91                      .register("deflate", DEFLATE)
92                      .build();
93          this.ignoreUnknown = ignoreUnknown;
94      }
95  
96      /**
97       * @since 4.5
98       */
99      public ResponseContentEncoding(final boolean ignoreUnknown) {
100         this(null, ignoreUnknown);
101     }
102 
103     /**
104      * @since 4.4
105      */
106     public ResponseContentEncoding(final Lookup<InputStreamFactory> decoderRegistry) {
107         this(decoderRegistry, true);
108     }
109 
110     /**
111      * Handles {@code gzip} and {@code deflate} compressed entities by using the following
112      * decoders:
113      * <ul>
114      * <li>gzip - see {@link GZIPInputStream}</li>
115      * <li>deflate - see {@link DeflateInputStream}</li>
116      * </ul>
117      */
118     public ResponseContentEncoding() {
119         this(null);
120     }
121 
122     @Override
123     public void process(
124             final HttpResponse response,
125             final HttpContext context) throws HttpException, IOException {
126         final HttpEntity entity = response.getEntity();
127 
128         final HttpClientContext clientContext = HttpClientContext.adapt(context);
129         final RequestConfig requestConfig = clientContext.getRequestConfig();
130         // entity can be null in case of 304 Not Modified, 204 No Content or similar
131         // check for zero length entity.
132         if (requestConfig.isContentCompressionEnabled() && entity != null && entity.getContentLength() != 0) {
133             final Header ceheader = entity.getContentEncoding();
134             if (ceheader != null) {
135                 final HeaderElement[] codecs = ceheader.getElements();
136                 for (final HeaderElement codec : codecs) {
137                     final String codecname = codec.getName().toLowerCase(Locale.ROOT);
138                     final InputStreamFactory decoderFactory = decoderRegistry.lookup(codecname);
139                     if (decoderFactory != null) {
140                         response.setEntity(new DecompressingEntity(response.getEntity(), decoderFactory));
141                         response.removeHeaders("Content-Length");
142                         response.removeHeaders("Content-Encoding");
143                         response.removeHeaders("Content-MD5");
144                     } else {
145                         if (!"identity".equals(codecname) && !ignoreUnknown) {
146                             throw new HttpException("Unsupported Content-Encoding: " + codec.getName());
147                         }
148                     }
149                 }
150             }
151         }
152     }
153 
154 }