Skip to content

Commit 6139d61

Browse files
committed
When parsing data redundancy type fails, returns Unknown instead of exception.
1 parent 2a3e60e commit 6139d61

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

src/main/java/com/aliyun/oss/internal/ResponseParsers.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,10 +2724,12 @@ public static BucketInfo parseGetBucketInfo(InputStream responseBody) throws Res
27242724

27252725
// owner
27262726
Bucket bucket = new Bucket();
2727-
String id = bucketElem.getChild("Owner").getChildText("ID");
2728-
String displayName = bucketElem.getChild("Owner").getChildText("DisplayName");
2729-
Owner owner = new Owner(id, displayName);
2730-
bucket.setOwner(owner);
2727+
if (bucketElem.getChild("Owner") != null) {
2728+
String id = bucketElem.getChild("Owner").getChildText("ID");
2729+
String displayName = bucketElem.getChild("Owner").getChildText("DisplayName");
2730+
Owner owner = new Owner(id, displayName);
2731+
bucket.setOwner(owner);
2732+
}
27312733

27322734
// bucket
27332735
bucket.setName(bucketElem.getChildText("Name"));

src/main/java/com/aliyun/oss/model/DataRedundancyType.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ public enum DataRedundancyType {
3131
/**
3232
* ZRS
3333
*/
34-
ZRS("ZRS");
34+
ZRS("ZRS"),
35+
36+
/**
37+
* Unknown
38+
*/
39+
Unknown("Unknown");
3540

3641
private final String dataRedundancyTypeString;
3742

@@ -60,6 +65,6 @@ public static DataRedundancyType parse(String dataRedundancyTypeString) {
6065
if (e.toString().equals(dataRedundancyTypeString))
6166
return e;
6267
}
63-
throw new IllegalArgumentException("Unsupported data redundancy type: " + dataRedundancyTypeString);
68+
return Unknown;
6469
}
6570
}

src/test/java/com/aliyun/oss/common/parser/ResponseParsersTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,8 @@ public void testParseGetBucketInfoBasic() {
931931
Assert.assertEquals(CannedAccessControlList.Private, result.getCannedACL());
932932
Assert.assertEquals("oss-cn-hangzhou", result.getBucket().getLocation());
933933
Assert.assertEquals("oss-example", result.getBucket().getName());
934+
Assert.assertEquals("username", result.getBucket().getOwner().getDisplayName());
935+
Assert.assertEquals("27183473914****", result.getBucket().getOwner().getId());
934936
}
935937

936938
@Test
@@ -1022,6 +1024,41 @@ public void testParseGetBucketInfo() {
10221024
Assert.assertEquals("xxx-id-123", result.getBucket().getResourceGroupId());
10231025
}
10241026

1027+
@Test
1028+
public void testParseGetBucketInfoUnsupportType() {
1029+
String respBody = "" +
1030+
"<BucketInfo>\n" +
1031+
" <Bucket>\n" +
1032+
" <CreationDate>2013-07-31T10:56:21.000Z</CreationDate>\n" +
1033+
" <ExtranetEndpoint>oss-cn-hangzhou.aliyuncs.com</ExtranetEndpoint>\n" +
1034+
" <IntranetEndpoint>oss-cn-hangzhou-internal.aliyuncs.com</IntranetEndpoint>\n" +
1035+
" <Location>oss-cn-hangzhou</Location>\n" +
1036+
" <AccessControlList>\n" +
1037+
" <Grant>private</Grant>\n" +
1038+
" </AccessControlList>\n" +
1039+
" <Comment>test</Comment>\n" +
1040+
" <DataRedundancyType>ZRSII</DataRedundancyType>\n" +
1041+
" </Bucket>\n" +
1042+
" </BucketInfo>";
1043+
1044+
InputStream instream = null;
1045+
try {
1046+
instream = new ByteArrayInputStream(respBody.getBytes("utf-8"));
1047+
} catch (UnsupportedEncodingException e) {
1048+
Assert.fail("UnsupportedEncodingException");
1049+
}
1050+
1051+
BucketInfo result = null;
1052+
try {
1053+
result = ResponseParsers.parseGetBucketInfo(instream);
1054+
} catch (ResponseParseException e) {
1055+
Assert.fail("parse bucket replication response body fail!");
1056+
}
1057+
1058+
Assert.assertEquals(DataRedundancyType.Unknown, result.getDataRedundancyType());
1059+
1060+
}
1061+
10251062
@Test
10261063
public void testParseListVpcip() {
10271064
String respBody = "" +

0 commit comments

Comments
 (0)