Skip to content

Commit 62fc2ed

Browse files
zhuxiaolong37huiguangjun
authored andcommitted
added get describe regions interface (#464)
1 parent 68ea8cf commit 62fc2ed

File tree

10 files changed

+243
-1
lines changed

10 files changed

+243
-1
lines changed

src/main/java/com/aliyun/oss/OSS.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5101,4 +5101,16 @@ public UdfApplicationLog getUdfApplicationLog(GetUdfApplicationLogRequest getUdf
51015101
* If any errors occurred in OSS while processing the request.
51025102
*/
51035103
VoidResult closeMetaQuery(String bucketName) throws OSSException, ClientException;
5104+
5105+
/**
5106+
* Describe regions from the OSS Server
5107+
* @param describeRegionsRequest
5108+
* @return A {@link DescribeRegionsResult} instance.
5109+
* @throws OSSException
5110+
* If any errors are encountered in the client while making the
5111+
* request or handling the response.
5112+
* @throws ClientException
5113+
* If any errors occurred in OSS while processing the request.
5114+
*/
5115+
DescribeRegionsResult describeRegions(DescribeRegionsRequest describeRegionsRequest) throws OSSException, ClientException;
51045116
}

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,11 @@ public VoidResult closeMetaQuery(String bucketName) throws OSSException, ClientE
19571957
return this.bucketOperation.closeMetaQuery(new GenericRequest(bucketName));
19581958
}
19591959

