Search in sources :

Example 6 with Callback

use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testMultipartUploadCallbackDefault.

/**
 * Tests default values with specified url and body.
 */
@Test
public void testMultipartUploadCallbackDefault() {
    String key = "multipart-upload-callback-default";
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        InputStream instream = genFixedLengthInputStream(instreamLength);
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(1);
        uploadPartRequest.setPartSize(instreamLength);
        uploadPartRequest.setUploadId(uploadId);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackBody("upload-object-callback");
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        completeMultipartUploadRequest.setCallback(callback);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        byte[] buffer = new byte[bufferLength];
        int nRead = completeMultipartUploadResult.getCallbackResponseBody().read(buffer);
        completeMultipartUploadResult.getCallbackResponseBody().close();
        Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
        OSSObject obj = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, obj.getKey());
        Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) Callback(com.aliyun.oss.model.Callback) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 7 with Callback

use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testPutObjectCallbackParamInvalid.

/**
 * PutObject callback negative case,Invalid argument.
 */
@Test
public void testPutObjectCallbackParamInvalid() {
    String key = "put-callback-negative";
    // callbackUrl不合法,地址不合法、port不合法、超过5个
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl("my");
        callback.setCallbackBody("put-object-callback");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl("my.com:test");
        callback.setCallbackBody("put-object-callback");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl("my1.com;my2.com;my3.com;my4.com;my5.com;my6.com;");
        callback.setCallbackBody("put-object-callback");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    // callbackBody is empty
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackBody("");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    // callbackBody var format is invalid.
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackBody("bucket=${bucket}&object=$(object)");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    // callback-var parameter's length is more than 5K
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackBody("bucket=${bucket}&object=${object}");
        putObjectRequest.setCallback(callback);
        char[] bigArr = new char[1024];
        Arrays.fill(bigArr, 0, 1024, 'A');
        for (int i = 0; i < 10; i++) {
            callback.addCallbackVar("x:var" + i, new String(bigArr));
        }
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
    // Callback failed. Expect return 203 (CallbackFailed).
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl("http://www.ifeng.com/");
        callback.setCallbackBody("put-object-callback");
        putObjectRequest.setCallback(callback);
        ossClient.putObject(putObjectRequest);
        Assert.fail("PutObject callback should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.CALLBACK_FAILED, e.getErrorCode());
    }
}
Also used : Callback(com.aliyun.oss.model.Callback) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) OSSException(com.aliyun.oss.OSSException) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) Test(org.junit.Test)

Example 8 with Callback

use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testMultipartUploadCallbackVar.

/**
 * callback var
 */
@Test
public void testMultipartUploadCallbackVar() {
    String key = "multipart-upload-callback-var";
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        InputStream instream = genFixedLengthInputStream(instreamLength);
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(1);
        uploadPartRequest.setPartSize(instreamLength);
        uploadPartRequest.setUploadId(uploadId);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
        callback.setCallbackBody("{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}");
        callback.setCalbackBodyType(CalbackBodyType.JSON);
        callback.addCallbackVar("x:var1", "value1");
        callback.addCallbackVar("x:var2", "value2");
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        completeMultipartUploadRequest.setCallback(callback);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        byte[] buffer = new byte[bufferLength];
        int nRead = completeMultipartUploadResult.getCallbackResponseBody().read(buffer);
        completeMultipartUploadResult.getCallbackResponseBody().close();
        Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
        OSSObject obj = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, obj.getKey());
        Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) Callback(com.aliyun.oss.model.Callback) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 9 with Callback

use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testMultipartUploadCallbackURLChar.

/**
 * callback body/var has special characters, type is url
 */
@Test
public void testMultipartUploadCallbackURLChar() {
    String key = "multipart-upload-callback-url-char";
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        InputStream instream = genFixedLengthInputStream(instreamLength);
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(1);
        uploadPartRequest.setPartSize(instreamLength);
        uploadPartRequest.setUploadId(uploadId);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
        callback.setCallbackBody("春水碧于天,画船听雨眠。垆边人似月,皓腕凝霜雪。");
        callback.setCalbackBodyType(CalbackBodyType.JSON);
        callback.addCallbackVar("x:键值1", "值1:凌波不过横塘路,但目送,芳尘去。");
        callback.addCallbackVar("x:键值2", "值2:长记曾携手处,千树压、西湖寒碧。");
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        completeMultipartUploadRequest.setCallback(callback);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        byte[] buffer = new byte[bufferLength];
        int nRead = completeMultipartUploadResult.getCallbackResponseBody().read(buffer);
        completeMultipartUploadResult.getCallbackResponseBody().close();
        Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
        OSSObject obj = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, obj.getKey());
        Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) Callback(com.aliyun.oss.model.Callback) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 10 with Callback

use of com.aliyun.oss.model.Callback in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testMultipartUploadCallbackJsonChar.

/**
 * callback body/var has special characters, type is json.
 */
@Test
public void testMultipartUploadCallbackJsonChar() {
    String key = "multipart-upload-callback-json-char";
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        InputStream instream = genFixedLengthInputStream(instreamLength);
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(1);
        uploadPartRequest.setPartSize(instreamLength);
        uploadPartRequest.setUploadId(uploadId);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
        callback.setCallbackBody("{\\\"上片\\\":\\\"夏日好,月色白如雪。\\\" ,\\\"下片\\\":\\\"东山照欢会,西山照离别。 夏日好,花月有清阴。\\\"}");
        callback.setCalbackBodyType(CalbackBodyType.JSON);
        callback.addCallbackVar("x:键值1", "值1:凌波不过横塘路,但目送,芳尘去。");
        callback.addCallbackVar("x:键值2", "值2:长记曾携手处,千树压、西湖寒碧。");
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        completeMultipartUploadRequest.setCallback(callback);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        byte[] buffer = new byte[bufferLength];
        int nRead = completeMultipartUploadResult.getCallbackResponseBody().read(buffer);
        completeMultipartUploadResult.getCallbackResponseBody().close();
        Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
        OSSObject obj = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, obj.getKey());
        Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) Callback(com.aliyun.oss.model.Callback) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Aggregations

OSSException (com.aliyun.oss.OSSException)15 Callback (com.aliyun.oss.model.Callback)15 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)14 InputStream (java.io.InputStream)14 Test (org.junit.Test)14 OSSObject (com.aliyun.oss.model.OSSObject)13 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)8 PutObjectResult (com.aliyun.oss.model.PutObjectResult)8 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)6 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)6 PartETag (com.aliyun.oss.model.PartETag)6 UploadPartRequest (com.aliyun.oss.model.UploadPartRequest)6 UploadPartResult (com.aliyun.oss.model.UploadPartResult)6 ArrayList (java.util.ArrayList)6 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 GeneratePresignedUrlRequest (com.aliyun.oss.model.GeneratePresignedUrlRequest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URL (java.net.URL)1