Example usage for com.amazonaws AmazonServiceException AmazonServiceException

List of usage examples for com.amazonaws AmazonServiceException AmazonServiceException

Introduction

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

Prototype

public AmazonServiceException(String errorMessage) 

Source Link

Document

Constructs a new AmazonServiceException with the specified message.

Usage

From source file:eu.openg.aws.sns.internal.SNSExceptionBuilder.java

License:Apache License

static SNSExceptionBuilder newAmazonServiceException(String message, String errorCode, int statusCode) {
    return new SNSExceptionBuilder(new AmazonServiceException(message), errorCode, statusCode);
}

From source file:io.druid.storage.s3.S3DataSegmentMover.java

License:Apache License

/**
 * Copies an object and after that checks that the object is present at the target location, via a separate API call.
 * If it is not, an exception is thrown, and the object is not deleted at the old location. This "paranoic" check
 * is added after it was observed that S3 may report a successful move, and the object is not found at the target
 * location.//from   ww  w . jav a 2 s .c  om
 */
private void selfCheckingMove(String s3Bucket, String targetS3Bucket, String s3Path, String targetS3Path,
        String copyMsg) throws IOException, SegmentLoadingException {
    if (s3Bucket.equals(targetS3Bucket) && s3Path.equals(targetS3Path)) {
        log.info("No need to move file[s3://%s/%s] onto itself", s3Bucket, s3Path);
        return;
    }
    if (s3Client.doesObjectExist(s3Bucket, s3Path)) {
        final ListObjectsV2Result listResult = s3Client.listObjectsV2(
                new ListObjectsV2Request().withBucketName(s3Bucket).withPrefix(s3Path).withMaxKeys(1));
        if (listResult.getKeyCount() == 0) {
            // should never happen
            throw new ISE("Unable to list object [s3://%s/%s]", s3Bucket, s3Path);
        }
        final S3ObjectSummary objectSummary = listResult.getObjectSummaries().get(0);
        if (objectSummary.getStorageClass() != null
                && StorageClass.fromValue(StringUtils.toUpperCase(objectSummary.getStorageClass()))
                        .equals(StorageClass.Glacier)) {
            throw new AmazonServiceException(StringUtils.format(
                    "Cannot move file[s3://%s/%s] of storage class glacier, skipping.", s3Bucket, s3Path));
        } else {
            log.info("Moving file %s", copyMsg);
            final CopyObjectRequest copyRequest = new CopyObjectRequest(s3Bucket, s3Path, targetS3Bucket,
                    targetS3Path);
            if (!config.getDisableAcl()) {
                copyRequest
                        .setAccessControlList(S3Utils.grantFullControlToBucketOwner(s3Client, targetS3Bucket));
            }
            s3Client.copyObject(copyRequest);
            if (!s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) {
                throw new IOE(
                        "After copy was reported as successful the file doesn't exist in the target location [%s]",
                        copyMsg);
            }
            deleteWithRetriesSilent(s3Bucket, s3Path);
            log.debug("Finished moving file %s", copyMsg);
        }
    } else {
        // ensure object exists in target location
        if (s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) {
            log.info("Not moving file [s3://%s/%s], already present in target location [s3://%s/%s]", s3Bucket,
                    s3Path, targetS3Bucket, targetS3Path);
        } else {
            throw new SegmentLoadingException(
                    "Unable to move file %s, not present in either source or target location", copyMsg);
        }
    }
}

From source file:net.firejack.aws.web.controller.AWSController.java

License:Apache License

@ResponseBody
@RequestMapping(value = "auth", method = RequestMethod.POST)
public List<Dropdown> auth(@RequestBody Auth auth) {
    if (!auth.isValid())
        throw new AmazonServiceException("Access or Secret Key is empty");

    if (amazonEC2 != null)
        amazonEC2.shutdown();/*from  w ww  .ja va 2s .  com*/

    amazonEC2 = new AmazonEC2Client(new BasicAWSCredentials(auth.getAccessKey(), auth.getSecretKey()));

    DescribeRegionsResult result = amazonEC2.describeRegions();
    List<Region> resultRegions = result.getRegions();

    List<Dropdown> regions = new ArrayList<Dropdown>(resultRegions.size());
    for (Region region : resultRegions)
        regions.add(new Dropdown(region.getRegionName(), region.getEndpoint()));

    return regions;
}

From source file:net.firejack.aws.web.controller.AWSController.java

License:Apache License

