forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseRequest.java
More file actions
524 lines (463 loc) · 15.2 KB
/
Copy pathBaseRequest.java
File metadata and controls
524 lines (463 loc) · 15.2 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// ------------------------------------------------------------------------------
// Copyright (c) 2017 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ------------------------------------------------------------------------------
package com.microsoft.graph.http;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.HttpUrl.Builder;
import com.microsoft.graph.concurrency.ICallback;
import com.microsoft.graph.concurrency.IProgressCallback;
import com.microsoft.graph.content.MSBatchRequestContent;
import com.microsoft.graph.content.MSBatchRequestStep;
import com.microsoft.graph.core.IBaseClient;
import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect;
import com.microsoft.graph.httpcore.middlewareoption.IShouldRetry;
import com.microsoft.graph.httpcore.middlewareoption.RedirectOptions;
import com.microsoft.graph.httpcore.middlewareoption.RetryOptions;
import com.microsoft.graph.options.FunctionOption;
import com.microsoft.graph.options.QueryOption;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.core.Constants;
import com.microsoft.graph.options.HeaderOption;
import com.microsoft.graph.options.Option;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
/**
* An HTTP request
*/
public abstract class BaseRequest implements IHttpRequest {
/**
* The request stats header name
*/
private static final String REQUEST_STATS_HEADER_NAME = "SdkVersion";
/**
* The request stats header value format string
*/
public static final String REQUEST_STATS_HEADER_VALUE_FORMAT_STRING = "graph-java/v%s";
/**
* The HTTP method for this request
*/
private HttpMethod method;
/**
* The URL for this request
*/
private final String requestUrl;
/**
* The backing client for this request
*/
private final IBaseClient client;
/**
* The header options for this request
*/
private final List<HeaderOption> headersOptions;
/**
* The query options for this request
*/
protected final List<QueryOption> queryOptions;
/**
* The function options for this request
*/
protected final List<FunctionOption> functionOptions;
/**
* The class for the response
*/
private final Class<?> responseClass;
/**
* Value to pass to setUseCaches in connection
*/
private boolean useCaches;
/**
* MaxRedirects of every request
*/
private int maxRedirects = RedirectOptions.DEFAULT_MAX_REDIRECTS;
/**
* ShouldRedirect callback for every request
*/
private IShouldRedirect shouldRedirect = RedirectOptions.DEFAULT_SHOULD_REDIRECT;
/**
* Max redirects for every request
*/
private int maxRetries = RetryOptions.DEFAULT_MAX_RETRIES;
/**
* Delay in seconds for every request
*/
private long delay = RetryOptions.DEFAULT_DELAY;
/**
* Callback before doing a retry
*/
private IShouldRetry shouldRetry = RetryOptions.DEFAULT_SHOULD_RETRY;
/**
* Creates the request
*
* @param requestUrl the URL to make the request against
* @param client the client which can issue the request
* @param options the options for this request
* @param responseClass the class for the response
*/
public BaseRequest(final String requestUrl,
final IBaseClient client,
final List<? extends Option> options,
final Class<?> responseClass) {
this.requestUrl = requestUrl;
this.client = client;
this.responseClass = responseClass;
headersOptions = new ArrayList<>();
queryOptions = new ArrayList<>();
functionOptions = new ArrayList<>();
if (options != null) {
for (final Option option : options) {
if (option instanceof HeaderOption) {
headersOptions.add((HeaderOption) option);
}
if (option instanceof QueryOption) {
queryOptions.add((QueryOption) option);
}
if (option instanceof FunctionOption) {
functionOptions.add((FunctionOption) option);
}
}
}
final HeaderOption requestStatsHeader = new HeaderOption(REQUEST_STATS_HEADER_NAME,
String.format(REQUEST_STATS_HEADER_VALUE_FORMAT_STRING, Constants.VERSION_NAME));
headersOptions.add(requestStatsHeader);
}
/**
* Gets the request URL
*
* @return the request URL
*/
@Override
public URL getRequestUrl() {
String requestUrl = addFunctionParameters();
final Builder uriBuilder = HttpUrl.parse(requestUrl).newBuilder();
for (final QueryOption option : queryOptions) {
uriBuilder.addQueryParameter(option.getName(), option.getValue().toString());
}
try {
return new URL(uriBuilder.build().toString());
} catch (final MalformedURLException e) {
if (this instanceof CustomRequest) {
this.getClient().getLogger().logError("Invalid custom URL: " + uriBuilder.toString(), e);
} else {
throw new ClientException("Invalid URL: " + uriBuilder.toString(), e);
}
}
return null;
}
/**
* Returns the Request object to be executed
* @return the Request object to be executed
*/
@Override
public Request getHttpRequest() throws ClientException {
return getHttpRequest(null);
}
/**
* Returns the Request object to be executed
* @param serializedObject the object to serialize at the body of the request
* @param <requestBodyType> the type of the serialized object
* @return the Request object to be executed
*/
@Override
public <requestBodyType> Request getHttpRequest(final requestBodyType serializedObject) throws ClientException {
return getHttpRequest(serializedObject, null);
}
/**
* Returns the Request object to be executed
* @param serializedObject the object to serialize at the body of the request
* @param progress the progress callback
* @param <requestBodyType> the type of the serialized object
* @param <responseType> the type of the response object
* @return the Request object to be executed
*/
@SuppressWarnings("unchecked")
@Override
public <requestBodyType, responseType> Request getHttpRequest(final requestBodyType serializedObject, final IProgressCallback<responseType> progress) throws ClientException {
return client.getHttpProvider().getHttpRequest(this, (Class<responseType>) responseClass, serializedObject, progress);
}
private String addFunctionParameters() {
final StringBuilder requestUrl = new StringBuilder(this.requestUrl);
if (!getFunctionOptions().isEmpty()) {
requestUrl.append("(");
for (int i = 0; i < functionOptions.size(); i++) {
final FunctionOption option = functionOptions.get(i);
requestUrl.append(option.getName());
requestUrl.append("=");
if (option.getValue() != null) {
if (option.getValue() instanceof String) {
requestUrl.append("'" + option.getValue() + "'");
} else {
requestUrl.append(option.getValue());
}
} else {
requestUrl.append("null");
}
if (i + 1 < functionOptions.size()) {
requestUrl.append(",");
}
}
requestUrl.append(")");
}
return requestUrl.toString();
}
/**
* Gets the HTTP method
*
* @return the HTTP method
*/
@Override
public HttpMethod getHttpMethod() {
return method;
}
/**
* Gets the headers
*
* @return the headers
*/
@Override
public List<HeaderOption> getHeaders() {
return headersOptions;
}
/**
* Adds a header to this request
*
* @param header the name of the header
* @param value the value of the header
*/
@Override
public void addHeader(final String header, final String value) {
headersOptions.add(new HeaderOption(header, value));
}
/**
* Sets useCaches parameter to cache the response
*
* @param useCaches the value of useCaches
*/
@Override
public void setUseCaches(boolean useCaches) {
this.useCaches = useCaches;
}
/**
* Gets useCaches parameter
*
* @return the value of useCaches
*/
@Override
public boolean getUseCaches() {
return useCaches;
}
/**
* Sends this request
*
* @param method the HTTP method
* @param callback the callback when this request complements
* @param serializedObject the object to serialize as the body
* @param <T1> the type of the callback result
* @param <T2> the type of the serialized body
*/
@SuppressWarnings("unchecked")
protected <T1, T2> void send(final HttpMethod method,
final ICallback<T1> callback,
final T2 serializedObject) {
this.method = method;
client.getHttpProvider().send(this, callback, (Class<T1>) responseClass, serializedObject);
}
/**
* Sends this request
*
* @param method the HTTP method
* @param serializedObject the object to serialize as the body
* @param <T1> the type of the callback result
* @param <T2> the type of the serialized body
* @return the response object
* @throws ClientException an exception occurs if there was an error while the request was sent
*/
@SuppressWarnings("unchecked")
protected <T1, T2> T1 send(final HttpMethod method,
final T2 serializedObject) throws ClientException {
this.method = method;
return (T1) client.getHttpProvider().send(this, responseClass, serializedObject);
}
/**
* Gets the query options for this request
*
* @return the query options for this request
*/
public List<QueryOption> getQueryOptions() {
return queryOptions;
}
/**
* Gets the function options for this request
*
* @return the function options for this request
*/
public List<FunctionOption> getFunctionOptions() {
return functionOptions;
}
/**
* Gets the full list of options for this request
*
* @return the full list of options for this request
*/
public List<Option> getOptions() {
final LinkedList<Option> list = new LinkedList<>();
list.addAll(headersOptions);
list.addAll(queryOptions);
list.addAll(functionOptions);
return Collections.unmodifiableList(list);
}
/**
* Adds a query option
*
* @param option the query option to add
*/
public void addQueryOption(final QueryOption option) {
getQueryOptions().add(option);
}
/**
* Adds a function option
*
* @param option the function option to add
*/
public void addFunctionOption(final FunctionOption option) {
getFunctionOptions().add(option);
}
/**
* Sets the HTTP method
*
* @param httpMethod the HTTP method
*/
public void setHttpMethod(final HttpMethod httpMethod) {
method = httpMethod;
}
/**
* Sets the HTTP method and returns the current request
*
* @param httpMethod the HTTP method
* @return the current request
*/
public IHttpRequest withHttpMethod(final HttpMethod httpMethod) {
method = httpMethod;
return this;
}
/**
* Gets the client
*
* @return the client
*/
public IBaseClient getClient() {
return client;
}
/**
* Gets the response type
*
* @return the response type
*/
public Class<?> getResponseType() {
return responseClass;
}
/**
* Sets the max redirects
*
* @param maxRedirects Max redirects that a request can take
*/
public void setMaxRedirects(int maxRedirects) {
this.maxRedirects = maxRedirects;
}
/**
* Gets the max redirects
*
* @return Max redirects that a request can take
*/
public int getMaxRedirects() {
return maxRedirects;
}
/**
* Sets the should redirect callback
*
* @param shouldRedirect Callback called before doing a redirect
*/
public void setShouldRedirect(IShouldRedirect shouldRedirect) {
this.shouldRedirect = shouldRedirect;
}
/**
* Gets the should redirect callback
*
* @return Callback which is called before redirect
*/
public IShouldRedirect getShouldRedirect() {
return shouldRedirect;
}
/**
* Sets the should retry callback
*
* @param shouldretry The callback called before retry
*/
public void setShouldRetry(IShouldRetry shouldretry) {
this.shouldRetry = shouldretry;
}
/**
* Gets the should retry callback
*
* @return Callback called before retry
*/
public IShouldRetry getShouldRetry() {
return shouldRetry;
}
/**
* Sets the max retries
*
* @param maxRetries Max retries for a request
*/
public void setMaxRetries(int maxRetries) {
this.maxRetries = maxRetries;
}
/**
* Gets max retries
*
* @return Max retries for a request
*/
public int getMaxRetries() {
return maxRetries;
}
/**
* Sets the delay in seconds between retires
*
* @param delay Delay in seconds between retries
*/
public void setDelay(long delay) {
this.delay = delay;
}
/**
* Gets delay between retries
*
* @return Delay between retries in seconds
*/
public long getDelay() {
return delay;
}
}