/
WSRequest.java
316 lines (261 loc) · 8.6 KB
/
WSRequest.java
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
*/
package play.libs.ws;
import com.fasterxml.jackson.databind.JsonNode;
import play.libs.F;
import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
/**
* This is the main interface to building a WS request in Java.
* <p>
* Note that this interface does not expose properties that are only exposed
* after building the request: notably, the URL, headers and query parameters
* are shown before an OAuth signature is calculated.
*/
public interface WSRequest {
//-------------------------------------------------------------------------
// "GET"
//-------------------------------------------------------------------------
/**
* Perform a GET on the request asynchronously.
*/
F.Promise<WSResponse> get();
//-------------------------------------------------------------------------
// "PATCH"
//-------------------------------------------------------------------------
/**
* Perform a PATCH on the request asynchronously.
*
* @param body represented as String
*/
F.Promise<WSResponse> patch(String body);
/**
* Perform a PATCH on the request asynchronously.
*
* @param body represented as JSON
*/
F.Promise<WSResponse> patch(JsonNode body);
/**
* Perform a PATCH on the request asynchronously.
*
* @param body represented as an InputStream
*/
F.Promise<WSResponse> patch(InputStream body);
/**
* Perform a PATCH on the request asynchronously.
*
* @param body represented as a File
*/
F.Promise<WSResponse> patch(File body);
//-------------------------------------------------------------------------
// "POST"
//-------------------------------------------------------------------------
/**
* Perform a POST on the request asynchronously.
*
* @param body represented as String
*/
F.Promise<WSResponse> post(String body);
/**
* Perform a POST on the request asynchronously.
*
* @param body represented as JSON
*/
F.Promise<WSResponse> post(JsonNode body);
/**
* Perform a POST on the request asynchronously.
*
* @param body represented as an InputStream
*/
F.Promise<WSResponse> post(InputStream body);
/**
* Perform a POST on the request asynchronously.
*
* @param body represented as a File
*/
F.Promise<WSResponse> post(File body);
//-------------------------------------------------------------------------
// "PUT"
//-------------------------------------------------------------------------
/**
* Perform a PUT on the request asynchronously.
*
* @param body represented as String
*/
F.Promise<WSResponse> put(String body);
/**
* Perform a PUT on the request asynchronously.
*
* @param body represented as JSON
*/
F.Promise<WSResponse> put(JsonNode body);
/**
* Perform a PUT on the request asynchronously.
*
* @param body represented as an InputStream
*/
F.Promise<WSResponse> put(InputStream body);
/**
* Perform a PUT on the request asynchronously.
*
* @param body represented as a File
*/
F.Promise<WSResponse> put(File body);
//-------------------------------------------------------------------------
// Miscellaneous execution methods
//-------------------------------------------------------------------------
/**
* Perform a DELETE on the request asynchronously.
*/
F.Promise<WSResponse> delete();
/**
* Perform a HEAD on the request asynchronously.
*/
F.Promise<WSResponse> head();
/**
* Perform an OPTIONS on the request asynchronously.
*/
F.Promise<WSResponse> options();
/**
* Execute an arbitrary method on the request asynchronously.
*
* @param method The method to execute
*/
F.Promise<WSResponse> execute(String method);
/**
* Execute an arbitrary method on the request asynchronously. Should be used with setMethod().
*/
F.Promise<WSResponse> execute();
//-------------------------------------------------------------------------
// Setters
//-------------------------------------------------------------------------
/**
* Set the HTTP method this request should use, where the no args execute() method is invoked.
*/
WSRequest setMethod(String method);
/**
* Set the body this request should use.
*/
WSRequest setBody(String body);
/**
* Set the body this request should use.
*/
WSRequest setBody(JsonNode body);
/**
* Set the body this request should use.
*/
WSRequest setBody(InputStream body);
/**
* Set the body this request should use.
*/
WSRequest setBody(File body);
/**
* Adds a header to the request. Note that duplicate headers are allowed
* by the HTTP specification, and removing a header is not available
* through this API.
*/
WSRequest setHeader(String name, String value);
/**
* Sets the query string to query.
*/
WSRequest setQueryString(String query);
/**
* Sets a query parameter with the given name, this can be called repeatedly. Duplicate query parameters are allowed.
*
* @param name
* @param value
*/
WSRequest setQueryParameter(String name, String value);
/**
* Sets the authentication header for the current request using BASIC authentication.
*
* @param userInfo
*/
WSRequest setAuth(String userInfo);
/**
* Sets the authentication header for the current request using BASIC authentication.
*
* @param username
* @param password
*/
WSRequest setAuth(String username, String password);
/**
* Sets the authentication header for the current request.
*
* @param username
* @param password
* @param scheme authentication scheme
*/
WSRequest setAuth(String username, String password, WSAuthScheme scheme);
/**
* Sets an (OAuth) signature calculator.
*/
WSRequest sign(WSSignatureCalculator calculator);
/**
* Sets whether redirects (301, 302) should be followed automatically.
*
* @param followRedirects
*/
WSRequest setFollowRedirects(Boolean followRedirects);
/**
* Sets the virtual host as a "hostname:port" string.
*/
WSRequest setVirtualHost(String virtualHost);
/**
* Sets the request timeout in milliseconds.
*
* @param timeout the request timeout in milliseconds. A value of -1 indicates an infinite request timeout.
* @return the modified WSRequest.
*/
WSRequest setRequestTimeout(long timeout);
/**
* Set the content type. If the request body is a String, and no charset parameter is included, then it will
* default to UTF-8.
*
* @param contentType The content type
*/
WSRequest setContentType(String contentType);
//-------------------------------------------------------------------------
// Getters
//-------------------------------------------------------------------------
/**
* @return the URL of the request. This has not passed through an internal request builder and so will not be signed.
*/
String getUrl();
/**
* @return the headers (a copy to prevent side-effects). This has not passed through an internal request builder and so will not be signed.
*/
Map<String, Collection<String>> getHeaders();
/**
* @return the query parameters (a copy to prevent side-effects). This has not passed through an internal request builder and so will not be signed.
*/
Map<String, Collection<String>> getQueryParameters();
/**
* @return the auth username, null if not an authenticated request.
*/
String getUsername();
/**
* @return the auth password, null if not an authenticated request
*/
String getPassword();
/**
* @return the auth scheme, null if not an authenticated request.
*/
WSAuthScheme getScheme();
/**
* @return the signature calculator (example: OAuth), null if none is set.
*/
WSSignatureCalculator getCalculator();
/**
* Gets the original request timeout in milliseconds, passed into the
* request as input.
*/
long getRequestTimeout();
/**
* @return true if the request is configure to follow redirect, false if it is configure not to, null if nothing is configured and the global client preference should be used instead.
*/
Boolean getFollowRedirects();
}