From 1236ffef7b83864def2322aba15e53c997ed5d81 Mon Sep 17 00:00:00 2001 From: zxl01071856 Date: Thu, 27 Apr 2023 13:10:56 +0800 Subject: [PATCH] added bucket callback policy --- src/main/java/com/aliyun/oss/OSS.java | 49 +++++++++ src/main/java/com/aliyun/oss/OSSClient.java | 15 +++ .../oss/common/parser/RequestMarshallers.java | 38 +++++++ .../oss/internal/OSSBucketOperation.java | 57 ++++++++++ .../aliyun/oss/internal/ResponseParsers.java | 36 ++++++ .../model/GetBucketCallbackPolicyResult.java | 17 +++ .../aliyun/oss/model/PolicyCallbackItem.java | 44 ++++++++ .../model/SetBucketCallbackPolicyRequest.java | 31 ++++++ src/samples/BucketCallbackPolicySample.java | 104 ++++++++++++++++++ .../common/parser/RequestMarshallersTest.java | 70 ++++++++++++ .../common/parser/ResponseParsersTest.java | 62 +++++++++++ .../BucketCallbackPolicyTest.java | 87 +++++++++++++++ 12 files changed, 610 insertions(+) create mode 100644 src/main/java/com/aliyun/oss/model/GetBucketCallbackPolicyResult.java create mode 100644 src/main/java/com/aliyun/oss/model/PolicyCallbackItem.java create mode 100644 src/main/java/com/aliyun/oss/model/SetBucketCallbackPolicyRequest.java create mode 100644 src/samples/BucketCallbackPolicySample.java create mode 100644 src/test/java/com/aliyun/oss/integrationtests/BucketCallbackPolicyTest.java diff --git a/src/main/java/com/aliyun/oss/OSS.java b/src/main/java/com/aliyun/oss/OSS.java index 228b5471..750b0695 100644 --- a/src/main/java/com/aliyun/oss/OSS.java +++ b/src/main/java/com/aliyun/oss/OSS.java @@ -5101,4 +5101,53 @@ public UdfApplicationLog getUdfApplicationLog(GetUdfApplicationLogRequest getUdf * If any errors occurred in OSS while processing the request. */ VoidResult closeMetaQuery(String bucketName) throws OSSException, ClientException; + + /** + * Sets the callback policy on the {@link Bucket} instance. + * + * @param setBucketCallbackPolicyRequest + * {@link SetBucketCallbackPolicyRequest} instance that has bucket + * information as well as policy information. + * + * @return A {@link VoidResult} instance wrapped void return and + * contains some basic response options, such as requestId. + * + * @throws OSSException + * If any errors are encountered in the client while making the + * request or handling the response. + * @throws ClientException + * If any errors occurred in OSS while processing the request. + */ + public VoidResult setBucketCallbackPolicy(SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest) throws OSSException, ClientException; + + /** + * Gets callback policy of the {@link Bucket} instance. + * + * @param genericRequest + * {@link GenericRequest} instance that has the bucket name. + * @return The policy's content in {@link InputStream}. + * @throws OSSException + * If any errors are encountered in the client while making the + * request or handling the response. + * @throws ClientException + * If any errors occurred in OSS while processing the request. + */ + public GetBucketCallbackPolicyResult getBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException; + + /** + * Delete callback policy of the {@link Bucket} instance. + * + * @param genericRequest + * {@link GenericRequest} instance that has the bucket name. + * + * @return A {@link VoidResult} instance wrapped void return and + * contains some basic response options, such as requestId. + * + * @throws OSSException + * If any errors are encountered in the client while making the + * request or handling the response. + * @throws ClientException + * If any errors occurred in OSS while processing the request. + */ + public VoidResult deleteBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException; } diff --git a/src/main/java/com/aliyun/oss/OSSClient.java b/src/main/java/com/aliyun/oss/OSSClient.java index 8b995acb..bc1b4034 100644 --- a/src/main/java/com/aliyun/oss/OSSClient.java +++ b/src/main/java/com/aliyun/oss/OSSClient.java @@ -1957,6 +1957,21 @@ public VoidResult closeMetaQuery(String bucketName) throws OSSException, ClientE return this.bucketOperation.closeMetaQuery(new GenericRequest(bucketName)); } + @Override + public VoidResult setBucketCallbackPolicy(SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest) throws OSSException, ClientException { + return this.bucketOperation.setBucketCallbackPolicy(setBucketCallbackPolicyRequest); + } + + @Override + public GetBucketCallbackPolicyResult getBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException { + return this.bucketOperation.getBucketCallbackPolicy(genericRequest); + } + + @Override + public VoidResult deleteBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException { + return this.bucketOperation.deleteBucketCallbackPolicy(genericRequest); + } + @Override public void shutdown() { try { diff --git a/src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java b/src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java index 9ede985b..43cc2691 100644 --- a/src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java +++ b/src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java @@ -100,6 +100,7 @@ public final class RequestMarshallers { public static final PutBucketTransferAccelerationRequestMarshaller putBucketTransferAccelerationRequestMarshaller = new PutBucketTransferAccelerationRequestMarshaller(); public static final PutBucketAccessMonitorRequestMarshaller putBucketAccessMonitorRequestMarshaller = new PutBucketAccessMonitorRequestMarshaller(); public static final DoMetaQueryRequestMarshaller doMetaQueryRequestMarshaller = new DoMetaQueryRequestMarshaller(); + public static final SetBucketCallbackPolicyRequestMarshaller setBucketCallbackPolicyRequestMarshaller = new SetBucketCallbackPolicyRequestMarshaller(); public interface RequestMarshaller extends Marshaller { @@ -1863,6 +1864,43 @@ public byte[] marshall(DoMetaQueryRequest input) { } } + public static final class SetBucketCallbackPolicyRequestMarshaller implements RequestMarshaller2 { + + @Override + public byte[] marshall(SetBucketCallbackPolicyRequest request) { + StringBuffer xmlBody = new StringBuffer(); + xmlBody.append(""); + + if(request.getPolicyCallbackItems() != null && request.getPolicyCallbackItems().size() > 0){ + for(PolicyCallbackItem policy : request.getPolicyCallbackItems()){ + xmlBody.append(""); + if(!StringUtils.isNullOrEmpty(policy.getPolicyName())){ + xmlBody.append(""+ policy.getPolicyName() +""); + } + if(!StringUtils.isNullOrEmpty(policy.getCallback())){ + xmlBody.append(""+ policy.getCallback() +""); + } + if(StringUtils.isNullOrEmpty(policy.getCallbackVar())){ + xmlBody.append(""); + } else { + xmlBody.append(""+ policy.getCallbackVar() +""); + } + + xmlBody.append(""); + } + } + xmlBody.append(""); + + byte[] rawData = null; + try { + rawData = xmlBody.toString().getBytes(DEFAULT_CHARSET_NAME); + } catch (UnsupportedEncodingException e) { + throw new ClientException("Unsupported encoding " + e.getMessage(), e); + } + return rawData; + } + } + private static enum EscapedChar { // "\r" RETURN(" "), diff --git a/src/main/java/com/aliyun/oss/internal/OSSBucketOperation.java b/src/main/java/com/aliyun/oss/internal/OSSBucketOperation.java index 56e40bb9..56c3d9f1 100644 --- a/src/main/java/com/aliyun/oss/internal/OSSBucketOperation.java +++ b/src/main/java/com/aliyun/oss/internal/OSSBucketOperation.java @@ -2120,4 +2120,61 @@ public VoidResult closeMetaQuery(GenericRequest genericRequest) throws OSSExcept return doOperation(request, requestIdResponseParser, bucketName, null, true); } + + public VoidResult setBucketCallbackPolicy(SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest) throws OSSException, ClientException { + + assertParameterNotNull(setBucketCallbackPolicyRequest, "setBucketCallbackPolicyRequest"); + + String bucketName = setBucketCallbackPolicyRequest.getBucketName(); + assertParameterNotNull(bucketName, "bucketName"); + ensureBucketNameValid(bucketName); + Map params = new HashMap(); + params.put(SUBRESOURCE_POLICY, null); + params.put(SUBRESOURCE_COMP, SUBRESOURCE_CALLBACK); + + byte[] rawContent = setBucketCallbackPolicyRequestMarshaller.marshall(setBucketCallbackPolicyRequest); + Map headers = new HashMap(); + addRequestRequiredHeaders(headers, rawContent); + + RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(setBucketCallbackPolicyRequest)) + .setMethod(HttpMethod.PUT).setBucket(bucketName).setParameters(params) + .setInputSize(rawContent.length).setInputStream(new ByteArrayInputStream(rawContent)) + .setOriginalRequest(setBucketCallbackPolicyRequest).build(); + + return doOperation(request, requestIdResponseParser, bucketName, null); + } + + public GetBucketCallbackPolicyResult getBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException { + assertParameterNotNull(genericRequest, "genericRequest"); + + String bucketName = genericRequest.getBucketName(); + assertParameterNotNull(bucketName, "bucketName"); + ensureBucketNameValid(bucketName); + Map params = new HashMap(); + params.put(SUBRESOURCE_POLICY, null); + params.put(SUBRESOURCE_COMP, SUBRESOURCE_CALLBACK); + RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest)) + .setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params) + .setOriginalRequest(genericRequest).build(); + return doOperation(request, getBucketCallbackPolicyResponseParser, bucketName, null, true); + } + + public VoidResult deleteBucketCallbackPolicy(GenericRequest genericRequest) throws OSSException, ClientException { + + assertParameterNotNull(genericRequest, "genericRequest"); + + String bucketName = genericRequest.getBucketName(); + assertParameterNotNull(bucketName, "bucketName"); + ensureBucketNameValid(bucketName); + + Map params = new HashMap(); + params.put(SUBRESOURCE_POLICY, null); + params.put(SUBRESOURCE_COMP, SUBRESOURCE_CALLBACK); + + RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest)) + .setMethod(HttpMethod.DELETE).setBucket(bucketName).setParameters(params) + .setOriginalRequest(genericRequest).build(); + + return doOperation(request, requestIdResponseParser, bucketName, null); + } } diff --git a/src/main/java/com/aliyun/oss/internal/ResponseParsers.java b/src/main/java/com/aliyun/oss/internal/ResponseParsers.java index ce5bc5c4..bd6e3e6e 100644 --- a/src/main/java/com/aliyun/oss/internal/ResponseParsers.java +++ b/src/main/java/com/aliyun/oss/internal/ResponseParsers.java @@ -141,6 +141,7 @@ public final class ResponseParsers { public static final GetBucketAccessMonitorResponseParser getBucketAccessMonitorResponseParser = new GetBucketAccessMonitorResponseParser(); public static final GetMetaQueryStatusResponseParser getMetaQueryStatusResponseParser = new GetMetaQueryStatusResponseParser(); public static final DoMetaQueryResponseParser doMetaQueryResponseParser = new DoMetaQueryResponseParser(); + public static final GetBucketCallbackPolicyResponseParser getBucketCallbackPolicyResponseParser = new GetBucketCallbackPolicyResponseParser(); public static Long parseLongWithDefault(String defaultValue){ if(defaultValue == null || "".equals(defaultValue)){ @@ -4157,4 +4158,39 @@ private DoMetaQueryResult parseDoMetaQueryResult(InputStream inputStream) throws } } } + + public static final class GetBucketCallbackPolicyResponseParser implements ResponseParser { + + @Override + public GetBucketCallbackPolicyResult parse(ResponseMessage response) throws ResponseParseException { + GetBucketCallbackPolicyResult result = parseGetBucketCallbackPolicy(response.getContent()); + setResultParameter(result, response); + return result; + } + + private GetBucketCallbackPolicyResult parseGetBucketCallbackPolicy(InputStream inputStream) throws ResponseParseException { + GetBucketCallbackPolicyResult result = new GetBucketCallbackPolicyResult(); + if (inputStream == null) { + return result; + } + + try { + Element root = getXmlRootElement(inputStream); + + List fileElem = root.getChildren(); + List policyCallbackItems = new ArrayList(); + if(fileElem.size() >0 ){ + for(Element elem : fileElem){ + PolicyCallbackItem policyCallbackItem = new PolicyCallbackItem(elem.getChildText("PolicyName"), elem.getChildText("Callback")); + policyCallbackItem.setCallbackVar(elem.getChildText("CallbackVar")); + policyCallbackItems.add(policyCallbackItem); + } + } + result.setPolicyCallbackItems(policyCallbackItems); + return result; + } catch (Exception e) { + throw new ResponseParseException(e.getMessage(), e); + } + } + } } diff --git a/src/main/java/com/aliyun/oss/model/GetBucketCallbackPolicyResult.java b/src/main/java/com/aliyun/oss/model/GetBucketCallbackPolicyResult.java new file mode 100644 index 00000000..76a645ab --- /dev/null +++ b/src/main/java/com/aliyun/oss/model/GetBucketCallbackPolicyResult.java @@ -0,0 +1,17 @@ +package com.aliyun.oss.model; + +import java.util.ArrayList; +import java.util.List; + +public class GetBucketCallbackPolicyResult extends GenericResult { + + private List policyCallbackItems = new ArrayList(); + + public List getPolicyCallbackItems() { + return policyCallbackItems; + } + + public void setPolicyCallbackItems(List policyCallbackItems) { + this.policyCallbackItems = policyCallbackItems; + } +} diff --git a/src/main/java/com/aliyun/oss/model/PolicyCallbackItem.java b/src/main/java/com/aliyun/oss/model/PolicyCallbackItem.java new file mode 100644 index 00000000..b1ce03cd --- /dev/null +++ b/src/main/java/com/aliyun/oss/model/PolicyCallbackItem.java @@ -0,0 +1,44 @@ +package com.aliyun.oss.model; + +public class PolicyCallbackItem { + // policy名称 + private String policyName; + // 回调参数 + private String callback; + // 自定义回调参数 + private String callbackVar; + + public PolicyCallbackItem(String policyName, String callback) { + this.policyName = policyName; + this.callback = callback; + } + + public String getPolicyName() { + return policyName; + } + + public void setPolicyName(String policyName) { + this.policyName = policyName; + } + + public String getCallback() { + return callback; + } + + public void setCallback(String callback) { + this.callback = callback; + } + + public String getCallbackVar() { + return callbackVar; + } + + public void setCallbackVar(String callbackVar) { + this.callbackVar = callbackVar; + } + + public PolicyCallbackItem withCallbackVar(String callbackVar) { + this.callbackVar = callbackVar; + return this; + } +} diff --git a/src/main/java/com/aliyun/oss/model/SetBucketCallbackPolicyRequest.java b/src/main/java/com/aliyun/oss/model/SetBucketCallbackPolicyRequest.java new file mode 100644 index 00000000..8aca16d1 --- /dev/null +++ b/src/main/java/com/aliyun/oss/model/SetBucketCallbackPolicyRequest.java @@ -0,0 +1,31 @@ +package com.aliyun.oss.model; + +import java.util.ArrayList; +import java.util.List; + +public class SetBucketCallbackPolicyRequest extends GenericRequest { + + private List policyCallbackItems = new ArrayList(); + + public SetBucketCallbackPolicyRequest(String bucketName) { + super(bucketName); + } + + public SetBucketCallbackPolicyRequest(String bucketName, List policyCallbackItems) { + super(bucketName); + this.policyCallbackItems = policyCallbackItems; + } + + public List getPolicyCallbackItems() { + return policyCallbackItems; + } + + public void setPolicyCallbackItems(List policyCallbackItems) { + this.policyCallbackItems = policyCallbackItems; + } + + public SetBucketCallbackPolicyRequest withPolicyCallbackItems(List policyCallbackItems) { + this.policyCallbackItems = policyCallbackItems; + return this; + } +} diff --git a/src/samples/BucketCallbackPolicySample.java b/src/samples/BucketCallbackPolicySample.java new file mode 100644 index 00000000..8d8a95e7 --- /dev/null +++ b/src/samples/BucketCallbackPolicySample.java @@ -0,0 +1,104 @@ +package samples; + +import java.io.ByteArrayInputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import com.aliyun.oss.common.utils.BinaryUtil; +import com.aliyun.oss.model.*; + +public class BucketCallbackPolicySample { + private static String endpoint = "*** Provide OSS endpoint ***"; + private static String accessKeyId = "*** Provide your AccessKeyId ***"; + private static String accessKeySecret = "*** Provide your AccessKeySecret ***"; + + private static String bucketName = "*** Provide bucket name ***"; + private static String key = "*** Provide key ***"; + + // callback policy name + private static String policyName = "test1"; + // callback content + private static String callbackContent = "{\"callbackUrl\":\"www.abc.com/callback\",\"callbackBody\":\"${etag}\"}"; + // custom callback parameters + private static String callbackVarContent = "{\"x:var1\":\"value1\",\"x:var2\":\"value2\"}"; + + // callback policy name + private static String policyName2 = "test_2"; + // callback content + private static String callbackContent2 = "{\"callbackUrl\":\"www.bbc.com/index.html\",\"callbackHost\":\"www.bbc.com\",\"callbackBody\":\"{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}\"}"; + // custom callback parameters + private static String callbackVarContent2 = "{\"x:a\":\"a\", \"x:b\":\"b\"}"; + + public static void main(String[] args) throws Throwable { + + OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); + + try { + SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest = new SetBucketCallbackPolicyRequest(bucketName); + // Set empty policy + ossClient.setBucketCallbackPolicy(setBucketCallbackPolicyRequest); + + String callback = BinaryUtil.toBase64String(callbackContent.getBytes()); + String callback2 = BinaryUtil.toBase64String(callbackContent2.getBytes()); + String callbackVar = BinaryUtil.toBase64String(callbackVarContent.getBytes()); + String callbackVar2 = BinaryUtil.toBase64String(callbackVarContent2.getBytes()); + + List policyCallbackItems = new ArrayList(); + PolicyCallbackItem policyCallbackItem = new PolicyCallbackItem(policyName, callback).withCallbackVar(callbackVar); + + PolicyCallbackItem policyCallbackItem2 = new PolicyCallbackItem(policyName2, callback2).withCallbackVar(callbackVar2); + policyCallbackItem2.setPolicyName(policyName2); + policyCallbackItem2.setCallback(callback2); + policyCallbackItem2.setCallbackVar(callbackVar2); + + policyCallbackItems.add(policyCallbackItem); + policyCallbackItems.add(policyCallbackItem2); + + SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest2 = new SetBucketCallbackPolicyRequest(bucketName).withPolicyCallbackItems(policyCallbackItems); + + // Set callback policy + VoidResult setResult = ossClient.setBucketCallbackPolicy(setBucketCallbackPolicyRequest2); + System.out.println("set bucket callback policy status: "+setResult.getResponse().getStatusCode()); + + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new ByteArrayInputStream("hello world".getBytes())); + + // Upload File Trigger Callback + String callbackText = BinaryUtil.toBase64String("{\"callbackPolicy\":\"test_2\"} ".getBytes()); + Map header = new HashMap(); + header.put("x-oss-callback", callbackText); + putObjectRequest.setHeaders(header); + ossClient.putObject(putObjectRequest); + + // Get callback policy + GetBucketCallbackPolicyResult getResult = ossClient.getBucketCallbackPolicy(new GenericRequest(bucketName)); + System.out.println("get bucket callback policy status: "+getResult.getResponse().getStatusCode()); + System.out.println("PolicyName: "+getResult.getPolicyCallbackItems().get(0).getPolicyName()); + System.out.println("Callback: "+getResult.getPolicyCallbackItems().get(0).getCallback()); + System.out.println("CallbackVar: "+getResult.getPolicyCallbackItems().get(0).getCallbackVar()); + + // Delete callback policy + VoidResult delResult = ossClient.deleteBucketCallbackPolicy(new GenericRequest(bucketName)); + System.out.println("delete bucket callback policy status: "+delResult.getResponse().getStatusCode()); + + } catch (OSSException oe) { + System.out.println("Caught an OSSException, which means your request made it to OSS, " + + "but was rejected with an error response for some reason."); + System.out.println("Error Message: " + oe.getErrorMessage()); + System.out.println("Error Code: " + oe.getErrorCode()); + System.out.println("Request ID: " + oe.getRequestId()); + System.out.println("Host ID: " + oe.getHostId()); + } catch (ClientException ce) { + System.out.println("Caught an ClientException, which means the client encountered " + + "a serious internal problem while trying to communicate with OSS, " + + "such as not being able to access the network."); + System.out.println("Error Message: " + ce.getMessage()); + } finally { + /* + * Do not forget to shut down the client finally to release all allocated resources. + */ + ossClient.shutdown(); + } + } + +} \ No newline at end of file diff --git a/src/test/java/com/aliyun/oss/common/parser/RequestMarshallersTest.java b/src/test/java/com/aliyun/oss/common/parser/RequestMarshallersTest.java index d79d8e84..7ea9c18c 100644 --- a/src/test/java/com/aliyun/oss/common/parser/RequestMarshallersTest.java +++ b/src/test/java/com/aliyun/oss/common/parser/RequestMarshallersTest.java @@ -6,6 +6,7 @@ import com.aliyun.oss.OSSException; import com.aliyun.oss.common.comm.io.FixedLengthInputStream; +import com.aliyun.oss.common.utils.BinaryUtil; import com.aliyun.oss.common.utils.DateUtil; import com.aliyun.oss.model.*; import junit.framework.Assert; @@ -1413,4 +1414,73 @@ public void testPutLifeCycleRequestMarshaller() { Assert.assertEquals("key2", root.getChild("Rule").getChild("Filter").getChildren("Not").get(1).getChild("Tag").getChildText("Key")); Assert.assertEquals("value2", root.getChild("Rule").getChild("Filter").getChildren("Not").get(1).getChild("Tag").getChildText("Value")); } + + @Test + public void testSetBucketCallbackPolicyRequestMarshaller() { + String policyName = "test1"; + String callbackContent = "{\"callbackUrl\":\"www.abc.com/callback\",\"callbackBody\":\"${etag}\"}"; + String callbackVarContent = "{\n" + + "\"x:var1\":\"value1\",\n" + + "\"x:var2\":\"value2\"\n" + + "}"; + + String policyName2 = "test_2"; + String callbackContent2 = "{\n" + + "\"callbackUrl\":\"42.192.82.9:9001/index.php?route=test/test/test\",\n" + + "\"callbackHost\":\"42.192.82.9\",\n" + + "\"callbackBody\":\"{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}\",\n" + + "\"callbackBodyType\":\"application/json\"\n" + + "}"; + String callbackVarContent2 = "{\n" + + "\"key1\":\"val1\",\n" + + "\"key2\":\"val2\"\n" + + "}"; + String callback = BinaryUtil.toBase64String(callbackContent.getBytes()); + String callback2 = BinaryUtil.toBase64String(callbackContent2.getBytes()); + String callbackVar = BinaryUtil.toBase64String(callbackVarContent.getBytes()); + String callbackVar2 = BinaryUtil.toBase64String(callbackVarContent2.getBytes()); + List policyCallbackItems = new ArrayList(); + PolicyCallbackItem policyCallbackItem = new PolicyCallbackItem(policyName, callback).withCallbackVar(callbackVar); + policyCallbackItem.setPolicyName(policyName); + policyCallbackItem.setCallback(callback); + policyCallbackItem.setCallbackVar(callbackVar); + + PolicyCallbackItem policyCallbackItem2 = new PolicyCallbackItem(policyName2, callback2).withCallbackVar(callbackVar2); + policyCallbackItem2.setPolicyName(policyName2); + policyCallbackItem2.setCallback(callback2); + policyCallbackItem2.setCallbackVar(callbackVar2); + + policyCallbackItems.add(policyCallbackItem); + policyCallbackItems.add(policyCallbackItem2); + SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest = new SetBucketCallbackPolicyRequest("bucket").withPolicyCallbackItems(policyCallbackItems); + + + byte[] data = setBucketCallbackPolicyRequestMarshaller.marshall(setBucketCallbackPolicyRequest); + ByteArrayInputStream is = new ByteArrayInputStream(data); + + SAXBuilder builder = new SAXBuilder(); + Document doc = null; + try { + doc = builder.build(is); + } catch (JDOMException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + Element root = doc.getRootElement(); + String policyNameReturn = root.getChildren("PolicyItem").get(0).getChildText("PolicyName"); + String callbackReturn = root.getChildren("PolicyItem").get(0).getChildText("Callback"); + String callbackVarReturn = root.getChildren("PolicyItem").get(0).getChildText("CallbackVar"); + String policyNameReturn2 = root.getChildren("PolicyItem").get(1).getChildText("PolicyName"); + String callbackReturn2 = root.getChildren("PolicyItem").get(1).getChildText("Callback"); + String callbackVarReturn2 = root.getChildren("PolicyItem").get(1).getChildText("CallbackVar"); + + Assert.assertEquals(policyName, policyNameReturn); + Assert.assertEquals(callback, callbackReturn); + Assert.assertEquals(callbackVar, callbackVarReturn); + Assert.assertEquals(policyName2, policyNameReturn2); + Assert.assertEquals(callback2, callbackReturn2); + Assert.assertEquals(callbackVar2, callbackVarReturn2); + } } diff --git a/src/test/java/com/aliyun/oss/common/parser/ResponseParsersTest.java b/src/test/java/com/aliyun/oss/common/parser/ResponseParsersTest.java index ae594d59..9f015c06 100644 --- a/src/test/java/com/aliyun/oss/common/parser/ResponseParsersTest.java +++ b/src/test/java/com/aliyun/oss/common/parser/ResponseParsersTest.java @@ -5140,4 +5140,66 @@ public void testParseListVersionsWithRestoreInfo() { Assert.assertEquals("object-with-special-restore", result.getVersionSummaries().get(0).getKey()); Assert.assertEquals("ongoing-request=\"true\"", result.getVersionSummaries().get(0).getRestoreInfo()); } + + + @Test + public void testGetBucketCallbackPolicyParser() { + String respBody = "" + + "\n" + + " \n" + + " test1\n" + + " eyJjYWxsYmFja1VybCI6Ind3dy5hYmMuY29tL2NhbGxiYWNrIiwiY2FsbGJhY2tCb2R5IjoiJHtldGFnfSJ9\n" + + " ewoieDp2YXIxIjoidmFsdWUxIiwKIng6dmFyMiI6InZhbHVlMiIKfQ==\n" + + " \n" + + " \n" + + " test_2\n" + + " ewoiY2FsbGJhY2tVcmwiOiIja0JvZHkiOiJ7XCJtaW1lVHqc29uIgp9\n" + + " ewoia2V5MSI6InZhbDEiLAoia2V5MiI6InZhbDIiCn0=\n" + + " \n" + + ""; + + InputStream instream = null; + try { + instream = new ByteArrayInputStream(respBody.getBytes("utf-8")); + } catch (UnsupportedEncodingException e) { + Assert.fail("UnsupportedEncodingException"); + } + + GetBucketCallbackPolicyResult result = null; + try { + ResponseMessage response = new ResponseMessage(null); + response.setContent(instream); + ResponseParsers.GetBucketCallbackPolicyResponseParser parser = new ResponseParsers.GetBucketCallbackPolicyResponseParser(); + result = parser.parse(response); + } catch (ResponseParseException e) { + Assert.fail("parse delete directory response body fail!"); + } + + Assert.assertEquals("test1", result.getPolicyCallbackItems().get(0).getPolicyName()); + Assert.assertEquals("eyJjYWxsYmFja1VybCI6Ind3dy5hYmMuY29tL2NhbGxiYWNrIiwiY2FsbGJhY2tCb2R5IjoiJHtldGFnfSJ9", result.getPolicyCallbackItems().get(0).getCallback()); + Assert.assertEquals("ewoieDp2YXIxIjoidmFsdWUxIiwKIng6dmFyMiI6InZhbHVlMiIKfQ==", result.getPolicyCallbackItems().get(0).getCallbackVar()); + Assert.assertEquals("test_2", result.getPolicyCallbackItems().get(1).getPolicyName()); + Assert.assertEquals("ewoiY2FsbGJhY2tVcmwiOiIja0JvZHkiOiJ7XCJtaW1lVHqc29uIgp9", result.getPolicyCallbackItems().get(1).getCallback()); + Assert.assertEquals("ewoia2V5MSI6InZhbDEiLAoia2V5MiI6InZhbDIiCn0=", result.getPolicyCallbackItems().get(1).getCallbackVar()); + + respBody = "" + + "\n" + + ""; + + try { + instream = new ByteArrayInputStream(respBody.getBytes("utf-8")); + } catch (UnsupportedEncodingException e) { + Assert.fail("UnsupportedEncodingException"); + } + + result = null; + try { + ResponseMessage response = new ResponseMessage(null); + response.setContent(instream); + ResponseParsers.GetBucketCallbackPolicyResponseParser parser = new ResponseParsers.GetBucketCallbackPolicyResponseParser(); + result = parser.parse(response); + } catch (ResponseParseException e) { + Assert.fail("parse delete directory response body fail!"); + } + } } diff --git a/src/test/java/com/aliyun/oss/integrationtests/BucketCallbackPolicyTest.java b/src/test/java/com/aliyun/oss/integrationtests/BucketCallbackPolicyTest.java new file mode 100644 index 00000000..906ee6f6 --- /dev/null +++ b/src/test/java/com/aliyun/oss/integrationtests/BucketCallbackPolicyTest.java @@ -0,0 +1,87 @@ +package com.aliyun.oss.integrationtests; + +import com.aliyun.oss.OSSException; +import com.aliyun.oss.common.utils.BinaryUtil; +import com.aliyun.oss.model.*; +import junit.framework.Assert; +import org.junit.Test; +import java.io.ByteArrayInputStream; +import java.util.*; + +public class BucketCallbackPolicyTest extends TestBase { + + @Test + public void testNormalPolicy() { + String objectName = "test-callback.txt"; + String policyName = "test1"; + String callbackContent = "{\"callbackUrl\":\"www.abc.com/callback\",\"callbackBody\":\"${etag}\"}"; + String callbackVarContent = "{\"x:var1\":\"value1\",\"x:var2\":\"value2\"}"; + + String policyName2 = "test_2"; + String callbackContent2 = "{\"callbackUrl\":\"www.bbc.com/index.html\",\"callbackHost\":\"www.bbc.com\",\"callbackBody\":\"{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}\"}"; + String callbackVarContent2 = "{\"x:a\":\"a\", \"x:b\":\"b\"}"; + + try { + SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest = new SetBucketCallbackPolicyRequest(bucketName); + // Set empty policy + ossClient.setBucketCallbackPolicy(setBucketCallbackPolicyRequest); + + String callback = BinaryUtil.toBase64String(callbackContent.getBytes()); + String callback2 = BinaryUtil.toBase64String(callbackContent2.getBytes()); + String callbackVar = BinaryUtil.toBase64String(callbackVarContent.getBytes()); + String callbackVar2 = BinaryUtil.toBase64String(callbackVarContent2.getBytes()); + + List policyCallbackItems = new ArrayList(); + PolicyCallbackItem policyCallbackItem = new PolicyCallbackItem(policyName, callback).withCallbackVar(callbackVar); + + PolicyCallbackItem policyCallbackItem2 = new PolicyCallbackItem(policyName2, callback2).withCallbackVar(callbackVar2); + policyCallbackItem2.setPolicyName(policyName2); + policyCallbackItem2.setCallback(callback2); + policyCallbackItem2.setCallbackVar(callbackVar2); + + policyCallbackItems.add(policyCallbackItem); + policyCallbackItems.add(policyCallbackItem2); + + SetBucketCallbackPolicyRequest setBucketCallbackPolicyRequest2 = new SetBucketCallbackPolicyRequest(bucketName).withPolicyCallbackItems(policyCallbackItems); + + // Set callback policy + VoidResult setResult = ossClient.setBucketCallbackPolicy(setBucketCallbackPolicyRequest2); + Assert.assertEquals(200, setResult.getResponse().getStatusCode()); + + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream("hello world".getBytes())); + + String callbackText = BinaryUtil.toBase64String("{\"callbackPolicy\":\"test_2\"} ".getBytes()); + Map header = new HashMap(); + header.put("x-oss-callback", callbackText); + putObjectRequest.setHeaders(header); + ossClient.putObject(putObjectRequest); + + // Get callback policy + GetBucketCallbackPolicyResult result2 = ossClient.getBucketCallbackPolicy(new GenericRequest(bucketName)); + Assert.assertEquals(200, result2.getResponse().getStatusCode()); + Assert.assertEquals(2, result2.getPolicyCallbackItems().size()); + Assert.assertEquals(policyName, result2.getPolicyCallbackItems().get(0).getPolicyName()); + Assert.assertEquals(callback, result2.getPolicyCallbackItems().get(0).getCallback()); + Assert.assertEquals(callbackVar, result2.getPolicyCallbackItems().get(0).getCallbackVar()); + Assert.assertEquals(policyName2, result2.getPolicyCallbackItems().get(1).getPolicyName()); + Assert.assertEquals(callback2, result2.getPolicyCallbackItems().get(1).getCallback()); + Assert.assertEquals(callbackVar2, result2.getPolicyCallbackItems().get(1).getCallbackVar()); + + // Delete callback policy + VoidResult delResult = ossClient.deleteBucketCallbackPolicy(new GenericRequest(bucketName)); + Assert.assertEquals(204, delResult.getResponse().getStatusCode()); + + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + GetBucketCallbackPolicyResult getResult = ossClient.getBucketCallbackPolicy(new GenericRequest(bucketName)); + Assert.assertEquals(0, getResult.getPolicyCallbackItems().size()); + + } catch (OSSException e) { + Assert.assertEquals("BucketCallbackPolicyNotExist", e.getErrorCode()); + } + } +}