Source code: org/apache/http/entity/AbstractHttpEntity.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/entity/AbstractHttpEntity.java $
3 * $Revision: 374897 $
4 * $Date: 2006-02-04 18:54:27 +0100 (Sat, 04 Feb 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.entity;
31
32 import java.io.IOException;
33
34 import org.apache.http.Header;
35 import org.apache.http.HttpEntity;
36 import org.apache.http.protocol.HTTP;
37
38 /**
39 * Abstract base class for entities.
40 * Provides the commonly used attributes for streamed and self-contained
41 * implementations of {@link HttpEntity HttpEntity}.
42 *
43 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
44 *
45 * @version $Revision: 374897 $
46 *
47 * @since 4.0
48 */
49 public abstract class AbstractHttpEntity implements HttpEntity {
50
51 /**
52 * The Content-Type header.
53 * Returned by {@link #getContentType getContentType},
54 * unless that method is overridden.
55 */
56 protected Header contentType;
57
58 /**
59 * The Content-Encoding header.
60 * Returned by {@link #getContentEncoding getContentEncoding},
61 * unless that method is overridden.
62 */
63 protected Header contentEncoding;
64
65 /**
66 * The 'chunked' flag.
67 * Returned by {@link #isChunked isChunked},
68 * unless that method is overridden.
69 */
70 protected boolean chunked;
71
72
73 /**
74 * Protected default constructor.
75 * The attributes of the created object remain
76 * <code>null</code> and <code>false</code>, respectively.
77 */
78 protected AbstractHttpEntity() {
79 super();
80 }
81
82
83 /**
84 * Obtains the Content-Type header.
85 * The default implementation returns the value of the
86 * {@link #contentType contentType} attribute.
87 *
88 * @return the Content-Type header, or <code>null</code>
89 */
90 public Header getContentType() {
91 return this.contentType;
92 }
93
94
95 /**
96 * Obtains the Content-Encoding header.
97 * The default implementation returns the value of the
98 * {@link #contentEncoding contentEncoding} attribute.
99 *
100 * @return the Content-Encoding header, or <code>null</code>
101 */
102 public Header getContentEncoding() {
103 return this.contentEncoding;
104 }
105
106 /**
107 * Obtains the 'chunked' flag.
108 * The default implementation returns the value of the
109 * {@link #chunked chunked} attribute.
110 *
111 * @return the 'chunked' flag
112 */
113 public boolean isChunked() {
114 return this.chunked;
115 }
116
117
118 /**
119 * Specifies the Content-Type header.
120 * The default implementation sets the value of the
121 * {@link #contentType contentType} attribute.
122 *
123 * @param contentType the new Content-Encoding header, or
124 * <code>null</code> to unset
125 */
126 public void setContentType(final Header contentType) {
127 this.contentType = contentType;
128 }
129
130 /**
131 * Specifies the Content-Type header, as a string.
132 * The default implementation calls
133 * {@link #setContentType(Header) setContentType(Header)}.
134 *
135 * @param ctString the new Content-Type header, or
136 * <code>null</code> to unset
137 */
138 public void setContentType(final String ctString) {
139 Header h = null;
140 if (ctString != null) {
141 h = new Header(HTTP.CONTENT_TYPE, ctString);
142 }
143 setContentType(h);
144 }
145
146
147 /**
148 * Specifies the Content-Encoding header.
149 * The default implementation sets the value of the
150 * {@link #contentEncoding contentEncoding} attribute.
151 *
152 * @param contentEncoding the new Content-Encoding header, or
153 * <code>null</code> to unset
154 */
155 public void setContentEncoding(final Header contentEncoding) {
156 this.contentEncoding = contentEncoding;
157 }
158
159 /**
160 * Specifies the Content-Encoding header, as a string.
161 * The default implementation calls
162 * {@link #setContentEncoding(Header) setContentEncoding(Header)}.
163 *
164 * @param ceString the new Content-Encoding header, or
165 * <code>null</code> to unset
166 */
167 public void setContentEncoding(final String ceString) {
168 Header h = null;
169 if (ceString != null) {
170 h = new Header(HTTP.CONTENT_ENCODING, ceString);
171 }
172 setContentEncoding(h);
173 }
174
175
176 /**
177 * Specifies the 'chunked' flag.
178 * The default implementation sets the value of the
179 * {@link #chunked chunked} attribute.
180 *
181 * @param b the new 'chunked' flag
182 */
183 public void setChunked(boolean b) {
184 this.chunked = b;
185 }
186
187
188 /**
189 * Does not consume anything.
190 * The default implementation does nothing if
191 * {@link HttpEntity#isStreaming isStreaming}
192 * returns <code>false</code>, and throws an exception
193 * if it returns <code>true</code>.
194 * This removes the burden of implementing
195 * an empty method for non-streaming entities.
196 *
197 * @throws IOException in case of an I/O problem
198 * @throws UnsupportedOperationException
199 * if a streaming subclass does not override this method
200 */
201 public void consumeContent()
202 throws IOException, UnsupportedOperationException{
203 if (isStreaming()) {
204 throw new UnsupportedOperationException
205 ("streaming entity does not implement consumeContent()");
206 }
207 } // consumeContent
208
209
210 } // class AbstractHttpEntity