Example usage for com.amazonaws AmazonServiceException getMessage

List of usage examples for com.amazonaws AmazonServiceException getMessage

Introduction

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

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:com.vmware.photon.controller.model.adapters.awsadapter.AWSEndpointAdapterService.java

License:Open Source License

/**
 * Method gets the aws accountId from the specified credentials.
 *
 * @param privateKeyId//from w w w . ja v a 2s .  co m
 * @param privateKey
 * @return account ID
 */
private String getAccountId(String privateKeyId, String privateKey) {
    AWSCredentials awsCredentials = new BasicAWSCredentials(privateKeyId, privateKey);
    AmazonIdentityManagementClient iamClient = new AmazonIdentityManagementClient(awsCredentials);
    String userId = null;
    try {
        if ((iamClient.getUser() != null) && (iamClient.getUser().getUser() != null)
                && (iamClient.getUser().getUser().getArn() != null)) {

            String arn = iamClient.getUser().getUser().getArn();
            /*
             * arn:aws:service:region:account:resource -> so limiting the split to 6 words and
             * extracting the accountId which is 5th one in list. If the user is not authorized
             * to perform iam:GetUser on that resource,still error mesage will have accountId
             */
            userId = arn.split(":", 6)[4];
        }
    } catch (AmazonServiceException ex) {
        if (ex.getErrorCode().compareTo("AccessDenied") == 0) {
            String msg = ex.getMessage();
            userId = msg.split(":", 7)[5];
        }
    }
    return userId;
}

From source file:com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.java

License:Open Source License

public static String allocateSecurityGroup(AWSAllocation aws) {
    String groupId;/* w  w w. ja v a  2  s  . c  o m*/
    SecurityGroup group;

    // use the security group provided in the description properties
    String sgId = getFromCustomProperties(aws.child.description, AWSConstants.AWS_SECURITY_GROUP_ID);
    if (sgId != null) {
        return sgId;
    }

    // if the group doesn't exist an exception is thrown. We won't throw a
    // missing group exception
    // we will continue and create the group
    try {
        group = getSecurityGroup(aws.amazonEC2Client);
        if (group != null) {
            return group.getGroupId();
        }
    } catch (AmazonServiceException t) {
        if (!t.getMessage().contains(DEFAULT_SECURITY_GROUP_NAME)) {
            throw t;
        }
    }

    // get the subnet cidr from the subnet provided in description properties (if any)
    String subnet = getSubnetFromDescription(aws);

    // if no subnet provided then get the default one for the default vpc
    if (subnet == null) {
        subnet = getDefaultVPCSubnet(aws);
    }

    // no subnet is not an option...
    if (subnet == null) {
        throw new AmazonServiceException("default VPC not found");
    }

    try {
        // create the security group for the the vpc
        // provided in the description properties (if any)
        String vpcId = getFromCustomProperties(aws.child.description, AWSConstants.AWS_VPC_ID);

        groupId = createSecurityGroup(aws.amazonEC2Client, vpcId);
        updateIngressRules(aws.amazonEC2Client, groupId, getDefaultRules(subnet));
    } catch (AmazonServiceException t) {
        if (t.getMessage().contains(DEFAULT_SECURITY_GROUP_NAME)) {
            return getSecurityGroup(aws.amazonEC2Client).getGroupId();
        } else {
            throw t;
        }
    }

    return groupId;
}

From source file:com.yahoo.ycsb.db.DynamoDBClient.java

License:Open Source License

@Override
public int read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {

    logger.debug("readkey: " + key + " from table: " + table);
    GetItemRequest req = new GetItemRequest(table, createPrimaryKey(key));
    req.setAttributesToGet(fields);// ww  w.  j av a2  s.  c  o  m
    req.setConsistentRead(consistentRead);
    GetItemResult res = null;

    try {
        res = dynamoDB.getItem(req);
    } catch (AmazonServiceException ex) {
        logger.error(ex.getMessage());
        return SERVER_ERROR;
    } catch (AmazonClientException ex) {
        logger.error(ex.getMessage());
        return CLIENT_ERROR;
    }

    if (null != res.getItem()) {
        result.putAll(extractResult(res.getItem()));
        logger.debug("Result: " + res.toString());
    }
    return OK;
}

From source file:com.yahoo.ycsb.db.DynamoDBClient.java

License:Open Source License

