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