Skip to content

Commit b0afc5d

Browse files
authored
Merge pull request #964 from AzureAD/avdunn/status-code-constants
Consistently use constants for HTTP status codes
2 parents 1894fed + 0869edb commit b0afc5d

22 files changed

+94
-109
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AadInstanceDiscoveryProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ static AadInstanceDiscoveryResponse sendInstanceDiscoveryRequest(URL authorityUr
235235

236236
AadInstanceDiscoveryResponse response = JsonHelper.convertJsonStringToJsonSerializableObject(httpResponse.body(), AadInstanceDiscoveryResponse::fromJson);
237237

238-
if (httpResponse.statusCode() != HttpHelper.HTTP_STATUS_200) {
239-
if (httpResponse.statusCode() == HttpHelper.HTTP_STATUS_400 && response.error().equals("invalid_instance")) {
238+
if (httpResponse.statusCode() != HttpStatus.HTTP_OK) {
239+
if (httpResponse.statusCode() == HttpStatus.HTTP_BAD_REQUEST && response.error().equals("invalid_instance")) {
240240
// instance discovery failed due to an invalid authority, throw an exception.
241241
throw MsalServiceExceptionFactory.fromHttpResponse(httpResponse);
242242
}
@@ -310,7 +310,7 @@ static String discoverRegion(MsalRequest msalRequest, ServiceBundle serviceBundl
310310
log.info("Starting call to IMDS endpoint.");
311311
IHttpResponse httpResponse = future.get(IMDS_TIMEOUT, IMDS_TIMEOUT_UNIT);
312312
//If call to IMDS endpoint was successful, return region from response body
313-
if (httpResponse.statusCode() == HttpHelper.HTTP_STATUS_200 && !httpResponse.body().isEmpty()) {
313+
if (httpResponse.statusCode() == HttpStatus.HTTP_OK && !httpResponse.body().isEmpty()) {
314314
log.info(String.format("Region retrieved from IMDS endpoint: %s", httpResponse.body()));
315315
currentRequest.regionSource(RegionTelemetry.REGION_SOURCE_IMDS.telemetryValue);
316316

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AuthorizationResponseHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AuthorizationResponseHandler implements HttpHandler {
4141
public void handle(HttpExchange httpExchange) throws IOException {
4242
try {
4343
if (!httpExchange.getRequestURI().getPath().equalsIgnoreCase("/")) {
44-
httpExchange.sendResponseHeaders(200, 0);
44+
httpExchange.sendResponseHeaders(HttpStatus.HTTP_OK, 0);
4545
return;
4646
}
4747
String responseBody = new BufferedReader(new InputStreamReader(
@@ -92,13 +92,13 @@ private void sendErrorResponse(HttpExchange httpExchange, String response) throw
9292
private void send302Response(HttpExchange httpExchange, String redirectUri) throws IOException {
9393
Headers responseHeaders = httpExchange.getResponseHeaders();
9494
responseHeaders.set("Location", redirectUri);
95-
httpExchange.sendResponseHeaders(302, 0);
95+
httpExchange.sendResponseHeaders(HttpStatus.HTTP_FOUND, 0);
9696
}
9797

9898
private void send200Response(HttpExchange httpExchange, String response) throws IOException {
9999
byte[] responseBytes = response.getBytes("UTF-8");
100100
httpExchange.getResponseHeaders().set("Content-Type", "text/html; charset=UTF-8");
101-
httpExchange.sendResponseHeaders(200, responseBytes.length);
101+
httpExchange.sendResponseHeaders(HttpStatus.HTTP_OK, responseBytes.length);
102102
OutputStream os = httpExchange.getResponseBody();
103103
os.write(responseBytes);
104104
os.close();

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/DeviceCodeFlowRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ DeviceCode acquireDeviceCode(String url,
4646
this.requestContext(),
4747
serviceBundle);
4848

49-
if (response.statusCode() != HttpHelper.HTTP_STATUS_200) {
49+
if (response.statusCode() != HttpStatus.HTTP_OK) {
5050
throw MsalServiceExceptionFactory.fromHttpResponse(response);
5151
}
5252

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/HttpHelper.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ class HttpHelper implements IHttpHelper {
1919
private static final Logger log = LoggerFactory.getLogger(HttpHelper.class);
2020
public static final String RETRY_AFTER_HEADER = "Retry-After";
2121

22-
public static final int HTTP_STATUS_200 = 200;
23-
public static final int HTTP_STATUS_400 = 400;
24-
public static final int HTTP_STATUS_429 = 429;
25-
public static final int HTTP_STATUS_500 = 500;
26-
2722
private IHttpClient httpClient;
2823
private IRetryPolicy retryPolicy;
2924

@@ -179,8 +174,8 @@ private void processThrottlingInstructions(IHttpResponse httpResponse, RequestCo
179174
Integer retryAfterHeaderVal = getRetryAfterHeader(httpResponse);
180175
if (retryAfterHeaderVal != null) {
181176
expirationTimestamp = System.currentTimeMillis() + retryAfterHeaderVal * 1000;
182-
} else if (httpResponse.statusCode() == HTTP_STATUS_429 ||
183-
(httpResponse.statusCode() >= HTTP_STATUS_500)) {
177+
} else if (httpResponse.statusCode() == HttpStatus.HTTP_TOO_MANY_REQUESTS ||
178+
(httpResponse.statusCode() >= HttpStatus.HTTP_INTERNAL_ERROR)) {
184179

185180
expirationTimestamp = System.currentTimeMillis() + ThrottlingCache.DEFAULT_THROTTLING_TIME_SEC * 1000;
186181
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/HttpStatus.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,26 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
enum HttpStatus {
7-
OK(200, "OK"),
8-
FOUND(302, "Found"),
9-
BAD_REQUEST(400, "Bad Request"),
10-
NOT_FOUND(404, "Not Found"),
11-
REQUEST_TIMEOUT(408, "Request Timeout"),
12-
GONE(410, "Gone"),
13-
TOO_MANY_REQUESTS(429, "Too Many Requests"),
14-
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
15-
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
16-
GATEWAY_TIMEOUT(504, "Gateway Timeout");
6+
class HttpStatus {
177

18-
private final int code;
19-
private final String description;
8+
static final int HTTP_OK = 200;
9+
static final int HTTP_FOUND = 302;
10+
static final int HTTP_BAD_REQUEST = 400;
11+
static final int HTTP_UNAUTHORIZED = 401;
12+
static final int HTTP_NOT_FOUND = 404;
13+
static final int HTTP_REQUEST_TIMEOUT = 408;
14+
static final int HTTP_GONE = 410;
15+
static final int HTTP_TOO_MANY_REQUESTS = 429;
16+
static final int HTTP_INTERNAL_ERROR = 500;
17+
static final int HTTP_UNAVAILABLE = 503;
18+
static final int HTTP_GATEWAY_TIMEOUT = 504;
2019

21-
HttpStatus(int code, String description) {
22-
this.code = code;
23-
this.description = description;
24-
}
25-
26-
int getCode() {
27-
return code;
28-
}
29-
30-
String getDescription() {
31-
return description;
32-
}
33-
34-
//All 5xx errors
20+
/**
21+
* Determines if the status code represents a server error (5xx).
22+
*
23+
* @param code The HTTP status code
24+
* @return true if the status code is between 500 and 599, inclusive
25+
*/
3526
static boolean isServerError(int code) {
3627
return code >= 500 && code < 600;
3728
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/IMDSRetryPolicy.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class IMDSRetryPolicy extends ManagedIdentityRetryPolicy {
2323

2424
private static final Set<Integer> RETRYABLE_STATUS_CODES = Collections.unmodifiableSet(
2525
new HashSet<>(Arrays.asList(
26-
HttpStatus.NOT_FOUND.getCode(),
27-
HttpStatus.REQUEST_TIMEOUT.getCode(),
28-
HttpStatus.GONE.getCode(),
29-
HttpStatus.TOO_MANY_REQUESTS.getCode()
26+
HttpStatus.HTTP_NOT_FOUND,
27+
HttpStatus.HTTP_REQUEST_TIMEOUT,
28+
HttpStatus.HTTP_GONE,
29+
HttpStatus.HTTP_TOO_MANY_REQUESTS
3030
))
3131
);
3232

@@ -40,13 +40,13 @@ public boolean isRetryable(IHttpResponse httpResponse) {
4040

4141
@Override
4242
public int getMaxRetryCount(IHttpResponse httpResponse) {
43-
return (httpResponse.statusCode() == HttpStatus.GONE.getCode()) ? LINEAR_RETRY_NUM : EXPONENTIAL_RETRY_NUM;
43+
return (httpResponse.statusCode() == HttpStatus.HTTP_GONE) ? LINEAR_RETRY_NUM : EXPONENTIAL_RETRY_NUM;
4444
}
4545

4646
@Override
4747
public int getRetryDelayMs(IHttpResponse httpResponse) {
4848
// Use exponential backoff for non-410 status codes
49-
if (lastStatusCode == HttpStatus.GONE.getCode()) {
49+
if (lastStatusCode == HttpStatus.HTTP_GONE) {
5050
return currentLinearRetryDelayMs;
5151
} else {
5252
return (int) (Math.pow(2, currentRetryCount) * exponentialLinearRetryDelayMs);

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ManagedIdentityRetryPolicy.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class ManagedIdentityRetryPolicy implements IRetryPolicy {
1919

2020
private static final Set<Integer> RETRYABLE_STATUS_CODES = Collections.unmodifiableSet(
2121
new HashSet<>(Arrays.asList(
22-
HttpStatus.NOT_FOUND.getCode(),
23-
HttpStatus.REQUEST_TIMEOUT.getCode(),
24-
HttpStatus.TOO_MANY_REQUESTS.getCode(),
25-
HttpStatus.INTERNAL_SERVER_ERROR.getCode(),
26-
HttpStatus.SERVICE_UNAVAILABLE.getCode(),
27-
HttpStatus.GATEWAY_TIMEOUT.getCode()
22+
HttpStatus.HTTP_NOT_FOUND,
23+
HttpStatus.HTTP_REQUEST_TIMEOUT,
24+
HttpStatus.HTTP_TOO_MANY_REQUESTS,
25+
HttpStatus.HTTP_INTERNAL_ERROR,
26+
HttpStatus.HTTP_UNAVAILABLE,
27+
HttpStatus.HTTP_GATEWAY_TIMEOUT
2828
))
2929
);
3030

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/OidcDiscoveryProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static OidcDiscoveryResponse performOidcDiscovery(OidcAuthority authority, Abstr
1414

1515
OidcDiscoveryResponse response = JsonHelper.convertJsonStringToJsonSerializableObject(httpResponse.body(), OidcDiscoveryResponse::fromJson);
1616

17-
if (httpResponse.statusCode() != HttpHelper.HTTP_STATUS_200) {
17+
if (httpResponse.statusCode() != HttpStatus.HTTP_OK) {
1818
throw MsalServiceExceptionFactory.fromHttpResponse(httpResponse);
1919
}
2020

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/TokenRequestExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private AuthenticationResult createAuthenticationResultFromOauthHttpResponse(
167167

168168
} else {
169169
// http codes indicating that STS did not log request
170-
if (oauthHttpResponse.getStatusCode() == HttpHelper.HTTP_STATUS_429 || oauthHttpResponse.getStatusCode() >= HttpHelper.HTTP_STATUS_500) {
170+
if (oauthHttpResponse.getStatusCode() == HttpStatus.HTTP_TOO_MANY_REQUESTS || oauthHttpResponse.getStatusCode() >= HttpStatus.HTTP_INTERNAL_ERROR) {
171171
serviceBundle.getServerSideTelemetry().previousRequests.putAll(
172172
serviceBundle.getServerSideTelemetry().previousRequestInProgress);
173173
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/UserDiscoveryRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static UserDiscoveryResponse execute(
2929
HttpRequest httpRequest = new HttpRequest(HttpMethod.GET, uri, headers);
3030
IHttpResponse response = serviceBundle.getHttpHelper().executeHttpRequest(httpRequest, requestContext, serviceBundle);
3131

32-
if (response.statusCode() != HttpHelper.HTTP_STATUS_200) {
32+
if (response.statusCode() != HttpStatus.HTTP_OK) {
3333
throw MsalServiceExceptionFactory.fromHttpResponse(response);
3434
}
3535

0 commit comments

Comments
 (0)