EntitySerializer.java revision 417f3b92ba4549b2f22340e3107d869d2b9c5bb8
1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/entity/EntitySerializer.java $
3 * $Revision: 560343 $
4 * $Date: 2007-07-27 11:18:19 -0700 (Fri, 27 Jul 2007) $
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
32package org.apache.http.impl.entity;
33
34import java.io.IOException;
35import java.io.OutputStream;
36
37import org.apache.http.HttpEntity;
38import org.apache.http.HttpException;
39import org.apache.http.HttpMessage;
40import org.apache.http.entity.ContentLengthStrategy;
41import org.apache.http.impl.io.ChunkedOutputStream;
42import org.apache.http.impl.io.ContentLengthOutputStream;
43import org.apache.http.impl.io.IdentityOutputStream;
44import org.apache.http.io.SessionOutputBuffer;
45
46/**
47 * Default implementation of an entity serializer.
48 * <p>
49 * This entity serializer currently supports only "chunked" and "identitiy" transfer-coding</a>
50 * </p>
51 *
52 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
53 *
54 * @version $Revision: 560343 $
55 *
56 * @since 4.0
57 */
58public class EntitySerializer {
59
60    private final ContentLengthStrategy lenStrategy;
61
62    public EntitySerializer(final ContentLengthStrategy lenStrategy) {
63        super();
64        if (lenStrategy == null) {
65            throw new IllegalArgumentException("Content length strategy may not be null");
66        }
67        this.lenStrategy = lenStrategy;
68    }
69
70    protected OutputStream doSerialize(
71            final SessionOutputBuffer outbuffer,
72            final HttpMessage message) throws HttpException, IOException {
73        long len = this.lenStrategy.determineLength(message);
74        if (len == ContentLengthStrategy.CHUNKED) {
75            return new ChunkedOutputStream(outbuffer);
76        } else if (len == ContentLengthStrategy.IDENTITY) {
77            return new IdentityOutputStream(outbuffer);
78        } else {
79            return new ContentLengthOutputStream(outbuffer, len);
80        }
81    }
82
83    public void serialize(
84            final SessionOutputBuffer outbuffer,
85            final HttpMessage message,
86            final HttpEntity entity) throws HttpException, IOException {
87        if (outbuffer == null) {
88            throw new IllegalArgumentException("Session output buffer may not be null");
89        }
90        if (message == null) {
91            throw new IllegalArgumentException("HTTP message may not be null");
92        }
93        if (entity == null) {
94            throw new IllegalArgumentException("HTTP entity may not be null");
95        }
96        OutputStream outstream = doSerialize(outbuffer, message);
97        entity.writeTo(outstream);
98        outstream.close();
99    }
100
101}
102