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.client.entity;
29  
30  import java.io.File;
31  import java.io.InputStream;
32  import java.io.Serializable;
33  import java.util.Arrays;
34  import java.util.List;
35  
36  import org.apache.http.HttpEntity;
37  import org.apache.http.NameValuePair;
38  import org.apache.http.entity.AbstractHttpEntity;
39  import org.apache.http.entity.BasicHttpEntity;
40  import org.apache.http.entity.ByteArrayEntity;
41  import org.apache.http.entity.ContentType;
42  import org.apache.http.entity.FileEntity;
43  import org.apache.http.entity.InputStreamEntity;
44  import org.apache.http.entity.SerializableEntity;
45  import org.apache.http.entity.StringEntity;
46  
47  /**
48   * Builder for {@link HttpEntity} instances.
49   * <p>
50   * Several setter methods of this builder are mutually exclusive. In case of multiple invocations
51   * of the following methods only the last one will have effect:
52   * </p>
53   * <ul>
54   *   <li>{@link #setText(String)}</li>
55   *   <li>{@link #setBinary(byte[])}</li>
56   *   <li>{@link #setStream(java.io.InputStream)}</li>
57   *   <li>{@link #setSerializable(java.io.Serializable)}</li>
58   *   <li>{@link #setParameters(java.util.List)}</li>
59   *   <li>{@link #setParameters(org.apache.http.NameValuePair...)}</li>
60   *   <li>{@link #setFile(java.io.File)}</li>
61   * </ul>
62   *
63   * @since 4.3
64   */
65  public class EntityBuilder {
66  
67      private String text;
68      private byte[] binary;
69      private InputStream stream;
70      private List<NameValuePair> parameters;
71      private Serializable serializable;
72      private File file;
73      private ContentType contentType;
74      private String contentEncoding;
75      private boolean chunked;
76      private boolean gzipCompress;
77  
78      EntityBuilder() {
79          super();
80      }
81  
82      public static EntityBuilder create() {
83          return new EntityBuilder();
84      }
85  
86      private void clearContent() {
87          this.text = null;
88          this.binary = null;
89          this.stream = null;
90          this.parameters = null;
91          this.serializable = null;
92          this.file = null;
93      }
94  
95      /**
96       * Returns entity content as a string if set using {@link #setText(String)} method.
97       */
98      public String getText() {
99          return text;
100     }
101 
102     /**
103      * Sets entity content as a string. This method is mutually exclusive with
104      * {@link #setBinary(byte[])},
105      * {@link #setStream(java.io.InputStream)} ,
106      * {@link #setSerializable(java.io.Serializable)} ,
107      * {@link #setParameters(java.util.List)},
108      * {@link #setParameters(org.apache.http.NameValuePair...)}
109      * {@link #setFile(java.io.File)} methods.
110      */
111     public EntityBuilder setText(final String text) {
112         clearContent();
113         this.text = text;
114         return this;
115     }
116 
117     /**
118      * Returns entity content as a byte array if set using
119      * {@link #setBinary(byte[])} method.
120      */
121     public byte[] getBinary() {
122         return binary;
123     }
124 
125     /**
126      * Sets entity content as a byte array. This method is mutually exclusive with
127      * {@link #setText(String)},
128      * {@link #setStream(java.io.InputStream)} ,
129      * {@link #setSerializable(java.io.Serializable)} ,
130      * {@link #setParameters(java.util.List)},
131      * {@link #setParameters(org.apache.http.NameValuePair...)}
132      * {@link #setFile(java.io.File)} methods.
133      */
134     public EntityBuilder setBinary(final byte[] binary) {
135         clearContent();
136         this.binary = binary;
137         return this;
138     }
139 
140     /**
141      * Returns entity content as a {@link InputStream} if set using
142      * {@link #setStream(java.io.InputStream)} method.
143      */
144     public InputStream getStream() {
145         return stream;
146     }
147 
148     /**
149      * Sets entity content as a {@link InputStream}. This method is mutually exclusive with
150      * {@link #setText(String)},
151      * {@link #setBinary(byte[])},
152      * {@link #setSerializable(java.io.Serializable)} ,
153      * {@link #setParameters(java.util.List)},
154      * {@link #setParameters(org.apache.http.NameValuePair...)}
155      * {@link #setFile(java.io.File)} methods.
156      */
157     public EntityBuilder setStream(final InputStream stream) {
158         clearContent();
159         this.stream = stream;
160         return this;
161     }
162 
163     /**
164      * Returns entity content as a parameter list if set using
165      * {@link #setParameters(java.util.List)} or
166      * {@link #setParameters(org.apache.http.NameValuePair...)} methods.
167      */
168     public List<NameValuePair> getParameters() {
169         return parameters;
170     }
171 
172     /**
173      * Sets entity content as a parameter list. This method is mutually exclusive with
174      * {@link #setText(String)},
175      * {@link #setBinary(byte[])},
176      * {@link #setStream(java.io.InputStream)} ,
177      * {@link #setSerializable(java.io.Serializable)} ,
178      * {@link #setFile(java.io.File)} methods.
179      */
180     public EntityBuilder setParameters(final List<NameValuePair> parameters) {
181         clearContent();
182         this.parameters = parameters;
183         return this;
184     }
185 
186     /**
187      * Sets entity content as a parameter list. This method is mutually exclusive with
188      * {@link #setText(String)},
189      * {@link #setBinary(byte[])},
190      * {@link #setStream(java.io.InputStream)} ,
191      * {@link #setSerializable(java.io.Serializable)} ,
192      * {@link #setFile(java.io.File)} methods.
193      */
194     public EntityBuilder setParameters(final NameValuePair... parameters) {
195         return setParameters(Arrays.asList(parameters));
196     }
197 
198     /**
199      * Returns entity content as a {@link Serializable} if set using
200      * {@link #setSerializable(java.io.Serializable)} method.
201      */
202     public Serializable getSerializable() {
203         return serializable;
204     }
205 
206     /**
207      * Sets entity content as a {@link Serializable}. This method is mutually exclusive with
208      * {@link #setText(String)},
209      * {@link #setBinary(byte[])},
210      * {@link #setStream(java.io.InputStream)} ,
211      * {@link #setParameters(java.util.List)},
212      * {@link #setParameters(org.apache.http.NameValuePair...)}
213      * {@link #setFile(java.io.File)} methods.
214      */
215     public EntityBuilder setSerializable(final Serializable serializable) {
216         clearContent();
217         this.serializable = serializable;
218         return this;
219     }
220 
221     /**
222      * Returns entity content as a {@link File} if set using
223      * {@link #setFile(java.io.File)} method.
224      */
225     public File getFile() {
226         return file;
227     }
228 
229     /**
230      * Sets entity content as a {@link File}. This method is mutually exclusive with
231      * {@link #setText(String)},
232      * {@link #setBinary(byte[])},
233      * {@link #setStream(java.io.InputStream)} ,
234      * {@link #setParameters(java.util.List)},
235      * {@link #setParameters(org.apache.http.NameValuePair...)}
236      * {@link #setSerializable(java.io.Serializable)} methods.
237      */
238     public EntityBuilder setFile(final File file) {
239         clearContent();
240         this.file = file;
241         return this;
242     }
243 
244     /**
245      * Returns {@link ContentType} of the entity, if set.
246      */
247     public ContentType getContentType() {
248         return contentType;
249     }
250 
251     /**
252      * Sets {@link ContentType} of the entity.
253      */
254     public EntityBuilder setContentType(final ContentType contentType) {
255         this.contentType = contentType;
256         return this;
257     }
258 
259     /**
260      * Returns content encoding of the entity, if set.
261      */
262     public String getContentEncoding() {
263         return contentEncoding;
264     }
265 
266     /**
267      * Sets content encoding of the entity.
268      */
269     public EntityBuilder setContentEncoding(final String contentEncoding) {
270         this.contentEncoding = contentEncoding;
271         return this;
272     }
273 
274     /**
275      * Returns {@code true} if entity is to be chunk coded, {@code false} otherwise.
276      */
277     public boolean isChunked() {
278         return chunked;
279     }
280 
281     /**
282      * Makes entity chunk coded.
283      */
284     public EntityBuilder chunked() {
285         this.chunked = true;
286         return this;
287     }
288 
289     /**
290      * Returns {@code true} if entity is to be GZIP compressed, {@code false} otherwise.
291      */
292     public boolean isGzipCompress() {
293         return gzipCompress;
294     }
295 
296     /**
297      * Makes entity GZIP compressed.
298      */
299     public EntityBuilder gzipCompress() {
300         this.gzipCompress = true;
301         return this;
302     }
303 
304     private ContentType getContentOrDefault(final ContentType def) {
305         return this.contentType != null ? this.contentType : def;
306     }
307 
308     /**
309      * Creates new instance of {@link HttpEntity} based on the current state.
310      */
311     public HttpEntity build() {
312         final AbstractHttpEntity e;
313         if (this.text != null) {
314             e = new StringEntity(this.text, getContentOrDefault(ContentType.DEFAULT_TEXT));
315         } else if (this.binary != null) {
316             e = new ByteArrayEntity(this.binary, getContentOrDefault(ContentType.DEFAULT_BINARY));
317         } else if (this.stream != null) {
318             e = new InputStreamEntity(this.stream, -1, getContentOrDefault(ContentType.DEFAULT_BINARY));
319         } else if (this.parameters != null) {
320             e = new UrlEncodedFormEntity(this.parameters,
321                     this.contentType != null ? this.contentType.getCharset() : null);
322         } else if (this.serializable != null) {
323             e = new SerializableEntity(this.serializable);
324             e.setContentType(ContentType.DEFAULT_BINARY.toString());
325         } else if (this.file != null) {
326             e = new FileEntity(this.file, getContentOrDefault(ContentType.DEFAULT_BINARY));
327         } else {
328             e = new BasicHttpEntity();
329         }
330         if (e.getContentType() != null && this.contentType != null) {
331             e.setContentType(this.contentType.toString());
332         }
333         e.setContentEncoding(this.contentEncoding);
334         e.setChunked(this.chunked);
335         if (this.gzipCompress) {
336             return new GzipCompressingEntity(e);
337         }
338         return e;
339     }
340 
341 }