Search in sources :

Example 46 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage in project aliyun-oss-java-sdk by aliyun.

the class OSSMultipartOperation method abortMultipartUpload.

/**
 * Abort multipart upload.
 */
public void abortMultipartUpload(AbortMultipartUploadRequest abortMultipartUploadRequest) throws OSSException, ClientException {
    assertParameterNotNull(abortMultipartUploadRequest, "abortMultipartUploadRequest");
    String key = abortMultipartUploadRequest.getKey();
    String bucketName = abortMultipartUploadRequest.getBucketName();
    String uploadId = abortMultipartUploadRequest.getUploadId();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(key, "key");
    ensureObjectKeyValid(key);
    assertStringNotNullOrEmpty(uploadId, "uploadId");
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put(UPLOAD_ID, uploadId);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.DELETE).setBucket(bucketName).setKey(key).setParameters(parameters).setOriginalRequest(abortMultipartUploadRequest).build();
    doOperation(request, emptyResponseParser, bucketName, key);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Example 47 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage in project aliyun-oss-java-sdk by aliyun.

the class CORSOperation method getBucketCORSRules.

/**
 * Return a list of CORS rules of the specified bucket.
 */
public List<CORSRule> getBucketCORSRules(GenericRequest genericRequest) {
    assertParameterNotNull(genericRequest, "genericRequest");
    String bucketName = genericRequest.getBucketName();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    Map<String, String> parameters = new LinkedHashMap<String, String>();
    parameters.put(SUBRESOURCE_CORS, null);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.GET).setParameters(parameters).setBucket(bucketName).setOriginalRequest(genericRequest).build();
    return doOperation(request, getBucketCorsResponseParser, bucketName, null, true);
}
Also used : RequestMessage(com.aliyun.oss.common.comm.RequestMessage) LinkedHashMap(java.util.LinkedHashMap)

Example 48 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage in project aliyun-oss-java-sdk by aliyun.

the class OSSClient method generatePresignedUrl.

