Example usage for com.amazonaws AmazonServiceException getErrorCode

List of usage examples for com.amazonaws AmazonServiceException getErrorCode

Introduction

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

Prototype

public String getErrorCode() 

Source Link

Document

Returns the AWS error code represented by this exception.

Usage

From source file:net.solarnetwork.node.backup.s3.SdkS3Client.java

License:Open Source License

@Override
public void deleteObjects(Set<String> keys) throws IOException {
    AmazonS3 client = getClient();/*from ww w  .j  a  v  a2  s. c  o m*/
    try {
        DeleteObjectsRequest req = new DeleteObjectsRequest(bucketName)
                .withKeys(keys.stream().map(k -> new KeyVersion(k)).collect(Collectors.toList()));
        client.deleteObjects(req);
    } catch (AmazonServiceException e) {
        log.warn("AWS error: {}; HTTP code {}; AWS code {}; type {}; request ID {}", e.getMessage(),
                e.getStatusCode(), e.getErrorCode(), e.getErrorType(), e.getRequestId());
        throw new RemoteServiceException("Error deleting S3 objects " + keys, e);
    } catch (AmazonClientException e) {
        log.debug("Error communicating with AWS: {}", e.getMessage());
        throw new IOException("Error communicating with AWS", e);
    }
}

From source file:org.akvo.flow.deploy.Deploy.java

License:Open Source License

public static void main(String[] args) throws IOException {
    if (args.length != 7) {
        System.err.println("Missing argument, please provide S3 access key, S3 secret key, "
                + "instanceId , apkPath, version, GAE username and GAE password");
        return;//from   ww w  .  j av  a2s  . c om
    }

    File file = new File(args[APK_PATH]);
    if (!file.exists()) {
        System.err.println("Can't find apk at " + args[APK_PATH]);
        return;
    }

    final String accessKey = args[S3_ACCESS_KEY];
    final String secretKey = args[S3_SECRET_KEY];
    final String instance = args[INSTANCE_ID];
    final String accountId = args[ACCOUNT_ID];
    final String accountSecret = args[ACCOUNT_SECRET];
    final String version = args[VERSION];

    final String s3Path = "apk/" + instance + "/" + file.getName();
    final String s3Url = "http://akvoflow.s3.amazonaws.com/apk/" + instance + '/' + file.getName();
    final String host = instance + ".appspot.com";

    try {
        uploadS3(accessKey, secretKey, s3Path, file);
        updateVersion(host, accountId, accountSecret, s3Url, version, getMD5Checksum(file));
    } catch (AmazonServiceException ase) {
        System.err.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.");
        System.err.println("Error Message:    " + ase.getMessage());
        System.err.println("HTTP Status Code: " + ase.getStatusCode());
        System.err.println("AWS Error Code:   " + ase.getErrorCode());
        System.err.println("Error Type:       " + ase.getErrorType());
        System.err.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.err.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.err.println("Error Message: " + ace.getMessage());
    } catch (IOException e) {
        System.err.println("Error updating APK version in GAE");
        e.printStackTrace();
    }

}

From source file:org.apache.airavata.core.gfac.provider.impl.EC2Provider.java

License:Apache License