1960+
@Override
1961+
public DescribeRegionsResult describeRegions(DescribeRegionsRequest describeRegionsRequest) throws OSSException, ClientException {
1962+
return this.bucketOperation.describeRegions(describeRegionsRequest);
1963+
}
1964+
19601965
@Override
19611966
public void shutdown() {
19621967
try {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,4 +2120,21 @@ public VoidResult closeMetaQuery(GenericRequest genericRequest) throws OSSExcept
21202120

21212121
return doOperation(request, requestIdResponseParser, bucketName, null, true);
21222122
}
2123+
2124+
public DescribeRegionsResult describeRegions(DescribeRegionsRequest describeRegionsRequest) throws OSSException, ClientException {
2125+
assertParameterNotNull(describeRegionsRequest, "describeRegionsRequest");
2126+
2127+
String bucketName = describeRegionsRequest.getBucketName();
2128+
String region = describeRegionsRequest.getRegion();
2129+
2130+
Map<String, String> params = new HashMap<String, String>();
2131+
params.put(REGIONS, region);
2132+
2133+
2134+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
2135+
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
2136+
.setOriginalRequest(describeRegionsRequest).build();
2137+
2138+
return doOperation(request, describeRegionsResponseParser, bucketName, null, true);
2139+
}
21232140
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,5 @@ public final class RequestParameters {
156156
public static final String ACCESS_MONITOR = "accessmonitor";
157157

158158
public static final String SUBRESOURCE_REGION_LIST = "regionList";
159-
159+
public static final String REGIONS = "regions";
160160
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public final class ResponseParsers {
141141
public static final GetBucketAccessMonitorResponseParser getBucketAccessMonitorResponseParser = new GetBucketAccessMonitorResponseParser();
142142
public static final GetMetaQueryStatusResponseParser getMetaQueryStatusResponseParser = new GetMetaQueryStatusResponseParser();
143143
public static final DoMetaQueryResponseParser doMetaQueryResponseParser = new DoMetaQueryResponseParser();
144+
public static final DescribeRegionsResponseParser describeRegionsResponseParser = new DescribeRegionsResponseParser();
144145

145146
public static Long parseLongWithDefault(String defaultValue){
146147
if(defaultValue == null || "".equals(defaultValue)){
@@ -4178,4 +4179,47 @@ private DoMetaQueryResult parseDoMetaQueryResult(InputStream inputStream) throws
41784179
}
41794180
}
41804181
}
4182+
4183+
public static final class DescribeRegionsResponseParser implements ResponseParser<DescribeRegionsResult> {
4184+
@Override
4185+
public DescribeRegionsResult parse(ResponseMessage response) throws ResponseParseException {
4186+
try {
4187+
DescribeRegionsResult result = parseDescribeRegionsResult(response.getContent());
4188+
setResultParameter(result, response);
4189+
return result;
4190+
} finally {
4191+
safeCloseResponse(response);
4192+
}
4193+
}
4194+
4195+
private DescribeRegionsResult parseDescribeRegionsResult(InputStream inputStream) throws ResponseParseException {
4196+
DescribeRegionsResult describeRegionsResult = new DescribeRegionsResult();
4197+
if (inputStream == null) {
4198+
return describeRegionsResult;
4199+
}
4200+
4201+
try {
4202+
Element root = getXmlRootElement(inputStream);
4203+
List<RegionInfo> regionInfoList = new ArrayList<RegionInfo>();
4204+
4205+
List<Element> regionListElems = root.getChildren("RegionInfo");
4206+
for (Element elem : regionListElems) {
4207+
RegionInfo regionInfo = new RegionInfo();
4208+
regionInfo.setRegion(elem.getChildText("Region"));
4209+
regionInfo.setInternetEndpoint(elem.getChildText("InternetEndpoint"));
4210+
regionInfo.setInternalEndpoint(elem.getChildText("InternalEndpoint"));
4211+
regionInfo.setAccelerateEndpoint(elem.getChildText("AccelerateEndpoint"));
4212+
4213+
regionInfoList.add(regionInfo);
4214+
}
4215+
describeRegionsResult.setRegionInfoList(regionInfoList);
4216+
4217+
return describeRegionsResult;
4218+
} catch (JDOMParseException e) {
4219+
throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
4220+
} catch (Exception e) {
4221+
throw new ResponseParseException(e.getMessage(), e);
4222+
}
4223+
}
4224+
}
41814225
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.aliyun.oss.model;
2+
3+
public class DescribeRegionsRequest extends GenericRequest {
4+
private String region;
5+
6+
public DescribeRegionsRequest() {}
7+
public DescribeRegionsRequest(String region) {
8+
this.region = region;
9+
}
10+
11+
public DescribeRegionsRequest(String bucketName, String region) {
12+
super(bucketName);
13+
this.region = region;
14+
}
15+
16+
public String getRegion() {
17+
return region;
18+
}
19+
20+
public void setRegion(String region) {
21+
this.region = region;
22+
}
23+
24+
public DescribeRegionsRequest WithRegion(String region) {
25+
setRegion(region);
26+
return this;
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.aliyun.oss.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class DescribeRegionsResult extends GenericResult {
7+
8+
private List<RegionInfo> regionInfoList = new ArrayList<RegionInfo>();
9+
10+
public List<RegionInfo> getRegionInfoList() {
11+
return regionInfoList;
12+
}
13+
14+
public void setRegionInfoList(List<RegionInfo> regionInfoList) {
15+
this.regionInfoList = regionInfoList;
16+
}
17+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.aliyun.oss.model;
2+
3+
public class RegionInfo extends GenericResult {
4+
5+
private String region;
6+
private String internetEndpoint;
7+
private String internalEndpoint;
8+
private String accelerateEndpoint;
9+
10+
public String getRegion() {
11+
return region;
12+
}
13+
14+
public void setRegion(String region) {
15+
this.region = region;
16+
}
17+
18+
public String getInternetEndpoint() {
19+
return internetEndpoint;
20+
}
21+
22+
public void setInternetEndpoint(String internetEndpoint) {
23+
this.internetEndpoint = internetEndpoint;
24+
}
25+
26+
public String getInternalEndpoint() {
27+
return internalEndpoint;
28+
}
29+
30+
public void setInternalEndpoint(String internalEndpoint) {
31+
this.internalEndpoint = internalEndpoint;
32+
}
33+
34+
public String getAccelerateEndpoint() {
35+
return accelerateEndpoint;
36+
}
37+
38+
public void setAccelerateEndpoint(String accelerateEndpoint) {
39+
this.accelerateEndpoint = accelerateEndpoint;
40+
}
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package samples;
2+
3+
import com.aliyun.oss.model.GetDescribeRegionsRequest;
4+
import com.aliyun.oss.model.GetDescribeRegionsResult;
5+
import com.aliyun.oss.model.RegionInfo;
6+
7+
public class DescribeRegionsSample {
8+
private static String endpoint = "*** Provide OSS endpoint ***";
9+
private static String accessKeyId = "*** Provide your AccessKeyId ***";
10+
private static String accessKeySecret = "*** Provide your AccessKeySecret ***";
11+
12+
public static void main(String[] args) {
13+
14+
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
15+
16+
try {
17+
GetDescribeRegionsResult result = ossClient.getDescribeRegions(new GetDescribeRegionsRequest());
18+
19+
for(RegionInfo r : result.getRegionInfoList()){
20+
System.out.println(r.getRegion());
21+
System.out.println(r.getInternetEndpoint());
22+
System.out.println(r.getInternetEndpoint());
23+
System.out.println(r.getAccelerateEndpoint());
24+
}
25+
System.out.println("return status code: " + result.getResponse().getStatusCode());
26+
} catch (OSSException oe) {
27+
System.out.println("Error Message: " + oe.getErrorMessage());
28+
System.out.println("Error Code: " + oe.getErrorCode());
29+
System.out.println("Request ID: " + oe.getRequestId());
30+
System.out.println("Host ID: " + oe.getHostId());
31+
} catch (ClientException ce) {
32+
System.out.println("Error Message: " + ce.getMessage());
33+
} finally {
34+
if(ossClient != null){
35+
ossClient.shutdown();
36+
}
37+
}
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.aliyun.oss.integrationtests;
2+
3+
import com.aliyun.oss.model.*;
4+
import junit.framework.Assert;
5+
import org.junit.Test;
6+
7+
public class BucketDescribeRegionsTest extends TestBase {
8+
9+
private static final String region = "oss-cn-chengdu";
10+
@Test
11+
public void testBucketDescribeRegions() {
12+
13+
try {
14+
DescribeRegionsResult regionsResult = ossClient.describeRegions(new DescribeRegionsRequest().WithRegion(region));
15+
16+
Assert.assertEquals(region, regionsResult.getRegionInfoList().get(0).getRegion());
17+
Assert.assertNotNull( regionsResult.getRegionInfoList().get(0).getInternalEndpoint());
18+
Assert.assertNotNull( regionsResult.getRegionInfoList().get(0).getInternetEndpoint());
19+
Assert.assertNotNull( regionsResult.getRegionInfoList().get(0).getAccelerateEndpoint());
20+
} catch (Exception e1) {
21+
Assert.fail(e1.getMessage());
22+
}
23+
}
24+
25+
@Test
26+
public void testBucketDescribeRegionsException() {
27+
28+
try {
29+
DescribeRegionsResult regionsResult = ossClient.describeRegions(new DescribeRegionsRequest());
30+
Assert.assertNotNull( regionsResult.getRegionInfoList().size());
31+
Assert.assertNotNull( regionsResult.getRegionInfoList().get(0).getRegion());
32+
Assert.assertNotNull( regionsResult.getRegionInfoList().get(0).getInternetEndpoint());
33+
Assert.assertNotNull( regionsResult.getRegionInfoList().get(0).getInternetEndpoint());
34+
Assert.assertNotNull( regionsResult.getRegionInfoList().get(0).getAccelerateEndpoint());
35+
} catch (Exception e1) {
36+
Assert.fail(e1.getMessage());
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)