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.entity;
028
029/*
030 * ====================================================================
031 * Licensed to the Apache Software Foundation (ASF) under one
032 * or more contributor license agreements.  See the NOTICE file
033 * distributed with this work for additional information
034 * regarding copyright ownership.  The ASF licenses this file
035 * to you under the Apache License, Version 2.0 (the
036 * "License"); you may not use this file except in compliance
037 * with the License.  You may obtain a copy of the License at
038 *
039 *   http://www.apache.org/licenses/LICENSE-2.0
040 *
041 * Unless required by applicable law or agreed to in writing,
042 * software distributed under the License is distributed on an
043 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
044 * KIND, either express or implied.  See the License for the
045 * specific language governing permissions and limitations
046 * under the License.
047 * ====================================================================
048 *
049 * This software consists of voluntary contributions made by many
050 * individuals on behalf of the Apache Software Foundation.  For more
051 * information on the Apache Software Foundation, please see
052 * <http://www.apache.org/>.
053 *
054 */
055
056import java.io.IOException;
057import java.io.InputStream;
058import java.io.OutputStream;
059import java.util.zip.GZIPOutputStream;
060
061import org.apache.http.Header;
062import org.apache.http.HttpEntity;
063import org.apache.http.entity.HttpEntityWrapper;
064import org.apache.http.message.BasicHeader;
065import org.apache.http.protocol.HTTP;
066import org.apache.http.util.Args;
067
068/**
069 * Wrapping entity that compresses content when {@link #writeTo writing}.
070 *
071 *
072 * @since 4.0
073 */
074public class GzipCompressingEntity extends HttpEntityWrapper {
075
076    private static final String GZIP_CODEC = "gzip";
077
078    public GzipCompressingEntity(final HttpEntity entity) {
079        super(entity);
080    }
081
082    @Override
083    public Header getContentEncoding() {
084        return new BasicHeader(HTTP.CONTENT_ENCODING, GZIP_CODEC);
085    }
086
087    @Override
088    public long getContentLength() {
089        return -1;
090    }
091
092    @Override
093    public boolean isChunked() {
094        // force content chunking
095        return true;
096    }
097
098    @Override
099    public InputStream getContent() throws IOException {
100        throw new UnsupportedOperationException();
101    }
102
103    @Override
104    public void writeTo(final OutputStream outstream) throws IOException {
105        Args.notNull(outstream, "Output stream");
106        final GZIPOutputStream gzip = new GZIPOutputStream(outstream);
107        wrappedEntity.writeTo(gzip);
108        // Only close output stream if the wrapped entity has been
109        // successfully written out
110        gzip.close();
111    }
112
113}