1 /*
2 * Copyright 2003-2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * You are receiving this code free of charge, which represents many hours of
17 * effort from other individuals and corporations. As a responsible member
18 * of the community, you are asked (but not required) to donate any
19 * enhancements or improvements back to the community under a similar open
20 * source license. Thank you. -TMN
21 */
22 package groovyx.net.http;
23
24 import static groovyx.net.http.ContentEncoding.Type.DEFLATE;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.zip.InflaterInputStream;
29
30 import org.apache.http.HttpEntity;
31 import org.apache.http.entity.HttpEntityWrapper;
32
33 /**
34 * Content encoding used to handle Deflate responses.
35 * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a>
36 */
37 public class DeflateEncoding extends ContentEncoding {
38
39 @Override
40 public String getContentEncoding() {
41 return DEFLATE.toString();
42 }
43
44
45 /**
46 * Wraps the raw entity in a {@link InflaterEntity}.
47 */
48 @Override
49 public HttpEntity wrapResponseEntity( HttpEntity raw ) {
50 return new InflaterEntity( raw );
51 }
52
53 /**
54 * Entity used to interpret a Deflate-encoded response
55 * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a>
56 */
57 public static class InflaterEntity extends HttpEntityWrapper {
58
59 public InflaterEntity(final HttpEntity entity) {
60 super(entity);
61 }
62
63 /**
64 * returns a {@link InflaterInputStream} which wraps the original entity's
65 * content stream
66 * @see HttpEntity#getContent()
67 */
68 @Override
69 public InputStream getContent() throws IOException, IllegalStateException {
70 return new InflaterInputStream( wrappedEntity.getContent() );
71 }
72
73 /**
74 * @return -1
75 */
76 @Override
77 public long getContentLength() {
78 // length of ungzipped content is not known
79 return -1;
80 }
81 }
82
83 }