Search in sources :

Example 86 with RequestMessage

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

the class OSSObjectOperation method getSymlink.

public OSSSymlink getSymlink(GenericRequest genericRequest) throws OSSException, ClientException {
    assertParameterNotNull(genericRequest, "genericRequest");
    String bucketName = genericRequest.getBucketName();
    String symlink = genericRequest.getKey();
    assertParameterNotNull(bucketName, "bucketName");
    assertParameterNotNull(symlink, "symlink");
    ensureBucketNameValid(bucketName);
    ensureObjectKeyValid(symlink);
    Map<String, String> params = new HashMap<String, String>();
    params.put(SUBRESOURCE_SYMLINK, null);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.GET).setBucket(bucketName).setKey(symlink).setParameters(params).setOriginalRequest(genericRequest).build();
    OSSSymlink symbolicLink = doOperation(request, getSymbolicLinkResponseParser, bucketName, symlink, true);
    if (symbolicLink != null) {
        symbolicLink.setSymlink(new String(symlink));
    }
    return symbolicLink;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) OSSSymlink(com.aliyun.oss.model.OSSSymlink)

Example 87 with RequestMessage

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

the class OSSObjectOperation method getObjectAcl.

public ObjectAcl getObjectAcl(GenericRequest genericRequest) throws OSSException, ClientException {
    assertParameterNotNull(genericRequest, "genericRequest");
    String bucketName = genericRequest.getBucketName();
    String key = genericRequest.getKey();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(key, "key");
    ensureObjectKeyValid(key);
    Map<String, String> params = new HashMap<String, String>();
    params.put(SUBRESOURCE_ACL, null);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.GET).setBucket(bucketName).setKey(key).setParameters(params).setOriginalRequest(genericRequest).build();
    return doOperation(request, getObjectAclResponseParser, bucketName, key, true);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Example 88 with RequestMessage

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

the class OSSObjectOperation method processObject.

public GenericResult processObject(ProcessObjectRequest processObjectRequest) throws OSSException, ClientException {
    assertParameterNotNull(processObjectRequest, "genericRequest");
    String bucketName = processObjectRequest.getBucketName();
    String key = processObjectRequest.getKey();
    String process = processObjectRequest.getProcess();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(key, "key");
    ensureObjectKeyValid(key);
    assertStringNotNullOrEmpty(process, "process");
    Map<String, String> params = new HashMap<String, String>();
    params.put(RequestParameters.SUBRESOURCE_PROCESS, null);
    byte[] rawContent = processObjectRequestMarshaller.marshall(processObjectRequest);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.POST).setBucket(bucketName).setKey(key).setParameters(params).setInputSize(rawContent.length).setInputStream(new ByteArrayInputStream(rawContent)).setOriginalRequest(processObjectRequest).build();
    return doOperation(request, ResponseParsers.processObjectResponseParser, bucketName, key, true);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Example 89 with RequestMessage

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

the class OSSObjectOperation method copyObject.

/**
 * Copy an existing object to another one.
 */
public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest) throws OSSException, ClientException {
    assertParameterNotNull(copyObjectRequest, "copyObjectRequest");
    Map<String, String> headers = new HashMap<String, String>();
    populateCopyObjectHeaders(copyObjectRequest, headers);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.PUT).setBucket(copyObjectRequest.getDestinationBucketName()).setKey(copyObjectRequest.getDestinationKey()).setHeaders(headers).setOriginalRequest(copyObjectRequest).build();
    return doOperation(request, copyObjectResponseParser, copyObjectRequest.getDestinationBucketName(), copyObjectRequest.getDestinationKey(), true);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Example 90 with RequestMessage

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

the class OSSObjectOperation method writeObjectInternal.

private <RequestType extends PutObjectRequest, ResponseType> ResponseType writeObjectInternal(WriteMode mode, RequestType originalRequest, ResponseParser<ResponseType> responseParser) {
    final String bucketName = originalRequest.getBucketName();
    final String key = originalRequest.getKey();
    InputStream originalInputStream = originalRequest.getInputStream();
    ObjectMetadata metadata = originalRequest.getMetadata();
    if (metadata == null) {
        metadata = new ObjectMetadata();
    }
    assertParameterNotNull(bucketName, "bucketName");
    assertParameterNotNull(key, "key");
    ensureBucketNameValid(bucketName);
    ensureObjectKeyValid(key);
    ensureCallbackValid(originalRequest.getCallback());
    InputStream repeatableInputStream = null;
    if (originalRequest.getFile() != null) {
        File toUpload = originalRequest.getFile();
        if (!checkFile(toUpload)) {
            getLog().info("Illegal file path: " + toUpload.getPath());
            throw new ClientException("Illegal file path: " + toUpload.getPath());
        }
        metadata.setContentLength(toUpload.length());
        if (metadata.getContentType() == null) {
            metadata.setContentType(Mimetypes.getInstance().getMimetype(toUpload, key));
        }
        try {
            repeatableInputStream = new RepeatableFileInputStream(toUpload);
        } catch (IOException ex) {
            logException("Cannot locate file to upload: ", ex);
            throw new ClientException("Cannot locate file to upload: ", ex);
        }
    } else {
        assertTrue(originalInputStream != null, "Please specify input stream or file to upload");
        if (metadata.getContentType() == null) {
            metadata.setContentType(Mimetypes.getInstance().getMimetype(key));
        }
        try {
            repeatableInputStream = newRepeatableInputStream(originalInputStream);
        } catch (IOException ex) {
            logException("Cannot wrap to repeatable input stream: ", ex);
            throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
        }
    }
    Map<String, String> headers = new HashMap<String, String>();
    populateRequestMetadata(headers, metadata);
    populateRequestCallback(headers, originalRequest.getCallback());
    Map<String, String> params = new LinkedHashMap<String, String>();
    populateWriteObjectParams(mode, originalRequest, params);
    RequestMessage httpRequest = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(WriteMode.getMappingMethod(mode)).setBucket(bucketName).setKey(key).setHeaders(headers).setParameters(params).setInputStream(repeatableInputStream).setInputSize(determineInputStreamLength(repeatableInputStream, metadata.getContentLength())).setOriginalRequest(originalRequest).build();
    List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
    reponseHandlers.add(new OSSCallbackErrorResponseHandler());
    final ProgressListener listener = originalRequest.getProgressListener();
    ResponseType result = null;
    try {
        publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
        if (originalRequest.getCallback() == null) {
            result = doOperation(httpRequest, responseParser, bucketName, key, true);
        } else {
            result = doOperation(httpRequest, responseParser, bucketName, key, true, null, reponseHandlers);
        }
        publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
    } catch (RuntimeException e) {
        publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
        throw e;
    }
    return result;
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RepeatableFileInputStream(com.aliyun.oss.common.comm.io.RepeatableFileInputStream) IOUtils.newRepeatableInputStream(com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RepeatableFileInputStream(com.aliyun.oss.common.comm.io.RepeatableFileInputStream) LinkedHashMap(java.util.LinkedHashMap) ProgressListener(com.aliyun.oss.event.ProgressListener) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) IOUtils.checkFile(com.aliyun.oss.common.utils.IOUtils.checkFile) File(java.io.File)

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