2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/impl/entity/EntityDeserializer.java $
4
* $Date: 2009-02-14 18:04:18 +0100 (Sat, 14 Feb 2009) $
6
* ====================================================================
7
* Licensed to the Apache Software Foundation (ASF) under one
8
* or more contributor license agreements. See the NOTICE file
9
* distributed with this work for additional information
10
* regarding copyright ownership. The ASF licenses this file
11
* to you under the Apache License, Version 2.0 (the
12
* "License"); you may not use this file except in compliance
13
* with the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing,
18
* software distributed under the License is distributed on an
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
* KIND, either express or implied. See the License for the
21
* specific language governing permissions and limitations
23
* ====================================================================
25
* This software consists of voluntary contributions made by many
26
* individuals on behalf of the Apache Software Foundation. For more
27
* information on the Apache Software Foundation, please see
28
* <http://www.apache.org/>.
32
package org.apache.http.impl.entity;
34
import java.io.IOException;
36
import org.apache.http.Header;
37
import org.apache.http.HttpEntity;
38
import org.apache.http.HttpException;
39
import org.apache.http.HttpMessage;
40
import org.apache.http.entity.BasicHttpEntity;
41
import org.apache.http.entity.ContentLengthStrategy;
42
import org.apache.http.impl.io.ChunkedInputStream;
43
import org.apache.http.impl.io.ContentLengthInputStream;
44
import org.apache.http.impl.io.IdentityInputStream;
45
import org.apache.http.io.SessionInputBuffer;
46
import org.apache.http.protocol.HTTP;
49
* HTTP entity deserializer.
51
* This entity deserializer supports "chunked" and "identitiy" transfer-coding
52
* and content length delimited content.
54
* This class relies on a specific implementation of
55
* {@link ContentLengthStrategy} to determine the content length or transfer
56
* encoding of the entity.
58
* This class generates an instance of {@link HttpEntity} based on
59
* properties of the message. The content of the entity will be decoded
60
* transparently for the consumer.
63
* @version $Revision: 744526 $
67
public class EntityDeserializer {
69
private final ContentLengthStrategy lenStrategy;
71
public EntityDeserializer(final ContentLengthStrategy lenStrategy) {
73
if (lenStrategy == null) {
74
throw new IllegalArgumentException("Content length strategy may not be null");
76
this.lenStrategy = lenStrategy;
80
* Creates a {@link BasicHttpEntity} based on properties of the given
81
* message. The content of the entity is created by wrapping
82
* {@link SessionInputBuffer} with a content decoder depending on the
83
* transfer mechanism used by the message.
85
* This method is called by the public
86
* {@link #deserialize(SessionInputBuffer, HttpMessage)}.
88
* @param inbuffer the session input buffer.
89
* @param message the message.
90
* @return HTTP entity.
91
* @throws HttpException in case of HTTP protocol violation.
92
* @throws IOException in case of an I/O error.
94
protected BasicHttpEntity doDeserialize(
95
final SessionInputBuffer inbuffer,
96
final HttpMessage message) throws HttpException, IOException {
97
BasicHttpEntity entity = new BasicHttpEntity();
99
long len = this.lenStrategy.determineLength(message);
100
if (len == ContentLengthStrategy.CHUNKED) {
101
entity.setChunked(true);
102
entity.setContentLength(-1);
103
entity.setContent(new ChunkedInputStream(inbuffer));
104
} else if (len == ContentLengthStrategy.IDENTITY) {
105
entity.setChunked(false);
106
entity.setContentLength(-1);
107
entity.setContent(new IdentityInputStream(inbuffer));
109
entity.setChunked(false);
110
entity.setContentLength(len);
111
entity.setContent(new ContentLengthInputStream(inbuffer, len));
114
Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
115
if (contentTypeHeader != null) {
116
entity.setContentType(contentTypeHeader);
118
Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
119
if (contentEncodingHeader != null) {
120
entity.setContentEncoding(contentEncodingHeader);
126
* Creates an {@link HttpEntity} based on properties of the given message.
127
* The content of the entity is created by wrapping
128
* {@link SessionInputBuffer} with a content decoder depending on the
129
* transfer mechanism used by the message.
131
* The content of the entity is NOT retrieved by this method.
133
* @param inbuffer the session input buffer.
134
* @param message the message.
135
* @return HTTP entity.
136
* @throws HttpException in case of HTTP protocol violation.
137
* @throws IOException in case of an I/O error.
139
public HttpEntity deserialize(
140
final SessionInputBuffer inbuffer,
141
final HttpMessage message) throws HttpException, IOException {
142
if (inbuffer == null) {
143
throw new IllegalArgumentException("Session input buffer may not be null");
145
if (message == null) {
146
throw new IllegalArgumentException("HTTP message may not be null");
148
return doDeserialize(inbuffer, message);