View Javadoc

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