~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/main/java/org/apache/http/impl/entity/EntityDeserializer.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 744526 $
 
4
 * $Date: 2009-02-14 18:04:18 +0100 (Sat, 14 Feb 2009) $
 
5
 *
 
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
 
14
 *
 
15
 *   http://www.apache.org/licenses/LICENSE-2.0
 
16
 *
 
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
 
22
 * under the License.
 
23
 * ====================================================================
 
24
 *
 
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/>.
 
29
 *
 
30
 */
 
31
 
 
32
package org.apache.http.impl.entity;
 
33
 
 
34
import java.io.IOException;
 
35
 
 
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;
 
47
 
 
48
/**
 
49
 * HTTP entity deserializer.
 
50
 * <p>
 
51
 * This entity deserializer supports "chunked" and "identitiy" transfer-coding
 
52
 * and content length delimited content.
 
53
 * <p>
 
54
 * This class relies on a specific implementation of 
 
55
 * {@link ContentLengthStrategy} to determine the content length or transfer
 
56
 * encoding of the entity.
 
57
 * <p>
 
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. 
 
61
 * 
 
62
 *
 
63
 * @version $Revision: 744526 $
 
64
 * 
 
65
 * @since 4.0
 
66
 */
 
67
public class EntityDeserializer {
 
68
 
 
69
    private final ContentLengthStrategy lenStrategy;
 
70
    
 
71
    public EntityDeserializer(final ContentLengthStrategy lenStrategy) {
 
72
        super();
 
73
        if (lenStrategy == null) {
 
74
            throw new IllegalArgumentException("Content length strategy may not be null");
 
75
        }
 
76
        this.lenStrategy = lenStrategy;
 
77
    }
 
78
 
 
79
    /**
 
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.
 
84
     * <p>
 
85
     * This method is called by the public
 
86
     * {@link #deserialize(SessionInputBuffer, HttpMessage)}.
 
87
     * 
 
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.
 
93
     */
 
94
    protected BasicHttpEntity doDeserialize(
 
95
            final SessionInputBuffer inbuffer,
 
96
            final HttpMessage message) throws HttpException, IOException {
 
97
        BasicHttpEntity entity = new BasicHttpEntity();
 
98
        
 
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));                            
 
108
        } else {
 
109
            entity.setChunked(false);
 
110
            entity.setContentLength(len);
 
111
            entity.setContent(new ContentLengthInputStream(inbuffer, len));
 
112
        }
 
113
        
 
114
        Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
 
115
        if (contentTypeHeader != null) {
 
116
            entity.setContentType(contentTypeHeader);    
 
117
        }
 
118
        Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
 
119
        if (contentEncodingHeader != null) {
 
120
            entity.setContentEncoding(contentEncodingHeader);    
 
121
        }
 
122
        return entity;
 
123
    }
 
124
        
 
125
    /**
 
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.
 
130
     * <p>
 
131
     * The content of the entity is NOT retrieved by this method.
 
132
     *  
 
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.
 
138
     */
 
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");
 
144
        }
 
145
        if (message == null) {
 
146
            throw new IllegalArgumentException("HTTP message may not be null");
 
147
        }
 
148
        return doDeserialize(inbuffer, message);
 
149
    }
 
150
    
 
151
}