use of com.aliyun.oss.model.ResponseHeaderOverrides in project aliyun-oss-java-sdk by aliyun.
the class OSSClientRequestTest method testGeneratePresignedUrlRequest.
@Test
public void testGeneratePresignedUrlRequest() throws Exception {
URI endpoint = new URI("http://localhost/");
String accessId = "test";
String accessKey = "test";
String bucketName = accessId + "bucket";
String objectKey = "object";
String server = endpoint.toString();
OSS ossClient = new OSSClientBuilder().build(server, accessId, accessKey);
URL url = null;
// Simple case
// 10 ειθΏζ
Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10);
try {
url = ossClient.generatePresignedUrl(bucketName, objectKey, expiration);
} catch (ClientException e) {
fail(e.getMessage());
}
String expectedUrlPrefix = endpoint.getScheme() + "://" + endpoint.getHost() + "/" + bucketName + "/" + objectKey + "?Expires=" + Long.toString(expiration.getTime() / 1000) + "&OSSAccessKeyId=" + accessId + "&Signature=";
Assert.assertTrue(url.toString().startsWith(expectedUrlPrefix));
// Override response header
ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
responseHeaders.setCacheControl("no-cache");
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectKey);
request.setExpiration(expiration);
request.setResponseHeaders(responseHeaders);
expectedUrlPrefix = endpoint.getScheme() + "://" + endpoint.getHost() + "/" + bucketName + "/" + objectKey + "?Expires=" + Long.toString(expiration.getTime() / 1000) + "&OSSAccessKeyId=" + accessId + "&Signature=";
try {
url = ossClient.generatePresignedUrl(request);
Assert.assertTrue(url.toString().startsWith(expectedUrlPrefix));
} catch (ClientException e) {
fail(e.getMessage());
}
}
use of com.aliyun.oss.model.ResponseHeaderOverrides in project aliyun-oss-java-sdk by aliyun.
the class OSSClientRequestTest method testGetObjectRequest.
@SuppressWarnings("serial")
@Test
public void testGetObjectRequest() {
TestAction test1 = new TestAction() {
public void run() throws Exception {
objectOp.getObject(new GetObjectRequest(bucketName, objectKey));
}
};
executeTest(test1, HttpMethod.GET, bucketName + "." + endpoint.getHost(), objectKey, null);
final GetObjectRequest request = new GetObjectRequest(bucketName, objectKey);
final List<String> matchingETags = new LinkedList<String>();
matchingETags.add("matching");
request.setMatchingETagConstraints(matchingETags);
final List<String> unmatchingETags = new LinkedList<String>();
unmatchingETags.add("unmatching");
request.setNonmatchingETagConstraints(unmatchingETags);
request.setModifiedSinceConstraint(new Date());
request.setUnmodifiedSinceConstraint(new Date());
final Map<String, String> expectedHeaders = new HashMap<String, String>() {
{
put("If-Match", matchingETags.get(0));
put("If-None-Match", unmatchingETags.get(0));
put("If-Modified-Since", DateUtil.formatRfc822Date(request.getModifiedSinceConstraint()));
put("If-Unmodified-Since", DateUtil.formatRfc822Date(request.getUnmodifiedSinceConstraint()));
}
};
TestAction test2 = new TestAction() {
public void run() throws Exception {
objectOp.getObject(request);
}
};
executeTest(test2, HttpMethod.GET, bucketName + "." + endpoint.getHost(), objectKey, expectedHeaders);
ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
responseHeaders.setCacheControl("no-cache");
request.setResponseHeaders(responseHeaders);
TestAction test3 = new TestAction() {
public void run() throws Exception {
objectOp.getObject(request);
}
};
executeTest(test3, HttpMethod.GET, bucketName + "." + endpoint.getHost(), objectKey + "?response-cache-control=no-cache", expectedHeaders);
}
Aggregations