Search in sources :

Example 1 with ResponseMessage

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

the class OSSObjectOperation method getObjectMetadata.

/**
 * Get object matadata.
 */
public ObjectMetadata getObjectMetadata(GenericRequest genericRequest) throws OSSException, ClientException {
    assertParameterNotNull(genericRequest, "genericRequest");
    String bucketName = genericRequest.getBucketName();
    String key = genericRequest.getKey();
    assertParameterNotNull(bucketName, "bucketName");
    assertParameterNotNull(key, "key");
    ensureBucketNameValid(bucketName);
    ensureObjectKeyValid(key);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.HEAD).setBucket(bucketName).setKey(key).setOriginalRequest(genericRequest).build();
    List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
    reponseHandlers.add(new ResponseHandler() {

        @Override
        public void handle(ResponseMessage response) throws ServiceException, ClientException {
            if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                safeCloseResponse(response);
                throw ExceptionFactory.createOSSException(response.getHeaders().get(OSSHeaders.OSS_HEADER_REQUEST_ID), OSSErrorCode.NO_SUCH_KEY, OSS_RESOURCE_MANAGER.getString("NoSuchKey"));
            }
        }
    });
    return doOperation(request, getObjectMetadataResponseParser, bucketName, key, true, null, reponseHandlers);
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) ServiceException(com.aliyun.oss.ServiceException) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ArrayList(java.util.ArrayList) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) ClientException(com.aliyun.oss.ClientException)

Example 2 with ResponseMessage

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

the class OSSMultipartOperation method uploadPart.

/**
 * Upload part.
 */
public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest) throws OSSException, ClientException {
    assertParameterNotNull(uploadPartRequest, "uploadPartRequest");
    String key = uploadPartRequest.getKey();
    String bucketName = uploadPartRequest.getBucketName();
    String uploadId = uploadPartRequest.getUploadId();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(key, "key");
    ensureObjectKeyValid(key);
    assertStringNotNullOrEmpty(uploadId, "uploadId");
    if (uploadPartRequest.getInputStream() == null) {
        throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("MustSetContentStream"));
    }
    InputStream repeatableInputStream = null;
    try {
        repeatableInputStream = newRepeatableInputStream(uploadPartRequest.buildPartialStream());
    } catch (IOException ex) {
        logException("Cannot wrap to repeatable input stream: ", ex);
        throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
    }
    int partNumber = uploadPartRequest.getPartNumber();
    if (!checkParamRange(partNumber, 0, false, MAX_PART_NUMBER, true)) {
        throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("PartNumberOutOfRange"));
    }
    Map<String, String> headers = new HashMap<String, String>();
    populateUploadPartOptionalHeaders(uploadPartRequest, headers);
    // Use a LinkedHashMap to preserve the insertion order.
    Map<String, String> params = new LinkedHashMap<String, String>();
    params.put(PART_NUMBER, Integer.toString(partNumber));
    params.put(UPLOAD_ID, uploadId);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.PUT).setBucket(bucketName).setKey(key).setParameters(params).setHeaders(headers).setInputStream(repeatableInputStream).setInputSize(uploadPartRequest.getPartSize()).setUseChunkEncoding(uploadPartRequest.isUseChunkEncoding()).setOriginalRequest(uploadPartRequest).build();
    final ProgressListener listener = uploadPartRequest.getProgressListener();
    ResponseMessage response = null;
    try {
        publishProgress(listener, ProgressEventType.TRANSFER_PART_STARTED_EVENT);
        response = doOperation(request, emptyResponseParser, bucketName, key);
        publishProgress(listener, ProgressEventType.TRANSFER_PART_COMPLETED_EVENT);
    } catch (RuntimeException e) {
        publishProgress(listener, ProgressEventType.TRANSFER_PART_FAILED_EVENT);
        throw e;
    }
    UploadPartResult result = new UploadPartResult();
    result.setPartNumber(partNumber);
    result.setETag(trimQuotes(response.getHeaders().get(OSSHeaders.ETAG)));
    result.setRequestId(response.getRequestId());
    result.setPartSize(uploadPartRequest.getPartSize());
    ResponseParsers.setCRC(result, response);
    if (getInnerClient().getClientConfiguration().isCrcCheckEnabled()) {
        OSSUtils.checkChecksum(result.getClientCRC(), result.getServerCRC(), result.getRequestId());
    }
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) IOUtils.newRepeatableInputStream(com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) LinkedHashMap(java.util.LinkedHashMap) UploadPartResult(com.aliyun.oss.model.UploadPartResult) ProgressListener(com.aliyun.oss.event.ProgressListener) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException)

