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 null} and {@code false}, 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}
75       */
76      @Override
77      public Header getContentType() {
78          return this.contentType;
79      }
80  
81  
82      /**
83       * Obtains the Content-Encoding header.
84       * The default implementation returns the value of the
85       * {@link #contentEncoding contentEncoding} attribute.
86       *
87       * @return  the Content-Encoding header, or {@code null}
88       */
89      @Override
90      public Header getContentEncoding() {
91          return this.contentEncoding;
92      }
93  
94      /**
95       * Obtains the 'chunked' flag.
96       * The default implementation returns the value of the
97       * {@link #chunked chunked} attribute.
98       *
99       * @return  the 'chunked' flag
100      */
101     @Override
102     public boolean isChunked() {
103         return this.chunked;
104     }
105 
106 
107     /**
108      * Specifies the Content-Type header.
109      * The default implementation sets the value of the
110      * {@link #contentType contentType} attribute.
111      *
112      * @param contentType       the new Content-Encoding header, or
113      *                          {@code null} to unset
114      */
115     public void setContentType(final Header contentType) {
116         this.contentType = contentType;
117     }
118 
119     /**
120      * Specifies the Content-Type header, as a string.
121      * The default implementation calls
122      * {@link #setContentType(Header) setContentType(Header)}.
123      *
124      * @param ctString     the new Content-Type header, or
125      *                     {@code null} to unset
126      */
127     public void setContentType(final String ctString) {
128         Header h = null;
129         if (ctString != null) {
130             h = new BasicHeader(HTTP.CONTENT_TYPE, ctString);
131         }
132         setContentType(h);
133     }
134 
135 
136     /**
137      * Specifies the Content-Encoding header.
138      * The default implementation sets the value of the
139      * {@link #contentEncoding contentEncoding} attribute.
140      *
141      * @param contentEncoding   the new Content-Encoding header, or
142      *                          {@code null} to unset
143      */
144     public void setContentEncoding(final Header contentEncoding) {
145         this.contentEncoding = contentEncoding;
146     }
147 
148     /**
149      * Specifies the Content-Encoding header, as a string.
150      * The default implementation calls
151      * {@link #setContentEncoding(Header) setContentEncoding(Header)}.
152      *
153      * @param ceString     the new Content-Encoding header, or
154      *                     {@code null} to unset
155      */
156     public void setContentEncoding(final String ceString) {
157         Header h = null;
158         if (ceString != null) {
159             h = new BasicHeader(HTTP.CONTENT_ENCODING, ceString);
160         }
161         setContentEncoding(h);
162     }
163 
164 
165     /**
166      * Specifies the 'chunked' flag.
167      * <p>
168      * Note that the chunked setting is a hint only.
169      * If using HTTP/1.0, chunking is never performed.
170      * Otherwise, even if chunked is false, HttpClient must
171      * use chunk coding if the entity content length is
172      * unknown (-1).
173      * <p>
174      * The default implementation sets the value of the
175      * {@link #chunked chunked} attribute.
176      *
177      * @param b         the new 'chunked' flag
178      */
179     public void setChunked(final boolean b) {
180         this.chunked = b;
181     }
182 
183 
184     /**
185      * The default implementation does not consume anything.
186      *
187      * @deprecated (4.1) Either use {@link #getContent()} and call {@link java.io.InputStream#close()} on that;
188      * otherwise call {@link #writeTo(java.io.OutputStream)} which is required to free the resources.
189      */
190     @Override
191     @Deprecated
192     public void consumeContent() throws IOException {
193     }
194 
195     @Override
196     public String toString() {
197         final StringBuilder sb = new StringBuilder();
198         sb.append('[');
199         if (contentType != null) {
200             sb.append("Content-Type: ");
201             sb.append(contentType.getValue());
202             sb.append(',');
203         }
204         if (contentEncoding != null) {
205             sb.append("Content-Encoding: ");
206             sb.append(contentEncoding.getValue());
207             sb.append(',');
208         }
209         final long len = getContentLength();
210         if (len >= 0) {
211             sb.append("Content-Length: ");
212             sb.append(len);
213             sb.append(',');
214         }
215         sb.append("Chunked: ");
216         sb.append(chunked);
217         sb.append(']');
218         return sb.toString();
219     }
220 
221 }