public EC2Provider(InvocationContext invocationContext) throws ProviderException {
    ExecutionContext execContext = invocationContext.getExecutionContext();
    OMElement omSecurityContextHeader = execContext.getSecurityContextHeader();

    ContextHeaderDocument document = null;
    try {/*from w  ww.  j  a v  a  2  s .c o  m*/
        document = ContextHeaderDocument.Factory.parse(omSecurityContextHeader.toStringWithConsume());
    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (XmlException e) {
        e.printStackTrace();
    }
    SecurityContextDocument.SecurityContext.AmazonWebservices amazonWebservices = document.getContextHeader()
            .getSecurityContext().getAmazonWebservices();
    String access_key = amazonWebservices.getAccessKeyId();

    String secret_key = amazonWebservices.getSecretAccessKey();
    String ami_id = amazonWebservices.getAmiId();
    String ins_id = amazonWebservices.getInstanceId();
    String ins_type = amazonWebservices.getInstanceType();
    this.username = amazonWebservices.getUsername();

    log.info("ACCESS_KEY:" + access_key);
    log.info("SECRET_KEY:" + secret_key);
    log.info("AMI_ID:" + ami_id);
    log.info("INS_ID:" + ins_id);
    log.info("INS_TYPE:" + ins_type);
    log.info("USERNAME:" + username);

    /*
     * Validation
     */
    if (access_key == null || access_key.isEmpty())
        throw new ProviderException("Access Key is empty");
    if (secret_key == null || secret_key.isEmpty())
        throw new ProviderException("Secret Key is empty");
    if ((ami_id == null && ins_id == null) || (ami_id != null && ami_id.isEmpty())
            || (ins_id != null && ins_id.isEmpty()))
        throw new ProviderException("AMI or Instance ID is empty");
    if (this.username == null || this.username.isEmpty())
        throw new ProviderException("Username is empty");

    /*
     * Need to start EC2 instance before running it
     */
    AWSCredentials credential = new BasicAWSCredentials(access_key, secret_key);
    AmazonEC2Client ec2client = new AmazonEC2Client(credential);

    try {
        /*
         * Build key pair before start instance
         */
        buildKeyPair(ec2client);

        // right now, we can run it on one host
        if (ami_id != null)
            this.instance = startInstances(ec2client, ami_id, ins_type, execContext).get(0);
        else {

            // already running instance
            DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
            DescribeInstancesResult describeInstancesResult = ec2client
                    .describeInstances(describeInstancesRequest.withInstanceIds(ins_id));

            if (describeInstancesResult.getReservations().size() == 0
                    || describeInstancesResult.getReservations().get(0).getInstances().size() == 0) {
                throw new GfacException("Instance not found:" + ins_id);
            }

            this.instance = describeInstancesResult.getReservations().get(0).getInstances().get(0);

            // check instance keypair
            if (this.instance.getKeyName() == null || !this.instance.getKeyName().equals(KEY_PAIR_NAME))
                throw new GfacException("Keypair for instance:" + ins_id + " is not valid");
        }

        //TODO send out instance id
        //execContext.getNotificationService().sendResourceMappingNotifications(this.instance.getPublicDnsName(), "EC2 Instance " + this.instance.getInstanceId() + " is running with public name " + this.instance.getPublicDnsName(), this.instance.getInstanceId());

        /*
         * Make sure port 22 is connectable
         */
        for (GroupIdentifier g : this.instance.getSecurityGroups()) {
            IpPermission ip = new IpPermission();
            ip.setIpProtocol("tcp");
            ip.setFromPort(22);
            ip.setToPort(22);
            AuthorizeSecurityGroupIngressRequest r = new AuthorizeSecurityGroupIngressRequest();
            r = r.withIpPermissions(ip.withIpRanges("0.0.0.0/0"));
            r.setGroupId(g.getGroupId());
            try {
                ec2client.authorizeSecurityGroupIngress(r);
            } catch (AmazonServiceException as) {
                /*
                 * If exception is from duplicate room, ignore it.
                 */
                if (!as.getErrorCode().equals("InvalidPermission.Duplicate"))
                    throw as;
            }
        }

    } catch (Exception e) {
        throw new ProviderException("Invalied Request", e);
    }

    SSHSecurityContextImpl sshContext = ((SSHSecurityContextImpl) invocationContext
            .getSecurityContext(SSH_SECURITY_CONTEXT));
    if (sshContext == null) {
        sshContext = new SSHSecurityContextImpl();
    }

    sshContext.setUsername(username);
    sshContext.setKeyPass("");
    sshContext.setPrivateKeyLoc(privateKeyFilePath);
    invocationContext.addSecurityContext(SSH_SECURITY_CONTEXT, sshContext);

    //set to super class
    /*setUsername(username);
    setPassword("");
    setKnownHostsFileName(null);
    setKeyFileName(privateKeyFilePath);*/

    // we need to erase gridftp URL since we will forcefully use SFTP
    // TODO
    /*execContext.setHost(this.instance.getPublicDnsName());
    execContext.getHostDesc().getHostConfiguration().setGridFTPArray(null);
    execContext.setFileTransferService(new SshFileTransferService(execContext, this.username, privateKeyFilePath));*/
}

From source file:org.apache.airavata.core.gfac.provider.impl.EC2Provider.java

License:Apache License

private void buildKeyPair(AmazonEC2Client ec2) throws NoSuchAlgorithmException, InvalidKeySpecException,
        AmazonServiceException, AmazonClientException, IOException {

    boolean newKey = false;

    File privateKeyFile = new File(privateKeyFilePath);
    File publicKeyFile = new File(privateKeyFilePath + ".pub");

    /*/*from  ww  w  .  j ava  2  s .  c o m*/
     * Check if Keypair already created on the server
     */
    if (!privateKeyFile.exists()) {

        // check folder and create if it does not exist
        File sshDir = new File(System.getProperty("user.home") + "/.ssh/");
        if (!sshDir.exists())
            sshDir.mkdir();

        // Generate a 1024-bit RSA key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(1024);
        java.security.KeyPair keypair = keyGen.genKeyPair();

        FileOutputStream fos = null;

        // Store Public Key.
        try {
            fos = new FileOutputStream(privateKeyFilePath + ".pub");
            // TODO
            //fos.write(Base64.encodeBytes(keypair.getPublic().getEncoded(), true).getBytes());
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                    fos = null;
                } catch (IOException ioe) {
                    throw ioe;
                }
            }
        }

        // Store Private Key.
        try {
            fos = new FileOutputStream(privateKeyFilePath);
            StringWriter stringWriter = new StringWriter();

            /*
             * Write in PEM format (openssl support)
             */
            PEMWriter pemFormatWriter = new PEMWriter(stringWriter);
            pemFormatWriter.writeObject(keypair.getPrivate());
            pemFormatWriter.close();
            fos.write(stringWriter.toString().getBytes());
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                    fos = null;
                } catch (IOException ioe) {
                    throw ioe;
                }
            }
        }

        privateKeyFile.setWritable(false, false);
        privateKeyFile.setExecutable(false, false);
        privateKeyFile.setReadable(false, false);
        privateKeyFile.setReadable(true);
        privateKeyFile.setWritable(true);

        // set that this key is just created
        newKey = true;
    }

    /*
     * Read Public Key
     */
    String encodedPublicKey = null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(publicKeyFile));
        encodedPublicKey = br.readLine();
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        if (br != null) {
            try {
                br.close();
                br = null;
            } catch (IOException ioe) {
                throw ioe;
            }
        }
    }

    /*
     * Generate key pair in Amazon if necessary
     */
    try {
        /*
         * Get current key pair in Amazon
         */
        DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
        ec2.describeKeyPairs(describeKeyPairsRequest.withKeyNames(KEY_PAIR_NAME));

        /*
         * If key exists and new key is created, delete old key and replace
         * with new one. Else, do nothing
         */

        if (newKey) {
            DeleteKeyPairRequest deleteKeyPairRequest = new DeleteKeyPairRequest(KEY_PAIR_NAME);
            ec2.deleteKeyPair(deleteKeyPairRequest);
            ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(KEY_PAIR_NAME,
                    encodedPublicKey);
            ec2.importKeyPair(importKeyPairRequest);
        }

    } catch (AmazonServiceException ase) {
        /*
         * Key doesn't exists, import new key.
         */
        if (ase.getErrorCode().equals("InvalidKeyPair.NotFound")) {
            ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(KEY_PAIR_NAME,
                    encodedPublicKey);
            ec2.importKeyPair(importKeyPairRequest);
        } else {
            throw ase;
        }
    }
}

