Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/com/aliyun/oss/internal/OSSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ public static String jsonizeCallback(Callback callback) {
} else if (callback.getCalbackBodyType() == CalbackBodyType.URL) {
jsonBody.append(",\"callbackBodyType\":\"application/x-www-form-urlencoded\"");
}

// SNI, optional
if (callback.getCallbackSNI()) {
jsonBody.append(",\"callbackSNI\":" + callback.getCallbackSNI());
}
jsonBody.append("}");

return jsonBody.toString();
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/aliyun/oss/model/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ public boolean hasCallbackVar() {
return false;
}

public boolean getCallbackSNI() {
return callbackSNI;
}

/**
* Sets the callback SNI.
* When the client initiates a callback request, Does OSS send a Server Name Indication (SNI) to the return source address specified through callbackUrl.
* Whether to send SNI depends on the server's configuration and requirements.
* For servers hosting multiple TLS/SSL certificates using the same IP address, it is recommended to choose to send SNI.
*
* @param callbackSNI
* Whether to send SNI.
*/
public void setCallbackSNI(boolean callbackSNI) {
this.callbackSNI = callbackSNI;
}

/**
* The callbackUrl after a successful upload
*/
Expand Down Expand Up @@ -184,4 +201,9 @@ public boolean hasCallbackVar() {
*/
private Map<String, String> callbackVar = new HashMap<String, String>();

/**
* When the client initiates a callback request, does the OSS send a server name indication SNI to the return source address specified through callbackUrl
*/
private boolean callbackSNI = false;

}
89 changes: 88 additions & 1 deletion src/test/java/com/aliyun/oss/integrationtests/CallbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -750,5 +750,92 @@ public void testGeneratePresignedUrlWithCallback() {
Assert.fail(ex.getMessage());
}
}


/**
* callback sni,type of callback body is url
*/
@Test
public void testPutObjectCallbackSNIWithURL() throws Exception {
String key = "put-callback-sni";

try {
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);

Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&"
+ "mimeType=${mimeType}&my_var1=${x:var1}&my_var2=${x:var2}");
callback.setCalbackBodyType(CalbackBodyType.URL);
callback.addCallbackVar("x:var1", "value1");
callback.addCallbackVar("x:var2", "value2");
callback.setCallbackSNI(true);
putObjectRequest.setCallback(callback);

PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
byte[] buffer = new byte[bufferLength];
int nRead = putObjectResult.getResponse().getContent().read(buffer);
putObjectResult.getResponse().getContent().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());
}
}

/**
* callback sni,type of callback body is json
*/
@Test
public void testPutObjectCallbackSNIWithJSON() {
String key = "multipart-upload-callback-json-sni";

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:长记曾携手处,千树压、西湖寒碧。");
callback.setCallbackSNI(true);

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());
}
}
}