1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/AbstractHttpEntity.java $ 3 * $Revision: 496070 $ 4 * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ 5 * 6 * ==================================================================== 7 * Licensed to the Apache Software Foundation (ASF) under one 8 * or more contributor license agreements. See the NOTICE file 9 * distributed with this work for additional information 10 * regarding copyright ownership. The ASF licenses this file 11 * to you under the Apache License, Version 2.0 (the 12 * "License"); you may not use this file except in compliance 13 * with the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, 18 * software distributed under the License is distributed on an 19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20 * KIND, either express or implied. See the License for the 21 * specific language governing permissions and limitations 22 * under the License. 23 * ==================================================================== 24 * 25 * This software consists of voluntary contributions made by many 26 * individuals on behalf of the Apache Software Foundation. For more 27 * information on the Apache Software Foundation, please see 28 * <http://www.apache.org/>. 29 * 30 */ 31 32 package org.apache.http.entity; 33 34 import java.io.IOException; 35 36 import org.apache.http.Header; 37 import org.apache.http.HttpEntity; 38 import org.apache.http.message.BasicHeader; 39 import org.apache.http.protocol.HTTP; 40 41 /** 42 * Abstract base class for entities. 43 * Provides the commonly used attributes for streamed and self-contained 44 * implementations of {@link HttpEntity HttpEntity}. 45 * 46 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 47 * 48 * @version $Revision: 496070 $ 49 * 50 * @since 4.0 51 */ 52 public abstract class AbstractHttpEntity implements HttpEntity { 53 54 /** 55 * The Content-Type header. 56 * Returned by {@link #getContentType getContentType}, 57 * unless that method is overridden. 58 */ 59 protected Header contentType; 60 61 /** 62 * The Content-Encoding header. 63 * Returned by {@link #getContentEncoding getContentEncoding}, 64 * unless that method is overridden. 65 */ 66 protected Header contentEncoding; 67 68 /** 69 * The 'chunked' flag. 70 * Returned by {@link #isChunked isChunked}, 71 * unless that method is overridden. 72 */ 73 protected boolean chunked; 74 75 76 /** 77 * Protected default constructor. 78 * The attributes of the created object remain 79 * <code>null</code> and <code>false</code>, respectively. 80 */ AbstractHttpEntity()81 protected AbstractHttpEntity() { 82 super(); 83 } 84 85 86 /** 87 * Obtains the Content-Type header. 88 * The default implementation returns the value of the 89 * {@link #contentType contentType} attribute. 90 * 91 * @return the Content-Type header, or <code>null</code> 92 */ getContentType()93 public Header getContentType() { 94 return this.contentType; 95 } 96 97 98 /** 99 * Obtains the Content-Encoding header. 100 * The default implementation returns the value of the 101 * {@link #contentEncoding contentEncoding} attribute. 102 * 103 * @return the Content-Encoding header, or <code>null</code> 104 */ getContentEncoding()105 public Header getContentEncoding() { 106 return this.contentEncoding; 107 } 108 109 /** 110 * Obtains the 'chunked' flag. 111 * The default implementation returns the value of the 112 * {@link #chunked chunked} attribute. 113 * 114 * @return the 'chunked' flag 115 */ isChunked()116 public boolean isChunked() { 117 return this.chunked; 118 } 119 120 121 /** 122 * Specifies the Content-Type header. 123 * The default implementation sets the value of the 124 * {@link #contentType contentType} attribute. 125 * 126 * @param contentType the new Content-Encoding header, or 127 * <code>null</code> to unset 128 */ setContentType(final Header contentType)129 public void setContentType(final Header contentType) { 130 this.contentType = contentType; 131 } 132 133 /** 134 * Specifies the Content-Type header, as a string. 135 * The default implementation calls 136 * {@link #setContentType(Header) setContentType(Header)}. 137 * 138 * @param ctString the new Content-Type header, or 139 * <code>null</code> to unset 140 */ setContentType(final String ctString)141 public void setContentType(final String ctString) { 142 Header h = null; 143 if (ctString != null) { 144 h = new BasicHeader(HTTP.CONTENT_TYPE, ctString); 145 } 146 setContentType(h); 147 } 148 149 150 /** 151 * Specifies the Content-Encoding header. 152 * The default implementation sets the value of the 153 * {@link #contentEncoding contentEncoding} attribute. 154 * 155 * @param contentEncoding the new Content-Encoding header, or 156 * <code>null</code> to unset 157 */ setContentEncoding(final Header contentEncoding)158 public void setContentEncoding(final Header contentEncoding) { 159 this.contentEncoding = contentEncoding; 160 } 161 162 /** 163 * Specifies the Content-Encoding header, as a string. 164 * The default implementation calls 165 * {@link #setContentEncoding(Header) setContentEncoding(Header)}. 166 * 167 * @param ceString the new Content-Encoding header, or 168 * <code>null</code> to unset 169 */ setContentEncoding(final String ceString)170 public void setContentEncoding(final String ceString) { 171 Header h = null; 172 if (ceString != null) { 173 h = new BasicHeader(HTTP.CONTENT_ENCODING, ceString); 174 } 175 setContentEncoding(h); 176 } 177 178 179 /** 180 * Specifies the 'chunked' flag. 181 * The default implementation sets the value of the 182 * {@link #chunked chunked} attribute. 183 * 184 * @param b the new 'chunked' flag 185 */ setChunked(boolean b)186 public void setChunked(boolean b) { 187 this.chunked = b; 188 } 189 190 191 /** 192 * Does not consume anything. 193 * The default implementation does nothing if 194 * {@link HttpEntity#isStreaming isStreaming} 195 * returns <code>false</code>, and throws an exception 196 * if it returns <code>true</code>. 197 * This removes the burden of implementing 198 * an empty method for non-streaming entities. 199 * 200 * @throws IOException in case of an I/O problem 201 * @throws UnsupportedOperationException 202 * if a streaming subclass does not override this method 203 */ consumeContent()204 public void consumeContent() 205 throws IOException, UnsupportedOperationException{ 206 if (isStreaming()) { 207 throw new UnsupportedOperationException 208 ("streaming entity does not implement consumeContent()"); 209 } 210 } // consumeContent 211 212 213 } // class AbstractHttpEntity 214