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