From c0b1fdf8d8b6663335743acae5e67cfbcade7128 Mon Sep 17 00:00:00 2001 From: Hemant Tanwar Date: Fri, 15 Nov 2019 13:00:06 -0800 Subject: [PATCH 1/2] Adding Options for Request Id Policy. --- .../core/http/policy/RequestIdPolicy.java | 38 +++++++++++++++-- .../http/policy/RequestIdPolicyOptions.java | 41 +++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicyOptions.java diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicy.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicy.java index 5fd67f4d3e4c..7592879001e8 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicy.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicy.java @@ -3,24 +3,56 @@ package com.azure.core.http.policy; +import com.azure.core.http.HttpHeaders; import com.azure.core.http.HttpPipelineCallContext; import com.azure.core.http.HttpPipelineNextPolicy; import com.azure.core.http.HttpResponse; import reactor.core.publisher.Mono; +import java.util.Objects; import java.util.UUID; /** - * The pipeline policy that puts a UUID in the request header. Azure uses the request id as + * The pipeline policy that puts a UUID or user provided id in the request header. Azure uses the request id as * the unique identifier for the request. + * @see RequestIdPolicyOptions */ public class RequestIdPolicy implements HttpPipelinePolicy { private static final String REQUEST_ID_HEADER = "x-ms-client-request-id"; + private final RequestIdPolicyOptions requestIdPolicyOptions; + + /** + * Creates {@link RequestIdPolicy} with default {@link ExponentialBackoff} as {@link RetryStrategy}and use + * 'retry-after-ms' in {@link HttpResponse} header for calculating retry delay. + */ + public RequestIdPolicy() { + this.requestIdPolicyOptions = null; + } + + /** + * Creates a {@link RequestIdPolicy} with the provided {@link RequestIdPolicyOptions}. + * User can specify {@link java.util.function.Function} to dynamically generated id for various id headers. + * + * @param requestIdPolicyOptions with given {@link RetryPolicyOptions}. + * @throws NullPointerException if {@code RequestIdPolicyOptions} is {@code null}. + */ + public RequestIdPolicy(RequestIdPolicyOptions requestIdPolicyOptions) { + this.requestIdPolicyOptions = Objects.requireNonNull(requestIdPolicyOptions, + "'retryPolicyOptions' cannot be null."); + } + + @Override public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { - String requestId = context.getHttpRequest().getHeaders().getValue(REQUEST_ID_HEADER); - if (requestId == null) { + if (!Objects.isNull(requestIdPolicyOptions)) { + HttpHeaders httpHeaders = requestIdPolicyOptions.getIdHeaderSupplier().get(); + if (!Objects.isNull(httpHeaders)) { + httpHeaders.stream().forEach(httpHeader -> + context.getHttpRequest().getHeaders().put(httpHeader.getName(), httpHeader.getValue()) + ); + } + } else { context.getHttpRequest().getHeaders().put(REQUEST_ID_HEADER, UUID.randomUUID().toString()); } return next.process(); diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicyOptions.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicyOptions.java new file mode 100644 index 000000000000..7b43ecf64d3b --- /dev/null +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicyOptions.java @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.core.http.policy; + +import com.azure.core.annotation.Immutable; +import com.azure.core.http.HttpHeaders; + +import java.util.Objects; +import java.util.function.Supplier; + +/** + * Immutable Configuration options for {@link RequestIdPolicy}. + * User can provide a {@link Supplier} to generated id for various id headers to be used in + * {@link com.azure.core.http.HttpRequest}. Examples of such headers are 'x-ms-client-request-id + * or 'x-ms-correlation-request-id' etc. + */ +@Immutable +public class RequestIdPolicyOptions { + + private volatile Supplier idHeaderSupplier; + + /** + * Creates {@link RequestIdPolicyOptions} with provided {@code idHeaderSupplier }. + * + * @param idHeaderSupplier {@link Supplier} to provide dynamically generated id for various request id headers. + * Examples of such headers are 'x-ms-client-request-id or 'x-ms-correlation-request-id' etc. + */ + public RequestIdPolicyOptions(Supplier idHeaderSupplier) { + this.idHeaderSupplier = Objects.requireNonNull(idHeaderSupplier, + "'idHeaderSupplier' cannot be null."); + } + + /** + * + * @return {@link Supplier} to provide dynamically generated id for various request id headers + */ + public Supplier getIdHeaderSupplier() { + return idHeaderSupplier; + } +} From 217b64fc938624c4624f1db1f120c119617570e9 Mon Sep 17 00:00:00 2001 From: Hemant Tanwar Date: Mon, 18 Nov 2019 13:36:47 -0800 Subject: [PATCH 2/2] incorporating review comments --- .../core/http/policy/RequestIdPolicy.java | 25 ++++++++++++------- .../http/policy/RequestIdPolicyOptions.java | 3 ++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicy.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicy.java index 7592879001e8..615a42ae3f1f 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicy.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicy.java @@ -23,8 +23,7 @@ public class RequestIdPolicy implements HttpPipelinePolicy { private final RequestIdPolicyOptions requestIdPolicyOptions; /** - * Creates {@link RequestIdPolicy} with default {@link ExponentialBackoff} as {@link RetryStrategy}and use - * 'retry-after-ms' in {@link HttpResponse} header for calculating retry delay. + * Creates {@link RequestIdPolicy} with default behaviour. */ public RequestIdPolicy() { this.requestIdPolicyOptions = null; @@ -35,25 +34,33 @@ public RequestIdPolicy() { * User can specify {@link java.util.function.Function} to dynamically generated id for various id headers. * * @param requestIdPolicyOptions with given {@link RetryPolicyOptions}. - * @throws NullPointerException if {@code RequestIdPolicyOptions} is {@code null}. */ public RequestIdPolicy(RequestIdPolicyOptions requestIdPolicyOptions) { - this.requestIdPolicyOptions = Objects.requireNonNull(requestIdPolicyOptions, - "'retryPolicyOptions' cannot be null."); + this.requestIdPolicyOptions = requestIdPolicyOptions; } @Override public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { + String requestId = context.getHttpRequest().getHeaders().getValue(REQUEST_ID_HEADER); if (!Objects.isNull(requestIdPolicyOptions)) { HttpHeaders httpHeaders = requestIdPolicyOptions.getIdHeaderSupplier().get(); if (!Objects.isNull(httpHeaders)) { - httpHeaders.stream().forEach(httpHeader -> - context.getHttpRequest().getHeaders().put(httpHeader.getName(), httpHeader.getValue()) - ); + httpHeaders.stream().forEach(httpHeader -> { + String requestIdHeaderValue = context.getHttpRequest().getHeaders().getValue(httpHeader.getName()); + if (requestIdHeaderValue == null) { + context.getHttpRequest().getHeaders().put(httpHeader.getName(), httpHeader.getValue()); + } + }); + }else { + if (requestId == null) { + context.getHttpRequest().getHeaders().put(REQUEST_ID_HEADER, UUID.randomUUID().toString()); + } } } else { - context.getHttpRequest().getHeaders().put(REQUEST_ID_HEADER, UUID.randomUUID().toString()); + if (requestId == null) { + context.getHttpRequest().getHeaders().put(REQUEST_ID_HEADER, UUID.randomUUID().toString()); + } } return next.process(); } diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicyOptions.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicyOptions.java index 7b43ecf64d3b..e4860b1d5749 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicyOptions.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/policy/RequestIdPolicyOptions.java @@ -18,7 +18,7 @@ @Immutable public class RequestIdPolicyOptions { - private volatile Supplier idHeaderSupplier; + private final Supplier idHeaderSupplier; /** * Creates {@link RequestIdPolicyOptions} with provided {@code idHeaderSupplier }. @@ -32,6 +32,7 @@ public RequestIdPolicyOptions(Supplier idHeaderSupplier) { } /** + * Get the provided {@link Supplier} which dynamically generate id for various request id headers. * * @return {@link Supplier} to provide dynamically generated id for various request id headers */