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  import java.io.OutputStream;
32  
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.ContentLengthStrategy;
39  import org.apache.http.impl.io.ChunkedOutputStream;
40  import org.apache.http.impl.io.ContentLengthOutputStream;
41  import org.apache.http.impl.io.IdentityOutputStream;
42  import org.apache.http.io.SessionOutputBuffer;
43  import org.apache.http.util.Args;
44  
45  /**
46   * HTTP entity serializer.
47   * <p>
48   * This entity serializer currently supports "chunked" and "identitiy"
49   * transfer-coding 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 writes out the content of {@link HttpEntity} to the data stream
56   * using a transfer coding based on properties on the HTTP message.
57   *
58   * @since 4.0
59   *
60   * @deprecated (4.3) use {@link org.apache.http.impl.BHttpConnectionBase}
61   */
62  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
63  @Deprecated
64  public class EntitySerializer {
65  
66      private final ContentLengthStrategy lenStrategy;
67  
68      public EntitySerializer(final ContentLengthStrategy lenStrategy) {
69          super();
70          this.lenStrategy = Args.notNull(lenStrategy, "Content length strategy");
71      }
72  
73      /**
74       * Creates a transfer codec based on properties of the given HTTP message
75       * and returns {@link OutputStream} instance that transparently encodes
76       * output data as it is being written out to the output stream.
77       * <p>
78       * This method is called by the public
79       * {@link #serialize(SessionOutputBuffer, HttpMessage, HttpEntity)}.
80       *
81       * @param outbuffer the session output buffer.
82       * @param message the HTTP message.
83       * @return output stream.
84       * @throws HttpException in case of HTTP protocol violation.
85       * @throws IOException in case of an I/O error.
86       */
87      protected OutputStream doSerialize(
88              final SessionOutputBuffer outbuffer,
89              final HttpMessage message) throws HttpException, IOException {
90          final long len = this.lenStrategy.determineLength(message);
91          if (len == ContentLengthStrategy.CHUNKED) {
92              return new ChunkedOutputStream(outbuffer);
93          } else if (len == ContentLengthStrategy.IDENTITY) {
94              return new IdentityOutputStream(outbuffer);
95          } else {
96              return new ContentLengthOutputStream(outbuffer, len);
97          }
98      }
99  
100     /**
101      * Writes out the content of the given HTTP entity to the session output
102      * buffer based on properties of the given HTTP message.
103      *
104      * @param outbuffer the output session buffer.
105      * @param message the HTTP message.
106      * @param entity the HTTP entity to be written out.
107      * @throws HttpException in case of HTTP protocol violation.
108      * @throws IOException in case of an I/O error.
109      */
110     public void serialize(
111             final SessionOutputBuffer outbuffer,
112             final HttpMessage message,
113             final HttpEntity entity) throws HttpException, IOException {
114         Args.notNull(outbuffer, "Session output buffer");
115         Args.notNull(message, "HTTP message");
116         Args.notNull(entity, "HTTP entity");
117         final OutputStream outStream = doSerialize(outbuffer, message);
118         entity.writeTo(outStream);
119         outStream.close();
120     }
121 
122 }