Example 3 with ResponseMessage

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

the class ResourceUtils method loadResponseFromResource.

public static ResponseMessage loadResponseFromResource(String resourceName) throws FileNotFoundException {
    ResponseMessage response = new ResponseMessage(null);
    if (resourceName != null) {
        String filename = getTestFilename(resourceName);
        File file = new File(filename);
        FileInputStream fis = new FileInputStream(file);
        response.setContent(fis);
        response.setContentLength(file.length());
    }
    return response;
}
Also used : ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with ResponseMessage

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

the class OSSBucketOperation method getBucketMetadata.

public BucketMetadata getBucketMetadata(GenericRequest genericRequest) throws OSSException, ClientException {
    assertParameterNotNull(genericRequest, "genericRequest");
    String bucketName = genericRequest.getBucketName();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.HEAD).setBucket(bucketName).setOriginalRequest(genericRequest).build();
    List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
    reponseHandlers.add(new ResponseHandler() {

        @Override
        public void handle(ResponseMessage response) throws ServiceException, ClientException {
            if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                safeCloseResponse(response);
                throw ExceptionFactory.createOSSException(response.getHeaders().get(OSSHeaders.OSS_HEADER_REQUEST_ID), OSSErrorCode.NO_SUCH_BUCKET, OSS_RESOURCE_MANAGER.getString("NoSuchBucket"));
            }
        }
    });
    return doOperation(request, ResponseParsers.getBucketMetadataResponseParser, bucketName, null, true, null, reponseHandlers);
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) ServiceException(com.aliyun.oss.ServiceException) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ArrayList(java.util.ArrayList) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) ClientException(com.aliyun.oss.ClientException)

Example 5 with ResponseMessage

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

the class OSSBucketOperation method createBucket.

/**
 * Create a bucket.
 */
public Bucket createBucket(CreateBucketRequest createBucketRequest) throws OSSException, ClientException {
    assertParameterNotNull(createBucketRequest, "createBucketRequest");
    String bucketName = createBucketRequest.getBucketName();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    Map<String, String> headers = new HashMap<String, String>();
    addOptionalACLHeader(headers, createBucketRequest.getCannedACL());
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.PUT).setBucket(bucketName).setHeaders(headers).setInputStreamWithLength(createBucketRequestMarshaller.marshall(createBucketRequest)).setOriginalRequest(createBucketRequest).build();
    ResponseMessage result = doOperation(request, emptyResponseParser, bucketName, null);
    return new Bucket(bucketName, result.getRequestId());
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Bucket(com.aliyun.oss.model.Bucket) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage)

Aggregations

ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)7 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)5 ClientException (com.aliyun.oss.ClientException)4 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)4 ServiceException (com.aliyun.oss.ServiceException)3 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ClientConfiguration (com.aliyun.oss.ClientConfiguration)1 OSSException (com.aliyun.oss.OSSException)1 RequestSigner (com.aliyun.oss.common.auth.RequestSigner)1 RequestChecksumHanlder (com.aliyun.oss.common.comm.RequestChecksumHanlder)1 RequestHandler (com.aliyun.oss.common.comm.RequestHandler)1 RequestProgressHanlder (com.aliyun.oss.common.comm.RequestProgressHanlder)1 ResponseChecksumHandler (com.aliyun.oss.common.comm.ResponseChecksumHandler)1 ResponseProgressHandler (com.aliyun.oss.common.comm.ResponseProgressHandler)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 IOUtils.newRepeatableInputStream (com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream)1