Search in sources :

Example 1 with ResponseHandler

use of com.aliyun.oss.common.comm.ResponseHandler 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 ResponseHandler

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

the class LiveChannelOperation method createLiveChannel.

public CreateLiveChannelResult createLiveChannel(CreateLiveChannelRequest createLiveChannelRequest) throws OSSException, ClientException {
    assertParameterNotNull(createLiveChannelRequest, "createLiveChannelRequest");
    String bucketName = createLiveChannelRequest.getBucketName();
    String liveChannelName = createLiveChannelRequest.getLiveChannelName();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(liveChannelName, "liveChannelName");
    ensureLiveChannelNameValid(liveChannelName);
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put(RequestParameters.SUBRESOURCE_LIVE, null);
    byte[] rawContent = createLiveChannelRequestMarshaller.marshall(createLiveChannelRequest);
    Map<String, String> headers = new HashMap<String, String>();
    addRequestRequiredHeaders(headers, rawContent);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.PUT).setBucket(bucketName).setKey(liveChannelName).setParameters(parameters).setHeaders(headers).setInputSize(rawContent.length).setInputStream(new ByteArrayInputStream(rawContent)).setOriginalRequest(createLiveChannelRequest).build();
    List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
    reponseHandlers.add(new OSSCallbackErrorResponseHandler());
    return doOperation(request, createLiveChannelResponseParser, bucketName, liveChannelName, true);
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ArrayList(java.util.ArrayList)

Example 3 with ResponseHandler

use of com.aliyun.oss.common.comm.ResponseHandler 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 4 with ResponseHandler

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

the class OSSMultipartOperation method completeMultipartUpload.

/**
 * Complete multipart upload.
 */
public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest completeMultipartUploadRequest) throws OSSException, ClientException {
    assertParameterNotNull(completeMultipartUploadRequest, "completeMultipartUploadRequest");
    String key = completeMultipartUploadRequest.getKey();
    String bucketName = completeMultipartUploadRequest.getBucketName();
    String uploadId = completeMultipartUploadRequest.getUploadId();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(key, "key");
    ensureObjectKeyValid(key);
    assertStringNotNullOrEmpty(uploadId, "uploadId");
    ensureCallbackValid(completeMultipartUploadRequest.getCallback());
    Map<String, String> headers = new HashMap<String, String>();
    populateCompleteMultipartUploadOptionalHeaders(completeMultipartUploadRequest, headers);
    populateRequestCallback(headers, completeMultipartUploadRequest.getCallback());
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put(UPLOAD_ID, uploadId);
    List<PartETag> partETags = completeMultipartUploadRequest.getPartETags();
    Collections.sort(partETags, new Comparator<PartETag>() {

        @Override
        public int compare(PartETag p1, PartETag p2) {
            return p1.getPartNumber() - p2.getPartNumber();
        }
    });
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.POST).setBucket(bucketName).setKey(key).setHeaders(headers).setParameters(parameters).setInputStreamWithLength(completeMultipartUploadRequestMarshaller.marshall(completeMultipartUploadRequest)).setOriginalRequest(completeMultipartUploadRequest).build();
    List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
    reponseHandlers.add(new OSSCallbackErrorResponseHandler());
    CompleteMultipartUploadResult result = null;
    if (!isNeedReturnResponse(completeMultipartUploadRequest)) {
        result = doOperation(request, completeMultipartUploadResponseParser, bucketName, key, true);
    } else {
        result = doOperation(request, completeMultipartUploadProcessResponseParser, bucketName, key, true, null, reponseHandlers);
    }
    result.setClientCRC(calcObjectCRCFromParts(completeMultipartUploadRequest.getPartETags()));
    if (getInnerClient().getClientConfiguration().isCrcCheckEnabled()) {
        OSSUtils.checkChecksum(result.getClientCRC(), result.getServerCRC(), result.getRequestId());
    }
    return result;
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Example 5 with ResponseHandler

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

the class OSSOperation method doOperation.

protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, String bucketName, String key, boolean keepResponseOpen, List<RequestHandler> requestHandlers, List<ResponseHandler> reponseHandlers) throws OSSException, ClientException {
    final WebServiceRequest originalRequest = request.getOriginalRequest();
    request.getHeaders().putAll(client.getClientConfiguration().getDefaultHeaders());
    request.getHeaders().putAll(originalRequest.getHeaders());
    request.getParameters().putAll(originalRequest.getParameters());
    ExecutionContext context = createDefaultContext(request.getMethod(), bucketName, key);
    if (context.getCredentials().useSecurityToken() && !request.isUseUrlSignature()) {
        request.addHeader(OSSHeaders.OSS_SECURITY_TOKEN, context.getCredentials().getSecurityToken());
    }
    context.addRequestHandler(new RequestProgressHanlder());
    if (requestHandlers != null) {
        for (RequestHandler handler : requestHandlers) context.addRequestHandler(handler);
    }
    if (client.getClientConfiguration().isCrcCheckEnabled()) {
        context.addRequestHandler(new RequestChecksumHanlder());
    }
    context.addResponseHandler(new ResponseProgressHandler(originalRequest));
    if (reponseHandlers != null) {
        for (ResponseHandler handler : reponseHandlers) context.addResponseHandler(handler);
    }
    if (client.getClientConfiguration().isCrcCheckEnabled()) {
        context.addResponseHandler(new ResponseChecksumHandler());
    }
    List<RequestSigner> signerHandlers = this.client.getClientConfiguration().getSignerHandlers();
    if (signerHandlers != null) {
        for (RequestSigner signer : signerHandlers) {
            context.addSignerHandler(signer);
        }
    }
    ResponseMessage response = send(request, context, keepResponseOpen);
    try {
        return parser.parse(response);
    } catch (ResponseParseException rpe) {
        OSSException oe = ExceptionFactory.createInvalidResponseException(response.getRequestId(), rpe.getMessage(), rpe);
        logException("Unable to parse response error: ", rpe);
        throw oe;
    }
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) RequestChecksumHanlder(com.aliyun.oss.common.comm.RequestChecksumHanlder) OSSException(com.aliyun.oss.OSSException) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) RequestProgressHanlder(com.aliyun.oss.common.comm.RequestProgressHanlder) ResponseProgressHandler(com.aliyun.oss.common.comm.ResponseProgressHandler) ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) WebServiceRequest(com.aliyun.oss.model.WebServiceRequest) RequestHandler(com.aliyun.oss.common.comm.RequestHandler) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ResponseChecksumHandler(com.aliyun.oss.common.comm.ResponseChecksumHandler) RequestSigner(com.aliyun.oss.common.auth.RequestSigner)

Aggregations

ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)7 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)6 ArrayList (java.util.ArrayList)5 ClientException (com.aliyun.oss.ClientException)4 ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)4 ServiceException (com.aliyun.oss.ServiceException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)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 RepeatableFileInputStream (com.aliyun.oss.common.comm.io.RepeatableFileInputStream)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1