List of usage examples for com.amazonaws AmazonServiceException AmazonServiceException
public AmazonServiceException(String errorMessage)
From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java
License:Apache License
/** * {@inheritDoc} <p/> <p> This implementation simulates a copyFile operation. </p> <p> This method copies files in-memory. </p> <p> The result {@link Copy} * has the following properties: <dl> <p/> <dt>description</dt> <dd>"MockTransfer"</dd> <p/> <dt>state</dt> <dd>{@link TransferState#Completed}</dd> <p/> * <dt>transferProgress.totalBytesToTransfer</dt> <dd>1024</dd> <p/> <dt>transferProgress.updateProgress</dt> <dd>1024</dd> <p/> </dl> <p/> All other * properties are set as default. </p> <p> This operation takes the following hints when suffixed in copyObjectRequest.sourceKey: <dl> <p/> * <dt>MOCK_S3_FILE_NAME_SERVICE_EXCEPTION</dt> <dd>Throws a AmazonServiceException</dd> <p/> </dl> </p> *///from ww w .jav a2 s. com @Override public Copy copyFile(final CopyObjectRequest copyObjectRequest, TransferManager transferManager) { LOGGER.debug("copyFile(): copyObjectRequest.getSourceBucketName() = " + copyObjectRequest.getSourceBucketName() + ", copyObjectRequest.getSourceKey() = " + copyObjectRequest.getSourceKey() + ", copyObjectRequest.getDestinationBucketName() = " + copyObjectRequest.getDestinationBucketName() + ", copyObjectRequest.getDestinationKey() = " + copyObjectRequest.getDestinationKey()); if (copyObjectRequest.getSourceKey().endsWith(MOCK_S3_FILE_NAME_SERVICE_EXCEPTION)) { throw new AmazonServiceException(null); } String sourceBucketName = copyObjectRequest.getSourceBucketName(); String sourceKey = copyObjectRequest.getSourceKey(); MockS3Bucket mockSourceS3Bucket = getOrCreateBucket(sourceBucketName); MockS3Object mockSourceS3Object = mockSourceS3Bucket.getObjects().get(sourceKey); if (mockSourceS3Object == null) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_NO_SUCH_KEY); amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_KEY); throw amazonServiceException; } // Set the result CopyImpl and TransferProgress. TransferProgress transferProgress = new TransferProgress(); transferProgress.setTotalBytesToTransfer(mockSourceS3Object.getObjectMetadata().getContentLength()); transferProgress.updateProgress(mockSourceS3Object.getObjectMetadata().getContentLength()); CopyImpl copy = new CopyImpl(MOCK_TRANSFER_DESCRIPTION, transferProgress, null, null); copy.setState(TransferState.Completed); // If an invalid KMS Id was passed in, mark the transfer as failed and return an exception via the transfer monitor. if (copyObjectRequest.getSSEAwsKeyManagementParams() != null) { final String kmsId = copyObjectRequest.getSSEAwsKeyManagementParams().getAwsKmsKeyId(); if (kmsId.startsWith(MOCK_KMS_ID_FAILED_TRANSFER)) { copy.setState(TransferState.Failed); copy.setMonitor(new TransferMonitor() { @Override public Future<?> getFuture() { if (!kmsId.equals(MOCK_KMS_ID_FAILED_TRANSFER_NO_EXCEPTION)) { throw new AmazonServiceException("Key '" + copyObjectRequest.getSSEAwsKeyManagementParams().getAwsKmsKeyId() + "' does not exist (Service: Amazon S3; Status Code: 400; Error Code: KMS.NotFoundException; Request ID: 1234567890123456)"); } // We don't want an exception to be thrown so return a basic future that won't throw an exception. BasicFuture<?> future = new BasicFuture<Void>(null); future.completed(null); return future; } @Override public boolean isDone() { return true; } }); } else if (kmsId.startsWith(MOCK_KMS_ID_CANCELED_TRANSFER)) { // If the KMS indicates a cancelled transfer, just update the state to canceled. copy.setState(TransferState.Canceled); } } // If copy operation is marked as completed, perform the actual file copy in memory. if (copy.getState().equals(TransferState.Completed)) { String destinationBucketName = copyObjectRequest.getDestinationBucketName(); String destinationObjectKey = copyObjectRequest.getDestinationKey(); String destinationObjectVersion = MOCK_S3_BUCKET_NAME_VERSIONING_ENABLED.equals(destinationBucketName) ? UUID.randomUUID().toString() : null; String destinationObjectKeyVersion = destinationObjectKey + (destinationObjectVersion != null ? destinationObjectVersion : ""); ObjectMetadata objectMetadata = copyObjectRequest.getNewObjectMetadata(); MockS3Object mockDestinationS3Object = new MockS3Object(); mockDestinationS3Object.setKey(destinationObjectKey); mockDestinationS3Object.setVersion(destinationObjectVersion); mockDestinationS3Object .setData(Arrays.copyOf(mockSourceS3Object.getData(), mockSourceS3Object.getData().length)); mockDestinationS3Object.setObjectMetadata(objectMetadata); MockS3Bucket mockDestinationS3Bucket = getOrCreateBucket(destinationBucketName); mockDestinationS3Bucket.getObjects().put(destinationObjectKey, mockDestinationS3Object); mockDestinationS3Bucket.getVersions().put(destinationObjectKeyVersion, mockDestinationS3Object); } return copy; }
From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java
License:Apache License
/** * {@inheritDoc} <p/> <p> Since a multipart upload in progress does not exist when in-memory, this method simply returns a preconfigured list. </p> <p> * Returns a mock {@link MultipartUploadListing} based on the parameters and hints provided. By default returns a mock listing as defiend by {@link * #getMultipartUploadListing()}. </p> <p> This operation takes the following hints when suffixed in listMultipartUploadsRequest.bucketName: <dl> <p/> * <dt>MOCK_S3_BUCKET_NAME_SERVICE_EXCEPTION</dt> <dd>Throws a AmazonServiceException</dd> <p/> <dt>MOCK_S3_BUCKET_NAME_TRUNCATED_MULTIPART_LISTING</dt> * <dd>Returns the listing as if it is truncated. See below for details.</dd> <p/> </dl> </p> *//*from w w w . j av a2s . com*/ @Override public MultipartUploadListing listMultipartUploads(ListMultipartUploadsRequest listMultipartUploadsRequest, AmazonS3 s3Client) { if (listMultipartUploadsRequest.getBucketName().equals(MOCK_S3_BUCKET_NAME_SERVICE_EXCEPTION)) { throw new AmazonServiceException(null); } else if (listMultipartUploadsRequest.getBucketName() .equals(MOCK_S3_BUCKET_NAME_TRUNCATED_MULTIPART_LISTING)) { MultipartUploadListing multipartUploadListing = getMultipartUploadListing(); // If listing request does not have upload ID marker set, mark the listing as truncated - this is done to truncate the multipart listing just once. if (listMultipartUploadsRequest.getUploadIdMarker() == null) { multipartUploadListing.setNextUploadIdMarker("TEST_UPLOAD_MARKER_ID"); multipartUploadListing.setNextKeyMarker("TEST_KEY_MARKER_ID"); multipartUploadListing.setTruncated(true); } return multipartUploadListing; } else { return getMultipartUploadListing(); } }
From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java
License:Apache License
/** * {@inheritDoc}/*w w w .ja v a 2 s . co m*/ * <p/> * If the bucket does not exist, returns a listing with an empty list. If a prefix is specified in listVersionsRequest, only versions starting with the * prefix will be returned. */ @Override public VersionListing listVersions(ListVersionsRequest listVersionsRequest, AmazonS3 s3Client) { LOGGER.debug( "listVersions(): listVersionsRequest.getBucketName() = " + listVersionsRequest.getBucketName()); String bucketName = listVersionsRequest.getBucketName(); if (MOCK_S3_BUCKET_NAME_NO_SUCH_BUCKET_EXCEPTION.equals(bucketName)) { AmazonS3Exception amazonS3Exception = new AmazonS3Exception( MOCK_S3_BUCKET_NAME_NO_SUCH_BUCKET_EXCEPTION); amazonS3Exception.setErrorCode("NoSuchBucket"); throw amazonS3Exception; } else if (MOCK_S3_BUCKET_NAME_INTERNAL_ERROR.equals(bucketName)) { throw new AmazonServiceException(S3Operations.ERROR_CODE_INTERNAL_ERROR); } VersionListing versionListing = new VersionListing(); versionListing.setBucketName(bucketName); MockS3Bucket mockS3Bucket = mockS3Buckets.get(bucketName); if (mockS3Bucket != null) { for (MockS3Object mockS3Object : mockS3Bucket.getVersions().values()) { String s3ObjectKey = mockS3Object.getKey(); if (listVersionsRequest.getPrefix() == null || s3ObjectKey.startsWith(listVersionsRequest.getPrefix())) { S3VersionSummary s3VersionSummary = new S3VersionSummary(); s3VersionSummary.setBucketName(bucketName); s3VersionSummary.setKey(s3ObjectKey); s3VersionSummary.setVersionId(mockS3Object.getVersion()); s3VersionSummary.setSize(mockS3Object.getData().length); s3VersionSummary.setStorageClass(mockS3Object.getObjectMetadata() != null ? mockS3Object.getObjectMetadata().getStorageClass() : null); versionListing.getVersionSummaries().add(s3VersionSummary); } } } return versionListing; }
From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java
License:Apache License
@Override public void restoreObject(RestoreObjectRequest requestRestore, AmazonS3 s3Client) { if (requestRestore.getKey().endsWith(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) { AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception"); throttlingException.setErrorCode("ThrottlingException"); throw throttlingException; } else if (MOCK_S3_BUCKET_NAME_NO_SUCH_BUCKET_EXCEPTION.equals(requestRestore.getBucketName())) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_NO_SUCH_BUCKET); amazonServiceException.setStatusCode(404); throw amazonServiceException; } else if (MOCK_S3_BUCKET_NAME_ACCESS_DENIED.equals(requestRestore.getBucketName())) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_ACCESS_DENIED); amazonServiceException.setStatusCode(403); throw amazonServiceException; } else if (MOCK_S3_BUCKET_NAME_INTERNAL_ERROR.equals(requestRestore.getBucketName()) || requestRestore.getKey().endsWith(MOCK_S3_FILE_NAME_SERVICE_EXCEPTION)) { throw new AmazonServiceException(S3Operations.ERROR_CODE_INTERNAL_ERROR); } else {//www.j av a2 s.c o m MockS3Bucket mockS3Bucket = getOrCreateBucket(requestRestore.getBucketName()); MockS3Object mockS3Object = mockS3Bucket.getObjects().get(requestRestore.getKey()); if (mockS3Object == null) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_NO_SUCH_KEY); amazonServiceException.setStatusCode(404); throw amazonServiceException; } // Get object metadata. ObjectMetadata objectMetadata = mockS3Object.getObjectMetadata(); // Fail if the object is not in Glacier. if (!StorageClass.Glacier.toString().equals(objectMetadata.getStorageClass())) { AmazonServiceException amazonServiceException = new AmazonServiceException( "object is not in Glacier"); throw amazonServiceException; } // Fail if the object is already being restored. if (objectMetadata.getOngoingRestore()) { AmazonServiceException amazonServiceException = new AmazonServiceException( "object is already being restored"); throw amazonServiceException; } // Update the object metadata to indicate that there is an ongoing restore request. objectMetadata.setOngoingRestore(true); } }
From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java
License:Apache License
/** * Gets a mock S3 object if one exists.//w w w . ja v a2s . co m * * @param s3BucketName the S3 bucket name * @param s3Key the S3 key * @param s3VersionId the S3 version identifier * * @return the mock S3 object */ private MockS3Object getMockS3Object(String s3BucketName, String s3Key, String s3VersionId) { if (s3Key.endsWith(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) { AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception"); throttlingException.setErrorCode("ThrottlingException"); throw throttlingException; } else if (MOCK_S3_BUCKET_NAME_NO_SUCH_BUCKET_EXCEPTION.equals(s3BucketName)) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_NO_SUCH_BUCKET); amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_BUCKET); amazonServiceException.setStatusCode(404); throw amazonServiceException; } else if (MOCK_S3_BUCKET_NAME_ACCESS_DENIED.equals(s3BucketName)) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_ACCESS_DENIED); amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_ACCESS_DENIED); amazonServiceException.setStatusCode(403); throw amazonServiceException; } else if (MOCK_S3_BUCKET_NAME_INTERNAL_ERROR.equals(s3BucketName) || s3Key.endsWith(MOCK_S3_FILE_NAME_SERVICE_EXCEPTION)) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_INTERNAL_ERROR); amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_INTERNAL_ERROR); throw amazonServiceException; } else { MockS3Bucket mockS3Bucket = getOrCreateBucket(s3BucketName); MockS3Object mockS3Object = StringUtils.isBlank(s3VersionId) ? mockS3Bucket.getObjects().get(s3Key) : mockS3Bucket.getVersions().get(s3Key + s3VersionId); if (mockS3Object == null) { AmazonServiceException amazonServiceException = new AmazonServiceException( S3Operations.ERROR_CODE_NO_SUCH_KEY); amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_KEY); amazonServiceException.setStatusCode(404); throw amazonServiceException; } return mockS3Object; } }
From source file:org.finra.herd.dao.impl.MockSqsOperationsImpl.java
License:Apache License
@Override public SendMessageResult sendMessage(String queueName, String messageText, Map<String, MessageAttributeValue> messageAttributes, AmazonSQS amazonSQS) { // Throw a throttling exception for a specific queue name for testing purposes. if (queueName.equals(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) { AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception"); throttlingException.setErrorCode("ThrottlingException"); throw throttlingException; }/*w w w . j a v a2 s . c o m*/ // Throw an illegal state exception for a specific queue name for testing purposes. if (queueName.equals(MOCK_SQS_QUEUE_NOT_FOUND_NAME)) { throw new IllegalStateException(String.format("AWS SQS queue with \"%s\" name not found.", queueName)); } // Nothing else to do in the normal case since our unit tests aren't reading messages once they have been published. return new SendMessageResult().withMessageId(AbstractDaoTest.MESSAGE_ID); }
From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java
License:Open Source License
public GetQueueUrlResult getQueueUrl(final GetQueueUrlRequest request) throws AmazonServiceException, AmazonClientException { if (request == null) { throw new AmazonClientException("Null GetQueueUrlRequest"); }/*from w w w . ja v a2 s .co m*/ final String queueName = request.getQueueName(); final String queueUrl = QUEUE_URL_PREFIX + queueName; checkURLForException(queueUrl); // Per documentation, supposedly throws QueueDoesNotExistException, // but in my tests, they actually just throw AmazonServiceException if (!allQueues.containsKey(queueUrl)) { throw new AmazonServiceException("Queue Not Found: " + queueUrl); } return new GetQueueUrlResult().withQueueUrl(queueUrl); }
From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java
License:Open Source License
public CreateQueueResult createQueue(final CreateQueueRequest request) throws AmazonServiceException, AmazonClientException { if (request == null) { throw new AmazonClientException("Null CreateQueueRequest"); }// w ww .j a v a 2s . co m final String queueName = request.getQueueName(); if (StringUtils.isBlank(queueName) || queueName.length() > 80) { throw new AmazonServiceException("Invalid queue name: " + queueName); } final String queueUrl = QUEUE_URL_PREFIX + queueName; checkURLForException(queueUrl); // Per documentation, throws QueueNameExistsException, but in my testing, they actually // just quietly return the CreateQueueResult // (Also note: we are ignoring the documented exception: QueueDeletedRecentlyException) if (!allQueues.containsKey(queueUrl)) { allQueues.put(queueUrl, Collections.synchronizedList(new ArrayList<Message>())); retrievedMessages.put(queueUrl, new ConcurrentHashMap<String, Message>()); } return new CreateQueueResult().withQueueUrl(queueUrl); }
From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java
License:Open Source License
public SendMessageResult sendMessage(final SendMessageRequest request) throws AmazonServiceException, AmazonClientException { if (request == null) { throw new AmazonClientException("Null SendMessageRequest"); }/* w ww. ja v a 2 s .co m*/ final String queueUrl = request.getQueueUrl(); checkURLForException(queueUrl); checkStringForExceptionMarker(request.getMessageBody()); // Ignoring the following exception: InvalidMessageContentsException (thrown for character set conditions?) if (!allQueues.containsKey(queueUrl)) { throw new AmazonServiceException("Queue Not Found: " + queueUrl); } final Long seq = incrementer.getAndIncrement(); final Message msg = new Message().withBody(StringUtils.defaultString(request.getMessageBody())) .withMD5OfBody(makeMD5(request.getMessageBody())).withMessageId(MESSAGE_ID_PREFIX + seq); allQueues.get(queueUrl).add(msg); return new SendMessageResult().withMD5OfMessageBody(msg.getMD5OfBody()).withMessageId(msg.getMessageId()); }
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 www .ja va2 s .co 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()); }