View Javadoc

1   /*
2    * ====================================================================
3    *
4    *  Licensed to the Apache Software Foundation (ASF) under one or more
5    *  contributor license agreements.  See the NOTICE file distributed with
6    *  this work for additional information regarding copyright ownership.
7    *  The ASF licenses this file to You under the Apache License, Version 2.0
8    *  (the "License"); you may not use this file except in compliance with
9    *  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, software
14   *  distributed under the License is distributed on an "AS IS" BASIS,
15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *  See the License for the specific language governing permissions and
17   *  limitations under the License.
18   * ====================================================================
19   *
20   * This software consists of voluntary contributions made by many
21   * individuals on behalf of the Apache Software Foundation.  For more
22   * information on the Apache Software Foundation, please see
23   * <http://www.apache.org/>.
24   *
25   */
26  
27  package org.apache.http.client.fluent;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.net.URI;
33  import java.nio.charset.Charset;
34  import java.text.SimpleDateFormat;
35  import java.util.Arrays;
36  import java.util.Date;
37  import java.util.Locale;
38  import java.util.TimeZone;
39  
40  import org.apache.http.Header;
41  import org.apache.http.HttpEntity;
42  import org.apache.http.HttpEntityEnclosingRequest;
43  import org.apache.http.HttpHost;
44  import org.apache.http.HttpVersion;
45  import org.apache.http.NameValuePair;
46  import org.apache.http.client.ClientProtocolException;
47  import org.apache.http.client.entity.UrlEncodedFormEntity;
48  import org.apache.http.client.methods.HttpDelete;
49  import org.apache.http.client.methods.HttpGet;
50  import org.apache.http.client.methods.HttpHead;
51  import org.apache.http.client.methods.HttpOptions;
52  import org.apache.http.client.methods.HttpPost;
53  import org.apache.http.client.methods.HttpPut;
54  import org.apache.http.client.methods.HttpRequestBase;
55  import org.apache.http.client.methods.HttpTrace;
56  import org.apache.http.conn.params.ConnRoutePNames;
57  import org.apache.http.entity.ByteArrayEntity;
58  import org.apache.http.entity.ContentType;
59  import org.apache.http.entity.FileEntity;
60  import org.apache.http.entity.InputStreamEntity;
61  import org.apache.http.entity.StringEntity;
62  import org.apache.http.params.CoreConnectionPNames;
63  import org.apache.http.params.CoreProtocolPNames;
64  import org.apache.http.params.HttpParams;
65  import org.apache.http.protocol.HTTP;
66  
67  public class Request {
68  
69      public static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
70      public static final Locale DATE_LOCALE = Locale.US;
71      public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("GMT");
72  
73      private final HttpRequestBase request;
74      private final HttpParams localParams;
75  
76      private SimpleDateFormat dateFormatter;
77  
78      public static Request Get(final URI uri) {
79          return new Request(new HttpGet(uri));
80      }
81  
82      public static Request Get(final String uri) {
83          return new Request(new HttpGet(uri));
84      }
85  
86      public static Request Head(final URI uri) {
87          return new Request(new HttpHead(uri));
88      }
89  
90      public static Request Head(final String uri) {
91          return new Request(new HttpHead(uri));
92      }
93  
94      public static Request Post(final URI uri) {
95          return new Request(new HttpPost(uri));
96      }
97  
98      public static Request Post(final String uri) {
99          return new Request(new HttpPost(uri));
100     }
101 
102     public static Request Put(final URI uri) {
103         return new Request(new HttpPut(uri));
104     }
105 
106     public static Request Put(final String uri) {
107         return new Request(new HttpPut(uri));
108     }
109 
110     public static Request Trace(final URI uri) {
111         return new Request(new HttpTrace(uri));
112     }
113 
114     public static Request Trace(final String uri) {
115         return new Request(new HttpTrace(uri));
116     }
117 
118     public static Request Delete(final URI uri) {
119         return new Request(new HttpDelete(uri));
120     }
121 
122     public static Request Delete(final String uri) {
123         return new Request(new HttpDelete(uri));
124     }
125 
126     public static Request Options(final URI uri) {
127         return new Request(new HttpOptions(uri));
128     }
129 
130     public static Request Options(final String uri) {
131         return new Request(new HttpOptions(uri));
132     }
133 
134     Request(final HttpRequestBase request) {
135         super();
136         this.request = request;
137         this.localParams = request.getParams();
138     }
139 
140     HttpRequestBase getHttpRequest() {
141         return this.request;
142     }
143 
144     public Response execute() throws ClientProtocolException, IOException {
145         return new Response(Executor.CLIENT.execute(this.request));
146     }
147 
148     public void abort() throws UnsupportedOperationException {
149         this.request.abort();
150     }
151 
152     //// HTTP header operations
153 
154     public Request addHeader(final Header header) {
155         this.request.addHeader(header);
156         return this;
157     }
158 
159     public Request addHeader(final String name, final String value) {
160         this.request.addHeader(name, value);
161         return this;
162     }
163 
164     public Request removeHeader(final Header header) {
165         this.request.removeHeader(header);
166         return this;
167     }
168 
169     public Request removeHeaders(final String name) {
170         this.request.removeHeaders(name);
171         return this;
172     }
173 
174     public Request setHeaders(final Header[] headers) {
175         this.request.setHeaders(headers);
176         return this;
177     }
178 
179     public Request setCacheControl(String cacheControl) {
180         this.request.setHeader(HttpHeader.CACHE_CONTROL, cacheControl);
181         return this;
182     }
183 
184     private SimpleDateFormat getDateFormat() {
185         if (this.dateFormatter == null) {
186             this.dateFormatter = new SimpleDateFormat(DATE_FORMAT, DATE_LOCALE);
187             this.dateFormatter.setTimeZone(TIME_ZONE);
188         }
189         return this.dateFormatter;
190     }
191 
192     public Request setDate(final Date date) {
193         this.request.setHeader(HttpHeader.DATE, getDateFormat().format(date));
194         return this;
195     }
196 
197     public Request setIfModifiedSince(final Date date) {
198         this.request.setHeader(HttpHeader.IF_MODIFIED_SINCE, getDateFormat().format(date));
199         return this;
200     }
201 
202     public Request setIfUnmodifiedSince(final Date date) {
203         this.request.setHeader(HttpHeader.IF_UNMODIFIED_SINCE, getDateFormat().format(date));
204         return this;
205     }
206 
207     //// HTTP config parameter operations
208 
209     public Request config(final String param, final Object object) {
210         this.localParams.setParameter(param, object);
211         return this;
212     }
213 
214     public Request removeConfig(final String param) {
215         this.localParams.removeParameter(param);
216         return this;
217     }
218 
219     //// HTTP protocol parameter operations
220 
221     public Request version(final HttpVersion version) {
222         return config(CoreProtocolPNames.PROTOCOL_VERSION, version);
223     }
224 
225     public Request elementCharset(final String charset) {
226         return config(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, charset);
227     }
228 
229     public Request useExpectContinue() {
230         return config(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
231     }
232 
233     public Request userAgent(final String agent) {
234         return config(CoreProtocolPNames.USER_AGENT, agent);
235     }
236 
237     //// HTTP connection parameter operations
238 
239     public Request socketTimeout(int timeout) {
240         return config(CoreConnectionPNames.SO_TIMEOUT, timeout);
241     }
242 
243     public Request connectTimeout(int timeout) {
244         return config(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
245     }
246 
247     public Request staleConnectionCheck(boolean b) {
248         return config(CoreConnectionPNames.STALE_CONNECTION_CHECK, b);
249     }
250 
251     //// HTTP connection route operations
252 
253     public Request viaProxy(final HttpHost proxy) {
254         return config(ConnRoutePNames.DEFAULT_PROXY, proxy);
255     }
256 
257     //// HTTP entity operations
258 
259     public Request body(final HttpEntity entity) {
260         if (this.request instanceof HttpEntityEnclosingRequest) {
261             ((HttpEntityEnclosingRequest) this.request).setEntity(entity);
262         } else {
263             throw new IllegalStateException(this.request.getMethod()
264                     + " request cannot enclose an entity");
265         }
266         return this;
267     }
268 
269     public Request bodyForm(final Iterable <? extends NameValuePair> formParams, final Charset charset) {
270         return body(new UrlEncodedFormEntity(formParams, charset));
271     }
272 
273     public Request bodyForm(final Iterable <? extends NameValuePair> formParams) {
274         return bodyForm(formParams, HTTP.DEF_CONTENT_CHARSET);
275     }
276 
277     public Request bodyForm(final NameValuePair... formParams) {
278         return bodyForm(Arrays.asList(formParams), HTTP.DEF_CONTENT_CHARSET);
279     }
280 
281     public Request bodyString(final String s, final ContentType contentType) {
282         return body(new StringEntity(s, contentType));
283     }
284 
285     public Request bodyFile(final File file, final ContentType contentType) {
286         return body(new FileEntity(file, contentType));
287     }
288 
289     public Request bodyByteArray(final byte[] b) {
290         return body(new ByteArrayEntity(b));
291     }
292 
293     public Request bodyByteArray(final byte[] b, int off, int len) {
294         return body(new ByteArrayEntity(b, off, len));
295     }
296 
297     public Request bodyStream(final InputStream instream) {
298         return body(new InputStreamEntity(instream, -1));
299     }
300 
301     public Request bodyStream(final InputStream instream, final ContentType contentType) {
302         return body(new InputStreamEntity(instream, -1, contentType));
303     }
304 
305     @Override
306     public String toString() {
307         return this.request.getRequestLine().toString();
308     }
309     
310 }