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.methods;
29  
30  import java.net.URI;
31  import java.net.URISyntaxException;
32  import java.util.ArrayList;
33  import java.util.LinkedList;
34  import java.util.List;
35  
36  import org.apache.http.Header;
37  import org.apache.http.HeaderIterator;
38  import org.apache.http.HttpEntity;
39  import org.apache.http.HttpEntityEnclosingRequest;
40  import org.apache.http.HttpRequest;
41  import org.apache.http.NameValuePair;
42  import org.apache.http.ProtocolVersion;
43  import org.apache.http.annotation.NotThreadSafe;
44  import org.apache.http.client.config.RequestConfig;
45  import org.apache.http.client.entity.UrlEncodedFormEntity;
46  import org.apache.http.client.utils.URIBuilder;
47  import org.apache.http.message.BasicHeader;
48  import org.apache.http.message.BasicNameValuePair;
49  import org.apache.http.message.HeaderGroup;
50  import org.apache.http.protocol.HTTP;
51  import org.apache.http.util.Args;
52  
53  /**
54   * @since 4.3
55   */
56  @NotThreadSafe
57  public class RequestBuilder {
58  
59      private String method;
60      private ProtocolVersion version;
61      private URI uri;
62      private HeaderGroup headergroup;
63      private HttpEntity entity;
64      private LinkedList<NameValuePair> parameters;
65      private RequestConfig config;
66  
67      RequestBuilder(final String method) {
68          super();
69          this.method = method;
70      }
71  
72      RequestBuilder() {
73          this(null);
74      }
75  
76      public static RequestBuilder create(final String method) {
77          Args.notBlank(method, "HTTP method");
78          return new RequestBuilder(method);
79      }
80  
81      public static RequestBuilder get() {
82          return new RequestBuilder(HttpGet.METHOD_NAME);
83      }
84  
85      public static RequestBuilder head() {
86          return new RequestBuilder(HttpHead.METHOD_NAME);
87      }
88  
89      public static RequestBuilder post() {
90          return new RequestBuilder(HttpPost.METHOD_NAME);
91      }
92  
93      public static RequestBuilder put() {
94          return new RequestBuilder(HttpPut.METHOD_NAME);
95      }
96  
97      public static RequestBuilder delete() {
98          return new RequestBuilder(HttpDelete.METHOD_NAME);
99      }
100 
101     public static RequestBuilder trace() {
102         return new RequestBuilder(HttpTrace.METHOD_NAME);
103     }
104 
105     public static RequestBuilder options() {
106         return new RequestBuilder(HttpOptions.METHOD_NAME);
107     }
108 
109     public static RequestBuilder copy(final HttpRequest request) {
110         Args.notNull(request, "HTTP request");
111         return new RequestBuilder().doCopy(request);
112     }
113 
114     private RequestBuilder doCopy(final HttpRequest request) {
115         if (request == null) {
116             return this;
117         }
118         method = request.getRequestLine().getMethod();
119         version = request.getRequestLine().getProtocolVersion();
120         if (request instanceof HttpUriRequest) {
121             uri = ((HttpUriRequest) request).getURI();
122         } else {
123             uri = URI.create(request.getRequestLine().getMethod());
124         }
125         if (headergroup == null) {
126             headergroup = new HeaderGroup();
127         }
128         headergroup.clear();
129         headergroup.setHeaders(request.getAllHeaders());
130         if (request instanceof HttpEntityEnclosingRequest) {
131             entity = ((HttpEntityEnclosingRequest) request).getEntity();
132         } else {
133             entity = null;
134         }
135         if (request instanceof Configurable) {
136             this.config = ((Configurable) request).getConfig();
137         } else {
138             this.config = null;
139         }
140         this.parameters = null;
141         return this;
142     }
143 
144     public String getMethod() {
145         return method;
146     }
147 
148     public ProtocolVersion getVersion() {
149         return version;
150     }
151 
152     public RequestBuilder setVersion(final ProtocolVersion version) {
153         this.version = version;
154         return this;
155     }
156 
157     public URI getUri() {
158         return uri;
159     }
160 
161     public RequestBuilder setUri(final URI uri) {
162         this.uri = uri;
163         return this;
164     }
165 
166     public RequestBuilder setUri(final String uri) {
167         this.uri = uri != null ? URI.create(uri) : null;
168         return this;
169     }
170 
171     public Header getFirstHeader(final String name) {
172         return headergroup != null ? headergroup.getFirstHeader(name) : null;
173     }
174 
175     public Header getLastHeader(final String name) {
176         return headergroup != null ? headergroup.getLastHeader(name) : null;
177     }
178 
179     public Header[] getHeaders(final String name) {
180         return headergroup != null ? headergroup.getHeaders(name) : null;
181     }
182 
183     public RequestBuilder addHeader(final Header header) {
184         if (headergroup == null) {
185             headergroup = new HeaderGroup();
186         }
187         headergroup.addHeader(header);
188         return this;
189     }
190 
191     public RequestBuilder addHeader(final String name, final String value) {
192         if (headergroup == null) {
193             headergroup = new HeaderGroup();
194         }
195         this.headergroup.addHeader(new BasicHeader(name, value));
196         return this;
197     }
198 
199     public RequestBuilder removeHeader(final Header header) {
200         if (headergroup == null) {
201             headergroup = new HeaderGroup();
202         }
203         headergroup.removeHeader(header);
204         return this;
205     }
206 
207     public RequestBuilder removeHeaders(final String name) {
208         if (name == null || headergroup == null) {
209             return this;
210         }
211         for (final HeaderIterator i = headergroup.iterator(); i.hasNext(); ) {
212             final Header header = i.nextHeader();
213             if (name.equalsIgnoreCase(header.getName())) {
214                 i.remove();
215             }
216         }
217         return this;
218     }
219 
220     public RequestBuilder setHeader(final Header header) {
221         if (headergroup != null) {
222             headergroup = new HeaderGroup();
223         }
224         this.headergroup.updateHeader(header);
225         return this;
226     }
227 
228     public RequestBuilder setHeader(final String name, final String value) {
229         if (headergroup != null) {
230             headergroup = new HeaderGroup();
231         }
232         this.headergroup.updateHeader(new BasicHeader(name, value));
233         return this;
234     }
235 
236     public HttpEntity getEntity() {
237         return entity;
238     }
239 
240     public RequestBuilder setEntity(final HttpEntity entity) {
241         this.entity = entity;
242         return this;
243     }
244 
245     public List<NameValuePair> getParameters() {
246         return parameters != null ? new ArrayList<NameValuePair>(parameters) :
247             new ArrayList<NameValuePair>();
248     }
249 
250     public RequestBuilder addParameter(final NameValuePair nvp) {
251         Args.notNull(nvp, "Name value pair");
252         if (parameters == null) {
253             parameters = new LinkedList<NameValuePair>();
254         }
255         parameters.add(nvp);
256         return this;
257     }
258 
259     public RequestBuilder addParameter(final String name, final String value) {
260         return addParameter(new BasicNameValuePair(name, value));
261     }
262 
263     public RequestBuilder addParameters(final NameValuePair... nvps) {
264         for (final NameValuePair nvp: nvps) {
265             addParameter(nvp);
266         }
267         return this;
268     }
269 
270     public RequestConfig getConfig() {
271         return config;
272     }
273 
274     public RequestBuilder setConfig(final RequestConfig config) {
275         this.config = config;
276         return this;
277     }
278 
279     public HttpUriRequest build() {
280         HttpRequestBase result;
281         URI uri = this.uri != null ? this.uri : URI.create("/");
282         HttpEntity entity = this.entity;
283         if (parameters != null && !parameters.isEmpty()) {
284             if (entity == null && (HttpPost.METHOD_NAME.equalsIgnoreCase(method)
285                     || HttpPut.METHOD_NAME.equalsIgnoreCase(method))) {
286                 entity = new UrlEncodedFormEntity(parameters, HTTP.DEF_CONTENT_CHARSET);
287             } else {
288                 try {
289                     uri = new URIBuilder(uri).addParameters(parameters).build();
290                 } catch (final URISyntaxException ex) {
291                     // should never happen
292                 }
293             }
294         }
295         if (entity == null) {
296             final InternalRequest request = new InternalRequest(method);
297             result = request;
298         } else {
299             final InternalEntityEclosingRequest request = new InternalEntityEclosingRequest(method);
300             request.setEntity(entity);
301             result = request;
302         }
303         result.setProtocolVersion(this.version);
304         result.setURI(uri);
305         if (this.headergroup != null) {
306             result.setHeaders(this.headergroup.getAllHeaders());
307         }
308         result.setConfig(this.config);
309         return result;
310     }
311 
312     static class InternalRequest extends HttpRequestBase {
313 
314         private final String method;
315 
316         InternalRequest(final String method) {
317             super();
318             this.method = method;
319         }
320 
321         @Override
322         public String getMethod() {
323             return this.method;
324         }
325 
326     }
327 
328     static class InternalEntityEclosingRequest extends HttpEntityEnclosingRequestBase {
329 
330         private final String method;
331 
332         InternalEntityEclosingRequest(final String method) {
333             super();
334             this.method = method;
335         }
336 
337         @Override
338         public String getMethod() {
339             return this.method;
340         }
341 
342     }
343 
344 }