From source file:org.apache.airavata.gfac.ec2.AmazonInstanceScheduler.java

License:Apache License

/**
 * Monitors the CPU Utilization using Amazon Cloud Watch. In order to monitor the instance, Cloud Watch Monitoring
 * should be enabled for the running instance.
 *
 * @param credential EC2 credentials//from  ww  w. j a  v a 2  s  .c  o m
 * @param instanceId instance id
 * @return average CPU utilization of the instance
 */
public static double monitorInstance(AWSCredentials credential, String instanceId) {
    try {
        AmazonCloudWatchClient cw = new AmazonCloudWatchClient(credential);

        long offsetInMilliseconds = 1000 * 60 * 60 * 24;
        GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
                .withStartTime(new Date(new Date().getTime() - offsetInMilliseconds)).withNamespace("AWS/EC2")
                .withPeriod(60 * 60)
                .withDimensions(new Dimension().withName("InstanceId").withValue(instanceId))
                .withMetricName("CPUUtilization").withStatistics("Average", "Maximum").withEndTime(new Date());
        GetMetricStatisticsResult getMetricStatisticsResult = cw.getMetricStatistics(request);

        double avgCPUUtilization = 0;
        List dataPoint = getMetricStatisticsResult.getDatapoints();
        for (Object aDataPoint : dataPoint) {
            Datapoint dp = (Datapoint) aDataPoint;
            avgCPUUtilization = dp.getAverage();
            log.info(instanceId + " instance's average CPU utilization : " + dp.getAverage());
        }

        return avgCPUUtilization;

    } catch (AmazonServiceException ase) {
        log.error("Caught an AmazonServiceException, which means the request was made  "
                + "to Amazon EC2, but was rejected with an error response for some reason.");
        log.error("Error Message:    " + ase.getMessage());
        log.error("HTTP Status Code: " + ase.getStatusCode());
        log.error("AWS Error Code:   " + ase.getErrorCode());
        log.error("Error Type:       " + ase.getErrorType());
        log.error("Request ID:       " + ase.getRequestId());

    }
    return 0;
}

