001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/entity/AbstractHttpEntity.java $
003: * $Revision: 496070 $
004: * $Date: 2007-01-14 13:18:34 +0100 (Sun, 14 Jan 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.entity;
033:
034: import java.io.IOException;
035:
036: import org.apache.http.Header;
037: import org.apache.http.HttpEntity;
038: import org.apache.http.message.BasicHeader;
039: import org.apache.http.protocol.HTTP;
040:
041: /**
042: * Abstract base class for entities.
043: * Provides the commonly used attributes for streamed and self-contained
044: * implementations of {@link HttpEntity HttpEntity}.
045: *
046: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
047: *
048: * @version $Revision: 496070 $
049: *
050: * @since 4.0
051: */
052: public abstract class AbstractHttpEntity implements HttpEntity {
053:
054: /**
055: * The Content-Type header.
056: * Returned by {@link #getContentType getContentType},
057: * unless that method is overridden.
058: */
059: protected Header contentType;
060:
061: /**
062: * The Content-Encoding header.
063: * Returned by {@link #getContentEncoding getContentEncoding},
064: * unless that method is overridden.
065: */
066: protected Header contentEncoding;
067:
068: /**
069: * The 'chunked' flag.
070: * Returned by {@link #isChunked isChunked},
071: * unless that method is overridden.
072: */
073: protected boolean chunked;
074:
075: /**
076: * Protected default constructor.
077: * The attributes of the created object remain
078: * <code>null</code> and <code>false</code>, respectively.
079: */
080: protected AbstractHttpEntity() {
081: super ();
082: }
083:
084: /**
085: * Obtains the Content-Type header.
086: * The default implementation returns the value of the
087: * {@link #contentType contentType} attribute.
088: *
089: * @return the Content-Type header, or <code>null</code>
090: */
091: public Header getContentType() {
092: return this .contentType;
093: }
094:
095: /**
096: * Obtains the Content-Encoding header.
097: * The default implementation returns the value of the
098: * {@link #contentEncoding contentEncoding} attribute.
099: *
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: * Specifies the Content-Type header.
119: * The default implementation sets the value of the
120: * {@link #contentType contentType} attribute.
121: *
122: * @param contentType the new Content-Encoding header, or
123: * <code>null</code> to unset
124: */
125: public void setContentType(final Header contentType) {
126: this .contentType = contentType;
127: }
128:
129: /**
130: * Specifies the Content-Type header, as a string.
131: * The default implementation calls
132: * {@link #setContentType(Header) setContentType(Header)}.
133: *
134: * @param ctString the new Content-Type header, or
135: * <code>null</code> to unset
136: */
137: public void setContentType(final String ctString) {
138: Header h = null;
139: if (ctString != null) {
140: h = new BasicHeader(HTTP.CONTENT_TYPE, ctString);
141: }
142: setContentType(h);
143: }
144:
145: /**
146: * Specifies the Content-Encoding header.
147: * The default implementation sets the value of the
148: * {@link #contentEncoding contentEncoding} attribute.
149: *
150: * @param contentEncoding the new Content-Encoding header, or
151: * <code>null</code> to unset
152: */
153: public void setContentEncoding(final Header contentEncoding) {
154: this .contentEncoding = contentEncoding;
155: }
156:
157: /**
158: * Specifies the Content-Encoding header, as a string.
159: * The default implementation calls
160: * {@link #setContentEncoding(Header) setContentEncoding(Header)}.
161: *
162: * @param ceString the new Content-Encoding header, or
163: * <code>null</code> to unset
164: */
165: public void setContentEncoding(final String ceString) {
166: Header h = null;
167: if (ceString != null) {
168: h = new BasicHeader(HTTP.CONTENT_ENCODING, ceString);
169: }
170: setContentEncoding(h);
171: }
172:
173: /**
174: * Specifies the 'chunked' flag.
175: * The default implementation sets the value of the
176: * {@link #chunked chunked} attribute.
177: *
178: * @param b the new 'chunked' flag
179: */
180: public void setChunked(boolean b) {
181: this .chunked = b;
182: }
183:
184: /**
185: * Does not consume anything.
186: * The default implementation does nothing if
187: * {@link HttpEntity#isStreaming isStreaming}
188: * returns <code>false</code>, and throws an exception
189: * if it returns <code>true</code>.
190: * This removes the burden of implementing
191: * an empty method for non-streaming entities.
192: *
193: * @throws IOException in case of an I/O problem
194: * @throws UnsupportedOperationException
195: * if a streaming subclass does not override this method
196: */
197: public void consumeContent() throws IOException,
198: UnsupportedOperationException {
199: if (isStreaming()) {
200: throw new UnsupportedOperationException(
201: "streaming entity does not implement consumeContent()");
202: }
203: } // consumeContent
204:
205: } // class AbstractHttpEntity
|