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 org.apache.http.entity;
29
30 import java.io.IOException;
31 import java.io.OutputStream;
32
33 import org.apache.http.Header;
34 import org.apache.http.HttpEntity;
35 import org.apache.http.annotation.NotThreadSafe;
36 import org.apache.http.message.BasicHeader;
37 import org.apache.http.protocol.HTTP;
38
39 /**
40 * Abstract base class for entities.
41 * Provides the commonly used attributes for streamed and self-contained
42 * implementations of {@link HttpEntity HttpEntity}.
43 *
44 * @since 4.0
45 */
46 @NotThreadSafe
47 public abstract class AbstractHttpEntity implements HttpEntity {
48
49 /**
50 * Buffer size for output stream processing.
51 *
52 * @since 4.3
53 */
54 protected static final int OUTPUT_BUFFER_SIZE = 4096;
55
56 protected Header contentType;
57 protected Header contentEncoding;
58 protected boolean chunked;
59
60 /**
61 * Protected default constructor.
62 * The contentType, contentEncoding and chunked attributes of the created object are set to
63 * <code>null</code>, <code>null</code> and <code>false</code>, respectively.
64 */
65 protected AbstractHttpEntity() {
66 super();
67 }
68
69
70 /**
71 * Obtains the Content-Type header.
72 * The default implementation returns the value of the
73 * {@link #contentType contentType} attribute.
74 *
75 * @return the Content-Type header, or <code>null</code>
76 */
77 public Header getContentType() {
78 return this.contentType;
79 }
80
81
82 /**
83 * Obtains the Content-Encoding header.
84 * The default implementation returns the value of the
85 * {@link #contentEncoding contentEncoding} attribute.
86 *
87 * @return the Content-Encoding header, or <code>null</code>
88 */
89 public Header getContentEncoding() {
90 return this.contentEncoding;
91 }
92
93 /**
94 * Obtains the 'chunked' flag.
95 * The default implementation returns the value of the
96 * {@link #chunked chunked} attribute.
97 *
98 * @return the 'chunked' flag
99 */
100 public boolean isChunked() {
101 return this.chunked;
102 }
103
104
105 /**
106 * Specifies the Content-Type header.
107 * The default implementation sets the value of the
108 * {@link #contentType contentType} attribute.
109 *
110 * @param contentType the new Content-Encoding header, or
111 * <code>null</code> to unset
112 */
113 public void setContentType(final Header contentType) {
114 this.contentType = contentType;
115 }
116
117 /**
118 * Specifies the Content-Type header, as a string.
119 * The default implementation calls
120 * {@link #setContentType(Header) setContentType(Header)}.
121 *
122 * @param ctString the new Content-Type header, or
123 * <code>null</code> to unset
124 */
125 public void setContentType(final String ctString) {
126 Header h = null;
127 if (ctString != null) {
128 h = new BasicHeader(HTTP.CONTENT_TYPE, ctString);
129 }
130 setContentType(h);
131 }
132
133
134 /**
135 * Specifies the Content-Encoding header.
136 * The default implementation sets the value of the
137 * {@link #contentEncoding contentEncoding} attribute.
138 *
139 * @param contentEncoding the new Content-Encoding header, or
140 * <code>null</code> to unset
141 */
142 public void setContentEncoding(final Header contentEncoding) {
143 this.contentEncoding = contentEncoding;
144 }
145
146 /**
147 * Specifies the Content-Encoding header, as a string.
148 * The default implementation calls
149 * {@link #setContentEncoding(Header) setContentEncoding(Header)}.
150 *
151 * @param ceString the new Content-Encoding header, or
152 * <code>null</code> to unset
153 */
154 public void setContentEncoding(final String ceString) {
155 Header h = null;
156 if (ceString != null) {
157 h = new BasicHeader(HTTP.CONTENT_ENCODING, ceString);
158 }
159 setContentEncoding(h);
160 }
161
162
163 /**
164 * Specifies the 'chunked' flag.
165 * <p>
166 * Note that the chunked setting is a hint only.
167 * If using HTTP/1.0, chunking is never performed.
168 * Otherwise, even if chunked is false, HttpClient must
169 * use chunk coding if the entity content length is
170 * unknown (-1).
171 * <p>
172 * The default implementation sets the value of the
173 * {@link #chunked chunked} attribute.
174 *
175 * @param b the new 'chunked' flag
176 */
177 public void setChunked(final boolean b) {
178 this.chunked = b;
179 }
180
181
182 /**
183 * The default implementation does not consume anything.
184 *
185 * @deprecated (4.1) Either use {@link #getContent()} and call {@link java.io.InputStream#close()} on that;
186 * otherwise call {@link #writeTo(OutputStream)} which is required to free the resources.
187 */
188 @Deprecated
189 public void consumeContent() throws IOException {
190 }
191
192 }