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  import org.apache.http.util.Args;
45  
46  /**
47   * HTTP entity deserializer.
48   * <p>
49   * This entity deserializer supports "chunked" and "identitiy" transfer-coding
50   * and content length delimited content.
51   * <p>
52   * This class relies on a specific implementation of
53   * {@link ContentLengthStrategy} to determine the content length or transfer
54   * encoding of the entity.
55   * <p>
56   * This class generates an instance of {@link HttpEntity} based on
57   * properties of the message. The content of the entity will be decoded
58   * transparently for the consumer.
59   *
60   * @since 4.0
61   *
62   * @deprecated (4.3) use {@link org.apache.http.impl.BHttpConnectionBase}
63   */
64  @Immutable // assuming injected dependencies are immutable
65  @Deprecated
66  public class EntityDeserializer {
67  
68      private final ContentLengthStrategy lenStrategy;
69  
70      public EntityDeserializer(final ContentLengthStrategy lenStrategy) {
71          super();
72          this.lenStrategy = Args.notNull(lenStrategy, "Content length strategy");
73      }
74  
75      /**
76       * Creates a {@link BasicHttpEntity} based on properties of the given
77       * message. The content of the entity is created by wrapping
78       * {@link SessionInputBuffer} with a content decoder depending on the
79       * transfer mechanism used by the message.
80       * <p>
81       * This method is called by the public
82       * {@link #deserialize(SessionInputBuffer, HttpMessage)}.
83       *
84       * @param inbuffer the session input buffer.
85       * @param message the message.
86       * @return HTTP entity.
87       * @throws HttpException in case of HTTP protocol violation.
88       * @throws IOException in case of an I/O error.
89       */
90      protected BasicHttpEntity doDeserialize(
91              final SessionInputBuffer inbuffer,
92              final HttpMessage message) throws HttpException, IOException {
93          final BasicHttpEntity entity = new BasicHttpEntity();
94  
95          final long len = this.lenStrategy.determineLength(message);
96          if (len == ContentLengthStrategy.CHUNKED) {
97              entity.setChunked(true);
98              entity.setContentLength(-1);
99              entity.setContent(new ChunkedInputStream(inbuffer));
100         } else if (len == ContentLengthStrategy.IDENTITY) {
101             entity.setChunked(false);
102             entity.setContentLength(-1);
103             entity.setContent(new IdentityInputStream(inbuffer));
104         } else {
105             entity.setChunked(false);
106             entity.setContentLength(len);
107             entity.setContent(new ContentLengthInputStream(inbuffer, len));
108         }
109 
110         final Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
111         if (contentTypeHeader != null) {
112             entity.setContentType(contentTypeHeader);
113         }
114         final Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
115         if (contentEncodingHeader != null) {
116             entity.setContentEncoding(contentEncodingHeader);
117         }
118         return entity;
119     }
120 
121     /**
122      * Creates an {@link HttpEntity} based on properties of the given message.
123      * The content of the entity is created by wrapping
124      * {@link SessionInputBuffer} with a content decoder depending on the
125      * transfer mechanism used by the message.
126      * <p>
127      * The content of the entity is NOT retrieved by this method.
128      *
129      * @param inbuffer the session input buffer.
130      * @param message the message.
131      * @return HTTP entity.
132      * @throws HttpException in case of HTTP protocol violation.
133      * @throws IOException in case of an I/O error.
134      */
135     public HttpEntity deserialize(
136             final SessionInputBuffer inbuffer,
137             final HttpMessage message) throws HttpException, IOException {
138         Args.notNull(inbuffer, "Session input buffer");
139         Args.notNull(message, "HTTP message");
140         return doDeserialize(inbuffer, message);
141     }
142 
143 }