Example usage for com.amazonaws AmazonClientException AmazonClientException

List of usage examples for com.amazonaws AmazonClientException AmazonClientException

Introduction

In this page you can find the example usage for com.amazonaws AmazonClientException AmazonClientException.

Prototype

public AmazonClientException(Throwable t) 

Source Link

Usage

From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java

License:Open Source License

public void deleteMessage(final DeleteMessageRequest request)
        throws AmazonServiceException, AmazonClientException {
    if (request == null) {
        throw new AmazonClientException("Null DeleteMessageRequest");
    }/*from w  w  w  .  j a  v  a 2 s.c o  m*/
    final String queueUrl = request.getQueueUrl();
    checkURLForException(queueUrl);
    checkStringForExceptionMarker(request.getReceiptHandle());
    // Ignoring the documented exception: InvalidIdFormatException
    if (!allQueues.containsKey(queueUrl)) {
        throw new AmazonServiceException("Queue Not Found: " + queueUrl);
    }
    if (!retrievedMessages.containsKey(queueUrl)) {
        throw new AmazonServiceException("Queue Not Found: " + queueUrl);
    }
    if (!retrievedMessages.get(queueUrl).containsKey(request.getReceiptHandle())) {
        throw new ReceiptHandleIsInvalidException("Reciept Handle Not Found: " + request.getReceiptHandle());
    }
    retrievedMessages.get(queueUrl).remove(request.getReceiptHandle());
}

From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java

License:Open Source License

public ReceiveMessageResult receiveMessage(final ReceiveMessageRequest request)
        throws AmazonServiceException, AmazonClientException {
    if (request == null) {
        throw new AmazonClientException("Null ReceiveMessageRequest");
    }//from w  ww.  j a v  a2  s.  co m
    final String queueUrl = request.getQueueUrl();
    checkURLForException(queueUrl);
    // Per documentation throws OverLimitException, but in my testing,
    // they actually only throw AmazonServiceException
    final Integer max = request.getMaxNumberOfMessages();
    if (max == null || max < 1 || max > 10) {
        throw new AmazonServiceException("MaxNumberOfMessages must be a value between [1,10]");
    }
    final ReceiveMessageResult result = new ReceiveMessageResult();
    int received = 0;
    boolean avail = true;
    while (received < request.getMaxNumberOfMessages() && avail) {
        try {
            final Message msg = allQueues.get(queueUrl).remove(0);
            received++;
            msg.setReceiptHandle(RECEIPT_ID_PREFIX + incrementer.getAndIncrement());
            retrievedMessages.get(queueUrl).put(msg.getReceiptHandle(), msg);
            result.withMessages(msg);
        } catch (Exception ex) {
            avail = false;
        }
    }
    return result;
}

From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java

License:Open Source License

public ListQueuesResult listQueues(final ListQueuesRequest request)
        throws AmazonServiceException, AmazonClientException {
    if (request == null) {
        throw new AmazonClientException("Null ListQueuesRequest");
    }/*w w w .j a  va  2 s .  c  o m*/
    checkStringForExceptionMarker(request.getQueueNamePrefix());

    final String effectivePrefix = QUEUE_URL_PREFIX
            + (request.getQueueNamePrefix() == null ? "" : request.getQueueNamePrefix());
    final ListQueuesResult result = new ListQueuesResult();
    for (final String url : allQueues.keySet()) {
        if (StringUtils.startsWith(url, effectivePrefix)) {
            result.withQueueUrls(url);
        }
    }
    return result;
}

From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java

License:Open Source License

public void deleteQueue(final DeleteQueueRequest request) throws AmazonServiceException, AmazonClientException {
    if (request == null) {
        throw new AmazonClientException("Null DeleteQueueRequest");
    }//  ww  w.j  av a 2s .  c  om
    final String queueUrl = request.getQueueUrl();
    checkURLForException(queueUrl);
    if (!allQueues.containsKey(queueUrl)) {
        throw new AmazonServiceException("Queue Not Found: " + queueUrl);
    }
    allQueues.remove(queueUrl);
    if (retrievedMessages.containsKey(queueUrl)) {
        retrievedMessages.remove(queueUrl);
    }
}

From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java

License:Open Source License

