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