mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/protocol/ResponseContentEncoding.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2  * ====================================================================
     3  *
     4  *  Licensed to the Apache Software Foundation (ASF) under one or more
     5  *  contributor license agreements.  See the NOTICE file distributed with
     6  *  this work for additional information regarding copyright ownership.
     7  *  The ASF licenses this file to You under the Apache License, Version 2.0
     8  *  (the "License"); you may not use this file except in compliance with
     9  *  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, software
    14  *  distributed under the License is distributed on an "AS IS" BASIS,
    15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  *  See the License for the specific language governing permissions and
    17  *  limitations under the License.
    18  * ====================================================================
    19  *
    20  * This software consists of voluntary contributions made by many
    21  * individuals on behalf of the Apache Software Foundation.  For more
    22  * information on the Apache Software Foundation, please see
    23  * <http://www.apache.org/>.
    24  *
    25  */
    27 package ch.boye.httpclientandroidlib.client.protocol;
    29 import java.io.IOException;
    30 import java.util.Locale;
    32 import ch.boye.httpclientandroidlib.Header;
    33 import ch.boye.httpclientandroidlib.HeaderElement;
    34 import ch.boye.httpclientandroidlib.HttpEntity;
    35 import ch.boye.httpclientandroidlib.HttpException;
    36 import ch.boye.httpclientandroidlib.HttpResponse;
    37 import ch.boye.httpclientandroidlib.HttpResponseInterceptor;
    38 import ch.boye.httpclientandroidlib.annotation.Immutable;
    39 import ch.boye.httpclientandroidlib.client.entity.DeflateDecompressingEntity;
    40 import ch.boye.httpclientandroidlib.client.entity.GzipDecompressingEntity;
    41 import ch.boye.httpclientandroidlib.protocol.HttpContext;
    43 /**
    44  * {@link HttpResponseInterceptor} responsible for processing Content-Encoding
    45  * responses.
    46  * <p>
    47  * Instances of this class are stateless and immutable, therefore threadsafe.
    48  *
    49  * @since 4.1
    50  *
    51  */
    52 @Immutable
    53 public class ResponseContentEncoding implements HttpResponseInterceptor {
    55     /**
    56      * Handles the following {@code Content-Encoding}s by
    57      * using the appropriate decompressor to wrap the response Entity:
    58      * <ul>
    59      * <li>gzip - see {@link GzipDecompressingEntity}</li>
    60      * <li>deflate - see {@link DeflateDecompressingEntity}</li>
    61      * <li>identity - no action needed</li>
    62      * </ul>
    63      *
    64      * @param response the response which contains the entity
    65      * @param  context not currently used
    66      *
    67      * @throws HttpException if the {@code Content-Encoding} is none of the above
    68      */
    69     public void process(
    70             final HttpResponse response,
    71             final HttpContext context) throws HttpException, IOException {
    72         HttpEntity entity = response.getEntity();
    74         // It wasn't a 304 Not Modified response, 204 No Content or similar
    75         if (entity != null) {
    76             Header ceheader = entity.getContentEncoding();
    77             if (ceheader != null) {
    78                 HeaderElement[] codecs = ceheader.getElements();
    79                 for (HeaderElement codec : codecs) {
    80                     String codecname = codec.getName().toLowerCase(Locale.US);
    81                     if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) {
    82                         response.setEntity(new GzipDecompressingEntity(response.getEntity()));
    83                         return;
    84                     } else if ("deflate".equals(codecname)) {
    85                         response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
    86                         return;
    87                     } else if ("identity".equals(codecname)) {
    89                         /* Don't need to transform the content - no-op */
    90                         return;
    91                     } else {
    92                         throw new HttpException("Unsupported Content-Coding: " + codec.getName());
    93                     }
    94                 }
    95             }
    96         }
    97     }
    99 }

mercurial