@Override
public int scan(String table, String startkey, int recordcount, Set<String> fields,
        Vector<HashMap<String, ByteIterator>> result) {
    logger.debug("scan " + recordcount + " records from key: " + startkey + " on table: " + table);
    /*/* www .j  a  va2 s. c o m*/
     * on DynamoDB's scan, startkey is *exclusive* so we need to
     * getItem(startKey) and then use scan for the res
     */
    GetItemRequest greq = new GetItemRequest(table, createPrimaryKey(startkey));
    greq.setAttributesToGet(fields);

    GetItemResult gres = null;

    try {
        gres = dynamoDB.getItem(greq);
    } catch (AmazonServiceException ex) {
        logger.error(ex.getMessage());
        return SERVER_ERROR;
    } catch (AmazonClientException ex) {
        logger.error(ex.getMessage());
        return CLIENT_ERROR;
    }

    if (null != gres.getItem()) {
        result.add(extractResult(gres.getItem()));
    }

    int count = 1; // startKey is done, rest to go.

    Key startKey = createPrimaryKey(startkey);
    ScanRequest req = new ScanRequest(table);
    req.setAttributesToGet(fields);
    while (count < recordcount) {
        req.setExclusiveStartKey(startKey);
        req.setLimit(recordcount - count);
        ScanResult res = null;
        try {
            res = dynamoDB.scan(req);
        } catch (AmazonServiceException ex) {
            logger.error(ex.getMessage());
            ex.printStackTrace();
            return SERVER_ERROR;
        } catch (AmazonClientException ex) {
            logger.error(ex.getMessage());
            ex.printStackTrace();
            return CLIENT_ERROR;
        }

        count += res.getCount();
        for (Map<String, AttributeValue> items : res.getItems()) {
            result.add(extractResult(items));
        }
        startKey = res.getLastEvaluatedKey();

    }

    return OK;
}

From source file:com.yahoo.ycsb.db.DynamoDBClient.java

License:Open Source License

@Override
public int update(String table, String key, HashMap<String, ByteIterator> values) {
    logger.debug("updatekey: " + key + " from table: " + table);

    Map<String, AttributeValueUpdate> attributes = new HashMap<String, AttributeValueUpdate>(values.size());
    for (Entry<String, ByteIterator> val : values.entrySet()) {
        AttributeValue v = new AttributeValue(val.getValue().toString());
        attributes.put(val.getKey(), new AttributeValueUpdate().withValue(v).withAction("PUT"));
    }/*www  . j  a  v  a2 s .  c om*/

    UpdateItemRequest req = new UpdateItemRequest(table, createPrimaryKey(key), attributes);

    try {
        dynamoDB.updateItem(req);
    } catch (AmazonServiceException ex) {
        logger.error(ex.getMessage());
        return SERVER_ERROR;
    } catch (AmazonClientException ex) {
        logger.error(ex.getMessage());
        return CLIENT_ERROR;
    }
    return OK;
}

From source file:com.yahoo.ycsb.db.DynamoDBClient.java

License:Open Source License

@Override
public int insert(String table, String key, HashMap<String, ByteIterator> values) {
    logger.debug("insertkey: " + primaryKeyName + "-" + key + " from table: " + table);
    Map<String, AttributeValue> attributes = createAttributes(values);
    // adding primary key
    attributes.put(primaryKeyName, new AttributeValue(key));

    PutItemRequest putItemRequest = new PutItemRequest(table, attributes);
    PutItemResult res = null;// www. j  a  va 2 s.c om
    try {
        res = dynamoDB.putItem(putItemRequest);
    } catch (AmazonServiceException ex) {
        logger.error(ex.getMessage());
        return SERVER_ERROR;
    } catch (AmazonClientException ex) {
        logger.error(ex.getMessage());
        return CLIENT_ERROR;
    }
    return OK;
}

From source file:com.yahoo.ycsb.db.DynamoDBClient.java

License:Open Source License

@Override
public int delete(String table, String key) {
    logger.debug("deletekey: " + key + " from table: " + table);
    DeleteItemRequest req = new DeleteItemRequest(table, createPrimaryKey(key));
    DeleteItemResult res = null;//w  w  w  .  j  av a 2  s. c  o  m

    try {
        res = dynamoDB.deleteItem(req);
    } catch (AmazonServiceException ex) {
        logger.error(ex.getMessage());
        return SERVER_ERROR;
    } catch (AmazonClientException ex) {
        logger.error(ex.getMessage());
        return CLIENT_ERROR;
    }
    return OK;
}

From source file:com.yahoo.ycsb.db.MailAppDynamoDBClient.java

License:Open Source License

