List of usage examples for com.amazonaws AmazonServiceException getStatusCode
public int getStatusCode()
From source file:org.finra.herd.service.helper.AwsServiceHelper.java
License:Apache License
/** * Handles the AmazonServiceException, throws corresponding exception based on status code in amazon exception. * * @param amazonServiceException the AWS exception that will be handled by this method. * @param message the message to include with this exception. * * @throws IllegalArgumentException the exception to be thrown with a HttpStatus.SC_BAD_REQUEST * @throws ObjectNotFoundException the exception to be thrown with a HttpStatus.SC_NOT_FOUND *///from w ww. j a v a 2 s . c om public void handleAmazonException(AmazonServiceException amazonServiceException, String message) throws IllegalArgumentException, ObjectNotFoundException { if (amazonServiceException.getStatusCode() == HttpStatus.SC_BAD_REQUEST) { throw new IllegalArgumentException(message + " Reason: " + amazonServiceException.getMessage(), amazonServiceException); } else if (amazonServiceException.getStatusCode() == HttpStatus.SC_NOT_FOUND) { throw new ObjectNotFoundException(message + " Reason: " + amazonServiceException.getMessage(), amazonServiceException); } throw amazonServiceException; }
From source file:org.freeeed.aws.SimpleQueueServiceSample.java
License:Apache License
public static void main(String[] args) throws Exception { /*//from w w w. j a v a2 s . com * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (~/.aws/credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", e); } AmazonSQS sqs = new AmazonSQSClient(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); sqs.setRegion(usWest2); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SQS"); System.out.println("===========================================\n"); try { // Create a queue System.out.println("Creating a new SQS queue called MyQueue.\n"); CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue"); String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); // List queues System.out.println("Listing all queues in your account.\n"); // TODO restore System.out.println(); // Send a message System.out.println("Sending a message to MyQueue.\n"); sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text.")); // Receive messages System.out.println("Receiving messages from MyQueue.\n"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message message : messages) { System.out.println(message.toString()); } System.out.println(); // Delete a message System.out.println("Deleting a message.\n"); String messageReceiptHandle = messages.get(0).getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageReceiptHandle)); // Delete a queue System.out.println("Deleting the test queue.\n"); sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java
License:Apache License
private Map<String, AttributeValue> getInternal(String tableName, Key key, int attempt) { GetItemRequest request = new GetItemRequest(tableName, key); try {/* w w w . j a v a2 s .com*/ GetItemResult result = ddb.getItem(request); Map<String, AttributeValue> attributes = result.getItem(); if (attributes == null || attributes.isEmpty()) { return null; } return attributes; } catch (AmazonServiceException e) { if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) { throw new IllegalArgumentException("no such table: " + tableName, e); } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) { //retry after a small pause DynamoDBUtil.sleepBeforeRetry(attempt); attempt++; return getInternal(tableName, key, attempt); } else { throw new DataStoreOperationException("problem with table: " + tableName + ", key: " + key, e); } } }
From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java
License:Apache License
private Map<String, AttributeValue> getConsistentInternal(String tableName, Key key, int attempt) { GetItemRequest request = new GetItemRequest(tableName, key); request.setConsistentRead(true);/* w ww .j a v a 2 s . c om*/ try { GetItemResult result = ddb.getItem(request); Map<String, AttributeValue> attributes = result.getItem(); if (attributes == null || attributes.isEmpty()) { return null; } return attributes; } catch (AmazonServiceException e) { if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) { throw new IllegalArgumentException("no such table: " + tableName, e); } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) { //retry after a small pause DynamoDBUtil.sleepBeforeRetry(attempt); attempt++; return getConsistentInternal(tableName, key, attempt); } else { throw new DataStoreOperationException("problem with table: " + tableName + ", key: " + key, e); } } }
From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java
License:Apache License
private void putItemInternal(String tableName, Map<String, AttributeValue> attributes, int attempt) throws DataAccessException { try {/* w w w . j av a2s.c o m*/ PutItemRequest request = new PutItemRequest(tableName, attributes); ddb.putItem(request); } catch (AmazonServiceException e) { if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) { throw new IllegalArgumentException("no such table: " + tableName, e); } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) { //retry after a small pause DynamoDBUtil.sleepBeforeRetry(attempt); attempt++; putItemInternal(tableName, attributes, attempt); } else { throw new DataStoreOperationException( "problem with table: " + tableName + ", attributes: " + attributes, e); } } }
From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java
License:Apache License
private void putItemVersionedInternal(String tableName, Key key, Map<String, AttributeValue> attributes, String expectedVersion, PersistentEntity persistentEntity, int attempt) throws DataAccessException { PutItemRequest request = new PutItemRequest(tableName, attributes) .withExpected(getOptimisticVersionCondition(expectedVersion)); try {// w w w .j a v a2 s . co m ddb.putItem(request); } catch (AmazonServiceException e) { if (DynamoDBUtil.AWS_ERR_CODE_CONDITIONAL_CHECK_FAILED.equals(e.getErrorCode())) { throw new OptimisticLockingException(persistentEntity, key); } else if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) { throw new IllegalArgumentException("no such table: " + tableName, e); } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) { //retry after a small pause DynamoDBUtil.sleepBeforeRetry(attempt); attempt++; putItemVersionedInternal(tableName, key, attributes, expectedVersion, persistentEntity, attempt); } else { throw new DataStoreOperationException( "problem with table: " + tableName + ", key: " + key + ", attributes: " + attributes, e); } } }
From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java
License:Apache License
private void updateItemInternal(String tableName, Key key, Map<String, AttributeValueUpdate> attributes, int attempt) throws DataAccessException { try {/*from w ww.j av a 2 s . c o m*/ UpdateItemRequest request = new UpdateItemRequest(tableName, key, attributes); ddb.updateItem(request); } catch (AmazonServiceException e) { if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) { throw new IllegalArgumentException("no such table: " + tableName, e); } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) { //retry after a small pause DynamoDBUtil.sleepBeforeRetry(attempt); attempt++; updateItemInternal(tableName, key, attributes, attempt); } else { throw new DataStoreOperationException( "problem with table: " + tableName + ", key: " + key + ", attributes: " + attributes, e); } } }
From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java
License:Apache License
private void updateItemVersionedInternal(String tableName, Key key, Map<String, AttributeValueUpdate> attributes, String expectedVersion, PersistentEntity persistentEntity, int attempt) throws DataAccessException { UpdateItemRequest request = new UpdateItemRequest(tableName, key, attributes) .withExpected(getOptimisticVersionCondition(expectedVersion)); try {/*from w w w. j av a2 s . com*/ ddb.updateItem(request); } catch (AmazonServiceException e) { if (DynamoDBUtil.AWS_ERR_CODE_CONDITIONAL_CHECK_FAILED.equals(e.getErrorCode())) { throw new OptimisticLockingException(persistentEntity, key); } else if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) { throw new IllegalArgumentException("no such table: " + tableName, e); } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) { //retry after a small pause DynamoDBUtil.sleepBeforeRetry(attempt); attempt++; updateItemVersionedInternal(tableName, key, attributes, expectedVersion, persistentEntity, attempt); } else { throw new DataStoreOperationException( "problem with table: " + tableName + ", key: " + key + ", attributes: " + attributes, e); } } }
From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java
License:Apache License
private void deleteItemInternal(String tableName, Key key, int attempt) { DeleteItemRequest request = new DeleteItemRequest(tableName, key); try {/*from w w w.j a v a2s.c om*/ ddb.deleteItem(request); } catch (AmazonServiceException e) { if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) { throw new IllegalArgumentException("no such table: " + tableName, e); } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) { //retry after a small pause DynamoDBUtil.sleepBeforeRetry(attempt); attempt++; deleteItemInternal(tableName, key, attempt); } else { throw new DataStoreOperationException("problem with table: " + tableName + ", key: " + key, e); } } }
From source file:org.grails.datastore.mapping.dynamodb.util.DynamoDBTemplateImpl.java
License:Apache License
private List<Map<String, AttributeValue>> scanInternal(String tableName, Map<String, Condition> filter, int max, int attempt) { LinkedList<Map<String, AttributeValue>> items = new LinkedList<Map<String, AttributeValue>>(); try {/* ww w. j a v a 2s.c o m*/ ScanRequest request = new ScanRequest(tableName).withScanFilter(filter); ScanResult result = ddb.scan(request); items.addAll(result.getItems()); //keep repeating until we get through all matched items Key lastKeyEvaluated = null; do { lastKeyEvaluated = result.getLastEvaluatedKey(); if (lastKeyEvaluated != null) { request = new ScanRequest(tableName).withScanFilter(filter) .withExclusiveStartKey(lastKeyEvaluated); result = ddb.scan(request); items.addAll(result.getItems()); } } while (lastKeyEvaluated != null && items.size() < max); //truncate if needed while (items.size() > max) { items.removeLast(); } return items; } catch (AmazonServiceException e) { if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) { throw new IllegalArgumentException("no such table: " + tableName, e); } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) { //retry after a small pause DynamoDBUtil.sleepBeforeRetry(attempt); attempt++; return scanInternal(tableName, filter, max, attempt); } else { throw new DataStoreOperationException("problem with table: " + tableName + ", filter: " + filter, e); } } }