Search in sources :

Example 1 with ResponseHeaderOverrides

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());
    }
}
Also used : GeneratePresignedUrlRequest(com.aliyun.oss.model.GeneratePresignedUrlRequest) ResponseHeaderOverrides(com.aliyun.oss.model.ResponseHeaderOverrides) URI(java.net.URI) URL(java.net.URL) Date(java.util.Date) Test(org.junit.Test)

Example 2 with ResponseHeaderOverrides

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);
}
Also used : HashMap(java.util.HashMap) ResponseHeaderOverrides(com.aliyun.oss.model.ResponseHeaderOverrides) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) LinkedList(java.util.LinkedList) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ResponseHeaderOverrides (com.aliyun.oss.model.ResponseHeaderOverrides)2 Date (java.util.Date)2 Test (org.junit.Test)2 GeneratePresignedUrlRequest (com.aliyun.oss.model.GeneratePresignedUrlRequest)1 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1