michael@0: /*
michael@0: * ====================================================================
michael@0: *
michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more
michael@0: * contributor license agreements. See the NOTICE file distributed with
michael@0: * this work for additional information regarding copyright ownership.
michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0
michael@0: * (the "License"); you may not use this file except in compliance with
michael@0: * the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing, software
michael@0: * distributed under the License is distributed on an "AS IS" BASIS,
michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0: * See the License for the specific language governing permissions and
michael@0: * limitations under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.client.protocol;
michael@0:
michael@0: import java.io.IOException;
michael@0: import java.util.Locale;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.Header;
michael@0: import ch.boye.httpclientandroidlib.HeaderElement;
michael@0: import ch.boye.httpclientandroidlib.HttpEntity;
michael@0: import ch.boye.httpclientandroidlib.HttpException;
michael@0: import ch.boye.httpclientandroidlib.HttpResponse;
michael@0: import ch.boye.httpclientandroidlib.HttpResponseInterceptor;
michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0: import ch.boye.httpclientandroidlib.client.entity.DeflateDecompressingEntity;
michael@0: import ch.boye.httpclientandroidlib.client.entity.GzipDecompressingEntity;
michael@0: import ch.boye.httpclientandroidlib.protocol.HttpContext;
michael@0:
michael@0: /**
michael@0: * {@link HttpResponseInterceptor} responsible for processing Content-Encoding
michael@0: * responses.
michael@0: *
michael@0: * Instances of this class are stateless and immutable, therefore threadsafe.
michael@0: *
michael@0: * @since 4.1
michael@0: *
michael@0: */
michael@0: @Immutable
michael@0: public class ResponseContentEncoding implements HttpResponseInterceptor {
michael@0:
michael@0: /**
michael@0: * Handles the following {@code Content-Encoding}s by
michael@0: * using the appropriate decompressor to wrap the response Entity:
michael@0: *
michael@0: * - gzip - see {@link GzipDecompressingEntity}
michael@0: * - deflate - see {@link DeflateDecompressingEntity}
michael@0: * - identity - no action needed
michael@0: *
michael@0: *
michael@0: * @param response the response which contains the entity
michael@0: * @param context not currently used
michael@0: *
michael@0: * @throws HttpException if the {@code Content-Encoding} is none of the above
michael@0: */
michael@0: public void process(
michael@0: final HttpResponse response,
michael@0: final HttpContext context) throws HttpException, IOException {
michael@0: HttpEntity entity = response.getEntity();
michael@0:
michael@0: // It wasn't a 304 Not Modified response, 204 No Content or similar
michael@0: if (entity != null) {
michael@0: Header ceheader = entity.getContentEncoding();
michael@0: if (ceheader != null) {
michael@0: HeaderElement[] codecs = ceheader.getElements();
michael@0: for (HeaderElement codec : codecs) {
michael@0: String codecname = codec.getName().toLowerCase(Locale.US);
michael@0: if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) {
michael@0: response.setEntity(new GzipDecompressingEntity(response.getEntity()));
michael@0: return;
michael@0: } else if ("deflate".equals(codecname)) {
michael@0: response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
michael@0: return;
michael@0: } else if ("identity".equals(codecname)) {
michael@0:
michael@0: /* Don't need to transform the content - no-op */
michael@0: return;
michael@0: } else {
michael@0: throw new HttpException("Unsupported Content-Coding: " + codec.getName());
michael@0: }
michael@0: }
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: }