@Override
  public String toString() {
    final StringBuilder builder = new StringBuilder();
    builder.append(getHttpMethod()).append(" ");
    builder.append(getEndpoint()).append(" ");
    String resourcePath = getResourcePath();

    if (resourcePath == null) {
      builder.append("/");
    } else {
      if (!resourcePath.startsWith("/")) {
        builder.append("/");
      }
      builder.append(resourcePath);
    }
    builder.append(" ");
    if (!getParameters().isEmpty()) {
      builder.append("Parameters: (").append(Jackson.toJsonString(parameters));
    }

    if (!getHeaders().isEmpty()) {
      builder.append("Headers: (");
      for (String key : getHeaders().keySet()) {
        String value = getHeaders().get(key);
        builder.append(key).append(": ").append(value).append(", ");
      }
      builder.append(") ");
    }

    return builder.toString();
  }
Beispiel #2
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);
    }
  }
  /**
   * 開発担当者にメッセージを通知する。
   *
   * @param subject タイトル
   * @param messageMap メッセージ
   * @param t 例外
   * @since 0.3
   */
  public void notifyDev(String subject, Map<String, String> messageMap, Throwable t) {
    messageMap.put("environment", env.toString());
    if (instanceInfo != null) {
      messageMap.put("instanceMetadata", Jackson.toJsonString(instanceInfo));
    } else if (instanceMetadata != null) {
      @SuppressWarnings("deprecation")
      String metadataString = instanceMetadata.toString();
      messageMap.put("instanceMetadata", metadataString);
    }

    if (t != null) {
      messageMap.put("stackTrace", toString(t));
    }

    notifyMessage0(devTopicArn, subject, createMessage(messageMap));
  }