Skip to content

Commit 1069f2a

Browse files
zhuxiaolong37huiguangjun
authored andcommitted
added bucket callback policy (#473)
1 parent 0faafdf commit 1069f2a

File tree

12 files changed

+632
-29
lines changed

12 files changed

+632
-29
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5113,4 +5113,53 @@ public UdfApplicationLog getUdfApplicationLog(GetUdfApplicationLogRequest getUdf
51135113
* If any errors occurred in OSS while processing the request.
51145114
*/
51155115
DescribeRegionsResult describeRegions(DescribeRegionsRequest describeRegionsRequest) throws OSSException, ClientException;
5116+
5117+
/**
5118+
* Sets the callback policy on the {@link Bucket} instance.
5119+
*
5120+
* @param setBucketCallbackPolicyRequest
5121+
* {@link SetBucketCallbackPolicyRequest} instance that has bucket
5122+
* information as well as policy information.
5123+
*
5124+
* @return A {@link VoidResult} instance wrapped void return and
5125+
* contains some basic response options, such as requestId.
5126+
*
5127+
* @throws OSSException
5128+
* If any errors are encountered in the client while making the
5129+
* request or handling the response.
5130+
* @throws ClientException
5131+
* If any errors occurred in OSS while processing the request.
5132+
*/
5133+
public VoidResult setBucketCallbackPolicy(SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest) throws OSSException, ClientException;
5134+
5135+
/**
5136+
* Gets callback policy of the {@link Bucket} instance.
5137+
*
5138+
* @param genericRequest
5139+
* {@link GenericRequest} instance that has the bucket name.
5140+
* @return The policy's content in {@link InputStream}.
5141+
* @throws OSSException
5142+
* If any errors are encountered in the client while making the
5143+
* request or handling the response.
5144+
* @throws ClientException
5145+
* If any errors occurred in OSS while processing the request.
5146+
*/
5147+
public GetBucketCallbackPolicyResult getBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException;
5148+
5149+
/**
5150+
* Delete callback policy of the {@link Bucket} instance.
5151+
*
5152+
* @param genericRequest
5153+
* {@link GenericRequest} instance that has the bucket name.
5154+
*
5155+
* @return A {@link VoidResult} instance wrapped void return and
5156+
* contains some basic response options, such as requestId.
5157+
*
5158+
* @throws OSSException
5159+
* If any errors are encountered in the client while making the
5160+
* request or handling the response.
5161+
* @throws ClientException
5162+
* If any errors occurred in OSS while processing the request.
5163+
*/
5164+
public VoidResult deleteBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException;
51165165
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,21 @@ public DescribeRegionsResult describeRegions(DescribeRegionsRequest describeRegi
19621962
return this.bucketOperation.describeRegions(describeRegionsRequest);
19631963
}
19641964

1965+
@Override
1966+
public VoidResult setBucketCallbackPolicy(SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest) throws OSSException, ClientException {
1967+
return this.bucketOperation.setBucketCallbackPolicy(setBucketCallbackPolicyRequest);
1968+
}
1969+
1970+
@Override
1971+
public GetBucketCallbackPolicyResult getBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException {
1972+
return this.bucketOperation.getBucketCallbackPolicy(genericRequest);
1973+
}
1974+
1975+
@Override
1976+
public VoidResult deleteBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException {
1977+
return this.bucketOperation.deleteBucketCallbackPolicy(genericRequest);
1978+
}
1979+
19651980
@Override
19661981
public void shutdown() {
19671982
try {

src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public final class RequestMarshallers {
100100
public static final PutBucketTransferAccelerationRequestMarshaller putBucketTransferAccelerationRequestMarshaller = new PutBucketTransferAccelerationRequestMarshaller();
101101
public static final PutBucketAccessMonitorRequestMarshaller putBucketAccessMonitorRequestMarshaller = new PutBucketAccessMonitorRequestMarshaller();
102102
public static final DoMetaQueryRequestMarshaller doMetaQueryRequestMarshaller = new DoMetaQueryRequestMarshaller();
103+
public static final SetBucketCallbackPolicyRequestMarshaller setBucketCallbackPolicyRequestMarshaller = new SetBucketCallbackPolicyRequestMarshaller();
103104

104105
public interface RequestMarshaller<R> extends Marshaller<FixedLengthInputStream, R> {
105106

@@ -1875,6 +1876,43 @@ public byte[] marshall(DoMetaQueryRequest input) {
18751876
}
18761877
}
18771878

1879+
public static final class SetBucketCallbackPolicyRequestMarshaller implements RequestMarshaller2<SetBucketCallbackPolicyRequest> {
1880+
1881+
@Override
1882+
public byte[] marshall(SetBucketCallbackPolicyRequest request) {
1883+
StringBuffer xmlBody = new StringBuffer();
1884+
xmlBody.append("<BucketCallbackPolicy>");
1885+
1886+
if(request.getPolicyCallbackItems() != null && request.getPolicyCallbackItems().size() > 0){
1887+
for(PolicyCallbackItem policy : request.getPolicyCallbackItems()){
1888+
xmlBody.append("<PolicyItem>");
1889+
if(!StringUtils.isNullOrEmpty(policy.getPolicyName())){
1890+
xmlBody.append("<PolicyName>"+ policy.getPolicyName() +"</PolicyName>");
1891+
}
1892+
if(!StringUtils.isNullOrEmpty(policy.getCallback())){
1893+
xmlBody.append("<Callback>"+ policy.getCallback() +"</Callback>");
1894+
}
1895+
if(StringUtils.isNullOrEmpty(policy.getCallbackVar())){
1896+
xmlBody.append("<CallbackVar></CallbackVar>");
1897+
} else {
1898+
xmlBody.append("<CallbackVar>"+ policy.getCallbackVar() +"</CallbackVar>");
1899+
}
1900+
1901+
xmlBody.append("</PolicyItem>");
1902+
}
1903+
}
1904+
xmlBody.append("</BucketCallbackPolicy>");
1905+
1906+
byte[] rawData = null;
1907+
try {
1908+
rawData = xmlBody.toString().getBytes(DEFAULT_CHARSET_NAME);
1909+
} catch (UnsupportedEncodingException e) {
1910+
throw new ClientException("Unsupported encoding " + e.getMessage(), e);
1911+
}
1912+
return rawData;
1913+
}
1914+
}
1915+
18781916
private static enum EscapedChar {
18791917
// "\r"
18801918
RETURN("&#x000D;"),

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,4 +2137,61 @@ public DescribeRegionsResult describeRegions(DescribeRegionsRequest describeRegi
21372137

21382138
return doOperation(request, describeRegionsResponseParser, bucketName, null, true);
21392139
}
2140+
2141+
public VoidResult setBucketCallbackPolicy(SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest) throws OSSException, ClientException {
2142+
2143+
assertParameterNotNull(setBucketCallbackPolicyRequest, "setBucketCallbackPolicyRequest");
2144+
2145+
String bucketName = setBucketCallbackPolicyRequest.getBucketName();
2146+
assertParameterNotNull(bucketName, "bucketName");
2147+
ensureBucketNameValid(bucketName);
2148+
Map<String, String> params = new HashMap<String, String>();
2149+
params.put(SUBRESOURCE_POLICY, null);
2150+
params.put(SUBRESOURCE_COMP, SUBRESOURCE_CALLBACK);
2151+
2152+
byte[] rawContent = setBucketCallbackPolicyRequestMarshaller.marshall(setBucketCallbackPolicyRequest);
2153+
Map<String, String> headers = new HashMap<String, String>();
2154+
addRequestRequiredHeaders(headers, rawContent);
2155+
2156+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(setBucketCallbackPolicyRequest))
2157+
.setMethod(HttpMethod.PUT).setBucket(bucketName).setParameters(params)
2158+
.setInputSize(rawContent.length).setInputStream(new ByteArrayInputStream(rawContent))
2159+
.setOriginalRequest(setBucketCallbackPolicyRequest).build();
2160+
2161+
return doOperation(request, requestIdResponseParser, bucketName, null);
2162+
}
2163+
2164+
public GetBucketCallbackPolicyResult getBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException {
2165+
assertParameterNotNull(genericRequest, "genericRequest");
2166+
2167+
String bucketName = genericRequest.getBucketName();
2168+
assertParameterNotNull(bucketName, "bucketName");
2169+
ensureBucketNameValid(bucketName);
2170+
Map<String, String> params = new HashMap<String, String>();
2171+
params.put(SUBRESOURCE_POLICY, null);
2172+
params.put(SUBRESOURCE_COMP, SUBRESOURCE_CALLBACK);
2173+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
2174+
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
2175+
.setOriginalRequest(genericRequest).build();
2176+
return doOperation(request, getBucketCallbackPolicyResponseParser, bucketName, null, true);
2177+
}
2178+
2179+
public VoidResult deleteBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException {
2180+
2181+
assertParameterNotNull(genericRequest, "genericRequest");
2182+
2183+
String bucketName = genericRequest.getBucketName();
2184+
assertParameterNotNull(bucketName, "bucketName");
2185+
ensureBucketNameValid(bucketName);
2186+
2187+
Map<String, String> params = new HashMap<String, String>();
2188+
params.put(SUBRESOURCE_POLICY, null);
2189+
params.put(SUBRESOURCE_COMP, SUBRESOURCE_CALLBACK);
2190+
2191+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
2192+
.setMethod(HttpMethod.DELETE).setBucket(bucketName).setParameters(params)
2193+
.setOriginalRequest(genericRequest).build();
2194+
2195+
return doOperation(request, requestIdResponseParser, bucketName, null);
2196+
}
21402197
}

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

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,40 @@
1919

2020
package com.aliyun.oss.internal;
2121

22-
import static com.aliyun.oss.common.utils.CodingUtils.isNullOrEmpty;
23-
import static com.aliyun.oss.internal.OSSHeaders.OSS_HEADER_WORM_ID;
24-
import static com.aliyun.oss.internal.OSSUtils.safeCloseResponse;
25-
import static com.aliyun.oss.internal.OSSUtils.trimQuotes;
26-
27-
import java.io.InputStream;
28-
import java.io.BufferedReader;
29-
import java.io.InputStreamReader;
30-
import java.math.BigInteger;
31-
import java.text.ParseException;
32-
import java.util.ArrayList;
33-
import java.util.Date;
34-
import java.util.HashMap;
35-
import java.util.Iterator;
36-
import java.util.List;
37-
import java.util.Map;
38-
import java.util.zip.CheckedInputStream;
39-
40-
import com.aliyun.oss.internal.model.OSSErrorResult;
41-
import com.aliyun.oss.model.*;
42-
import org.jdom2.Document;
43-
import org.jdom2.Element;
44-
import org.jdom2.input.JDOMParseException;
45-
import org.jdom2.input.SAXBuilder;
46-
4722
import com.aliyun.oss.common.comm.ResponseMessage;
4823
import com.aliyun.oss.common.parser.ResponseParseException;
4924
import com.aliyun.oss.common.parser.ResponseParser;
5025
import com.aliyun.oss.common.utils.DateUtil;
5126
import com.aliyun.oss.common.utils.HttpUtil;
5227
import com.aliyun.oss.common.utils.StringUtils;
28+
import com.aliyun.oss.internal.model.OSSErrorResult;
29+
import com.aliyun.oss.model.*;
5330
import com.aliyun.oss.model.AddBucketReplicationRequest.ReplicationAction;
5431
import com.aliyun.oss.model.DeleteVersionsResult.DeletedVersion;
55-
import com.aliyun.oss.model.LiveChannelStat.AudioStat;
56-
import com.aliyun.oss.model.LiveChannelStat.VideoStat;
32+
import com.aliyun.oss.model.LifecycleRule.NoncurrentVersionExpiration;
33+
import com.aliyun.oss.model.LifecycleRule.NoncurrentVersionStorageTransition;
5734
import com.aliyun.oss.model.LifecycleRule.RuleStatus;
5835
import com.aliyun.oss.model.LifecycleRule.StorageTransition;
36+
import com.aliyun.oss.model.LiveChannelStat.AudioStat;
37+
import com.aliyun.oss.model.LiveChannelStat.VideoStat;
5938
import com.aliyun.oss.model.SetBucketCORSRequest.CORSRule;
60-
import com.aliyun.oss.model.LifecycleRule.NoncurrentVersionStorageTransition;
61-
import com.aliyun.oss.model.LifecycleRule.NoncurrentVersionExpiration;
39+
import org.jdom2.Document;
40+
import org.jdom2.Element;
41+
import org.jdom2.input.JDOMParseException;
42+
import org.jdom2.input.SAXBuilder;
43+
44+
import java.io.BufferedReader;
45+
import java.io.InputStream;
46+
import java.io.InputStreamReader;
47+
import java.math.BigInteger;
48+
import java.text.ParseException;
49+
import java.util.*;
50+
import java.util.zip.CheckedInputStream;
51+
52+
import static com.aliyun.oss.common.utils.CodingUtils.isNullOrEmpty;
53+
import static com.aliyun.oss.internal.OSSHeaders.OSS_HEADER_WORM_ID;
54+
import static com.aliyun.oss.internal.OSSUtils.safeCloseResponse;
55+
import static com.aliyun.oss.internal.OSSUtils.trimQuotes;
6256

6357
/*
6458
* A collection of parsers that parse HTTP reponses into corresponding human-readable results.
@@ -142,6 +136,7 @@ public final class ResponseParsers {
142136
public static final GetMetaQueryStatusResponseParser getMetaQueryStatusResponseParser = new GetMetaQueryStatusResponseParser();
143137
public static final DoMetaQueryResponseParser doMetaQueryResponseParser = new DoMetaQueryResponseParser();
144138
public static final DescribeRegionsResponseParser describeRegionsResponseParser = new DescribeRegionsResponseParser();
139+
public static final GetBucketCallbackPolicyResponseParser getBucketCallbackPolicyResponseParser = new GetBucketCallbackPolicyResponseParser();
145140

146141
public static Long parseLongWithDefault(String defaultValue){
147142
if(defaultValue == null || "".equals(defaultValue)){
@@ -4222,4 +4217,41 @@ private DescribeRegionsResult parseDescribeRegionsResult(InputStream inputStream
42224217
}
42234218
}
42244219
}
4220+
4221+
public static final class GetBucketCallbackPolicyResponseParser implements ResponseParser<GetBucketCallbackPolicyResult> {
4222+
4223+
@Override
4224+
public GetBucketCallbackPolicyResult parse(ResponseMessage response) throws ResponseParseException {
4225+
try {
4226+
GetBucketCallbackPolicyResult result = parseGetBucketCallbackPolicy(response.getContent());
4227+
setResultParameter(result, response);
4228+
return result;
4229+
} finally {
4230+
safeCloseResponse(response);
4231+
}
4232+
}
4233+
4234+
private GetBucketCallbackPolicyResult parseGetBucketCallbackPolicy(InputStream inputStream) throws ResponseParseException {
4235+
GetBucketCallbackPolicyResult result = new GetBucketCallbackPolicyResult();
4236+
if (inputStream == null) {
4237+
return result;
4238+
}
4239+
4240+
try {
4241+
Element root = getXmlRootElement(inputStream);
4242+
4243+
List<Element> fileElem = root.getChildren();
4244+
List<PolicyCallbackItem> policyCallbackItems = new ArrayList<PolicyCallbackItem>();
4245+
for(Element elem : fileElem){
4246+
PolicyCallbackItem policyCallbackItem = new PolicyCallbackItem(elem.getChildText("PolicyName"), elem.getChildText("Callback"));
4247+
policyCallbackItem.setCallbackVar(elem.getChildText("CallbackVar"));
4248+
policyCallbackItems.add(policyCallbackItem);
4249+
}
4250+
result.setPolicyCallbackItems(policyCallbackItems);
4251+
return result;
4252+
} catch (Exception e) {
4253+
throw new ResponseParseException(e.getMessage(), e);
4254+
}
4255+
}
4256+
}
42254257
}
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 GetBucketCallbackPolicyResult extends GenericResult {
7+
8+
private List<PolicyCallbackItem> policyCallbackItems = new ArrayList<PolicyCallbackItem>();
9+
10+
public List<PolicyCallbackItem> getPolicyCallbackItems() {
11+
return policyCallbackItems;
12+
}
13+
14+
public void setPolicyCallbackItems(List<PolicyCallbackItem> policyCallbackItems) {
15+
this.policyCallbackItems = policyCallbackItems;
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 PolicyCallbackItem {
4+
private String policyName;
5+
private String callback;
6+
private String callbackVar;
7+
8+
public PolicyCallbackItem(String policyName, String callback) {
9+
this.policyName = policyName;
10+
this.callback = callback;
11+
}
12+
13+
public String getPolicyName() {
14+
return policyName;
15+
}
16+
17+
public void setPolicyName(String policyName) {
18+
this.policyName = policyName;
19+
}
20+
21+
public String getCallback() {
22+
return callback;
23+
}
24+
25+
public void setCallback(String callback) {
26+
this.callback = callback;
27+
}
28+
29+
public String getCallbackVar() {
30+
return callbackVar;
31+
}
32+
33+
public void setCallbackVar(String callbackVar) {
34+
this.callbackVar = callbackVar;
35+
}
36+
37+
public PolicyCallbackItem withCallbackVar(String callbackVar) {
38+
this.callbackVar = callbackVar;
39+
return this;
40+
}
41+
}

0 commit comments

Comments
 (0)