Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public T correlationId(String val) {
super.serviceBundle = new ServiceBundle(
builder.executorService,
new TelemetryManager(telemetryConsumer, builder.onlySendFailureTelemetry),
new HttpHelper(this, new DefaultRetryPolicy())
new ThrottlingRequestChain(this, new DefaultRetryPolicy())
);

if (aadAadInstanceDiscoveryResponse != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.aad.msal4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Correlation-id link of the HTTP request chain: verifies that the correlation id sent with the
* request matches the one returned in the response, once retries have been exhausted.
*/
class CorrelationIdRequestChain implements IRequestChain {
private static final Logger LOG = LoggerFactory.getLogger(CorrelationIdRequestChain.class);

private final IRequestChain next;

CorrelationIdRequestChain(IRequestChain next) {
this.next = next;
}

@Override
public IHttpResponse executeHttpRequest(HttpRequest httpRequest, RequestContext requestContext, ServiceBundle serviceBundle)
throws Exception {
IHttpResponse httpResponse = next.executeHttpRequest(httpRequest, requestContext, serviceBundle);

if (httpResponse.headers() != null) {
verifyReturnedCorrelationId(httpRequest, httpResponse);
}

return httpResponse;
}

private static void verifyReturnedCorrelationId(final HttpRequest httpRequest,
IHttpResponse httpResponse) {

String sentCorrelationId = httpRequest.headerValue(
HttpHeaders.CORRELATION_ID_HEADER_NAME);

String returnedCorrelationId = HttpUtils.headerValue(
httpResponse.headers(),
HttpHeaders.CORRELATION_ID_HEADER_NAME);

if (StringHelper.isBlank(returnedCorrelationId) ||
!returnedCorrelationId.equals(sentCorrelationId)) {

String msg = LogHelper.createMessage(
String.format(
"Sent (%s) Correlation Id is not same as received (%s).",
sentCorrelationId,
returnedCorrelationId),
sentCorrelationId);

LOG.info(msg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DefaultRetryPolicy implements IRetryPolicy {
@Override
public boolean isRetryable(IHttpResponse httpResponse) {
return HttpStatus.isServerError(httpResponse.statusCode()) &&
HttpHelper.getRetryAfterHeader(httpResponse) == null;
ThrottlingRequestChain.getRetryAfterHeader(httpResponse) == null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.aad.msal4j;

import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import javax.net.ssl.SSLException;

/**
* Default policy for retrying HTTP requests that failed due to a network-level exception.
*/
class DefaultRetryableExceptionPolicy implements IRetryableExceptionPolicy {
private static final int RETRY_NUM = 1;
private static final int RETRY_DELAY_MS = 1000;

@Override
public boolean isRetryable(Exception exception) {
if (exception instanceof SSLException || exception instanceof UnknownHostException) {
return false;
}
return exception instanceof SocketException || exception instanceof SocketTimeoutException;
}

@Override
public int getMaxRetryCount(Exception exception) {
return RETRY_NUM;
}

@Override
public int getRetryDelayMs(Exception exception) {
return RETRY_DELAY_MS;
}
}
Loading