~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/main/java/org/apache/http/impl/entity/EntitySerializer.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 744526 $
 
4
 * $Date: 2009-02-14 18:04:18 +0100 (Sat, 14 Feb 2009) $
 
5
 *
 
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
 
14
 *
 
15
 *   http://www.apache.org/licenses/LICENSE-2.0
 
16
 *
 
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
 
22
 * under the License.
 
23
 * ====================================================================
 
24
 *
 
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/>.
 
29
 *
 
30
 */
 
31
 
 
32
package org.apache.http.impl.entity;
 
33
 
 
34
import java.io.IOException;
 
35
import java.io.OutputStream;
 
36
 
 
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;
 
45
 
 
46
/**
 
47
 * HTTP entity serializer.
 
48
 * <p>
 
49
 * This entity serializer currently supports "chunked" and "identitiy" 
 
50
 * transfer-coding and content length delimited content.
 
51
 * <p>
 
52
 * This class relies on a specific implementation of 
 
53
 * {@link ContentLengthStrategy} to determine the content length or transfer
 
54
 * encoding of the entity.
 
55
 * <p>
 
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. 
 
58
 * 
 
59
 *
 
60
 * @version $Revision: 744526 $
 
61
 * 
 
62
 * @since 4.0
 
63
 */
 
64
public class EntitySerializer {
 
65
 
 
66
    private final ContentLengthStrategy lenStrategy;
 
67
    
 
68
    public EntitySerializer(final ContentLengthStrategy lenStrategy) {
 
69
        super();
 
70
        if (lenStrategy == null) {
 
71
            throw new IllegalArgumentException("Content length strategy may not be null");
 
72
        }
 
73
        this.lenStrategy = lenStrategy;
 
74
    }
 
75
 
 
76
    /**
 
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.      
 
80
     * <p>
 
81
     * This method is called by the public
 
82
     * {@link #serialize(SessionOutputBuffer, HttpMessage, HttpEntity)}.
 
83
     * 
 
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.
 
89
     */
 
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);
 
98
        } else {
 
99
            return new ContentLengthOutputStream(outbuffer, len);
 
100
        }
 
101
    }
 
102
 
 
103
    /**
 
104
     * Writes out the content of the given HTTP entity to the session output
 
105
     * buffer based on properties of the given HTTP message.
 
106
     * 
 
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.
 
112
     */
 
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");
 
119
        }
 
120
        if (message == null) {
 
121
            throw new IllegalArgumentException("HTTP message may not be null");
 
122
        }
 
123
        if (entity == null) {
 
124
            throw new IllegalArgumentException("HTTP entity may not be null");
 
125
        }
 
126
        OutputStream outstream = doSerialize(outbuffer, message);
 
127
        entity.writeTo(outstream);
 
128
        outstream.close();
 
129
    }
 
130
    
 
131
}