|
| 1 | +/* |
| 2 | + * ApplicationInsights-Java |
| 3 | + * Copyright (c) Microsoft Corporation |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * MIT License |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 8 | + * software and associated documentation files (the ""Software""), to deal in the Software |
| 9 | + * without restriction, including without limitation the rights to use, copy, modify, merge, |
| 10 | + * publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 11 | + * persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 13 | + * substantial portions of the Software. |
| 14 | + * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 17 | + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 18 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | + * DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.microsoft.applicationinsights.internal.authentication; |
| 23 | + |
| 24 | +import com.azure.core.http.HttpPipelineCallContext; |
| 25 | +import com.azure.core.http.HttpPipelineNextPolicy; |
| 26 | +import com.azure.core.http.HttpRequest; |
| 27 | +import com.azure.core.http.HttpResponse; |
| 28 | +import com.azure.core.http.policy.HttpPipelinePolicy; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | +import reactor.core.publisher.Mono; |
| 32 | + |
| 33 | +import java.net.HttpURLConnection; |
| 34 | + |
| 35 | +// This is a copy from Azure Monitor Open Telemetry Exporter SDK AzureMonitorRedirectPolicy |
| 36 | +public final class AzureMonitorRedirectPolicy implements HttpPipelinePolicy { |
| 37 | + |
| 38 | + private static final int PERMANENT_REDIRECT_STATUS_CODE = 308; |
| 39 | + private static final int TEMP_REDIRECT_STATUS_CODE = 307; |
| 40 | + // Based on Stamp specific redirects design doc |
| 41 | + private static final int MAX_REDIRECT_RETRIES = 10; |
| 42 | + private static final Logger logger = LoggerFactory.getLogger(AzureMonitorRedirectPolicy.class); |
| 43 | + private volatile String redirectedEndpointUrl; |
| 44 | + |
| 45 | + @Override |
| 46 | + public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { |
| 47 | + return attemptRetry(context, next, context.getHttpRequest(), 0); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Function to process through the HTTP Response received in the pipeline |
| 52 | + * and retry sending the request with new redirect url. |
| 53 | + */ |
| 54 | + private Mono<HttpResponse> attemptRetry(final HttpPipelineCallContext context, |
| 55 | + final HttpPipelineNextPolicy next, |
| 56 | + final HttpRequest originalHttpRequest, |
| 57 | + final int retryCount) { |
| 58 | + // make sure the context is not modified during retry, except for the URL |
| 59 | + context.setHttpRequest(originalHttpRequest.copy()); |
| 60 | + if (this.redirectedEndpointUrl != null) { |
| 61 | + context.getHttpRequest().setUrl(this.redirectedEndpointUrl); |
| 62 | + } |
| 63 | + return next.clone().process() |
| 64 | + .flatMap(httpResponse -> { |
| 65 | + if (shouldRetryWithRedirect(httpResponse.getStatusCode(), retryCount)) { |
| 66 | + String responseLocation = httpResponse.getHeaderValue("Location"); |
| 67 | + if (responseLocation != null) { |
| 68 | + this.redirectedEndpointUrl = responseLocation; |
| 69 | + return attemptRetry(context, next, originalHttpRequest, retryCount + 1); |
| 70 | + } |
| 71 | + } |
| 72 | + return Mono.just(httpResponse); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Determines if it's a valid retry scenario based on statusCode and tryCount. |
| 78 | + * |
| 79 | + * @param statusCode HTTP response status code |
| 80 | + * @param tryCount Redirect retries so far |
| 81 | + * @return True if statusCode corresponds to HTTP redirect response codes and redirect |
| 82 | + * retries is less than {@code MAX_REDIRECT_RETRIES}. |
| 83 | + */ |
| 84 | + private boolean shouldRetryWithRedirect(int statusCode, int tryCount) { |
| 85 | + if (tryCount >= MAX_REDIRECT_RETRIES) { |
| 86 | + logger.warn("Max redirect retries limit reached:{}.", MAX_REDIRECT_RETRIES); |
| 87 | + return false; |
| 88 | + } |
| 89 | + return statusCode == HttpURLConnection.HTTP_MOVED_TEMP |
| 90 | + || statusCode == HttpURLConnection.HTTP_MOVED_PERM |
| 91 | + || statusCode == PERMANENT_REDIRECT_STATUS_CODE |
| 92 | + || statusCode == TEMP_REDIRECT_STATUS_CODE; |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments