2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/impl/entity/EntitySerializer.java $
4
* $Date: 2009-02-14 18:04:18 +0100 (Sat, 14 Feb 2009) $
6
* ====================================================================
7
* Licensed to the Apache Software Foundation (ASF) under one
8
* or more contributor license agreements. See the NOTICE file
9
* distributed with this work for additional information
10
* regarding copyright ownership. The ASF licenses this file
11
* to you under the Apache License, Version 2.0 (the
12
* "License"); you may not use this file except in compliance
13
* with the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing,
18
* software distributed under the License is distributed on an
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
* KIND, either express or implied. See the License for the
21
* specific language governing permissions and limitations
23
* ====================================================================
25
* This software consists of voluntary contributions made by many
26
* individuals on behalf of the Apache Software Foundation. For more
27
* information on the Apache Software Foundation, please see
28
* <http://www.apache.org/>.
32
package org.apache.http.impl.entity;
34
import java.io.IOException;
35
import java.io.OutputStream;
37
import org.apache.http.HttpEntity;
38
import org.apache.http.HttpException;
39
import org.apache.http.HttpMessage;
40
import org.apache.http.entity.ContentLengthStrategy;
41
import org.apache.http.impl.io.ChunkedOutputStream;
42
import org.apache.http.impl.io.ContentLengthOutputStream;
43
import org.apache.http.impl.io.IdentityOutputStream;
44
import org.apache.http.io.SessionOutputBuffer;
47
* HTTP entity serializer.
49
* This entity serializer currently supports "chunked" and "identitiy"
50
* transfer-coding and content length delimited content.
52
* This class relies on a specific implementation of
53
* {@link ContentLengthStrategy} to determine the content length or transfer
54
* encoding of the entity.
56
* This class writes out the content of {@link HttpEntity} to the data stream
57
* using a transfer coding based on properties on the HTTP message.
60
* @version $Revision: 744526 $
64
public class EntitySerializer {
66
private final ContentLengthStrategy lenStrategy;
68
public EntitySerializer(final ContentLengthStrategy lenStrategy) {
70
if (lenStrategy == null) {
71
throw new IllegalArgumentException("Content length strategy may not be null");
73
this.lenStrategy = lenStrategy;
77
* Creates a transfer codec based on properties of the given HTTP message
78
* and returns {@link OutputStream} instance that transparently encodes
79
* output data as it is being written out to the output stream.
81
* This method is called by the public
82
* {@link #serialize(SessionOutputBuffer, HttpMessage, HttpEntity)}.
84
* @param outbuffer the session output buffer.
85
* @param message the HTTP message.
86
* @return output stream.
87
* @throws HttpException in case of HTTP protocol violation.
88
* @throws IOException in case of an I/O error.
90
protected OutputStream doSerialize(
91
final SessionOutputBuffer outbuffer,
92
final HttpMessage message) throws HttpException, IOException {
93
long len = this.lenStrategy.determineLength(message);
94
if (len == ContentLengthStrategy.CHUNKED) {
95
return new ChunkedOutputStream(outbuffer);
96
} else if (len == ContentLengthStrategy.IDENTITY) {
97
return new IdentityOutputStream(outbuffer);
99
return new ContentLengthOutputStream(outbuffer, len);
104
* Writes out the content of the given HTTP entity to the session output
105
* buffer based on properties of the given HTTP message.
107
* @param outbuffer the output session buffer.
108
* @param message the HTTP message.
109
* @param entity the HTTP entity to be written out.
110
* @throws HttpException in case of HTTP protocol violation.
111
* @throws IOException in case of an I/O error.
113
public void serialize(
114
final SessionOutputBuffer outbuffer,
115
final HttpMessage message,
116
final HttpEntity entity) throws HttpException, IOException {
117
if (outbuffer == null) {
118
throw new IllegalArgumentException("Session output buffer may not be null");
120
if (message == null) {
121
throw new IllegalArgumentException("HTTP message may not be null");
123
if (entity == null) {
124
throw new IllegalArgumentException("HTTP entity may not be null");
126
OutputStream outstream = doSerialize(outbuffer, message);
127
entity.writeTo(outstream);