From source file:org.apache.airavata.gfac.ec2.EC2Provider.java

License:Apache License

/**
 * Checks whether the port 22 of the Amazon instance is accessible.
 *
 * @param instance Amazon instance id.//from w w  w  .  java 2  s  . c om
 * @param ec2client AmazonEC2Client object
 */
private void checkConnection(Instance instance, AmazonEC2Client ec2client) {
    /* Make sure port 22 is connectible */
    for (GroupIdentifier g : instance.getSecurityGroups()) {
        IpPermission ip = new IpPermission();
        ip.setIpProtocol("tcp");
        ip.setFromPort(SSH_PORT);
        ip.setToPort(SSH_PORT);
        AuthorizeSecurityGroupIngressRequest r = new AuthorizeSecurityGroupIngressRequest();
        r = r.withIpPermissions(ip.withIpRanges("0.0.0.0/0"));
        r.setGroupId(g.getGroupId());
        try {
            ec2client.authorizeSecurityGroupIngress(r);
        } catch (AmazonServiceException as) {
            /* If exception is from duplicate room, ignore it. */
            if (!as.getErrorCode().equals("InvalidPermission.Duplicate"))
                throw as;
        }
    }
}

From source file:org.apache.airavata.gfac.ec2.util.EC2ProviderUtil.java

License:Apache License

/**
 * Builds a key pair with the given AmazonEC2Client and the generated key will have
 * the name keyPairName./*w w w  . j  av  a  2 s .co  m*/
 *
 * @param ec2 ec2client
 * @param keyPairName name for the generated key pair
 * @throws NoSuchAlgorithmException NoSuchAlgorithmException
 * @throws InvalidKeySpecException InvalidKeySpecException
 * @throws AmazonServiceException AmazonServiceException
 * @throws AmazonClientException AmazonClientException
 * @throws IOException IOException
 */
public static void buildKeyPair(AmazonEC2Client ec2, String keyPairName) throws NoSuchAlgorithmException,
        InvalidKeySpecException, AmazonServiceException, AmazonClientException, IOException {
    boolean newKey = false;

    String privateKeyFilePath = System.getProperty("user.home") + "/.ssh/" + keyPairName;
    File privateKeyFile = new File(privateKeyFilePath);
    File publicKeyFile = new File(privateKeyFilePath + ".pub");

    /* Check if Key-pair already created on the server */
    if (!privateKeyFile.exists()) {

        // check folder and create if it does not exist
        File sshDir = new File(System.getProperty("user.home") + "/.ssh/");
        if (!sshDir.exists())
            sshDir.mkdir();

        // Generate a 1024-bit RSA key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(1024);
        KeyPair keypair = keyGen.genKeyPair();

        FileOutputStream fos = null;

        // Store Public Key.
        try {
            fos = new FileOutputStream(privateKeyFilePath + ".pub");
            fos.write(Base64.encodeBytes(keypair.getPublic().getEncoded(), true).getBytes());
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                    fos = null;
                } catch (IOException ioe) {
                    throw ioe;
                }
            }
        }

        // Store Private Key.
        try {
            fos = new FileOutputStream(privateKeyFilePath);
            StringWriter stringWriter = new StringWriter();

            /* Write in PEM format (openssl support) */
            PEMWriter pemFormatWriter = new PEMWriter(stringWriter);
            pemFormatWriter.writeObject(keypair.getPrivate());
            pemFormatWriter.close();
            fos.write(stringWriter.toString().getBytes());
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                    fos = null;
                } catch (IOException ioe) {
                    throw ioe;
                }
            }
        }

        privateKeyFile.setWritable(false, false);
        privateKeyFile.setExecutable(false, false);
        privateKeyFile.setReadable(false, false);
        privateKeyFile.setReadable(true);
        privateKeyFile.setWritable(true);

        // set that this key is just created
        newKey = true;
    }

    /* Read Public Key */
    String encodedPublicKey = null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(publicKeyFile));
        encodedPublicKey = br.readLine();
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        if (br != null) {
            try {
                br.close();
                br = null;
            } catch (IOException ioe) {
                throw ioe;
            }
        }
    }

    /* Generate key pair in Amazon if necessary */
    try {
        /* Get current key pair in Amazon */
        DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
        ec2.describeKeyPairs(describeKeyPairsRequest.withKeyNames(keyPairName));

        /* If key exists and new key is created, delete old key and replace
         * with new one. Else, do nothing */
        if (newKey) {
            DeleteKeyPairRequest deleteKeyPairRequest = new DeleteKeyPairRequest(keyPairName);
            ec2.deleteKeyPair(deleteKeyPairRequest);
            ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(keyPairName, encodedPublicKey);
            ec2.importKeyPair(importKeyPairRequest);
        }

    } catch (AmazonServiceException ase) {
        /* Key doesn't exists, import new key. */
        if (ase.getErrorCode().equals("InvalidKeyPair.NotFound")) {
            ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(keyPairName, encodedPublicKey);
            ec2.importKeyPair(importKeyPairRequest);
        } else {
            throw ase;
        }
    }
}

