use of com.aliyun.oss.event.ProgressListener in project aliyun-oss-java-sdk by aliyun.
the class RequestProgressHanlder method handle.
@Override
public void handle(RequestMessage request) throws OSSException, ClientException {
final WebServiceRequest originalRequest = request.getOriginalRequest();
final ProgressListener listener = originalRequest.getProgressListener();
Map<String, String> headers = request.getHeaders();
String s = headers.get(HttpHeaders.CONTENT_LENGTH);
if (s != null) {
try {
long contentLength = Long.parseLong(s);
publishRequestContentLength(listener, contentLength);
} catch (NumberFormatException e) {
logException("Cannot parse the Content-Length header of the request: ", e);
}
}
InputStream content = request.getContent();
if (content == null) {
return;
}
if (!content.markSupported()) {
content = new BufferedInputStream(content);
}
request.setContent(listener == ProgressListener.NOOP ? content : ProgressInputStream.inputStreamForRequest(content, originalRequest));
}
use of com.aliyun.oss.event.ProgressListener in project aliyun-oss-java-sdk by aliyun.
the class ResponseProgressHandler method handle.
@Override
public void handle(ResponseMessage response) throws OSSException, ClientException {
final ProgressListener listener = this.originalRequest.getProgressListener();
Map<String, String> headers = response.getHeaders();
String s = headers.get(HttpHeaders.CONTENT_LENGTH);
if (s != null) {
try {
long contentLength = Long.parseLong(s);
publishResponseContentLength(listener, contentLength);
} catch (NumberFormatException e) {
logException("Cannot parse the Content-Length header of the response: ", e);
}
}
InputStream content = response.getContent();
if (content != null && listener != ProgressListener.NOOP) {
InputStream progressInputStream = ProgressInputStream.inputStreamForResponse(content, originalRequest);
response.setContent(progressInputStream);
}
}
use of com.aliyun.oss.event.ProgressListener in project aliyun-oss-java-sdk by aliyun.
the class OSSUploadOperation method upload.
private ArrayList<PartResult> upload(UploadCheckPoint uploadCheckPoint, UploadFileRequest uploadFileRequest) throws Throwable {
ArrayList<PartResult> taskResults = new ArrayList<PartResult>();
ExecutorService service = Executors.newFixedThreadPool(uploadFileRequest.getTaskNum());
ArrayList<Future<PartResult>> futures = new ArrayList<Future<PartResult>>();
ProgressListener listener = uploadFileRequest.getProgressListener();
// Compute the size of the data pending upload.
long contentLength = 0;
for (int i = 0; i < uploadCheckPoint.uploadParts.size(); i++) {
if (!uploadCheckPoint.uploadParts.get(i).isCompleted) {
contentLength += uploadCheckPoint.uploadParts.get(i).size;
}
}
ProgressPublisher.publishRequestContentLength(listener, contentLength);
uploadFileRequest.setProgressListener(null);
// Upload parts.
for (int i = 0; i < uploadCheckPoint.uploadParts.size(); i++) {
if (!uploadCheckPoint.uploadParts.get(i).isCompleted) {
futures.add(service.submit(new Task(i, "upload-" + i, uploadCheckPoint, i, uploadFileRequest, multipartOperation, listener)));
} else {
taskResults.add(new PartResult(i + 1, uploadCheckPoint.uploadParts.get(i).offset, uploadCheckPoint.uploadParts.get(i).size));
}
}
service.shutdown();
// Waiting for parts upload complete.
service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
for (Future<PartResult> future : futures) {
try {
PartResult tr = future.get();
taskResults.add(tr);
} catch (ExecutionException e) {
uploadFileRequest.setProgressListener(listener);
throw e.getCause();
}
}
// Sorts PartResult by the part numnber.
Collections.sort(taskResults, new Comparator<PartResult>() {
@Override
public int compare(PartResult p1, PartResult p2) {
return p1.getNumber() - p2.getNumber();
}
});
uploadFileRequest.setProgressListener(listener);
return taskResults;
}
use of com.aliyun.oss.event.ProgressListener in project aliyun-oss-java-sdk by aliyun.
the class OSSUploadOperation method uploadFileWithCheckpoint.
private UploadFileResult uploadFileWithCheckpoint(UploadFileRequest uploadFileRequest) throws Throwable {
UploadFileResult uploadFileResult = new UploadFileResult();
UploadCheckPoint uploadCheckPoint = new UploadCheckPoint();
// checkpoint file.
if (uploadFileRequest.isEnableCheckpoint()) {
// whole file needs the re-upload.
try {
uploadCheckPoint.load(uploadFileRequest.getCheckpointFile());
} catch (Exception e) {
remove(uploadFileRequest.getCheckpointFile());
}
// The file uploaded is updated, re-upload.
if (!uploadCheckPoint.isValid(uploadFileRequest.getUploadFile())) {
prepare(uploadCheckPoint, uploadFileRequest);
remove(uploadFileRequest.getCheckpointFile());
}
} else {
// The checkpoint is not enabled, re-upload.
prepare(uploadCheckPoint, uploadFileRequest);
}
// The progress tracker starts
ProgressListener listener = uploadFileRequest.getProgressListener();
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
// Concurrently upload parts.
List<PartResult> partResults = upload(uploadCheckPoint, uploadFileRequest);
for (PartResult partResult : partResults) {
if (partResult.isFailed()) {
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_PART_FAILED_EVENT);
throw partResult.getException();
}
}
// The progress tracker publishes the data.
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
// Complete parts.
CompleteMultipartUploadResult multipartUploadResult = complete(uploadCheckPoint, uploadFileRequest);
uploadFileResult.setMultipartUploadResult(multipartUploadResult);
// The checkpoint is enabled and upload the checkpoint file.
if (uploadFileRequest.isEnableCheckpoint()) {
remove(uploadFileRequest.getCheckpointFile());
}
return uploadFileResult;
}
use of com.aliyun.oss.event.ProgressListener 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;
}
Aggregations