001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/entity/EntityDeserializer.java $
003: * $Revision: 560358 $
004: * $Date: 2007-07-27 21:30:42 +0200 (Fri, 27 Jul 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.entity;
033:
034: import java.io.IOException;
035:
036: import org.apache.http.Header;
037: import org.apache.http.HttpEntity;
038: import org.apache.http.HttpException;
039: import org.apache.http.HttpMessage;
040: import org.apache.http.entity.BasicHttpEntity;
041: import org.apache.http.entity.ContentLengthStrategy;
042: import org.apache.http.impl.io.ChunkedInputStream;
043: import org.apache.http.impl.io.ContentLengthInputStream;
044: import org.apache.http.impl.io.IdentityInputStream;
045: import org.apache.http.io.SessionInputBuffer;
046: import org.apache.http.protocol.HTTP;
047:
048: /**
049: * Default implementation of an entity deserializer.
050: * <p>
051: * This entity deserializer currently supports only "chunked" and "identitiy" transfer-coding</a>
052: * </p>
053: *
054: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
055: *
056: * @version $Revision: 560358 $
057: *
058: * @since 4.0
059: */
060: public class EntityDeserializer {
061:
062: private final ContentLengthStrategy lenStrategy;
063:
064: public EntityDeserializer(final ContentLengthStrategy lenStrategy) {
065: super ();
066: if (lenStrategy == null) {
067: throw new IllegalArgumentException(
068: "Content length strategy may not be null");
069: }
070: this .lenStrategy = lenStrategy;
071: }
072:
073: protected BasicHttpEntity doDeserialize(
074: final SessionInputBuffer inbuffer, final HttpMessage message)
075: throws HttpException, IOException {
076: BasicHttpEntity entity = new BasicHttpEntity();
077:
078: long len = this .lenStrategy.determineLength(message);
079: if (len == ContentLengthStrategy.CHUNKED) {
080: entity.setChunked(true);
081: entity.setContentLength(-1);
082: entity.setContent(new ChunkedInputStream(inbuffer));
083: } else if (len == ContentLengthStrategy.IDENTITY) {
084: entity.setChunked(false);
085: entity.setContentLength(-1);
086: entity.setContent(new IdentityInputStream(inbuffer));
087: } else {
088: entity.setChunked(false);
089: entity.setContentLength(len);
090: entity.setContent(new ContentLengthInputStream(inbuffer,
091: len));
092: }
093:
094: Header contentTypeHeader = message
095: .getFirstHeader(HTTP.CONTENT_TYPE);
096: if (contentTypeHeader != null) {
097: entity.setContentType(contentTypeHeader);
098: }
099: Header contentEncodingHeader = message
100: .getFirstHeader(HTTP.CONTENT_ENCODING);
101: if (contentEncodingHeader != null) {
102: entity.setContentEncoding(contentEncodingHeader);
103: }
104: return entity;
105: }
106:
107: public HttpEntity deserialize(final SessionInputBuffer inbuffer,
108: final HttpMessage message) throws HttpException,
109: IOException {
110: if (inbuffer == null) {
111: throw new IllegalArgumentException(
112: "Session input buffer may not be null");
113: }
114: if (message == null) {
115: throw new IllegalArgumentException(
116: "HTTP message may not be null");
117: }
118: return doDeserialize(inbuffer, message);
119: }
120:
121: }
|