if (needsToLoadCredentials()) {
try {
String credentialsResponse = new EC2MetadataClient()
.getDefaultCredentials();
JSONObject jsonObject = new JSONObject(credentialsResponse);
if (jsonObject.has("Token")) {
credentials = new BasicSessionCredentials(
jsonObject.getString("AccessKeyId"),
jsonObject.getString("SecretAccessKey"),
jsonObject.getString("Token"));
} else {
credentials = new BasicAWSCredentials(
jsonObject.getString("AccessKeyId"),
jsonObject.getString("SecretAccessKey"));
}
if (jsonObject.has("Expiration")) {
/*
* TODO: The expiration string comes in a different format
* than what we deal with in other parts of the SDK, so we
* have to convert it to the ISO8601 syntax we expect.
*/
String expiration = jsonObject.getString("Expiration");
expiration = expiration.replaceAll("\\+0000$", "Z");
credentialsExpiration = new DateUtils()
.parseIso8601Date(expiration);
}