View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.entity;
29  
30  import java.io.IOException;
31  
32  import org.apache.http.Header;
33  import org.apache.http.HttpEntity;
34  import org.apache.http.HttpException;
35  import org.apache.http.HttpMessage;
36  import org.apache.http.annotation.Immutable;
37  import org.apache.http.entity.BasicHttpEntity;
38  import org.apache.http.entity.ContentLengthStrategy;
39  import org.apache.http.impl.io.ChunkedInputStream;
40  import org.apache.http.impl.io.ContentLengthInputStream;
41  import org.apache.http.impl.io.IdentityInputStream;
42  import org.apache.http.io.SessionInputBuffer;
43  import org.apache.http.protocol.HTTP;
44  
45  /**
46   * HTTP entity deserializer.
47   * <p>
48   * This entity deserializer supports "chunked" and "identitiy" transfer-coding
49   * and content length delimited content.
50   * <p>
51   * This class relies on a specific implementation of
52   * {@link ContentLengthStrategy} to determine the content length or transfer
53   * encoding of the entity.
54   * <p>
55   * This class generates an instance of {@link HttpEntity} based on
56   * properties of the message. The content of the entity will be decoded
57   * transparently for the consumer.
58   *
59   * @since 4.0
60   */
61  @Immutable // assuming injected dependencies are immutable
62  public class EntityDeserializer {
63  
64      private final ContentLengthStrategy lenStrategy;
65  
66      public EntityDeserializer(final ContentLengthStrategy lenStrategy) {
67          super();
68          if (lenStrategy == null) {
69              throw new IllegalArgumentException("Content length strategy may not be null");
70          }
71          this.lenStrategy = lenStrategy;
72      }
73  
74      /**
75       * Creates a {@link BasicHttpEntity} based on properties of the given
76       * message. The content of the entity is created by wrapping
77       * {@link SessionInputBuffer} with a content decoder depending on the
78       * transfer mechanism used by the message.
79       * <p>
80       * This method is called by the public
81       * {@link #deserialize(SessionInputBuffer, HttpMessage)}.
82       *
83       * @param inbuffer the session input buffer.
84       * @param message the message.
85       * @return HTTP entity.
86       * @throws HttpException in case of HTTP protocol violation.
87       * @throws IOException in case of an I/O error.
88       */
89      protected BasicHttpEntity doDeserialize(
90              final SessionInputBuffer inbuffer,
91              final HttpMessage message) throws HttpException, IOException {
92          BasicHttpEntity entity = new BasicHttpEntity();
93  
94          long len = this.lenStrategy.determineLength(message);
95          if (len == ContentLengthStrategy.CHUNKED) {
96              entity.setChunked(true);
97              entity.setContentLength(-1);
98              entity.setContent(new ChunkedInputStream(inbuffer));
99          } else if (len == ContentLengthStrategy.IDENTITY) {
100             entity.setChunked(false);
101             entity.setContentLength(-1);
102             entity.setContent(new IdentityInputStream(inbuffer));
103         } else {
104             entity.setChunked(false);
105             entity.setContentLength(len);
106             entity.setContent(new ContentLengthInputStream(inbuffer, len));
107         }
108 
109         Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
110         if (contentTypeHeader != null) {
111             entity.setContentType(contentTypeHeader);
112         }
113         Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
114         if (contentEncodingHeader != null) {
115             entity.setContentEncoding(contentEncodingHeader);
116         }
117         return entity;
118     }
119 
120     /**
121      * Creates an {@link HttpEntity} based on properties of the given message.
122      * The content of the entity is created by wrapping
123      * {@link SessionInputBuffer} with a content decoder depending on the
124      * transfer mechanism used by the message.
125      * <p>
126      * The content of the entity is NOT retrieved by this method.
127      *
128      * @param inbuffer the session input buffer.
129      * @param message the message.
130      * @return HTTP entity.
131      * @throws HttpException in case of HTTP protocol violation.
132      * @throws IOException in case of an I/O error.
133      */
134     public HttpEntity deserialize(
135             final SessionInputBuffer inbuffer,
136             final HttpMessage message) throws HttpException, IOException {
137         if (inbuffer == null) {
138             throw new IllegalArgumentException("Session input buffer may not be null");
139         }
140         if (message == null) {
141             throw new IllegalArgumentException("HTTP message may not be null");
142         }
143         return doDeserialize(inbuffer, message);
144     }
145 
146 }