@ResponseBody
@ExceptionHandler(Exception.class)
@RequestMapping(value = "region/{region}", method = RequestMethod.GET)
public InstanceDetails changeRegion(@PathVariable("region") String region) {
    if (amazonEC2 == null)
        throw new AmazonServiceException("Amazon service can't initialize");
    if (region.isEmpty())
        throw new AmazonServiceException("Region is empty");

    amazonEC2.setRegion(RegionUtils.getRegion(region));

    InstanceDetails details = new InstanceDetails();

    DescribeImagesRequest request = new DescribeImagesRequest();
    request.setOwners(Arrays.asList(owner));
    DescribeImagesResult result = amazonEC2.describeImages(request);

    List<Image> images = result.getImages();
    List<Dropdown> amis = new ArrayList<Dropdown>(images.size());

    for (Image image : images)
        amis.add(new Dropdown(image.getImageId(), image.getName()));
    details.setAmis(amis);// w  w  w .ja  va  2 s . com

    DescribeSecurityGroupsResult securityGroupsResult = amazonEC2.describeSecurityGroups();

    List<SecurityGroup> securityGroups = securityGroupsResult.getSecurityGroups();
    List<Dropdown> groups = new ArrayList<Dropdown>(securityGroups.size());

    for (SecurityGroup group : securityGroups)
        groups.add(new Dropdown(group.getGroupId(), group.getGroupName()));
    details.setSecurityGroups(groups);

    DescribeKeyPairsResult keyPairsResult = amazonEC2.describeKeyPairs();

    List<KeyPairInfo> keyPairInfos = keyPairsResult.getKeyPairs();
    List<Dropdown> keyPairs = new ArrayList<Dropdown>(keyPairInfos.size());

    for (KeyPairInfo keyPairInfo : keyPairInfos)
        keyPairs.add(new Dropdown(keyPairInfo.getKeyName(), keyPairInfo.getKeyName()));
    details.setKeys(keyPairs);
    details.setInstanceTypes(InstanceType.values());

    return details;
}

From source file:net.firejack.aws.web.controller.AWSController.java

License:Apache License

@ResponseBody
@RequestMapping(value = "instance", method = RequestMethod.POST)
public Status spotInstance(@RequestBody InstanceModel instance) {
    if (amazonEC2 == null)
        throw new AmazonServiceException("Amazon service can't initialize");
    if (!instance.isValid())
        throw new AmazonServiceException("Invalid message");

    RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
    runInstancesRequest.setInstanceType(instance.getInstanceType());
    runInstancesRequest.setImageId(instance.getAmi());
    runInstancesRequest.setMinCount(1);/*from   w  ww  .j  ava2s .co m*/
    runInstancesRequest.setMaxCount(1);
    runInstancesRequest.setKeyName(instance.getKey());
    runInstancesRequest.setSecurityGroupIds(Arrays.asList(instance.getSecurityGroup()));

    amazonEC2.runInstances(runInstancesRequest);

    return new Status("Server has been started");
}

From source file:net.firejack.aws.web.controller.AWSController.java

License:Apache License

@ResponseBody
@RequestMapping(value = "install", method = RequestMethod.POST)
public Status startInstance(@RequestBody Auth auth) {
    if (!auth.isValid())
        throw new AmazonServiceException("Access or Secret Key is empty");

    if (amazonEC2 != null) {
        amazonEC2.shutdown();/*  w  ww . j  a v a 2s.co  m*/
    }

    amazonEC2 = new AmazonEC2Client(new BasicAWSCredentials(auth.getAccessKey(), auth.getSecretKey()));
    amazonEC2.setRegion(RegionUtils.getRegion(instanceRegion));

    RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
    runInstancesRequest.setInstanceType(InstanceType.fromValue(instanceType));
    runInstancesRequest.setImageId(instanceAmi);
    runInstancesRequest.setMinCount(1);
    runInstancesRequest.setMaxCount(1);

    KeyPair keyPair = createKeyPair();
    String privateKey = keyPair.getKeyMaterial();
    String fileName;
    try {
        fileName = saveKeyFile(keyPair.getKeyName(), privateKey);
    } catch (FileNotFoundException e) {
        throw new AmazonServiceException("Could not create the key file");
    } catch (UnsupportedEncodingException e) {
        throw new AmazonServiceException("Could not create the key file");
    }
    runInstancesRequest.setKeyName(keyPair.getKeyName());

    CreateSecurityGroupResult securityGroupResult = createSecurityGroupWithRules();
    Collection securityGroupIds = new ArrayList();
    securityGroupIds.add(securityGroupResult.getGroupId());
    runInstancesRequest.setSecurityGroupIds(securityGroupIds);

    amazonEC2.runInstances(runInstancesRequest);

    return new Status("Server has been started", fileName);
}

From source file:net.firejack.aws.web.controller.AWSController.java

License:Apache License

private KeyPair createKeyPair() {
    if (amazonEC2 == null) {
        throw new AmazonServiceException("Amazon service can't initialize");
    }/*from  w ww .  j av a2 s  . c o  m*/

    CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest();
    String name = keyPairName + "-" + genRandomString();
    createKeyPairRequest.withKeyName(name);
    CreateKeyPairResult createKeyPairResult = amazonEC2.createKeyPair(createKeyPairRequest);
    KeyPair keyPair = createKeyPairResult.getKeyPair();

    return keyPair;
}

From source file:net.firejack.aws.web.controller.AWSController.java

License:Apache License