public GetQueueAttributesResult getQueueAttributes(final GetQueueAttributesRequest request)
        throws AmazonServiceException, AmazonClientException {
    if (request == null) {
        throw new AmazonClientException("Null GetQueueAttributesRequest");
    }/*from   w  w w  .j  av a  2s.  c o  m*/
    final String queueUrl = request.getQueueUrl();
    checkURLForException(queueUrl);
    for (final String attb : request.getAttributeNames()) {
        checkStringForExceptionMarker(attb);
        if (!attbs.contains(attb)) {
            throw new InvalidAttributeNameException("Invalid Attribute Name: " + attb);
        }
    }

    final Map<String, String> results = new ConcurrentHashMap<String, String>();
    final boolean hasAll = request.getAttributeNames().contains(ALL);
    if (hasAll || request.getAttributeNames().contains(NUM_MSGS)) {
        results.put(NUM_MSGS, allQueues.get(queueUrl) == null ? "0" : allQueues.get(queueUrl).size() + "");
    }
    if (hasAll || request.getAttributeNames().contains(NUM_NOT_VISIBLE)) {
        results.put(NUM_NOT_VISIBLE,
                retrievedMessages.get(queueUrl) == null ? "0" : retrievedMessages.get(queueUrl).size() + "");
    }
    if (hasAll || request.getAttributeNames().contains(VIS_TIMEOUT)) {
        throw new RuntimeException(NYI_EXCEPTION);
    }
    if (hasAll || request.getAttributeNames().contains(CREATED_TIMESTAMP)) {
        throw new RuntimeException(NYI_EXCEPTION);
    }
    if (hasAll || request.getAttributeNames().contains(MODIFIED_TIMESTAMP)) {
        throw new RuntimeException(NYI_EXCEPTION);
    }
    if (hasAll || request.getAttributeNames().contains(POLICY)) {
        throw new RuntimeException(NYI_EXCEPTION);
    }
    if (hasAll || request.getAttributeNames().contains(MAX_SIZE)) {
        throw new RuntimeException(NYI_EXCEPTION);
    }
    if (hasAll || request.getAttributeNames().contains(RETENTION)) {
        throw new RuntimeException(NYI_EXCEPTION);
    }
    if (hasAll || request.getAttributeNames().contains(ARN)) {
        throw new RuntimeException(NYI_EXCEPTION);
    }
    if (hasAll || request.getAttributeNames().contains(MSGS_DELAYED)) {
        throw new RuntimeException(NYI_EXCEPTION);
    }
    if (hasAll || request.getAttributeNames().contains(DELAY_SEC)) {
        throw new RuntimeException(NYI_EXCEPTION);
    }

    return new GetQueueAttributesResult().withAttributes(results);
}

From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java

License:Open Source License