From source file:org.apache.camel.component.aws.ddb.DdbEndpoint.java

License:Apache License

private void waitForTableToBecomeAvailable(String tableName) {
    LOG.trace("Waiting for [{}] to become ACTIVE...", tableName);

    long waitTime = 5 * 60 * 1000;
    while (waitTime > 0) {
        try {//from  ww  w  . j ava  2  s.  c  o m
            Thread.sleep(1000 * 5);
            waitTime -= 5000;
        } catch (Exception e) {
        }
        try {
            DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
            TableDescription tableDescription = getDdbClient().describeTable(request).getTable();
            if (isTableActive(tableDescription)) {
                LOG.trace("Table [{}] became active", tableName);
                return;
            }
            LOG.trace("Table [{}] not active yet", tableName);
        } catch (AmazonServiceException ase) {
            if (!ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException")) {
                throw ase;
            }
        }
    }

    throw new RuntimeException("Table " + tableName + " never went active");
}

From source file:org.apache.druid.indexing.kinesis.KinesisRecordSupplier.java

License:Apache License

private static boolean isServiceExceptionRecoverable(AmazonServiceException ex) {
    final boolean isIOException = ex.getCause() instanceof IOException;
    final boolean isTimeout = "RequestTimeout".equals(ex.getErrorCode());
    return isIOException || isTimeout;
}

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

License:Apache License

private Optional<ByteSource> streamTaskFile(final long offset, String taskKey) throws IOException {
    try {/*from  w  w w.j a  v  a2s  .  co  m*/
        final ObjectMetadata objectMetadata = service.getObjectMetadata(config.getS3Bucket(), taskKey);

        return Optional.of(new ByteSource() {
            @Override
            public InputStream openStream() throws IOException {
                try {
                    final long start;
                    final long end = objectMetadata.getContentLength() - 1;

                    if (offset > 0 && offset < objectMetadata.getContentLength()) {
                        start = offset;
                    } else if (offset < 0 && (-1 * offset) < objectMetadata.getContentLength()) {
                        start = objectMetadata.getContentLength() + offset;
                    } else {
                        start = 0;
                    }

                    final GetObjectRequest request = new GetObjectRequest(config.getS3Bucket(), taskKey)
                            .withMatchingETagConstraint(objectMetadata.getETag()).withRange(start, end);

                    return service.getObject(request).getObjectContent();
                } catch (AmazonServiceException e) {
                    throw new IOException(e);
                }
            }
        });
    } catch (AmazonS3Exception e) {
        if (404 == e.getStatusCode() || "NoSuchKey".equals(e.getErrorCode())
                || "NoSuchBucket".equals(e.getErrorCode())) {
            return Optional.absent();
        } else {
            throw new IOE(e, "Failed to stream logs from: %s", taskKey);
        }
    }
}