private CreateSecurityGroupResult createSecurityGroupWithRules() {
    if (amazonEC2 == null) {
        throw new AmazonServiceException("Amazon service can't initialize");
    }/*from   ww  w .  j a v  a  2s  . com*/

    CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest();
    String name = securityGroupName + "-" + genRandomString();
    createSecurityGroupRequest.withGroupName(name).withDescription("Security Group for Platform running");
    CreateSecurityGroupResult securityGroupResult = amazonEC2.createSecurityGroup(createSecurityGroupRequest);

    IpPermission sshPermission = new IpPermission();
    sshPermission.withIpRanges("0.0.0.0/0").withIpProtocol("tcp").withFromPort(22).withToPort(22);

    IpPermission httpPermission = new IpPermission();
    httpPermission.withIpRanges("0.0.0.0/0").withIpProtocol("tcp").withFromPort(8080).withToPort(8080);

    AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest();

    authorizeSecurityGroupIngressRequest.withGroupName(name).withIpPermissions(sshPermission, httpPermission);

    amazonEC2.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);

    return securityGroupResult;
}

From source file:org.apache.camel.component.aws.s3.AmazonS3ClientMock.java

License:Apache License

@Override
public ObjectListing listObjects(ListObjectsRequest listObjectsRequest)
        throws AmazonClientException, AmazonServiceException {
    if ("nonExistingBucket".equals(listObjectsRequest.getBucketName()) && !nonExistingBucketCreated) {
        AmazonServiceException ex = new AmazonServiceException("Unknow bucket");
        ex.setStatusCode(404);// www .  j  a v a 2 s. c  om
        throw ex;
    }

    ObjectListing objectListing = new ObjectListing();
    int capacity = listObjectsRequest.getMaxKeys();

    for (int index = 0; index < objects.size() && index < capacity; index++) {
        S3ObjectSummary s3ObjectSummary = new S3ObjectSummary();
        s3ObjectSummary.setBucketName(objects.get(index).getBucketName());
        s3ObjectSummary.setKey(objects.get(index).getKey());

        objectListing.getObjectSummaries().add(s3ObjectSummary);
    }

    return objectListing;
}

From source file:org.apache.druid.storage.s3.S3DataSegmentMover.java

License:Apache License

/**
 * Copies an object and after that checks that the object is present at the target location, via a separate API call.
 * If it is not, an exception is thrown, and the object is not deleted at the old location. This "paranoic" check
 * is added after it was observed that S3 may report a successful move, and the object is not found at the target
 * location.//from  ww w  .  j  a  v  a  2 s.  co m
 */
private void selfCheckingMove(String s3Bucket, String targetS3Bucket, String s3Path, String targetS3Path,
        String copyMsg) throws IOException, SegmentLoadingException {
    if (s3Bucket.equals(targetS3Bucket) && s3Path.equals(targetS3Path)) {
        log.info("No need to move file[s3://%s/%s] onto itself", s3Bucket, s3Path);
        return;
    }
    if (s3Client.doesObjectExist(s3Bucket, s3Path)) {
        final ListObjectsV2Result listResult = s3Client.listObjectsV2(
                new ListObjectsV2Request().withBucketName(s3Bucket).withPrefix(s3Path).withMaxKeys(1));
        // Using getObjectSummaries().size() instead of getKeyCount as, in some cases
        // it is observed that even though the getObjectSummaries returns some data
        // keyCount is still zero.
        if (listResult.getObjectSummaries().size() == 0) {
            // should never happen
            throw new ISE("Unable to list object [s3://%s/%s]", s3Bucket, s3Path);
        }
        final S3ObjectSummary objectSummary = listResult.getObjectSummaries().get(0);
        if (objectSummary.getStorageClass() != null
                && StorageClass.fromValue(StringUtils.toUpperCase(objectSummary.getStorageClass()))
                        .equals(StorageClass.Glacier)) {
            throw new AmazonServiceException(StringUtils.format(
                    "Cannot move file[s3://%s/%s] of storage class glacier, skipping.", s3Bucket, s3Path));
        } else {
            log.info("Moving file %s", copyMsg);
            final CopyObjectRequest copyRequest = new CopyObjectRequest(s3Bucket, s3Path, targetS3Bucket,
                    targetS3Path);
            if (!config.getDisableAcl()) {
                copyRequest
                        .setAccessControlList(S3Utils.grantFullControlToBucketOwner(s3Client, targetS3Bucket));
            }
            s3Client.copyObject(copyRequest);
            if (!s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) {
                throw new IOE(
                        "After copy was reported as successful the file doesn't exist in the target location [%s]",
                        copyMsg);
            }
            deleteWithRetriesSilent(s3Bucket, s3Path);
            log.debug("Finished moving file %s", copyMsg);
        }
    } else {
        // ensure object exists in target location
        if (s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) {
            log.info("Not moving file [s3://%s/%s], already present in target location [s3://%s/%s]", s3Bucket,
                    s3Path, targetS3Bucket, targetS3Path);
        } else {
            throw new SegmentLoadingException(
                    "Unable to move file %s, not present in either source or target location", copyMsg);
        }
    }
}