Source code: org/apache/http/impl/entity/DefaultEntitySerializer.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/entity/DefaultEntitySerializer.java $
3 * $Revision: 385851 $
4 * $Date: 2006-03-14 19:46:52 +0100 (Tue, 14 Mar 2006) $
5 *
6 * ====================================================================
7 *
8 * Copyright 1999-2006 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 */
29
30 package org.apache.http.impl.entity;
31
32 import java.io.IOException;
33 import java.io.OutputStream;
34
35 import org.apache.http.Header;
36 import org.apache.http.HttpEntity;
37 import org.apache.http.HttpException;
38 import org.apache.http.HttpMessage;
39 import org.apache.http.HttpVersion;
40 import org.apache.http.ProtocolException;
41 import org.apache.http.entity.EntitySerializer;
42 import org.apache.http.io.ChunkedOutputStream;
43 import org.apache.http.io.ContentLengthOutputStream;
44 import org.apache.http.io.HttpDataTransmitter;
45 import org.apache.http.io.IdentityOutputStream;
46 import org.apache.http.protocol.HTTP;
47
48 /**
49 * Default implementation of an entity writer on the client side.
50 *
51 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
52 *
53 * @version $Revision: 385851 $
54 *
55 * @since 4.0
56 */
57 public class DefaultEntitySerializer implements EntitySerializer {
58
59 public DefaultEntitySerializer() {
60 super();
61 }
62
63 protected OutputStream doSerialize(
64 final HttpDataTransmitter datatransmitter,
65 final HttpMessage message) throws HttpException, IOException {
66 Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
67 Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
68 if (transferEncodingHeader != null) {
69 String s = transferEncodingHeader.getValue();
70 if (HTTP.CHUNK_CODING.equalsIgnoreCase(s)) {
71 if (message.getHttpVersion().lessEquals(HttpVersion.HTTP_1_0)) {
72 throw new ProtocolException(
73 "Chunked transfer encoding not allowed for " +
74 message.getHttpVersion());
75 }
76 return new ChunkedOutputStream(datatransmitter);
77 } else if (HTTP.IDENTITY_CODING.equalsIgnoreCase(s)) {
78 return new IdentityOutputStream(datatransmitter);
79 } else {
80 throw new ProtocolException(
81 "Unsupported transfer encoding: " + s);
82 }
83 } else if (contentLengthHeader != null) {
84 String s = contentLengthHeader.getValue();
85 try {
86 long len = Long.parseLong(s);
87 return new ContentLengthOutputStream(datatransmitter, len);
88 } catch (NumberFormatException e) {
89 throw new ProtocolException("Invalid content length: " + s);
90 }
91 } else {
92 return new IdentityOutputStream(datatransmitter);
93 }
94 }
95
96 public void serialize(
97 final HttpDataTransmitter datatransmitter,
98 final HttpMessage message,
99 final HttpEntity entity) throws HttpException, IOException {
100 if (datatransmitter == null) {
101 throw new IllegalArgumentException("HTTP data transmitter may not be null");
102 }
103 if (message == null) {
104 throw new IllegalArgumentException("HTTP message may not be null");
105 }
106 if (entity == null) {
107 throw new IllegalArgumentException("HTTP entity may not be null");
108 }
109 OutputStream outstream = doSerialize(datatransmitter, message);
110 entity.writeTo(outstream);
111 outstream.close();
112 }
113
114 }