private static void checkStringForExceptionMarker(final String str) {
    final String src = StringUtils.isBlank(str) ? "" : str.toLowerCase();
    if (src.contains(MARKER_CLIENT_EXCEPTION)) {
        throw new AmazonClientException("Forced AmazonClientException");
    }//from  w  w  w.  ja v a2  s . c  om
    if (src.contains(MARKER_SERVICE_EXCEPTION)) {
        throw new AmazonServiceException("Forced AmazonServiceException");
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.CloudFrontBinaryManager.java

License:Apache License

private static PrivateKey loadPrivateKey(String privateKeyPath) throws InvalidKeySpecException, IOException {
    if (privateKeyPath == null) {
        return null;
    }//from  w w w .  j  a v a2 s.co  m

    try (FileInputStream is = new FileInputStream(new File(privateKeyPath))) {
        if (privateKeyPath.toLowerCase().endsWith(".pem")) {
            return PEM.readPrivateKey(is);
        }

        if (privateKeyPath.toLowerCase().endsWith(".der")) {
            return RSA.privateKeyFromPKCS8(IOUtils.toByteArray(is));
        }

        throw new AmazonClientException("Unsupported file type for private key");
    }
}

From source file:org.springframework.integration.aws.outbound.S3MessageHandler.java

License:Apache License

private Transfer upload(Message<?> requestMessage) {
    Object payload = requestMessage.getPayload();
    String bucketName = obtainBucket(requestMessage);

    String key = null;//  w  w  w .j av  a  2s.  c om
    if (this.keyExpression != null) {
        key = this.keyExpression.getValue(this.evaluationContext, requestMessage, String.class);
    }

    if (payload instanceof File && ((File) payload).isDirectory()) {
        File fileToUpload = (File) payload;
        if (key == null) {
            key = fileToUpload.getName();
        }
        return this.transferManager.uploadDirectory(bucketName, key, fileToUpload, true,
                new MessageHeadersObjectMetadataProvider(requestMessage.getHeaders()));
    } else {
        ObjectMetadata metadata = new ObjectMetadata();
        if (this.uploadMetadataProvider != null) {
            this.uploadMetadataProvider.populateMetadata(metadata, requestMessage);
        }
        InputStream inputStream;
        if (payload instanceof InputStream) {
            inputStream = (InputStream) payload;
        } else if (payload instanceof File) {
            File fileToUpload = (File) payload;
            if (key == null) {
                key = fileToUpload.getName();
            }
            try {
                inputStream = new FileInputStream(fileToUpload);

                if (metadata.getContentLength() == 0) {
                    metadata.setContentLength(fileToUpload.length());
                }
                if (metadata.getContentType() == null) {
                    metadata.setContentType(Mimetypes.getInstance().getMimetype(fileToUpload));
                }

            } catch (FileNotFoundException e) {
                throw new AmazonClientException(e);
            }
        } else if (payload instanceof byte[]) {
            inputStream = new ByteArrayInputStream((byte[]) payload);
        } else {
            throw new IllegalArgumentException("Unsupported payload type: [" + payload.getClass()
                    + "]. The only supported payloads for the upload request are "
                    + "java.io.File, java.io.InputStream, byte[] and PutObjectRequest.");
        }

        Assert.state(key != null,
                "The 'keyExpression' must not be null for non-File payloads and can't evaluate to null. "
                        + "Root object is: " + requestMessage);

        if (key == null) {
            if (this.keyExpression != null) {
                throw new IllegalStateException(
                        "The 'keyExpression' [" + this.keyExpression.getExpressionString()
                                + "] must not evaluate to null. Root object is: " + requestMessage);
            } else {
                throw new IllegalStateException("Specify a 'keyExpression' for non-java.io.File payloads");
            }
        }

        if (metadata.getContentMD5() == null) {
            String contentMd5 = null;
            try {
                contentMd5 = Md5Utils.md5AsBase64(StreamUtils.copyToByteArray(inputStream));
                if (inputStream.markSupported()) {
                    inputStream.reset();
                }
                metadata.setContentMD5(contentMd5);
            } catch (IOException e) {
                throw new MessageHandlingException(requestMessage, e);
            }
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata);

        S3ProgressListener progressListener = this.s3ProgressListener;

        if (this.objectAclExpression != null) {
            Object acl = this.objectAclExpression.getValue(this.evaluationContext, requestMessage);
            Assert.state(acl instanceof AccessControlList || acl instanceof CannedAccessControlList,
                    "The 'objectAclExpression' [" + this.objectAclExpression.getExpressionString()
                            + "] must evaluate to com.amazonaws.services.s3.model.AccessControlList "
                            + "or must evaluate to com.amazonaws.services.s3.model.CannedAccessControlList. "
                            + "Gotten: [" + acl + "]");

            SetObjectAclRequest aclRequest;

            if (acl instanceof AccessControlList) {
                aclRequest = new SetObjectAclRequest(bucketName, key, (AccessControlList) acl);
            } else {
                aclRequest = new SetObjectAclRequest(bucketName, key, (CannedAccessControlList) acl);
            }

            final SetObjectAclRequest theAclRequest = aclRequest;
            progressListener = new S3ProgressListener() {

                @Override
                public void onPersistableTransfer(PersistableTransfer persistableTransfer) {

                }

                @Override
                public void progressChanged(ProgressEvent progressEvent) {
                    if (ProgressEventType.TRANSFER_COMPLETED_EVENT.equals(progressEvent.getEventType())) {
                        S3MessageHandler.this.transferManager.getAmazonS3Client().setObjectAcl(theAclRequest);
                    }
                }

            };

            if (this.s3ProgressListener != null) {
                progressListener = new S3ProgressListenerChain(this.s3ProgressListener, progressListener);
            }

        }

        if (progressListener != null) {
            return this.transferManager.upload(putObjectRequest, progressListener);
        } else {
            return this.transferManager.upload(putObjectRequest);
        }
    }
}

From source file:org.xmlsh.aws.util.AWSEnvCredentialsProvider.java

License:Open Source License

@Override
public AWSCredentials getCredentials() {
    if (access_key != null && secret_key != null) {

        return new BasicAWSCredentials(access_key, secret_key);
    }/*w  ww  .ja  v a2  s .  c  o m*/

    throw new AmazonClientException("Unable to load AWS credentials from environment variables " + "("
            + ACCESS_KEY_ENV_VAR + " and " + SECRET_KEY_ENV_VAR + ")");
}

From source file:test1.SimpleQueueServiceSample.java

License:Open Source License

public static AmazonSQS init() {
    AWSCredentials credentials = null;//from w w  w.j  av  a  2 s .  c  o m
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException(e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usEast1);
    return sqs;
}