Example usage for com.amazonaws.services.s3.model AmazonS3Exception getErrorType

List of usage examples for com.amazonaws.services.s3.model AmazonS3Exception getErrorType

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model AmazonS3Exception getErrorType.

Prototype

public ErrorType getErrorType() 

Source Link

Document

Indicates who is responsible for this exception (caller, service, or unknown).

Usage

From source file:com.netflix.exhibitor.core.backup.s3.S3BackupProvider.java

License:Apache License

@Override
public BackupStream getBackupStream(Exhibitor exhibitor, BackupMetaData backup,
        Map<String, String> configValues) throws Exception {
    long startMs = System.currentTimeMillis();
    RetryPolicy retryPolicy = makeRetryPolicy(configValues);
    S3Object object = null;/*from  w  ww  .  j av  a2  s .  co  m*/
    int retryCount = 0;
    while (object == null) {
        try {
            object = s3Client.getObject(configValues.get(CONFIG_BUCKET.getKey()), toKey(backup, configValues));
        } catch (AmazonS3Exception e) {
            if (e.getErrorType() == AmazonServiceException.ErrorType.Client) {
                exhibitor.getLog().add(ActivityLog.Type.ERROR,
                        "Amazon client error: " + ActivityLog.getExceptionMessage(e));
                return null;
            }

            if (!retryPolicy.allowRetry(retryCount++, System.currentTimeMillis() - startMs,
                    RetryLoop.getDefaultRetrySleeper())) {
                exhibitor.getLog().add(ActivityLog.Type.ERROR,
                        "Retries exhausted: " + ActivityLog.getExceptionMessage(e));
                return null;
            }
        }
    }

    final Throttle throttle = makeThrottle(configValues);
    final InputStream in = object.getObjectContent();
    final InputStream wrappedstream = new InputStream() {
        @Override
        public void close() throws IOException {
            in.close();
        }

        @Override
        public int read() throws IOException {
            throttle.throttle(1);
            return in.read();
        }

        @Override
        public int read(byte[] b) throws IOException {
            int bytesRead = in.read(b);
            if (bytesRead > 0) {
                throttle.throttle(bytesRead);
            }
            return bytesRead;
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            int bytesRead = in.read(b, off, len);
            if (bytesRead > 0) {
                throttle.throttle(bytesRead);
            }
            return bytesRead;
        }
    };

    return new BackupStream() {
        @Override
        public InputStream getStream() {
            return wrappedstream;
        }

        @Override
        public void close() throws IOException {
            in.close();
        }
    };
}