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.annotation.NotThreadSafe;
39  import org.apache.http.entity.AbstractHttpEntity;
40  import org.apache.http.entity.BasicHttpEntity;
41  import org.apache.http.entity.ByteArrayEntity;
42  import org.apache.http.entity.ContentType;
43  import org.apache.http.entity.FileEntity;
44  import org.apache.http.entity.InputStreamEntity;
45  import org.apache.http.entity.SerializableEntity;
46  import org.apache.http.entity.StringEntity;
47  
48  /**
49   * @since 4.3
50   */
51  @NotThreadSafe
52  public class EntityBuilder {
53  
54      private String text;
55      private byte[] binary;
56      private InputStream stream;
57      private List<NameValuePair> parameters;
58      private Serializable serializable;
59      private File file;
60      private ContentType contentType;
61      private String contentEncoding;
62      private boolean chunked;
63      private boolean gzipCompress;
64  
65      EntityBuilder() {
66          super();
67      }
68  
69      public static EntityBuilder create() {
70          return new EntityBuilder();
71      }
72  
73      private void clearContent() {
74          this.text = null;
75          this.binary = null;
76          this.stream = null;
77          this.parameters = null;
78          this.serializable = null;
79          this.file = null;
80      }
81  
82      public String getText() {
83          return text;
84      }
85  
86      public EntityBuilder setText(final String text) {
87          clearContent();
88          this.text = text;
89          return this;
90      }
91  
92      public byte[] getBinary() {
93          return binary;
94      }
95  
96      public EntityBuilder setBinary(final byte[] binary) {
97          clearContent();
98          this.binary = binary;
99          return this;
100     }
101 
102     public InputStream getStream() {
103         return stream;
104     }
105 
106     public EntityBuilder setStream(final InputStream stream) {
107         clearContent();
108         this.stream = stream;
109         return this;
110     }
111 
112     public List<NameValuePair> getParameters() {
113         return parameters;
114     }
115 
116     public EntityBuilder setParameters(final List<NameValuePair> parameters) {
117         clearContent();
118         this.parameters = parameters;
119         return this;
120     }
121 
122     public EntityBuilder setParameters(final NameValuePair... parameters) {
123         return setParameters(Arrays.asList(parameters));
124     }
125 
126     public Serializable getSerializable() {
127         return serializable;
128     }
129 
130     public EntityBuilder setSerializable(final Serializable serializable) {
131         clearContent();
132         this.serializable = serializable;
133         return this;
134     }
135 
136     public File getFile() {
137         return file;
138     }
139 
140     public EntityBuilder setFile(final File file) {
141         clearContent();
142         this.file = file;
143         return this;
144     }
145 
146     public ContentType getContentType() {
147         return contentType;
148     }
149 
150     public EntityBuilder setContentType(final ContentType contentType) {
151         this.contentType = contentType;
152         return this;
153     }
154 
155     public String getContentEncoding() {
156         return contentEncoding;
157     }
158 
159     public EntityBuilder setContentEncoding(final String contentEncoding) {
160         this.contentEncoding = contentEncoding;
161         return this;
162     }
163 
164     public boolean isChunked() {
165         return chunked;
166     }
167 
168     public EntityBuilder chunked() {
169         this.chunked = true;
170         return this;
171     }
172 
173     public boolean isGzipCompress() {
174         return gzipCompress;
175     }
176 
177     public EntityBuilder gzipCompress() {
178         this.gzipCompress = true;
179         return this;
180     }
181 
182     private ContentType getContentOrDefault(final ContentType def) {
183         return this.contentType != null ? this.contentType : def;
184     }
185 
186     public HttpEntity build() {
187         AbstractHttpEntity e;
188         if (this.text != null) {
189             e = new StringEntity(this.text, getContentOrDefault(ContentType.DEFAULT_TEXT));
190         } else if (this.binary != null) {
191             e = new ByteArrayEntity(this.binary, getContentOrDefault(ContentType.DEFAULT_BINARY));
192         } else if (this.stream != null) {
193             e = new InputStreamEntity(this.stream, 1, getContentOrDefault(ContentType.DEFAULT_BINARY));
194         } else if (this.parameters != null) {
195             e = new UrlEncodedFormEntity(this.parameters,
196                     this.contentType != null ? this.contentType.getCharset() : null);
197         } else if (this.serializable != null) {
198             e = new SerializableEntity(this.serializable);
199             e.setContentType(ContentType.DEFAULT_BINARY.toString());
200         } else if (this.file != null) {
201             e = new FileEntity(this.file, getContentOrDefault(ContentType.DEFAULT_BINARY));
202         } else {
203             e = new BasicHttpEntity();
204         }
205         if (e.getContentType() != null && this.contentType != null) {
206             e.setContentType(this.contentType.toString());
207         }
208         e.setContentEncoding(this.contentEncoding);
209         e.setChunked(this.chunked);
210         if (this.gzipCompress) {
211             return new GzipCompressingEntity(e);
212         }
213         return e;
214     }
215 
216 }