1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/EntityDeserializer.java $
3 * $Revision: 560358 $
4 * $Date: 2007-07-27 12:30:42 -0700 (Fri, 27 Jul 2007) $
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
32package org.apache.http.impl.entity;
33
34import java.io.IOException;
35
36import org.apache.http.Header;
37import org.apache.http.HttpEntity;
38import org.apache.http.HttpException;
39import org.apache.http.HttpMessage;
40import org.apache.http.entity.BasicHttpEntity;
41import org.apache.http.entity.ContentLengthStrategy;
42import org.apache.http.impl.io.ChunkedInputStream;
43import org.apache.http.impl.io.ContentLengthInputStream;
44import org.apache.http.impl.io.IdentityInputStream;
45import org.apache.http.io.SessionInputBuffer;
46import org.apache.http.protocol.HTTP;
47
48/**
49 * Default implementation of an entity deserializer.
50 * <p>
51 * This entity deserializer currently supports only "chunked" and "identitiy" transfer-coding</a>
52 * </p>
53 *
54 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
55 *
56 * @version $Revision: 560358 $
57 *
58 * @since 4.0
59 */
60public 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    protected BasicHttpEntity doDeserialize(
73            final SessionInputBuffer inbuffer,
74            final HttpMessage message) throws HttpException, IOException {
75        BasicHttpEntity entity = new BasicHttpEntity();
76
77        long len = this.lenStrategy.determineLength(message);
78        if (len == ContentLengthStrategy.CHUNKED) {
79            entity.setChunked(true);
80            entity.setContentLength(-1);
81            entity.setContent(new ChunkedInputStream(inbuffer));
82        } else if (len == ContentLengthStrategy.IDENTITY) {
83            entity.setChunked(false);
84            entity.setContentLength(-1);
85            entity.setContent(new IdentityInputStream(inbuffer));
86        } else {
87            entity.setChunked(false);
88            entity.setContentLength(len);
89            entity.setContent(new ContentLengthInputStream(inbuffer, len));
90        }
91
92        Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
93        if (contentTypeHeader != null) {
94            entity.setContentType(contentTypeHeader);
95        }
96        Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
97        if (contentEncodingHeader != null) {
98            entity.setContentEncoding(contentEncodingHeader);
99        }
100        return entity;
101    }
102
103    public HttpEntity deserialize(
104            final SessionInputBuffer inbuffer,
105            final HttpMessage message) throws HttpException, IOException {
106        if (inbuffer == null) {
107            throw new IllegalArgumentException("Session input buffer may not be null");
108        }
109        if (message == null) {
110            throw new IllegalArgumentException("HTTP message may not be null");
111        }
112        return doDeserialize(inbuffer, message);
113    }
114
115}
116