-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathRESTClient.java
More file actions
265 lines (248 loc) · 10.9 KB
/
Copy pathRESTClient.java
File metadata and controls
265 lines (248 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright 2008-2011 Thomas Nichols. http://blog.thomnichols.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You are receiving this code free of charge, which represents many hours of
* effort from other individuals and corporations. As a responsible member
* of the community, you are encouraged (but not required) to donate any
* enhancements or improvements back to the community under a similar open
* source license. Thank you. -TMN
*/
package groovyx.net.http;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpPatch;
/**
* Extension to HTTPBuilder that basically attempts to provide a slightly more
* REST-ful face on top of HTTPBuilder. The differences between this class
* and HTTPBuilder are such:
*
* <ul>
* <li>Access to response headers. All "request" methods on this class by
* default return an instance of {@link HttpResponseDecorator}, which allows for simple
* evaluation of the response.</li>
* <li>No streaming responses. Responses are expected to either not carry data
* (in the case of HEAD or DELETE) or be parse-able into some sort of object.
* That object is accessible via {@link HttpResponseDecorator#getData()}.</li>
* </ul>
*
* <p>By default, all request method methods will return a {@link HttpResponseDecorator}
* instance, which provides convenient access to response headers and the parsed
* response body. The response body is parsed based on content-type, identical
* to how HTTPBuilder's {@link HTTPBuilder#defaultSuccessHandler(HttpResponseDecorator,
* Object) default response handler} functions.</p>
*
* <p>Failed requests (i.e. responses which return a status code > 399) will
* by default throw a {@link HttpResponseException}. This exception may be used
* to retrieve additional information regarding the response as well.</p>
*
* @author <a href='mailto:tomstrummer+httpbuilder@gmail.com'>Tom Nichols</a>
* @since 0.5
*/
public class RESTClient extends HTTPBuilder {
/**
* Constructor.
* @see HTTPBuilder#HTTPBuilder()
*/
public RESTClient() { super(); }
/**
* See {@link HTTPBuilder#HTTPBuilder(Object)}
* @param defaultURI default request URI (String, URI, URL or {@link URIBuilder})
* @throws URISyntaxException
*/
public RESTClient( Object defaultURI ) throws URISyntaxException {
super( defaultURI );
}
/**
* See {@link HTTPBuilder#HTTPBuilder(Object, Object)}
* @param defaultURI default request URI (String, URI, URL or {@link URIBuilder})
* @param defaultContentType default content-type (String or {@link ContentType})
* @throws URISyntaxException
*/
public RESTClient( Object defaultURI, Object defaultContentType ) throws URISyntaxException {
super( defaultURI, defaultContentType );
}
/**
* <p>Convenience method to perform an HTTP GET request. It will use the HTTPBuilder's
* {@link #getHandler() registered response handlers} to handle success or
* failure status codes. By default, the
* {@link #defaultSuccessHandler(HttpResponseDecorator, Object)}
* <code>success</code> response handler will return a decorated response
* object that can be used to read response headers and data.</p>
*
* <p>A 'failed' response (i.e. any HTTP status code > 399) will be handled
* by the registered 'failure' handler.
* The {@link #defaultFailureHandler(HttpResponseDecorator, Object)
* default failure handler} throws a {@link HttpResponseException}.</p>
*
* @see #defaultSuccessHandler(HttpResponseDecorator, Object)
* @see #defaultFailureHandler(HttpResponseDecorator, Object)
* @param args named parameters - see
* {@link HTTPBuilder.RequestConfigDelegate#setPropertiesFromMap(Map)}
* @return a {@link HttpResponseDecorator}, unless the default success
* handler is overridden.
* @throws URISyntaxException
* @throws IOException
* @throws ClientProtocolException
*/
public Object get( Map<String,?> args ) throws ClientProtocolException,
IOException, URISyntaxException {
return doRequest( new RequestConfigDelegate( args, new HttpGet(), null ) );
}
/**
* <p>Convenience method to perform a POST request.</p>
*
* <p>The request body (specified by a <code>body</code> named parameter)
* will be encoded based on the <code>requestContentType</code> named
* parameter, or if none is given, the default
* {@link HTTPBuilder#setContentType(Object) content-type} for this instance.
* </p>
*
* @param args named parameters - see
* {@link HTTPBuilder.RequestConfigDelegate#setPropertiesFromMap(Map)}
* @return a {@link HttpResponseDecorator}, unless the default success
* handler is overridden.
* @throws ClientProtocolException
* @throws IOException
* @throws URISyntaxException
*/
@Override public Object post( Map<String,?> args )
throws URISyntaxException, ClientProtocolException, IOException {
return doRequest( new RequestConfigDelegate( args, new HttpPost(), null ) );
}
/**
* <p> Convenience method to perform a PUT request.</p>
*
* <p>The request body (specified by a <code>body</code> named parameter)
* will be encoded based on the <code>requestContentType</code> named
* parameter, or if none is given, the default
* {@link HTTPBuilder#setContentType(Object) content-type} for this instance.
* </p>
*
* @param args named parameters - see
* {@link HTTPBuilder.RequestConfigDelegate#setPropertiesFromMap(Map)}
* @return a {@link HttpResponseDecorator}, unless the default success
* handler is overridden.
* @throws ClientProtocolException
* @throws IOException
* @throws URISyntaxException
*/
public Object put( Map<String,?> args ) throws URISyntaxException,
ClientProtocolException, IOException {
return this.doRequest( new RequestConfigDelegate( args, new HttpPut(), null ) );
}
/**
* <p> Convenience method to perform a PATCH request.</p>
*
* <p>The request body (specified by a <code>body</code> named parameter)
* will be encoded based on the <code>requestContentType</code> named
* parameter, or if none is given, the default
* {@link HTTPBuilder#setContentType(Object) content-type} for this instance.
* </p>
*
* @param args named parameters - see
* {@link HTTPBuilder.RequestConfigDelegate#setPropertiesFromMap(Map)}
* @return a {@link HttpResponseDecorator}, unless the default success
* handler is overridden.
* @throws ClientProtocolException
* @throws IOException
* @throws URISyntaxException
*/
public Object patch( Map<String,?> args ) throws URISyntaxException,
ClientProtocolException, IOException {
return this.doRequest( new RequestConfigDelegate( args, new HttpPatch(), null ) );
}
/**
* <p>Perform a HEAD request, often used to check preconditions before
* sending a large PUT or POST request.</p>
*
* @param args named parameters - see
* {@link HTTPBuilder.RequestConfigDelegate#setPropertiesFromMap(Map)}
* @return a {@link HttpResponseDecorator}, unless the default success
* handler is overridden.
* @throws ClientProtocolException
* @throws IOException
* @throws URISyntaxException
*/
public Object head( Map<String,?> args ) throws URISyntaxException,
ClientProtocolException, IOException {
return this.doRequest( new RequestConfigDelegate( args, new HttpHead(), null ) );
}
/**
* <p>Perform a DELETE request. This method does not accept a
* <code>body</code> argument.</p>
*
* @param args named parameters - see
* {@link HTTPBuilder.RequestConfigDelegate#setPropertiesFromMap(Map)}
* @return a {@link HttpResponseDecorator}, unless the default success
* handler is overridden.
* @throws ClientProtocolException
* @throws IOException
* @throws URISyntaxException
*/
public Object delete( Map<String,?> args ) throws URISyntaxException,
ClientProtocolException, IOException {
return this.doRequest( new RequestConfigDelegate( args, new HttpDeleteWithBody(), null ) );
}
/**
* <p>Perform an OPTIONS request.</p>
*
* @param args named parameters - see
* {@link HTTPBuilder.RequestConfigDelegate#setPropertiesFromMap(Map)}
* @return a {@link HttpResponseDecorator}, unless the default success
* handler is overridden.
* @throws ClientProtocolException
* @throws IOException
* @throws URISyntaxException
*/
public Object options( Map<String,?> args ) throws ClientProtocolException,
IOException, URISyntaxException {
return this.doRequest( new RequestConfigDelegate( args, new HttpOptions(), null ) );
}
/**
* Returns an {@link HttpResponseDecorator}, which provides simplified
* access to headers, response code, and parsed response body, as well as
* the underlying {@link HttpResponse} instance.
*/
@Override
protected HttpResponseDecorator defaultSuccessHandler( HttpResponseDecorator resp, Object data )
throws ResponseParseException {
resp.setData( super.defaultSuccessHandler( resp, data ) );
return resp;
}
/**
* Throws an exception for non-successful HTTP response codes. The
* exception instance will have a reference to the response object, in
* order to inspect status code and headers within the <code>catch</code>
* block.
* @param resp response object
* @param data parsed response data
* @throws HttpResponseException exception which can access the response
* object.
*/
protected void defaultFailureHandler( HttpResponseDecorator resp, Object data )
throws HttpResponseException {
resp.setData( super.defaultSuccessHandler( resp, data ) );
throw new HttpResponseException( resp );
}
}