1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/entity/EntityDeserializer.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.entity; 1.32 + 1.33 +import java.io.IOException; 1.34 + 1.35 +import ch.boye.httpclientandroidlib.Header; 1.36 +import ch.boye.httpclientandroidlib.HttpEntity; 1.37 +import ch.boye.httpclientandroidlib.HttpException; 1.38 +import ch.boye.httpclientandroidlib.HttpMessage; 1.39 +import ch.boye.httpclientandroidlib.entity.BasicHttpEntity; 1.40 +import ch.boye.httpclientandroidlib.entity.ContentLengthStrategy; 1.41 +import ch.boye.httpclientandroidlib.impl.io.ChunkedInputStream; 1.42 +import ch.boye.httpclientandroidlib.impl.io.ContentLengthInputStream; 1.43 +import ch.boye.httpclientandroidlib.impl.io.IdentityInputStream; 1.44 +import ch.boye.httpclientandroidlib.io.SessionInputBuffer; 1.45 +import ch.boye.httpclientandroidlib.protocol.HTTP; 1.46 + 1.47 +/** 1.48 + * HTTP entity deserializer. 1.49 + * <p> 1.50 + * This entity deserializer supports "chunked" and "identitiy" transfer-coding 1.51 + * and content length delimited content. 1.52 + * <p> 1.53 + * This class relies on a specific implementation of 1.54 + * {@link ContentLengthStrategy} to determine the content length or transfer 1.55 + * encoding of the entity. 1.56 + * <p> 1.57 + * This class generates an instance of {@link HttpEntity} based on 1.58 + * properties of the message. The content of the entity will be decoded 1.59 + * transparently for the consumer. 1.60 + * 1.61 + * @since 4.0 1.62 + */ 1.63 +public class EntityDeserializer { 1.64 + 1.65 + private final ContentLengthStrategy lenStrategy; 1.66 + 1.67 + public EntityDeserializer(final ContentLengthStrategy lenStrategy) { 1.68 + super(); 1.69 + if (lenStrategy == null) { 1.70 + throw new IllegalArgumentException("Content length strategy may not be null"); 1.71 + } 1.72 + this.lenStrategy = lenStrategy; 1.73 + } 1.74 + 1.75 + /** 1.76 + * Creates a {@link BasicHttpEntity} based on properties of the given 1.77 + * message. The content of the entity is created by wrapping 1.78 + * {@link SessionInputBuffer} with a content decoder depending on the 1.79 + * transfer mechanism used by the message. 1.80 + * <p> 1.81 + * This method is called by the public 1.82 + * {@link #deserialize(SessionInputBuffer, HttpMessage)}. 1.83 + * 1.84 + * @param inbuffer the session input buffer. 1.85 + * @param message the message. 1.86 + * @return HTTP entity. 1.87 + * @throws HttpException in case of HTTP protocol violation. 1.88 + * @throws IOException in case of an I/O error. 1.89 + */ 1.90 + protected BasicHttpEntity doDeserialize( 1.91 + final SessionInputBuffer inbuffer, 1.92 + final HttpMessage message) throws HttpException, IOException { 1.93 + BasicHttpEntity entity = new BasicHttpEntity(); 1.94 + 1.95 + long len = this.lenStrategy.determineLength(message); 1.96 + if (len == ContentLengthStrategy.CHUNKED) { 1.97 + entity.setChunked(true); 1.98 + entity.setContentLength(-1); 1.99 + entity.setContent(new ChunkedInputStream(inbuffer)); 1.100 + } else if (len == ContentLengthStrategy.IDENTITY) { 1.101 + entity.setChunked(false); 1.102 + entity.setContentLength(-1); 1.103 + entity.setContent(new IdentityInputStream(inbuffer)); 1.104 + } else { 1.105 + entity.setChunked(false); 1.106 + entity.setContentLength(len); 1.107 + entity.setContent(new ContentLengthInputStream(inbuffer, len)); 1.108 + } 1.109 + 1.110 + Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE); 1.111 + if (contentTypeHeader != null) { 1.112 + entity.setContentType(contentTypeHeader); 1.113 + } 1.114 + Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING); 1.115 + if (contentEncodingHeader != null) { 1.116 + entity.setContentEncoding(contentEncodingHeader); 1.117 + } 1.118 + return entity; 1.119 + } 1.120 + 1.121 + /** 1.122 + * Creates an {@link HttpEntity} based on properties of the given message. 1.123 + * The content of the entity is created by wrapping 1.124 + * {@link SessionInputBuffer} with a content decoder depending on the 1.125 + * transfer mechanism used by the message. 1.126 + * <p> 1.127 + * The content of the entity is NOT retrieved by this method. 1.128 + * 1.129 + * @param inbuffer the session input buffer. 1.130 + * @param message the message. 1.131 + * @return HTTP entity. 1.132 + * @throws HttpException in case of HTTP protocol violation. 1.133 + * @throws IOException in case of an I/O error. 1.134 + */ 1.135 + public HttpEntity deserialize( 1.136 + final SessionInputBuffer inbuffer, 1.137 + final HttpMessage message) throws HttpException, IOException { 1.138 + if (inbuffer == null) { 1.139 + throw new IllegalArgumentException("Session input buffer may not be null"); 1.140 + } 1.141 + if (message == null) { 1.142 + throw new IllegalArgumentException("HTTP message may not be null"); 1.143 + } 1.144 + return doDeserialize(inbuffer, message); 1.145 + } 1.146 + 1.147 +}