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);
}
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);
}
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);
}
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;
}
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;
}
}
Aggregations