Search in sources :

Example 1 with ProgressInputStream

use of com.aliyun.oss.event.ProgressInputStream in project aliyun-oss-java-sdk by aliyun.

the class OSSObjectOperation method getObject.

/**
 * Pull an object from oss.
 */
public OSSObject getObject(GetObjectRequest getObjectRequest) throws OSSException, ClientException {
    assertParameterNotNull(getObjectRequest, "getObjectRequest");
    String bucketName = null;
    String key = null;
    RequestMessage request = null;
    if (!getObjectRequest.isUseUrlSignature()) {
        assertParameterNotNull(getObjectRequest, "getObjectRequest");
        bucketName = getObjectRequest.getBucketName();
        key = getObjectRequest.getKey();
        assertParameterNotNull(bucketName, "bucketName");
        assertParameterNotNull(key, "key");
        ensureBucketNameValid(bucketName);
        ensureObjectKeyValid(key);
        Map<String, String> headers = new HashMap<String, String>();
        populateGetObjectRequestHeaders(getObjectRequest, headers);
        Map<String, String> params = new HashMap<String, String>();
        populateResponseHeaderParameters(params, getObjectRequest.getResponseHeaders());
        String process = getObjectRequest.getProcess();
        if (process != null) {
            params.put(RequestParameters.SUBRESOURCE_PROCESS, process);
        }
        request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.GET).setBucket(bucketName).setKey(key).setHeaders(headers).setParameters(params).setOriginalRequest(getObjectRequest).build();
    } else {
        request = new RequestMessage(getObjectRequest, bucketName, key);
        request.setMethod(HttpMethod.GET);
        request.setAbsoluteUrl(getObjectRequest.getAbsoluteUri());
        request.setUseUrlSignature(true);
        request.setHeaders(getObjectRequest.getHeaders());
    }
    final ProgressListener listener = getObjectRequest.getProgressListener();
    OSSObject ossObject = null;
    try {
        publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
        ossObject = doOperation(request, new GetObjectResponseParser(bucketName, key), bucketName, key, true);
        InputStream instream = ossObject.getObjectContent();
        ProgressInputStream progressInputStream = new ProgressInputStream(instream, listener) {

            @Override
            protected void onEOF() {
                publishProgress(getListener(), ProgressEventType.TRANSFER_COMPLETED_EVENT);
            }
        };
        CRC64 crc = new CRC64();
        CheckedInputStream checkedInputstream = new CheckedInputStream(progressInputStream, crc);
        ossObject.setObjectContent(checkedInputstream);
    } catch (RuntimeException e) {
        publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
        throw e;
    }
    return ossObject;
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) CRC64(com.aliyun.oss.common.utils.CRC64) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) 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) CheckedInputStream(java.util.zip.CheckedInputStream) ProgressListener(com.aliyun.oss.event.ProgressListener) GetObjectResponseParser(com.aliyun.oss.internal.ResponseParsers.GetObjectResponseParser) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Example 2 with ProgressInputStream

use of com.aliyun.oss.event.ProgressInputStream 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 3 with ProgressInputStream

use of com.aliyun.oss.event.ProgressInputStream in project aliyun-oss-java-sdk by aliyun.

the class OSSUdfOperation method getUdfApplicationLog.

public UdfApplicationLog getUdfApplicationLog(GetUdfApplicationLogRequest getUdfApplicationLogRequest) throws OSSException, ClientException {
    assertParameterNotNull(getUdfApplicationLogRequest, "resizeUdfApplicationRequest");
    String udfName = getUdfApplicationLogRequest.getName();
    assertParameterNotNull(udfName, "udfName");
    ensureBucketNameValid(udfName);
    Map<String, String> params = new HashMap<String, String>();
    populateGetUdfApplicationLogParameters(params, getUdfApplicationLogRequest);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.GET).setParameters(params).setOriginalRequest(getUdfApplicationLogRequest).build();
    ProgressListener listener = getUdfApplicationLogRequest.getProgressListener();
    UdfApplicationLog udfApplicationLog = null;
    try {
        publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
        udfApplicationLog = doOperation(request, new GetUdfApplicationLogResponseParser(udfName), null, null, true);
        InputStream instream = udfApplicationLog.getLogContent();
        ProgressInputStream progressInputStream = new ProgressInputStream(instream, listener) {

            @Override
            protected void onEOF() {
                publishProgress(getListener(), ProgressEventType.TRANSFER_COMPLETED_EVENT);
            }
        };
        CRC64 crc = new CRC64();
        CheckedInputStream checkedInputstream = new CheckedInputStream(progressInputStream, crc);
        udfApplicationLog.setLogContent(checkedInputstream);
    } catch (RuntimeException e) {
        publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
        throw e;
    }
    return udfApplicationLog;
}
Also used : GetUdfApplicationLogResponseParser(com.aliyun.oss.internal.ResponseParsers.GetUdfApplicationLogResponseParser) CRC64(com.aliyun.oss.common.utils.CRC64) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) IOUtils.newRepeatableInputStream(com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream) InputStream(java.io.InputStream) CheckedInputStream(java.util.zip.CheckedInputStream) UdfApplicationLog(com.aliyun.oss.model.UdfApplicationLog) ProgressListener(com.aliyun.oss.event.ProgressListener) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Aggregations

ProgressInputStream (com.aliyun.oss.event.ProgressInputStream)3 ProgressListener (com.aliyun.oss.event.ProgressListener)3 InputStream (java.io.InputStream)3 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)2 CRC64 (com.aliyun.oss.common.utils.CRC64)2 IOUtils.newRepeatableInputStream (com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 CheckedInputStream (java.util.zip.CheckedInputStream)2 RepeatableFileInputStream (com.aliyun.oss.common.comm.io.RepeatableFileInputStream)1 GetObjectResponseParser (com.aliyun.oss.internal.ResponseParsers.GetObjectResponseParser)1 GetUdfApplicationLogResponseParser (com.aliyun.oss.internal.ResponseParsers.GetUdfApplicationLogResponseParser)1 OSSObject (com.aliyun.oss.model.OSSObject)1 UdfApplicationLog (com.aliyun.oss.model.UdfApplicationLog)1