001 /*
002 * Copyright 2003-2008 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * You are receiving this code free of charge, which represents many hours of
017 * effort from other individuals and corporations. As a responsible member
018 * of the community, you are asked (but not required) to donate any
019 * enhancements or improvements back to the community under a similar open
020 * source license. Thank you. -TMN
021 */
022 package groovyx.net.http;
023
024 import static groovyx.net.http.ContentEncoding.Type.DEFLATE;
025
026 import java.io.IOException;
027 import java.io.InputStream;
028 import java.util.zip.InflaterInputStream;
029
030 import org.apache.http.HttpEntity;
031 import org.apache.http.entity.HttpEntityWrapper;
032
033 /**
034 * Content encoding used to handle Deflate responses.
035 * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a>
036 */
037 public class DeflateEncoding extends ContentEncoding {
038
039 @Override
040 public String getContentEncoding() {
041 return DEFLATE.toString();
042 }
043
044
045 /**
046 * Wraps the raw entity in a {@link InflaterEntity}.
047 */
048 @Override
049 public HttpEntity wrapResponseEntity( HttpEntity raw ) {
050 return new InflaterEntity( raw );
051 }
052
053 /**
054 * Entity used to interpret a Deflate-encoded response
055 * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a>
056 */
057 public static class InflaterEntity extends HttpEntityWrapper {
058
059 public InflaterEntity(final HttpEntity entity) {
060 super(entity);
061 }
062
063 /**
064 * returns a {@link InflaterInputStream} which wraps the original entity's
065 * content stream
066 * @see HttpEntity#getContent()
067 */
068 @Override
069 public InputStream getContent() throws IOException, IllegalStateException {
070 return new InflaterInputStream( wrappedEntity.getContent() );
071 }
072
073 /**
074 * @return -1
075 */
076 @Override
077 public long getContentLength() {
078 // length of ungzipped content is not known
079 return -1;
080 }
081 }
082
083 }