Skip to content

Commit 0644e56

Browse files
authored
Modified logic for request condition validation to throw after determining all invalid conditions (Azure#22532)
1 parent 08a7248 commit 0644e56

10 files changed

+244
-130
lines changed

sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/implementation/util/BlobRequestConditionProperty.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,44 @@
33

44
package com.azure.storage.blob.implementation.util;
55

6+
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonValue;
8+
69
public enum BlobRequestConditionProperty {
7-
LEASE_ID,
8-
TAGS_CONDITIONS,
9-
IF_MODIFIED_SINCE,
10-
IF_UNMODIFIED_SINCE,
11-
IF_MATCH,
12-
IF_NONE_MATCH;
10+
LEASE_ID("LeaseId"),
11+
TAGS_CONDITIONS("TagsConditions"),
12+
IF_MODIFIED_SINCE("IfModifiedSince"),
13+
IF_UNMODIFIED_SINCE("IfUnmodifiedSince"),
14+
IF_MATCH("IfMatch"),
15+
IF_NONE_MATCH("IfNoneMatch");
16+
17+
/** The actual serialized value for a BlobRequestConditionProperty instance. */
18+
private final String value;
19+
20+
BlobRequestConditionProperty(String value) {
21+
this.value = value;
22+
}
23+
24+
/**
25+
* Parses a serialized value to a BlobRequestConditionProperty instance.
26+
*
27+
* @param value the serialized value to parse.
28+
* @return the parsed BlobRequestConditionProperty object, or null if unable to parse.
29+
*/
30+
@JsonCreator
31+
public static BlobRequestConditionProperty fromString(String value) {
32+
BlobRequestConditionProperty[] items = BlobRequestConditionProperty.values();
33+
for (BlobRequestConditionProperty item : items) {
34+
if (item.toString().equalsIgnoreCase(value)) {
35+
return item;
36+
}
37+
}
38+
return null;
39+
}
40+
41+
@JsonValue
42+
@Override
43+
public String toString() {
44+
return this.value;
45+
}
1346
}

sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/implementation/util/ModelHelper.java

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -504,37 +504,65 @@ public static BlobQueryHeaders transformQueryHeaders(HttpHeaders headers) {
504504
}
505505

506506
public static void validateConditionsNotPresent(BlobRequestConditions requestConditions,
507-
EnumSet<BlobRequestConditionProperty> invalidConditions) {
507+
EnumSet<BlobRequestConditionProperty> invalidConditions, String operationName, String parameterName) {
508508
if (requestConditions == null) {
509509
return;
510510
}
511-
if (invalidConditions.contains(BlobRequestConditionProperty.LEASE_ID)
512-
&& requestConditions.getLeaseId() != null) {
513-
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'leaseId' is not applicable to this API."));
514-
}
515-
if (invalidConditions.contains(BlobRequestConditionProperty.TAGS_CONDITIONS)
516-
&& requestConditions.getTagsConditions() != null) {
517-
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'tagsConditions' is not applicable to "
518-
+ "this API."));
519-
}
520-
if (invalidConditions.contains(BlobRequestConditionProperty.IF_MODIFIED_SINCE)
521-
&& requestConditions.getIfModifiedSince() != null) {
522-
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifModifiedSince' is not applicable to "
523-
+ "this API."));
524-
}
525-
if (invalidConditions.contains(BlobRequestConditionProperty.IF_UNMODIFIED_SINCE)
526-
&& requestConditions.getIfUnmodifiedSince() != null) {
527-
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifUnmodifiedSince' is not applicable to "
528-
+ "this API."));
529-
}
530-
if (invalidConditions.contains(BlobRequestConditionProperty.IF_MATCH)
531-
&& requestConditions.getIfMatch() != null) {
532-
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifMatch' is not applicable to this API."));
511+
List<String> invalidConditionsFound = null;
512+
513+
for (BlobRequestConditionProperty condition : invalidConditions) {
514+
switch (condition) {
515+
case LEASE_ID:
516+
if (requestConditions.getLeaseId() != null) {
517+
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
518+
: invalidConditionsFound;
519+
invalidConditionsFound.add(BlobRequestConditionProperty.LEASE_ID.toString());
520+
}
521+
break;
522+
case TAGS_CONDITIONS:
523+
if (requestConditions.getTagsConditions() != null) {
524+
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
525+
: invalidConditionsFound;
526+
invalidConditionsFound.add(BlobRequestConditionProperty.TAGS_CONDITIONS.toString());
527+
}
528+
break;
529+
case IF_MODIFIED_SINCE:
530+
if (requestConditions.getIfModifiedSince() != null) {
531+
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
532+
: invalidConditionsFound;
533+
invalidConditionsFound.add(BlobRequestConditionProperty.IF_MODIFIED_SINCE.toString());
534+
}
535+
break;
536+
case IF_UNMODIFIED_SINCE:
537+
if (requestConditions.getIfUnmodifiedSince() != null) {
538+
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
539+
: invalidConditionsFound;
540+
invalidConditionsFound.add(BlobRequestConditionProperty.IF_UNMODIFIED_SINCE.toString());
541+
}
542+
break;
543+
case IF_MATCH:
544+
if (requestConditions.getIfMatch() != null) {
545+
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
546+
: invalidConditionsFound;
547+
invalidConditionsFound.add(BlobRequestConditionProperty.IF_MATCH.toString());
548+
}
549+
break;
550+
case IF_NONE_MATCH:
551+
if (requestConditions.getIfNoneMatch() != null) {
552+
invalidConditionsFound = invalidConditionsFound == null ? new ArrayList<>()
553+
: invalidConditionsFound;
554+
invalidConditionsFound.add(BlobRequestConditionProperty.IF_NONE_MATCH.toString());
555+
}
556+
break;
557+
default:
558+
break;
559+
}
533560
}
534-
if (invalidConditions.contains(BlobRequestConditionProperty.IF_NONE_MATCH)
535-
&& requestConditions.getIfNoneMatch() != null) {
536-
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifNoneMatch' is not applicable to this "
537-
+ "API."));
561+
if (invalidConditionsFound != null && !invalidConditionsFound.isEmpty()) {
562+
String unsupported = String.join(", ", invalidConditionsFound);
563+
throw LOGGER.logExceptionAsError(new IllegalArgumentException(
564+
String.format("%s does not support the %s request condition(s) for parameter '%s'.",
565+
operationName, unsupported, parameterName)));
538566
}
539567
}
540568
}

sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobAsyncClientBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2256,7 +2256,8 @@ Mono<Response<BlobImmutabilityPolicy>> setImmutabilityPolicyWithResponse(
22562256
ModelHelper.validateConditionsNotPresent(finalRequestConditions,
22572257
EnumSet.of(BlobRequestConditionProperty.LEASE_ID, BlobRequestConditionProperty.TAGS_CONDITIONS,
22582258
BlobRequestConditionProperty.IF_MATCH, BlobRequestConditionProperty.IF_NONE_MATCH,
2259-
BlobRequestConditionProperty.IF_MODIFIED_SINCE));
2259+
BlobRequestConditionProperty.IF_MODIFIED_SINCE), "setImmutabilityPolicy(WithResponse)",
2260+
"requestConditions");
22602261

22612262
return this.azureBlobStorage.getBlobs().setImmutabilityPolicyWithResponseAsync(containerName, blobName, null,
22622263
null, finalRequestConditions.getIfUnmodifiedSince(), finalImmutabilityPolicy.getExpiryTime(),

sdk/storage/azure-storage-blob/src/test/java/com/azure/storage/blob/ImmutableStorageWithVersioningTest.groovy

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,16 @@ class ImmutableStorageWithVersioningTest extends APISpec {
275275

276276
then:
277277
def e = thrown(IllegalArgumentException)
278-
e.getMessage() == wrongCondition + " is not applicable to this API."
278+
e.getMessage() == String.format("%s does not support the %s request condition(s) for parameter 'requestConditions'.", "setImmutabilityPolicy(WithResponse)", wrongCondition)
279279

280280
where:
281281
leaseId | tags | ifMatch | ifNoneMatch | ifModifiedSince || wrongCondition
282-
"leaseId" | null | null | null | null || "'leaseId'"
283-
null | "tagsConditions" | null | null | null || "'tagsConditions'"
284-
null | null | "ifMatch" | null | null || "'ifMatch'"
285-
null | null | null | "ifNoneMatch" | null || "'ifNoneMatch'"
286-
null | null | null | null | oldDate || "'ifModifiedSince'"
282+
"leaseId" | null | null | null | null || "LeaseId"
283+
null | "tagsConditions" | null | null | null || "TagsConditions"
284+
null | null | "ifMatch" | null | null || "IfMatch"
285+
null | null | null | "ifNoneMatch" | null || "IfNoneMatch"
286+
null | null | null | null | oldDate || "IfModifiedSince"
287+
"leaseId" | "tagsConditions" | "ifMatch" | "ifNoneMatch" | oldDate || "LeaseId, TagsConditions, IfModifiedSince, IfMatch, IfNoneMatch"
287288
}
288289

289290
def "set immutability policy error"() {
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
{
22
"networkCallRecords" : [ {
33
"Method" : "PUT",
4-
"Uri" : "https://REDACTED.blob.core.windows.net/1e8e8c1301e8e8c13d1e4448478653498e9004a219f2?restype=container",
4+
"Uri" : "https://REDACTED.blob.core.windows.net/1e8e8c1301e8e8c134c16197853efefaa66994939a49?restype=container",
55
"Headers" : {
66
"x-ms-version" : "2020-10-02",
7-
"User-Agent" : "azsdk-java-azure-storage-blob/12.12.0-beta.2 (11.0.7; Windows 10; 10.0)",
8-
"x-ms-client-request-id" : "da7835f2-2cb0-40eb-9f13-0e808de4b12f"
7+
"User-Agent" : "azsdk-java-azure-storage-blob/12.13.0-beta.1 (11.0.7; Windows 10; 10.0)",
8+
"x-ms-client-request-id" : "38ccfa4b-f382-457e-8fcb-488cfede7886"
99
},
1010
"Response" : {
1111
"Transfer-Encoding" : "chunked",
1212
"x-ms-version" : "2020-10-02",
1313
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
14-
"eTag" : "0x8D93288C91D6509",
15-
"Last-Modified" : "Fri, 18 Jun 2021 18:42:12 GMT",
14+
"eTag" : "0x8D9375C70795929",
15+
"Last-Modified" : "Thu, 24 Jun 2021 22:07:21 GMT",
1616
"retry-after" : "0",
1717
"StatusCode" : "201",
18-
"x-ms-request-id" : "fc34c3f0-f01e-0001-1571-649c3e000000",
19-
"x-ms-client-request-id" : "da7835f2-2cb0-40eb-9f13-0e808de4b12f",
20-
"Date" : "Fri, 18 Jun 2021 18:42:11 GMT"
18+
"x-ms-request-id" : "8dd6dbdf-401e-0021-1f45-69bb8d000000",
19+
"x-ms-client-request-id" : "38ccfa4b-f382-457e-8fcb-488cfede7886",
20+
"Date" : "Thu, 24 Jun 2021 22:07:20 GMT"
2121
},
2222
"Exception" : null
2323
}, {
2424
"Method" : "PUT",
25-
"Uri" : "https://REDACTED.blob.core.windows.net/1e8e8c1311e8e8c13d1e86956ffcc18240e2b4ad1b61/1e8e8c1321e8e8c13d1e55651a3205039d57541cc88c",
25+
"Uri" : "https://REDACTED.blob.core.windows.net/1e8e8c1311e8e8c134c1025670c7fcee3e5fd4a51807/1e8e8c1321e8e8c134c169613ee066555b8ed4e12bb0",
2626
"Headers" : {
2727
"x-ms-version" : "2020-10-02",
28-
"User-Agent" : "azsdk-java-azure-storage-blob/12.12.0-beta.2 (11.0.7; Windows 10; 10.0)",
29-
"x-ms-client-request-id" : "b96decb4-acca-4a6c-ada0-f82fe8090be7",
28+
"User-Agent" : "azsdk-java-azure-storage-blob/12.13.0-beta.1 (11.0.7; Windows 10; 10.0)",
29+
"x-ms-client-request-id" : "5b028f24-a557-4591-a57c-deca6a9d878b",
3030
"Content-Type" : "application/octet-stream"
3131
},
3232
"Response" : {
3333
"Transfer-Encoding" : "chunked",
3434
"x-ms-version" : "2020-10-02",
3535
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
3636
"x-ms-content-crc64" : "AAAAAAAAAAA=",
37-
"Last-Modified" : "Fri, 18 Jun 2021 18:42:15 GMT",
38-
"x-ms-version-id" : "2021-06-18T18:42:15.2590905Z",
37+
"Last-Modified" : "Thu, 24 Jun 2021 22:07:23 GMT",
38+
"x-ms-version-id" : "2021-06-24T22:07:23.7038470Z",
3939
"retry-after" : "0",
4040
"StatusCode" : "201",
4141
"x-ms-request-server-encrypted" : "true",
42-
"Date" : "Fri, 18 Jun 2021 18:42:15 GMT",
42+
"Date" : "Thu, 24 Jun 2021 22:07:22 GMT",
4343
"Content-MD5" : "1B2M2Y8AsgTpgAmY7PhCfg==",
44-
"eTag" : "0x8D93288CB03A639",
45-
"x-ms-request-id" : "fc34c401-f01e-0001-2271-649c3e000000",
46-
"x-ms-client-request-id" : "b96decb4-acca-4a6c-ada0-f82fe8090be7"
44+
"eTag" : "0x8D9375C71E5C586",
45+
"x-ms-request-id" : "8dd6dbf3-401e-0021-2c45-69bb8d000000",
46+
"x-ms-client-request-id" : "5b028f24-a557-4591-a57c-deca6a9d878b"
4747
},
4848
"Exception" : null
4949
} ],
50-
"variables" : [ "1e8e8c1301e8e8c13d1e4448478653498e9004a219f2", "1e8e8c1311e8e8c13d1e86956ffcc18240e2b4ad1b61", "1e8e8c1321e8e8c13d1e55651a3205039d57541cc88c", "2021-06-18T18:42:16.251301Z" ]
50+
"variables" : [ "1e8e8c1301e8e8c134c16197853efefaa66994939a49", "1e8e8c1311e8e8c134c1025670c7fcee3e5fd4a51807", "1e8e8c1321e8e8c134c169613ee066555b8ed4e12bb0", "2021-06-24T22:07:24.781735500Z" ]
5151
}
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
{
22
"networkCallRecords" : [ {
33
"Method" : "PUT",
4-
"Uri" : "https://REDACTED.blob.core.windows.net/0795bd5200795bd5264a871964e42487c620d4113860?restype=container",
4+
"Uri" : "https://REDACTED.blob.core.windows.net/0795bd5200795bd52d4e792326e09af9e30304f7c95d?restype=container",
55
"Headers" : {
66
"x-ms-version" : "2020-10-02",
7-
"User-Agent" : "azsdk-java-azure-storage-blob/12.12.0-beta.2 (11.0.7; Windows 10; 10.0)",
8-
"x-ms-client-request-id" : "57037666-1f95-4d07-9930-802b4c50fce4"
7+
"User-Agent" : "azsdk-java-azure-storage-blob/12.13.0-beta.1 (11.0.7; Windows 10; 10.0)",
8+
"x-ms-client-request-id" : "fc52422a-ff7c-4fc6-b325-b5fec7607ace"
99
},
1010
"Response" : {
1111
"Transfer-Encoding" : "chunked",
1212
"x-ms-version" : "2020-10-02",
1313
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
14-
"eTag" : "0x8D93288CC0E4AE6",
15-
"Last-Modified" : "Fri, 18 Jun 2021 18:42:17 GMT",
14+
"eTag" : "0x8D9375C72E47FA9",
15+
"Last-Modified" : "Thu, 24 Jun 2021 22:07:25 GMT",
1616
"retry-after" : "0",
1717
"StatusCode" : "201",
18-
"x-ms-request-id" : "fc34c419-f01e-0001-3671-649c3e000000",
19-
"x-ms-client-request-id" : "57037666-1f95-4d07-9930-802b4c50fce4",
20-
"Date" : "Fri, 18 Jun 2021 18:42:16 GMT"
18+
"x-ms-request-id" : "8dd6dbfb-401e-0021-3245-69bb8d000000",
19+
"x-ms-client-request-id" : "fc52422a-ff7c-4fc6-b325-b5fec7607ace",
20+
"Date" : "Thu, 24 Jun 2021 22:07:25 GMT"
2121
},
2222
"Exception" : null
2323
}, {
2424
"Method" : "PUT",
25-
"Uri" : "https://REDACTED.blob.core.windows.net/0795bd5210795bd5264a783820c02b3169fc34074a83/0795bd5220795bd5264a93710522f48ec6a7f418d8e3",
25+
"Uri" : "https://REDACTED.blob.core.windows.net/0795bd5210795bd52d4e888129f3fcdcd93de47a491b/0795bd5220795bd52d4e26102b804dc34efd24decb63",
2626
"Headers" : {
2727
"x-ms-version" : "2020-10-02",
28-
"User-Agent" : "azsdk-java-azure-storage-blob/12.12.0-beta.2 (11.0.7; Windows 10; 10.0)",
29-
"x-ms-client-request-id" : "f6693c7e-5d3d-4091-9256-e17a00c03888",
28+
"User-Agent" : "azsdk-java-azure-storage-blob/12.13.0-beta.1 (11.0.7; Windows 10; 10.0)",
29+
"x-ms-client-request-id" : "cb80a63c-6649-4bd0-981d-399bab45a4b1",
3030
"Content-Type" : "application/octet-stream"
3131
},
3232
"Response" : {
3333
"Transfer-Encoding" : "chunked",
3434
"x-ms-version" : "2020-10-02",
3535
"Server" : "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0",
3636
"x-ms-content-crc64" : "AAAAAAAAAAA=",
37-
"Last-Modified" : "Fri, 18 Jun 2021 18:42:19 GMT",
38-
"x-ms-version-id" : "2021-06-18T18:42:19.0179529Z",
37+
"Last-Modified" : "Thu, 24 Jun 2021 22:07:26 GMT",
38+
"x-ms-version-id" : "2021-06-24T22:07:26.2584157Z",
3939
"retry-after" : "0",
4040
"StatusCode" : "201",
4141
"x-ms-request-server-encrypted" : "true",
42-
"Date" : "Fri, 18 Jun 2021 18:42:18 GMT",
42+
"Date" : "Thu, 24 Jun 2021 22:07:26 GMT",
4343
"Content-MD5" : "1B2M2Y8AsgTpgAmY7PhCfg==",
44-
"eTag" : "0x8D93288CD4134C9",
45-
"x-ms-request-id" : "fc34c428-f01e-0001-4271-649c3e000000",
46-
"x-ms-client-request-id" : "f6693c7e-5d3d-4091-9256-e17a00c03888"
44+
"eTag" : "0x8D9375C736B915D",
45+
"x-ms-request-id" : "8dd6dc13-401e-0021-4945-69bb8d000000",
46+
"x-ms-client-request-id" : "cb80a63c-6649-4bd0-981d-399bab45a4b1"
4747
},
4848
"Exception" : null
4949
} ],
50-
"variables" : [ "0795bd5200795bd5264a871964e42487c620d4113860", "0795bd5210795bd5264a783820c02b3169fc34074a83", "0795bd5220795bd5264a93710522f48ec6a7f418d8e3", "2021-06-18T18:42:19.976318300Z" ]
50+
"variables" : [ "0795bd5200795bd52d4e792326e09af9e30304f7c95d", "0795bd5210795bd52d4e888129f3fcdcd93de47a491b", "0795bd5220795bd52d4e26102b804dc34efd24decb63", "2021-06-24T22:07:27.286533500Z" ]
5151
}

0 commit comments

Comments
 (0)