|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with 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, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.impl.entity; |
|
29 |
|
30 import java.io.IOException; |
|
31 |
|
32 import ch.boye.httpclientandroidlib.Header; |
|
33 import ch.boye.httpclientandroidlib.HttpEntity; |
|
34 import ch.boye.httpclientandroidlib.HttpException; |
|
35 import ch.boye.httpclientandroidlib.HttpMessage; |
|
36 import ch.boye.httpclientandroidlib.entity.BasicHttpEntity; |
|
37 import ch.boye.httpclientandroidlib.entity.ContentLengthStrategy; |
|
38 import ch.boye.httpclientandroidlib.impl.io.ChunkedInputStream; |
|
39 import ch.boye.httpclientandroidlib.impl.io.ContentLengthInputStream; |
|
40 import ch.boye.httpclientandroidlib.impl.io.IdentityInputStream; |
|
41 import ch.boye.httpclientandroidlib.io.SessionInputBuffer; |
|
42 import ch.boye.httpclientandroidlib.protocol.HTTP; |
|
43 |
|
44 /** |
|
45 * HTTP entity deserializer. |
|
46 * <p> |
|
47 * This entity deserializer supports "chunked" and "identitiy" transfer-coding |
|
48 * and content length delimited content. |
|
49 * <p> |
|
50 * This class relies on a specific implementation of |
|
51 * {@link ContentLengthStrategy} to determine the content length or transfer |
|
52 * encoding of the entity. |
|
53 * <p> |
|
54 * This class generates an instance of {@link HttpEntity} based on |
|
55 * properties of the message. The content of the entity will be decoded |
|
56 * transparently for the consumer. |
|
57 * |
|
58 * @since 4.0 |
|
59 */ |
|
60 public class EntityDeserializer { |
|
61 |
|
62 private final ContentLengthStrategy lenStrategy; |
|
63 |
|
64 public EntityDeserializer(final ContentLengthStrategy lenStrategy) { |
|
65 super(); |
|
66 if (lenStrategy == null) { |
|
67 throw new IllegalArgumentException("Content length strategy may not be null"); |
|
68 } |
|
69 this.lenStrategy = lenStrategy; |
|
70 } |
|
71 |
|
72 /** |
|
73 * Creates a {@link BasicHttpEntity} based on properties of the given |
|
74 * message. The content of the entity is created by wrapping |
|
75 * {@link SessionInputBuffer} with a content decoder depending on the |
|
76 * transfer mechanism used by the message. |
|
77 * <p> |
|
78 * This method is called by the public |
|
79 * {@link #deserialize(SessionInputBuffer, HttpMessage)}. |
|
80 * |
|
81 * @param inbuffer the session input buffer. |
|
82 * @param message the message. |
|
83 * @return HTTP entity. |
|
84 * @throws HttpException in case of HTTP protocol violation. |
|
85 * @throws IOException in case of an I/O error. |
|
86 */ |
|
87 protected BasicHttpEntity doDeserialize( |
|
88 final SessionInputBuffer inbuffer, |
|
89 final HttpMessage message) throws HttpException, IOException { |
|
90 BasicHttpEntity entity = new BasicHttpEntity(); |
|
91 |
|
92 long len = this.lenStrategy.determineLength(message); |
|
93 if (len == ContentLengthStrategy.CHUNKED) { |
|
94 entity.setChunked(true); |
|
95 entity.setContentLength(-1); |
|
96 entity.setContent(new ChunkedInputStream(inbuffer)); |
|
97 } else if (len == ContentLengthStrategy.IDENTITY) { |
|
98 entity.setChunked(false); |
|
99 entity.setContentLength(-1); |
|
100 entity.setContent(new IdentityInputStream(inbuffer)); |
|
101 } else { |
|
102 entity.setChunked(false); |
|
103 entity.setContentLength(len); |
|
104 entity.setContent(new ContentLengthInputStream(inbuffer, len)); |
|
105 } |
|
106 |
|
107 Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE); |
|
108 if (contentTypeHeader != null) { |
|
109 entity.setContentType(contentTypeHeader); |
|
110 } |
|
111 Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING); |
|
112 if (contentEncodingHeader != null) { |
|
113 entity.setContentEncoding(contentEncodingHeader); |
|
114 } |
|
115 return entity; |
|
116 } |
|
117 |
|
118 /** |
|
119 * Creates an {@link HttpEntity} based on properties of the given message. |
|
120 * The content of the entity is created by wrapping |
|
121 * {@link SessionInputBuffer} with a content decoder depending on the |
|
122 * transfer mechanism used by the message. |
|
123 * <p> |
|
124 * The content of the entity is NOT retrieved by this method. |
|
125 * |
|
126 * @param inbuffer the session input buffer. |
|
127 * @param message the message. |
|
128 * @return HTTP entity. |
|
129 * @throws HttpException in case of HTTP protocol violation. |
|
130 * @throws IOException in case of an I/O error. |
|
131 */ |
|
132 public HttpEntity deserialize( |
|
133 final SessionInputBuffer inbuffer, |
|
134 final HttpMessage message) throws HttpException, IOException { |
|
135 if (inbuffer == null) { |
|
136 throw new IllegalArgumentException("Session input buffer may not be null"); |
|
137 } |
|
138 if (message == null) { |
|
139 throw new IllegalArgumentException("HTTP message may not be null"); |
|
140 } |
|
141 return doDeserialize(inbuffer, message); |
|
142 } |
|
143 |
|
144 } |