Example usage for com.amazonaws AmazonServiceException setStatusCode

List of usage examples for com.amazonaws AmazonServiceException setStatusCode

Introduction

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

Prototype

public void setStatusCode(int statusCode) 

Source Link

Document

Sets the HTTP status code that was returned with this service exception.

Usage

From source file:org.finra.dm.dao.impl.MockEmrOperationsImpl.java

License:Apache License

/**
 * Add Job Flow Step to AmazonElasticMapReduceClient
 *//*w  w w. j ava2s .c  o m*/
@Override
public List<String> addJobFlowStepsRequest(AmazonElasticMapReduceClient emrClient,
        AddJobFlowStepsRequest addJobFlowStepsRequest) {
    if (addJobFlowStepsRequest.getSteps() != null && addJobFlowStepsRequest.getSteps().get(0) != null) {
        StepConfig firstStep = addJobFlowStepsRequest.getSteps().get(0);

        if (firstStep.getName().equals(MockAwsOperationsHelper.AMAZON_BAD_REQUEST)) {
            AmazonServiceException badRequestException = new AmazonServiceException(
                    MockAwsOperationsHelper.AMAZON_BAD_REQUEST);
            badRequestException.setStatusCode(HttpStatus.SC_BAD_REQUEST);
            throw badRequestException;
        } else if (firstStep.getName().equals(MockAwsOperationsHelper.AMAZON_NOT_FOUND)) {
            AmazonServiceException notFoundException = new AmazonServiceException(
                    MockAwsOperationsHelper.AMAZON_NOT_FOUND);
            notFoundException.setStatusCode(HttpStatus.SC_NOT_FOUND);
            throw notFoundException;
        } else if (firstStep.getName().equals(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION)) {
            throw new AmazonServiceException(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION);
        }
    }

    MockEmrJobFlow cluster = getClusterById(addJobFlowStepsRequest.getJobFlowId());
    if (cluster == null) {
        throw new AmazonServiceException(
                "No Cluster exists with jobFlowId: " + addJobFlowStepsRequest.getJobFlowId());
    }
    List<String> jobIds = new ArrayList<>();
    for (StepConfig step : addJobFlowStepsRequest.getSteps()) {
        jobIds.add(addClusterStep(cluster.getJobFlowId(), step).getJobFlowId());
    }
    return jobIds;
}

From source file:org.finra.dm.dao.impl.MockS3OperationsImpl.java

License:Apache License

/**
 * <p>//from  www.ja v a 2s  .  c o  m
 * Creates and returns a new {@link ObjectMetadata} with the given parameters. Content length is defaulted to 1 bytes unless a hint is provided.
 * </p>
 * <p>
 * Takes the following hints when filePath is suffixed:
 * </p>
 * <dl>
 * <p/>
 * <dt>AMAZON_THROTTLING_EXCEPTION</dt>
 * <dd>Throws AmazonServiceException with the error code "ThrottlingException"</dd>
 * <p/>
 * <dt>MOCK_S3_FILE_NAME_SERVICE_EXCEPTION</dt>
 * <dd>Throws AmazonServiceException</dd>
 * <p/>
 * <dt>MOCK_S3_FILE_NAME_NOT_FOUND</dt>
 * <dd>Throws AmazonServiceException with status code SC_NOT_FOUND</dd>
 * <p/>
 * <dt>MOCK_S3_FILE_NAME_0_BYTE_SIZE</dt>
 * <dd>Sets content length to 0 bytes</dd>
 * <p/>
 * </dl>
 */
@Override
public ObjectMetadata getObjectMetadata(String sourceBucketName, String filePath, AmazonS3Client s3Client) {
    ObjectMetadata objectMetadata = new ObjectMetadata();

    if (filePath.endsWith(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) {
        AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception");
        throttlingException.setErrorCode("ThrottlingException");

        throw throttlingException;
    } else if (filePath.endsWith(MOCK_S3_FILE_NAME_SERVICE_EXCEPTION)) {
        throw new AmazonServiceException(null);
    } else if (filePath.endsWith(MOCK_S3_FILE_NAME_NOT_FOUND)) {
        AmazonServiceException exception = new AmazonServiceException(null);
        exception.setStatusCode(HttpStatus.SC_NOT_FOUND);
        throw exception;
    } else if (filePath.endsWith(MOCK_S3_FILE_NAME_0_BYTE_SIZE)) {
        objectMetadata.setContentLength(AbstractCoreTest.FILE_SIZE_0_BYTE);
    } else {
        objectMetadata.setContentLength(AbstractCoreTest.FILE_SIZE_1_KB);
    }

    return objectMetadata;
}

From source file:org.finra.herd.dao.impl.MockEmrOperationsImpl.java

License:Apache License

@Override
public String runEmrJobFlow(AmazonElasticMapReduceClient emrClient, RunJobFlowRequest jobFlowRequest) {
    String clusterStatus = ClusterState.BOOTSTRAPPING.toString();

    StatusChangeReason reason = new StatusChangeReason(ClusterStateChangeReasonCode.USER_REQUEST.toString(),
            "Started " + clusterStatus);
    StatusTimeline timeline = new StatusTimeline();
    timeline.setCreationTime(HerdDateUtils.getXMLGregorianCalendarValue(new Date()));

    if (StringUtils.isNotBlank(jobFlowRequest.getAmiVersion())) {
        if (jobFlowRequest.getAmiVersion().equals(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) {
            AmazonServiceException throttlingException = new AmazonServiceException(
                    "test throttling exception");
            throttlingException.setErrorCode("ThrottlingException");

            throw throttlingException;
        } else if (jobFlowRequest.getAmiVersion().equals(MockAwsOperationsHelper.AMAZON_BAD_REQUEST)) {
            AmazonServiceException badRequestException = new AmazonServiceException(
                    MockAwsOperationsHelper.AMAZON_BAD_REQUEST);
            badRequestException.setStatusCode(HttpStatus.SC_BAD_REQUEST);
            throw badRequestException;
        } else if (jobFlowRequest.getAmiVersion().equals(MockAwsOperationsHelper.AMAZON_NOT_FOUND)) {
            AmazonServiceException notFoundException = new AmazonServiceException(
                    MockAwsOperationsHelper.AMAZON_NOT_FOUND);
            notFoundException.setStatusCode(HttpStatus.SC_NOT_FOUND);
            throw notFoundException;
        } else if (jobFlowRequest.getAmiVersion().equals(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION)) {
            throw new AmazonServiceException(MockAwsOperationsHelper.AMAZON_SERVICE_EXCEPTION);
        } else if (jobFlowRequest.getAmiVersion()
                .equals(MockAwsOperationsHelper.AMAZON_CLUSTER_STATUS_WAITING)) {
            clusterStatus = ClusterState.WAITING.toString();
        } else if (jobFlowRequest.getAmiVersion()
                .equals(MockAwsOperationsHelper.AMAZON_CLUSTER_STATUS_RUNNING)) {
            clusterStatus = ClusterState.RUNNING.toString();
        }//w w w. j a v a  2  s  . c  om
    }

    return createNewCluster(jobFlowRequest, clusterStatus, reason, timeline).getJobFlowId();
}

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 {//w  w  w  .j  a v a2 s  . co  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./*from w  w  w. j  a  va  2  s  .  c o  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;
    }
}