@Override
public int read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    if (key.contains(hashAndRangeKeyDelimiter)) {
        String[] keys = key.split(hashAndRangeKeyDelimiter);
        String hashKey = keys[0];
        String rangeKey = keys[1];
        return readWithHashAndRangeKey(table, hashKey, rangeKey, fields, result);
    } else {/*from  w w w. ja v  a 2  s  .  c  o  m*/

        logger.debug("readkey: " + key + " from table: " + table);

        GetItemRequest req = new GetItemRequest(table, createPrimaryKey(key));
        req.setAttributesToGet(fields);
        req.setConsistentRead(consistentRead);
        GetItemResult res = null;

        try {
            res = dynamoDB.getItem(req);
        } catch (AmazonServiceException ex) {
            logger.error(ex.getMessage());
            return SERVER_ERROR;
        } catch (AmazonClientException ex) {
            logger.error(ex.getMessage());
            return CLIENT_ERROR;
        }

        if (null != res.getItem()) {
            result.putAll(extractResult(res.getItem()));
            logger.debug("Result: " + res.toString());
        }

        /*
                
        Key lastKeyEvaluated = null;
                
        do {
                   
                
        QueryRequest queryRequest = new QueryRequest()
        .withTableName(table)
        .withHashKeyValue(new AttributeValue(key))
        .withExclusiveStartKey(lastKeyEvaluated);
        QueryResult res;
        try{
        res = dynamoDB.query(queryRequest);
         }catch (AmazonServiceException ex) {
        logger.error(ex.getMessage());
        return SERVER_ERROR;
          }catch (AmazonClientException ex){
        logger.error(ex.getMessage());
        return CLIENT_ERROR;
          }
                
          if (res.getCount()>0)
          {
                     
        result.putAll(extractResult(res.getItems().get(0)));
        logger.debug("Result: " + res.toString());
          }
                  
          lastKeyEvaluated = res.getLastEvaluatedKey();
                
        } while (lastKeyEvaluated != null);
                    
        */
        //-------
        /*  GetItemRequest req = new GetItemRequest(table, createPrimaryKey(key));
          req.setAttributesToGet(fields);
          req.setConsistentRead(consistentRead);
          GetItemResult res = null;
                
          try {
              res = dynamoDB.getItem(req);
          }catch (AmazonServiceException ex) {
              logger.error(ex.getMessage());
              return SERVER_ERROR;
          }catch (AmazonClientException ex){
              logger.error(ex.getMessage());
              return CLIENT_ERROR;
          }
                
          if (null != res.getItem())
          {
              result.putAll(extractResult(res.getItem()));
              logger.debug("Result: " + res.toString());
          }
          */
        //return OK;
        return (int) Math.ceil(res.getConsumedCapacityUnits());
    }
}

From source file:com.yahoo.ycsb.db.MailAppDynamoDBClient.java

License:Open Source License

public int readWithHashAndRangeKey(String table, String hashKey, String rangeKey, Set<String> fields,
        HashMap<String, ByteIterator> result) {

    logger.debug("readkey: " + hashKey + "rangeKey: " + rangeKey + " from table: " + table);
    Key key = new Key().withHashKeyElement(new AttributeValue(hashKey))
            .withRangeKeyElement(new AttributeValue(rangeKey));
    GetItemRequest req = new GetItemRequest().withTableName(table).withKey(key);
    req.setAttributesToGet(fields);/*from   w  w w. ja  v  a  2 s .  c  o  m*/
    req.setConsistentRead(consistentRead);
    GetItemResult res = null;

    try {
        res = dynamoDB.getItem(req);
    } catch (AmazonServiceException ex) {
        logger.error(ex.getMessage());
        return SERVER_ERROR;
    } catch (AmazonClientException ex) {
        logger.error(ex.getMessage());
        return CLIENT_ERROR;
    }

    if (null != res.getItem()) {
        result.putAll(extractResult(res.getItem()));
        logger.debug("Result: " + res.toString());
    }
    //return OK;
    return (int) Math.ceil(res.getConsumedCapacityUnits());

}

From source file:com.yahoo.ycsb.db.MailAppDynamoDBClient.java

License:Open Source License

@Override
public int scan(String table, String startkey, int recordcount, Set<String> fields,
        Vector<HashMap<String, ByteIterator>> result) {
    if (startkey.contains(hashAndRangeKeyDelimiter)) {
        String[] keys = startkey.split(hashAndRangeKeyDelimiter);
        //do a query instead of scan, if rangeKyDelimiter is in key string
        String hashKey = keys[0];
        return query(table, hashKey, recordcount, fields, result);
    } else {//  www. ja  va 2  s. c o m

        logger.debug("scan " + recordcount + " records from key: " + startkey + " on table: " + table);
        /*
         * on DynamoDB's scan, startkey is *exclusive* so we need to
         * getItem(startKey) and then use scan for the res
         */

        GetItemRequest greq = new GetItemRequest(table, createPrimaryKey(startkey));
        greq.setAttributesToGet(fields);

        GetItemResult gres = null;

        try {
            gres = dynamoDB.getItem(greq);
        } catch (AmazonServiceException ex) {
            logger.error(ex.getMessage());
            return SERVER_ERROR;
        } catch (AmazonClientException ex) {
            logger.error(ex.getMessage());
            return CLIENT_ERROR;
        }

        if (null != gres.getItem()) {
            result.add(extractResult(gres.getItem()));
        }

        int count = 1; // startKey is done, rest to go.

        Key startKey = createPrimaryKey(startkey);
        ScanRequest req = new ScanRequest(table);
        req.setAttributesToGet(fields);
        while (count < recordcount) {
            req.setExclusiveStartKey(startKey);
            req.setLimit(recordcount - count);
            ScanResult res = null;
            try {
                res = dynamoDB.scan(req);
            } catch (AmazonServiceException ex) {
                logger.error(ex.getMessage());
                ex.printStackTrace();
                return SERVER_ERROR;
            } catch (AmazonClientException ex) {
                logger.error(ex.getMessage());
                ex.printStackTrace();
                return CLIENT_ERROR;
            }

            count += res.getCount();
            for (Map<String, AttributeValue> items : res.getItems()) {
                result.add(extractResult(items));
            }
            startKey = res.getLastEvaluatedKey();

        }

        return OK;

    }
}