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