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