8
8
9
9
package org .elasticsearch .rest ;
10
10
11
+ import org .elasticsearch .action .support .master .AcknowledgedRequest ;
11
12
import org .elasticsearch .common .Strings ;
12
13
import org .elasticsearch .core .Booleans ;
14
+ import org .elasticsearch .core .Nullable ;
13
15
import org .elasticsearch .core .TimeValue ;
14
16
15
17
import java .nio .charset .Charset ;
20
22
import java .util .function .UnaryOperator ;
21
23
import java .util .regex .Pattern ;
22
24
25
+ import static org .elasticsearch .action .support .master .AcknowledgedRequest .DEFAULT_ACK_TIMEOUT ;
23
26
import static org .elasticsearch .rest .RestRequest .PATH_RESTRICTED ;
24
27
25
28
public class RestUtils {
@@ -267,6 +270,11 @@ public static Optional<String> extractTraceId(String traceparent) {
267
270
*/
268
271
public static final TimeValue REST_MASTER_TIMEOUT_DEFAULT = TimeValue .timeValueSeconds (30 );
269
272
273
+ /**
274
+ * The name of the common {@code ?timeout} query parameter.
275
+ */
276
+ public static final String REST_TIMEOUT_PARAM = "timeout" ;
277
+
270
278
/**
271
279
* Extract the {@code ?master_timeout} parameter from the request, imposing the common default of {@code 30s} in case the parameter is
272
280
* missing.
@@ -280,4 +288,28 @@ public static TimeValue getMasterNodeTimeout(RestRequest restRequest) {
280
288
return restRequest .paramAsTime (REST_MASTER_TIMEOUT_PARAM , REST_MASTER_TIMEOUT_DEFAULT );
281
289
}
282
290
291
+ /**
292
+ * Extract the {@code ?timeout} parameter from the request, imposing the common default of {@code 30s} in case the parameter is
293
+ * missing.
294
+ *
295
+ * @param restRequest The request from which to extract the {@code ?timeout} parameter
296
+ * @return the timeout from the request, with a default of {@link AcknowledgedRequest#DEFAULT_ACK_TIMEOUT} ({@code 30s}) if the request
297
+ * does not specify the parameter
298
+ */
299
+ public static TimeValue getAckTimeout (RestRequest restRequest ) {
300
+ assert restRequest != null ;
301
+ return restRequest .paramAsTime (REST_TIMEOUT_PARAM , DEFAULT_ACK_TIMEOUT );
302
+ }
303
+
304
+ /**
305
+ * Extract the {@code ?timeout} parameter from the request, returning null in case the parameter is missing.
306
+ *
307
+ * @param restRequest The request from which to extract the {@code ?timeout} parameter
308
+ * @return the timeout from the request, with a default of {@code null} if the request does not specify the parameter
309
+ */
310
+ @ Nullable
311
+ public static TimeValue getTimeout (RestRequest restRequest ) {
312
+ assert restRequest != null ;
313
+ return restRequest .paramAsTime (REST_TIMEOUT_PARAM , null );
314
+ }
283
315
}
0 commit comments