Пример #1
0
  private int parseClockSkewOffset(
      org.apache.http.HttpResponse response, AmazonServiceException exception) {
    Date deviceDate = new Date();
    Date serverDate = null;
    String serverDateStr = null;
    Header[] responseDateHeader = response.getHeaders("Date");

    try {

      if (responseDateHeader.length == 0) {
        // SQS doesn't return Date header
        serverDateStr = getServerDateFromException(exception.getMessage());
        serverDate = DateUtils.parseCompressedISO8601Date(serverDateStr);
      } else {
        serverDateStr = responseDateHeader[0].getValue();
        serverDate = DateUtils.parseRFC822Date(serverDateStr);
      }
    } catch (RuntimeException e) {
      log.warn("Unable to parse clock skew offset from response: " + serverDateStr, e);
      return 0;
    }

    long diff = deviceDate.getTime() - serverDate.getTime();
    return (int) (diff / 1000);
  }
    public Date unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
      String dateString = unmarshallerContext.readText();
      if (dateString == null) return null;

      try {
        return dateUtils.parseIso8601Date(dateString);
      } catch (ParseException e) {
        log.warn("Unable to parse date '" + dateString + "':  " + e.getMessage(), e);
        return null;
      }
    }
  @Override
  public AttributeValue marshall(Object obj) {
    @SuppressWarnings("unchecked")
    Set<Date> dates = (Set<Date>) obj;

    List<String> timestamps = new ArrayList<String>(dates.size());
    for (Date date : dates) {
      timestamps.add(DateUtils.formatISO8601Date(date));
    }

    return new AttributeValue().withSS(timestamps);
  }
Пример #4
0
  /** Fetches the credentials from the endpoint. */
  private synchronized void fetchCredentials() {
    if (!needsToLoadCredentials()) return;

    JsonNode accessKey;
    JsonNode secretKey;
    JsonNode node;
    JsonNode token;
    try {
      lastInstanceProfileCheck = new Date();

      String credentialsResponse =
          EC2CredentialsUtils.readResource(credentailsEndpointProvider.getCredentialsEndpoint());

      node = Jackson.jsonNodeOf(credentialsResponse);
      accessKey = node.get(ACCESS_KEY_ID);
      secretKey = node.get(SECRET_ACCESS_KEY);
      token = node.get(TOKEN);

      if (null == accessKey || null == secretKey) {
        throw new AmazonClientException("Unable to load credentials.");
      }

      if (null != token) {
        credentials =
            new BasicSessionCredentials(accessKey.asText(), secretKey.asText(), token.asText());
      } else {
        credentials = new BasicAWSCredentials(accessKey.asText(), secretKey.asText());
      }

      JsonNode expirationJsonNode = node.get("Expiration");
      if (null != expirationJsonNode) {
        /*
         * 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 = expirationJsonNode.asText();
        expiration = expiration.replaceAll("\\+0000$", "Z");

        try {
          credentialsExpiration = DateUtils.parseISO8601Date(expiration);
        } catch (Exception ex) {
          handleError("Unable to parse credentials expiration date from Amazon EC2 instance", ex);
        }
      }
    } catch (JsonMappingException e) {
      handleError("Unable to parse response returned from service endpoint", e);
    } catch (IOException e) {
      handleError("Unable to load credentials from service endpoint", e);
    } catch (URISyntaxException e) {
      handleError("Unable to load credentials from service endpoint", e);
    }
  }
Пример #5
0
 public static String formatRfc822Date(Date date) {
   return dateUtils.formatRfc822Date(date);
 }
Пример #6
0
 public static Date parseRfc822Date(String dateString) throws ParseException {
   return dateUtils.parseRfc822Date(dateString);
 }
Пример #7
0
 public static String formatIso8601Date(Date date) {
   return dateUtils.formatIso8601Date(date);
 }
 /**
  * Constructs a new access policy condition that compares the current time (on the AWS servers) to
  * the specified date.
  *
  * @param type The type of comparison to perform. For example, {@link
  *     DateComparisonType#DateLessThan} will cause this policy condition to evaluate to true if
  *     the current date is less than the date specified in the second argument.
  * @param date The date to compare against.
  */
 public DateCondition(DateComparisonType type, Date date) {
   super.type = type.toString();
   super.conditionKey = ConditionFactory.CURRENT_TIME_CONDITION_KEY;
   super.values = Arrays.asList(new String[] {dateUtils.formatIso8601Date(date)});
 }