@Override
public URL generatePresignedUrl(GeneratePresignedUrlRequest request) throws ClientException {
    assertParameterNotNull(request, "request");
    String bucketName = request.getBucketName();
    if (request.getBucketName() == null) {
        throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("MustSetBucketName"));
    }
    ensureBucketNameValid(request.getBucketName());
    if (request.getExpiration() == null) {
        throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("MustSetExpiration"));
    }
    Credentials currentCreds = credsProvider.getCredentials();
    String accessId = currentCreds.getAccessKeyId();
    String accessKey = currentCreds.getSecretAccessKey();
    boolean useSecurityToken = currentCreds.useSecurityToken();
    HttpMethod method = request.getMethod() != null ? request.getMethod() : HttpMethod.GET;
    String expires = String.valueOf(request.getExpiration().getTime() / 1000L);
    String key = request.getKey();
    ClientConfiguration config = serviceClient.getClientConfiguration();
    String resourcePath = OSSUtils.determineResourcePath(bucketName, key, config.isSLDEnabled());
    RequestMessage requestMessage = new RequestMessage(bucketName, key);
    requestMessage.setEndpoint(OSSUtils.determineFinalEndpoint(endpoint, bucketName, config));
    requestMessage.setMethod(method);
    requestMessage.setResourcePath(resourcePath);
    requestMessage.setHeaders(request.getHeaders());
    requestMessage.addHeader(HttpHeaders.DATE, expires);
    if (request.getContentType() != null && !request.getContentType().trim().equals("")) {
        requestMessage.addHeader(HttpHeaders.CONTENT_TYPE, request.getContentType());
    }
    if (request.getContentMD5() != null && request.getContentMD5().trim().equals("")) {
        requestMessage.addHeader(HttpHeaders.CONTENT_MD5, request.getContentMD5());
    }
    for (Map.Entry<String, String> h : request.getUserMetadata().entrySet()) {
        requestMessage.addHeader(OSSHeaders.OSS_USER_METADATA_PREFIX + h.getKey(), h.getValue());
    }
    Map<String, String> responseHeaderParams = new HashMap<String, String>();
    populateResponseHeaderParameters(responseHeaderParams, request.getResponseHeaders());
    if (responseHeaderParams.size() > 0) {
        requestMessage.setParameters(responseHeaderParams);
    }
    if (request.getQueryParameter() != null && request.getQueryParameter().size() > 0) {
        for (Map.Entry<String, String> entry : request.getQueryParameter().entrySet()) {
            requestMessage.addParameter(entry.getKey(), entry.getValue());
        }
    }
    if (request.getProcess() != null && !request.getProcess().trim().equals("")) {
        requestMessage.addParameter(RequestParameters.SUBRESOURCE_PROCESS, request.getProcess());
    }
    if (useSecurityToken) {
        requestMessage.addParameter(SECURITY_TOKEN, currentCreds.getSecurityToken());
    }
    String canonicalResource = "/" + ((bucketName != null) ? bucketName : "") + ((key != null ? "/" + key : ""));
    String canonicalString = SignUtils.buildCanonicalString(method.toString(), canonicalResource, requestMessage, expires);
    String signature = ServiceSignature.create().computeSignature(accessKey, canonicalString);
    Map<String, String> params = new LinkedHashMap<String, String>();
    params.put(HttpHeaders.EXPIRES, expires);
    params.put(OSS_ACCESS_KEY_ID, accessId);
    params.put(SIGNATURE, signature);
    params.putAll(requestMessage.getParameters());
    String queryString = HttpUtil.paramToQueryString(params, DEFAULT_CHARSET_NAME);
    /* Compse HTTP request uri. */
    String url = requestMessage.getEndpoint().toString();
    if (!url.endsWith("/")) {
        url += "/";
    }
    url += resourcePath + "?" + queryString;
    try {
        return new URL(url);
    } catch (MalformedURLException e) {
        throw new ClientException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Credentials(com.aliyun.oss.common.auth.Credentials)

Example 49 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage in project aliyun-oss-java-sdk by aliyun.

the class CORSOperation method setBucketCORS.

/**
 * Set bucket cors.
 */
public void setBucketCORS(SetBucketCORSRequest setBucketCORSRequest) {
    checkSetBucketCORSRequestValidity(setBucketCORSRequest);
    Map<String, String> parameters = new LinkedHashMap<String, String>();
    parameters.put(SUBRESOURCE_CORS, null);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.PUT).setBucket(setBucketCORSRequest.getBucketName()).setParameters(parameters).setInputStreamWithLength(setBucketCORSRequestMarshaller.marshall(setBucketCORSRequest)).setOriginalRequest(setBucketCORSRequest).build();
    doOperation(request, emptyResponseParser, setBucketCORSRequest.getBucketName(), null);
}
Also used : RequestMessage(com.aliyun.oss.common.comm.RequestMessage) LinkedHashMap(java.util.LinkedHashMap)

Example 50 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage in project aliyun-oss-java-sdk by aliyun.

the class CORSOperation method deleteBucketCORS.

/**
 * Delete bucket cors.
 */
public void deleteBucketCORS(GenericRequest genericRequest) {
    assertParameterNotNull(genericRequest, "genericRequest");
    String bucketName = genericRequest.getBucketName();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    Map<String, String> parameters = new LinkedHashMap<String, String>();
    parameters.put(SUBRESOURCE_CORS, null);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.DELETE).setParameters(parameters).setBucket(bucketName).setOriginalRequest(genericRequest).build();
    doOperation(request, emptyResponseParser, bucketName, null);
}
Also used : RequestMessage(com.aliyun.oss.common.comm.RequestMessage) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

RequestMessage (com.aliyun.oss.common.comm.RequestMessage)98 LinkedHashMap (java.util.LinkedHashMap)88 HashMap (java.util.HashMap)78 ByteArrayInputStream (java.io.ByteArrayInputStream)23 ClientException (com.aliyun.oss.ClientException)9 ClientConfiguration (com.aliyun.oss.ClientConfiguration)6 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)6 InputStream (java.io.InputStream)6 ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)5 IOUtils.newRepeatableInputStream (com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream)5 ProgressListener (com.aliyun.oss.event.ProgressListener)5 ArrayList (java.util.ArrayList)5 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)4 ProgressInputStream (com.aliyun.oss.event.ProgressInputStream)4 URI (java.net.URI)4 CheckedInputStream (java.util.zip.CheckedInputStream)4 ServiceException (com.aliyun.oss.ServiceException)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 Credentials (com.aliyun.oss.common.auth.Credentials)2