michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: *
michael@0: * This entity deserializer supports "chunked" and "identitiy" transfer-coding michael@0: * and content length delimited content. michael@0: *
michael@0: * This class relies on a specific implementation of michael@0: * {@link ContentLengthStrategy} to determine the content length or transfer michael@0: * encoding of the entity. michael@0: *
michael@0: * This class generates an instance of {@link HttpEntity} based on michael@0: * properties of the message. The content of the entity will be decoded michael@0: * transparently for the consumer. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public class EntityDeserializer { michael@0: michael@0: private final ContentLengthStrategy lenStrategy; michael@0: michael@0: public EntityDeserializer(final ContentLengthStrategy lenStrategy) { michael@0: super(); michael@0: if (lenStrategy == null) { michael@0: throw new IllegalArgumentException("Content length strategy may not be null"); michael@0: } michael@0: this.lenStrategy = lenStrategy; michael@0: } michael@0: michael@0: /** michael@0: * Creates a {@link BasicHttpEntity} based on properties of the given michael@0: * message. The content of the entity is created by wrapping michael@0: * {@link SessionInputBuffer} with a content decoder depending on the michael@0: * transfer mechanism used by the message. michael@0: *
michael@0: * This method is called by the public michael@0: * {@link #deserialize(SessionInputBuffer, HttpMessage)}. michael@0: * michael@0: * @param inbuffer the session input buffer. michael@0: * @param message the message. michael@0: * @return HTTP entity. michael@0: * @throws HttpException in case of HTTP protocol violation. michael@0: * @throws IOException in case of an I/O error. michael@0: */ michael@0: protected BasicHttpEntity doDeserialize( michael@0: final SessionInputBuffer inbuffer, michael@0: final HttpMessage message) throws HttpException, IOException { michael@0: BasicHttpEntity entity = new BasicHttpEntity(); michael@0: michael@0: long len = this.lenStrategy.determineLength(message); michael@0: if (len == ContentLengthStrategy.CHUNKED) { michael@0: entity.setChunked(true); michael@0: entity.setContentLength(-1); michael@0: entity.setContent(new ChunkedInputStream(inbuffer)); michael@0: } else if (len == ContentLengthStrategy.IDENTITY) { michael@0: entity.setChunked(false); michael@0: entity.setContentLength(-1); michael@0: entity.setContent(new IdentityInputStream(inbuffer)); michael@0: } else { michael@0: entity.setChunked(false); michael@0: entity.setContentLength(len); michael@0: entity.setContent(new ContentLengthInputStream(inbuffer, len)); michael@0: } michael@0: michael@0: Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE); michael@0: if (contentTypeHeader != null) { michael@0: entity.setContentType(contentTypeHeader); michael@0: } michael@0: Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING); michael@0: if (contentEncodingHeader != null) { michael@0: entity.setContentEncoding(contentEncodingHeader); michael@0: } michael@0: return entity; michael@0: } michael@0: michael@0: /** michael@0: * Creates an {@link HttpEntity} based on properties of the given message. michael@0: * The content of the entity is created by wrapping michael@0: * {@link SessionInputBuffer} with a content decoder depending on the michael@0: * transfer mechanism used by the message. michael@0: *
michael@0: * The content of the entity is NOT retrieved by this method. michael@0: * michael@0: * @param inbuffer the session input buffer. michael@0: * @param message the message. michael@0: * @return HTTP entity. michael@0: * @throws HttpException in case of HTTP protocol violation. michael@0: * @throws IOException in case of an I/O error. michael@0: */ michael@0: public HttpEntity deserialize( michael@0: final SessionInputBuffer inbuffer, michael@0: final HttpMessage message) throws HttpException, IOException { michael@0: if (inbuffer == null) { michael@0: throw new IllegalArgumentException("Session input buffer may not be null"); michael@0: } michael@0: if (message == null) { michael@0: throw new IllegalArgumentException("HTTP message may not be null"); michael@0: } michael@0: return doDeserialize(inbuffer, message); michael@0: } michael@0: michael@0: }