Skip to content

Commit b47a305

Browse files
authored
[Key Vault] Added null checks for HttpResponseException.getResponse() (#47801)
* Added null checks for HttpResponseException.getResponse() * Updated CHANGELOGs
1 parent 9d3a743 commit b47a305

File tree

13 files changed

+43
-29
lines changed

13 files changed

+43
-29
lines changed

sdk/keyvault/azure-security-keyvault-administration/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fixed an issue where certain `HttpResponseException.getResponse()` calls could cause a `NullPointerException`. ([#47801](https://github.com/Azure/azure-sdk-for-java/issues/47801))
12+
1113
### Other Changes
1214

1315
## 4.7.4 (2025-10-27)

sdk/keyvault/azure-security-keyvault-administration/src/main/java/com/azure/security/keyvault/administration/KeyVaultAdministrationUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static <E extends HttpResponseException> Response<Void> swallowExceptionForStatu
9696

9797
HttpResponse httpResponse = httpResponseException.getResponse();
9898

99-
if (httpResponse.getStatusCode() == statusCode) {
99+
if (httpResponse != null && httpResponse.getStatusCode() == statusCode) {
100100
return new SimpleResponse<>(httpResponse.getRequest(), httpResponse.getStatusCode(),
101101
httpResponse.getHeaders(), null);
102102
}

sdk/keyvault/azure-security-keyvault-certificates/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fixed an issue where certain `HttpResponseException.getResponse()` calls could cause a `NullPointerException`. ([#47801](https://github.com/Azure/azure-sdk-for-java/issues/47801))
12+
1113
### Other Changes
1214

1315
## 4.8.4 (2025-10-27)

sdk/keyvault/azure-security-keyvault-certificates/src/main/java/com/azure/security/keyvault/certificates/CertificateAsyncClient.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ private Mono<CertificateOperation> createCertificateActivation(String certificat
410410
* @return A {@link ResourceModifiedException} created from the {@link HttpResponseException}.
411411
*/
412412
static HttpResponseException mapCreateCertificateException(HttpResponseException e) {
413-
return e.getResponse().getStatusCode() == 400
413+
return e.getResponse() != null && e.getResponse().getStatusCode() == 400
414414
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
415415
: e;
416416
}
@@ -430,7 +430,7 @@ private Mono<PollResponse<CertificateOperation>> certificatePollOperation(String
430430
* @return A {@link ResourceModifiedException} created from the {@link HttpResponseException}.
431431
*/
432432
static HttpResponseException mapGetCertificateOperationException(HttpResponseException e) {
433-
return e.getResponse().getStatusCode() == 400
433+
return e.getResponse() != null && e.getResponse().getStatusCode() == 400
434434
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
435435
: e;
436436
}
@@ -472,7 +472,7 @@ private Mono<CertificateOperation> certificateCancellationOperation(String certi
472472
}
473473

474474
static HttpResponseException mapUpdateCertificateOperationException(HttpResponseException e) {
475-
return e.getResponse().getStatusCode() == 400
475+
return e.getResponse() != null && e.getResponse().getStatusCode() == 400
476476
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
477477
: e;
478478
}
@@ -486,7 +486,7 @@ private Mono<KeyVaultCertificateWithPolicy> fetchCertificateOperation(String cer
486486
}
487487

488488
static HttpResponseException mapGetCertificateException(HttpResponseException e) {
489-
return e.getResponse().getStatusCode() == 403
489+
return e.getResponse() != null && e.getResponse().getStatusCode() == 403
490490
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
491491
: e;
492492
}
@@ -813,7 +813,7 @@ private Mono<DeletedCertificate> deleteCertificateActivation(String certificateN
813813
}
814814

815815
static HttpResponseException mapDeleteCertificateException(HttpResponseException e) {
816-
return e.getResponse().getStatusCode() == 404
816+
return e.getResponse() != null && e.getResponse().getStatusCode() == 404
817817
? new ResourceNotFoundException(e.getMessage(), e.getResponse(), e.getValue())
818818
: e;
819819
}
@@ -825,7 +825,7 @@ private Mono<PollResponse<DeletedCertificate>> deleteCertificatePollOperation(St
825825
.map(binaryData -> new PollResponse<>(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED,
826826
createDeletedCertificate(binaryData.toObject(DeletedCertificateBundle.class))))
827827
.onErrorResume(HttpResponseException.class, e -> {
828-
if (e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
828+
if (e.getResponse() != null && e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
829829
return Mono.just(new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS,
830830
pollingContext.getLatestResponse().getValue()));
831831
} else {
@@ -1021,7 +1021,7 @@ private Mono<PollResponse<KeyVaultCertificateWithPolicy>> recoverDeletedCertific
10211021
.map(binaryData -> new PollResponse<>(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED,
10221022
createCertificateWithPolicy(binaryData.toObject(CertificateBundle.class))))
10231023
.onErrorResume(HttpResponseException.class, e -> {
1024-
if (e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
1024+
if (e.getResponse() != null && e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
10251025
return Mono.just(new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS,
10261026
pollingContext.getLatestResponse().getValue()));
10271027
} else {
@@ -1167,7 +1167,7 @@ public Mono<Response<KeyVaultCertificateWithPolicy>> restoreCertificateBackupWit
11671167
}
11681168

11691169
static HttpResponseException mapRestoreCertificateException(HttpResponseException e) {
1170-
return e.getResponse().getStatusCode() == 400
1170+
return e.getResponse() != null && e.getResponse().getStatusCode() == 400
11711171
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
11721172
: e;
11731173
}
@@ -1523,7 +1523,7 @@ public Mono<Response<CertificatePolicy>> getCertificatePolicyWithResponse(String
15231523
}
15241524

15251525
static HttpResponseException mapGetCertificatePolicyException(HttpResponseException e) {
1526-
return e.getResponse().getStatusCode() == 403
1526+
return e.getResponse() != null && e.getResponse().getStatusCode() == 403
15271527
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
15281528
: e;
15291529
}
@@ -2123,7 +2123,7 @@ public Mono<Response<CertificateOperation>> deleteCertificateOperationWithRespon
21232123
}
21242124

21252125
static HttpResponseException mapDeleteCertificateOperationException(HttpResponseException e) {
2126-
return e.getResponse().getStatusCode() == 400
2126+
return e.getResponse() != null && e.getResponse().getStatusCode() == 400
21272127
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
21282128
: e;
21292129
}

sdk/keyvault/azure-security-keyvault-certificates/src/main/java/com/azure/security/keyvault/certificates/CertificateClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ private PollResponse<DeletedCertificate> deleteCertificatePollOperation(String c
708708
.getValue()
709709
.toObject(DeletedCertificateBundle.class)));
710710
} catch (HttpResponseException e) {
711-
if (e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
711+
if (e.getResponse() != null && e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
712712
return new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS,
713713
pollingContext.getLatestResponse().getValue());
714714
} else {
@@ -896,7 +896,7 @@ private PollResponse<KeyVaultCertificateWithPolicy> recoverDeletedCertificatePol
896896
.getValue()
897897
.toObject(CertificateBundle.class)));
898898
} catch (HttpResponseException e) {
899-
if (e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
899+
if (e.getResponse() != null && e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
900900
return new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS,
901901
pollingContext.getLatestResponse().getValue());
902902
} else {

sdk/keyvault/azure-security-keyvault-keys/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fixed an issue where certain `HttpResponseException.getResponse()` calls could cause a `NullPointerException`. ([#47801](https://github.com/Azure/azure-sdk-for-java/issues/47801))
12+
1113
### Other Changes
1214

1315
## 4.10.4 (2025-10-27)

sdk/keyvault/azure-security-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyAsyncClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public Mono<Response<KeyVaultKey>> createKeyWithResponse(CreateKeyOptions create
365365
}
366366

367367
static HttpResponseException mapCreateKeyException(HttpResponseException e) {
368-
return (e.getResponse().getStatusCode() == 400)
368+
return (e.getResponse() != null && e.getResponse().getStatusCode() == 400)
369369
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
370370
: e;
371371
}
@@ -983,7 +983,7 @@ public Mono<Response<KeyVaultKey>> getKeyWithResponse(String name, String versio
983983
* @return The {@link HttpResponseException} that maps from the {@link HttpResponseException}.
984984
*/
985985
static HttpResponseException mapGetKeyException(HttpResponseException e) {
986-
return e.getResponse().getStatusCode() == 403
986+
return e.getResponse() != null && e.getResponse().getStatusCode() == 403
987987
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
988988
: e;
989989
}
@@ -1177,7 +1177,7 @@ private Function<PollingContext<DeletedKey>, Mono<PollResponse<DeletedKey>>> del
11771177
.map(response -> new PollResponse<>(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED,
11781178
createDeletedKey(response.getValue().toObject(DeletedKeyBundle.class))))
11791179
.onErrorResume(HttpResponseException.class, e -> {
1180-
if (e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
1180+
if (e.getResponse() != null && e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
11811181
return Mono.just(new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS,
11821182
pollingContext.getLatestResponse().getValue()));
11831183
} else {
@@ -1364,7 +1364,7 @@ private Function<PollingContext<KeyVaultKey>, Mono<KeyVaultKey>> recoverActivati
13641364
.map(response -> new PollResponse<>(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED,
13651365
createKeyVaultKey(response.getValue().toObject(KeyBundle.class))))
13661366
.onErrorResume(HttpResponseException.class, e -> {
1367-
if (e.getResponse().getStatusCode() == 404) {
1367+
if (e.getResponse() != null && e.getResponse().getStatusCode() == 404) {
13681368
return Mono.just(new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS,
13691369
pollingContext.getLatestResponse().getValue()));
13701370
} else {
@@ -1548,7 +1548,7 @@ public Mono<Response<KeyVaultKey>> restoreKeyBackupWithResponse(byte[] backup) {
15481548
}
15491549

15501550
static HttpResponseException mapRestoreKeyException(HttpResponseException e) {
1551-
return (e.getResponse().getStatusCode() == 400)
1551+
return (e.getResponse() != null && e.getResponse().getStatusCode() == 400)
15521552
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
15531553
: e;
15541554
}

sdk/keyvault/azure-security-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ private Function<PollingContext<DeletedKey>, PollResponse<DeletedKey>> deletePol
11041104
.getValue()
11051105
.toObject(DeletedKeyBundle.class)));
11061106
} catch (HttpResponseException e) {
1107-
if (e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
1107+
if (e.getResponse() != null && e.getResponse().getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
11081108
return new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS,
11091109
pollingContext.getLatestResponse().getValue());
11101110
} else {
@@ -1285,7 +1285,7 @@ private Function<PollingContext<KeyVaultKey>, PollResponse<KeyVaultKey>> recover
12851285
return new PollResponse<>(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, createKeyVaultKey(
12861286
implClient.getKeyWithResponse(keyName, null, EMPTY_OPTIONS).getValue().toObject(KeyBundle.class)));
12871287
} catch (HttpResponseException e) {
1288-
if (e.getResponse().getStatusCode() == 404) {
1288+
if (e.getResponse() != null && e.getResponse().getStatusCode() == 404) {
12891289
return new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS,
12901290
pollingContext.getLatestResponse().getValue());
12911291
} else {

sdk/keyvault/azure-security-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/cryptography/implementation/CryptographyClientImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public String getKeyCollection() {
9393
public Mono<Response<KeyVaultKey>> getKeyAsync() {
9494
return keyClient.getKeyWithResponseAsync(keyName, keyVersion, EMPTY_OPTIONS)
9595
.onErrorMap(HttpResponseException.class,
96-
e -> e.getResponse().getStatusCode() == 403
96+
e -> e.getResponse() != null && e.getResponse().getStatusCode() == 403
9797
? new ResourceModifiedException(e.getMessage(), e.getResponse(), e.getValue())
9898
: e)
9999
.map(response -> new SimpleResponse<>(response,

sdk/keyvault/azure-security-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/cryptography/implementation/CryptographyUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ public static void verifyKeyPermissions(JsonWebKey jsonWebKey, KeyOperation keyO
152152

153153
public static boolean isThrowableRetryable(Throwable e) {
154154
if (e instanceof HttpResponseException) {
155-
int statusCode = ((HttpResponseException) e).getResponse().getStatusCode();
155+
HttpResponseException httpResponseException = (HttpResponseException) e;
156+
157+
if (httpResponseException.getResponse() == null) {
158+
return false;
159+
}
160+
161+
int statusCode = httpResponseException.getResponse().getStatusCode();
156162

157163
// Not a retriable error code.
158164
return statusCode != 501

0 commit comments

Comments
 (0)