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 *
60 * @deprecated Please use {@link java.net.URL#openConnection} instead.
61 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
62 *     for further details.
63 */
64@Deprecated
65public class EntityDeserializer {
66
67    private final ContentLengthStrategy lenStrategy;
68
69    public EntityDeserializer(final ContentLengthStrategy lenStrategy) {
70        super();
71        if (lenStrategy == null) {
72            throw new IllegalArgumentException("Content length strategy may not be null");
73        }
74        this.lenStrategy = lenStrategy;
75    }
76
77    protected BasicHttpEntity doDeserialize(
78            final SessionInputBuffer inbuffer,
79            final HttpMessage message) throws HttpException, IOException {
80        BasicHttpEntity entity = new BasicHttpEntity();
81
82        long len = this.lenStrategy.determineLength(message);
83        if (len == ContentLengthStrategy.CHUNKED) {
84            entity.setChunked(true);
85            entity.setContentLength(-1);
86            entity.setContent(new ChunkedInputStream(inbuffer));
87        } else if (len == ContentLengthStrategy.IDENTITY) {
88            entity.setChunked(false);
89            entity.setContentLength(-1);
90            entity.setContent(new IdentityInputStream(inbuffer));
91        } else {
92            entity.setChunked(false);
93            entity.setContentLength(len);
94            entity.setContent(new ContentLengthInputStream(inbuffer, len));
95        }
96
97        Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
98        if (contentTypeHeader != null) {
99            entity.setContentType(contentTypeHeader);
100        }
101        Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
102        if (contentEncodingHeader != null) {
103            entity.setContentEncoding(contentEncodingHeader);
104        }
105        return entity;
106    }
107
108    public HttpEntity deserialize(
109            final SessionInputBuffer inbuffer,
110            final HttpMessage message) throws HttpException, IOException {
111        if (inbuffer == null) {
112            throw new IllegalArgumentException("Session input buffer may not be null");
113        }
114        if (message == null) {
115            throw new IllegalArgumentException("HTTP message may not be null");
116        }
117        return doDeserialize(inbuffer, message);
118    }
119
120}
121