Home | History | Annotate | Download | only in entity
      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  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     53  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     54  *     for further details.
     55  */
     56 @Deprecated
     57 public abstract class AbstractHttpEntity implements HttpEntity {
     58 
     59     /**
     60      * The Content-Type header.
     61      * Returned by {@link #getContentType getContentType},
     62      * unless that method is overridden.
     63      */
     64     protected Header contentType;
     65 
     66     /**
     67      * The Content-Encoding header.
     68      * Returned by {@link #getContentEncoding getContentEncoding},
     69      * unless that method is overridden.
     70      */
     71     protected Header contentEncoding;
     72 
     73     /**
     74      * The 'chunked' flag.
     75      * Returned by {@link #isChunked isChunked},
     76      * unless that method is overridden.
     77      */
     78     protected boolean chunked;
     79 
     80 
     81     /**
     82      * Protected default constructor.
     83      * The attributes of the created object remain
     84      * <code>null</code> and <code>false</code>, respectively.
     85      */
     86     protected AbstractHttpEntity() {
     87         super();
     88     }
     89 
     90 
     91     /**
     92      * Obtains the Content-Type header.
     93      * The default implementation returns the value of the
     94      * {@link #contentType contentType} attribute.
     95      *
     96      * @return  the Content-Type header, or <code>null</code>
     97      */
     98     public Header getContentType() {
     99         return this.contentType;
    100     }
    101 
    102 
    103     /**
    104      * Obtains the Content-Encoding header.
    105      * The default implementation returns the value of the
    106      * {@link #contentEncoding contentEncoding} attribute.
    107      *
    108      * @return  the Content-Encoding header, or <code>null</code>
    109      */
    110     public Header getContentEncoding() {
    111         return this.contentEncoding;
    112     }
    113 
    114     /**
    115      * Obtains the 'chunked' flag.
    116      * The default implementation returns the value of the
    117      * {@link #chunked chunked} attribute.
    118      *
    119      * @return  the 'chunked' flag
    120      */
    121     public boolean isChunked() {
    122         return this.chunked;
    123     }
    124 
    125 
    126     /**
    127      * Specifies the Content-Type header.
    128      * The default implementation sets the value of the
    129      * {@link #contentType contentType} attribute.
    130      *
    131      * @param contentType       the new Content-Encoding header, or
    132      *                          <code>null</code> to unset
    133      */
    134     public void setContentType(final Header contentType) {
    135         this.contentType = contentType;
    136     }
    137 
    138     /**
    139      * Specifies the Content-Type header, as a string.
    140      * The default implementation calls
    141      * {@link #setContentType(Header) setContentType(Header)}.
    142      *
    143      * @param ctString     the new Content-Type header, or
    144      *                     <code>null</code> to unset
    145      */
    146     public void setContentType(final String ctString) {
    147         Header h = null;
    148         if (ctString != null) {
    149             h = new BasicHeader(HTTP.CONTENT_TYPE, ctString);
    150         }
    151         setContentType(h);
    152     }
    153 
    154 
    155     /**
    156      * Specifies the Content-Encoding header.
    157      * The default implementation sets the value of the
    158      * {@link #contentEncoding contentEncoding} attribute.
    159      *
    160      * @param contentEncoding   the new Content-Encoding header, or
    161      *                          <code>null</code> to unset
    162      */
    163     public void setContentEncoding(final Header contentEncoding) {
    164         this.contentEncoding = contentEncoding;
    165     }
    166 
    167     /**
    168      * Specifies the Content-Encoding header, as a string.
    169      * The default implementation calls
    170      * {@link #setContentEncoding(Header) setContentEncoding(Header)}.
    171      *
    172      * @param ceString     the new Content-Encoding header, or
    173      *                     <code>null</code> to unset
    174      */
    175     public void setContentEncoding(final String ceString) {
    176         Header h = null;
    177         if (ceString != null) {
    178             h = new BasicHeader(HTTP.CONTENT_ENCODING, ceString);
    179         }
    180         setContentEncoding(h);
    181     }
    182 
    183 
    184     /**
    185      * Specifies the 'chunked' flag.
    186      * The default implementation sets the value of the
    187      * {@link #chunked chunked} attribute.
    188      *
    189      * @param b         the new 'chunked' flag
    190      */
    191     public void setChunked(boolean b) {
    192         this.chunked = b;
    193     }
    194 
    195 
    196     /**
    197      * Does not consume anything.
    198      * The default implementation does nothing if
    199      * {@link HttpEntity#isStreaming isStreaming}
    200      * returns <code>false</code>, and throws an exception
    201      * if it returns <code>true</code>.
    202      * This removes the burden of implementing
    203      * an empty method for non-streaming entities.
    204      *
    205      * @throws IOException      in case of an I/O problem
    206      * @throws UnsupportedOperationException
    207      *          if a streaming subclass does not override this method
    208      */
    209     public void consumeContent()
    210         throws IOException, UnsupportedOperationException{
    211         if (isStreaming()) {
    212             throw new UnsupportedOperationException
    213                 ("streaming entity does not implement consumeContent()");
    214         }
    215     } // consumeContent
    216 
    217 
    218 } // class AbstractHttpEntity
    219