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 517719e91314..389386519cf9 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,63 +3,22 @@ package com.azure.core.http.policy; -import com.azure.core.http.HttpHeader; -import com.azure.core.http.HttpHeaders; -import com.azure.core.http.HttpRequest; 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; -import java.util.function.Supplier; /** - * The pipeline policy that adds request id header in {@link HttpRequest} once. These id does not change - * when request is retried. Azure uses the request id as the unique identifier for the request. - * Example of these headers are 'x-ms-client-request-id' and 'x-ms-correlation-request-id'. + * The pipeline policy that puts a UUID in the request header. Azure uses the request id as + * the unique identifier for the request. */ public class RequestIdPolicy implements HttpPipelinePolicy { private static final String REQUEST_ID_HEADER = "x-ms-client-request-id"; - private final Supplier requestIdSupplier; - - /** - * Creates default {@link RequestIdPolicy}. - */ - public RequestIdPolicy() { - requestIdSupplier = () -> new HttpHeaders().put(REQUEST_ID_HEADER, UUID.randomUUID().toString()); - } - - /** - * Creates {@link RequestIdPolicy} with provided {@link Supplier} to dynamically generate request id for each - * {@link HttpRequest}. - * - * @param requestIdSupplier to dynamically generate to request id for each {@link HttpRequest}. It is suggested - * that this {@link Supplier} provides unique value every time it is called. - * Example of these headers are 'x-ms-client-request-id', 'x-ms-correlation-request-id'. - * - * @throws NullPointerException when {@code requestIdSupplier} is {@code null}. - */ - public RequestIdPolicy(Supplier requestIdSupplier) { - this.requestIdSupplier = Objects.requireNonNull(requestIdSupplier, "'requestIdSupplier' must not be null"); - } @Override public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { - - HttpHeaders httpHeaders = requestIdSupplier.get(); - if (Objects.nonNull(httpHeaders) && httpHeaders.getSize() > 0) { - for (HttpHeader header : httpHeaders) { - String requestIdHeaderValue = context.getHttpRequest().getHeaders().getValue(header.getName()); - if (requestIdHeaderValue == null) { - context.getHttpRequest().getHeaders().put(header.getName(), header.getValue()); - } - } - return next.process(); - } - - // If we were not able to set client provided Request ID header, we will set default 'REQUEST_ID_HEADER'. String requestId = context.getHttpRequest().getHeaders().getValue(REQUEST_ID_HEADER); if (requestId == null) { context.getHttpRequest().getHeaders().put(REQUEST_ID_HEADER, UUID.randomUUID().toString()); @@ -67,3 +26,4 @@ public Mono process(HttpPipelineCallContext context, HttpPipelineN return next.process(); } } + diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java index 79c44467cdcc..8225182df2a5 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java @@ -20,8 +20,6 @@ import java.nio.charset.Charset; import java.time.Duration; import java.time.temporal.ChronoUnit; -import java.util.UUID; -import java.util.function.Supplier; public class RequestIdPolicyTests { private final HttpResponse mockResponse = new HttpResponse(null) { @@ -115,37 +113,4 @@ public Mono send(HttpRequest request) { pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); } - - @Test - public void clientProvidedRequestIdForRetry() throws Exception { - - String customRequestIdHeaderName = "x-ms-client-custom-request-id"; - String clientProvidedRequestId = UUID.randomUUID().toString(); - - Supplier requestIdSupplier = () -> new HttpHeaders().put(customRequestIdHeaderName, clientProvidedRequestId); - final HttpPipeline pipeline = new HttpPipelineBuilder() - .httpClient(new NoOpHttpClient() { - String firstRequestId = null; - - @Override - public Mono send(HttpRequest request) { - if (firstRequestId != null) { - String newRequestId = request.getHeaders().getValue(customRequestIdHeaderName); - Assertions.assertNotNull(newRequestId); - Assertions.assertEquals(newRequestId, firstRequestId); - Assertions.assertEquals(newRequestId, clientProvidedRequestId); - } - firstRequestId = request.getHeaders().getValue(customRequestIdHeaderName); - if (firstRequestId == null) { - Assertions.fail(); - } - return Mono.just(mockResponse); - } - }) - .policies(new RequestIdPolicy(requestIdSupplier), new RetryPolicy(new FixedDelay(1, Duration.of(0, ChronoUnit.SECONDS)))) - .build(); - - pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); - } - }