Search in sources :

Example 6 with ProgressListener

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));
}
Also used : WebServiceRequest(com.aliyun.oss.model.WebServiceRequest) ProgressListener(com.aliyun.oss.event.ProgressListener) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) InputStream(java.io.InputStream)

Example 7 with ProgressListener

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);
    }
}
Also used : ProgressListener(com.aliyun.oss.event.ProgressListener) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) InputStream(java.io.InputStream)

Example 8 with ProgressListener

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;
}
Also used : ArrayList(java.util.ArrayList) ProgressListener(com.aliyun.oss.event.ProgressListener) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) UploadPartResult(com.aliyun.oss.model.UploadPartResult) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with ProgressListener

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;
}
Also used : ProgressListener(com.aliyun.oss.event.ProgressListener) UploadFileResult(com.aliyun.oss.model.UploadFileResult) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) UploadPartResult(com.aliyun.oss.model.UploadPartResult) LogUtils.logException(com.aliyun.oss.common.utils.LogUtils.logException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with ProgressListener

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

ProgressListener (com.aliyun.oss.event.ProgressListener)11 InputStream (java.io.InputStream)7 ProgressInputStream (com.aliyun.oss.event.ProgressInputStream)6 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)5 IOUtils.newRepeatableInputStream (com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 ExecutionException (java.util.concurrent.ExecutionException)4 CheckedInputStream (java.util.zip.CheckedInputStream)4 ClientException (com.aliyun.oss.ClientException)3 UploadPartResult (com.aliyun.oss.model.UploadPartResult)3 ArrayList (java.util.ArrayList)3 RepeatableFileInputStream (com.aliyun.oss.common.comm.io.RepeatableFileInputStream)2 CRC64 (com.aliyun.oss.common.utils.CRC64)2 LogUtils.logException (com.aliyun.oss.common.utils.LogUtils.logException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)1