public static void main(String[] args) {
    /*
     * Set up the signed requests helper
     */
    SignedRequestsHelper helper;
    try {
      helper = SignedRequestsHelper.getInstance(ENDPOINT, AWS_ACCESS_KEY_ID, AWS_SECRET_KEY);
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }

    String requestUrl = null;
    String title = null;

    /* The helper can sign requests in two forms - map form and string form */

    /*
     * Here is an example in map form, where the request parameters are stored in a map.
     */
    System.out.println("Map form example:");
    Map<String, String> params = new HashMap<String, String>();
    params.put("Service", "AWSECommerceService");
    params.put("Version", "2009-03-31");
    params.put("Operation", "ItemSearch");
    params.put("SearchIndex", "Music");
    params.put("Keywords", "girls generation");
    params.put("ResponseGroup", "Small");

    requestUrl = helper.sign(params);
    System.out.println("Signed Request is \"" + requestUrl + "\"");

    ArrayList<String> titles = fetchTitles(requestUrl);
    System.out.println("Girls GGeneration Albums!");
    for (int i = 0; i < titles.size(); i++) {
      System.out.println(titles.get(i));
    }
  }
Esempio n. 2
0
  /**
   * Searches the amazon ws for a movie by the given id.
   *
   * @param params
   * @return
   */
  private static List<AmazonResult> search(Map<String, String> params) {

    String awsEndPoint = ConfigFactory.load().getString("dvddb.amazon.endPoint");
    if (StringUtils.isEmpty(awsEndPoint) == true) {
      if (Logger.isErrorEnabled() == true) {
        Logger.error("No AWS endPoint set in the configuration.");
      }
      return null;
    }
    String awsKeyId = ConfigFactory.load().getString("dvddb.amazon.aws.keyid");
    if (StringUtils.isEmpty(awsKeyId) == true) {
      if (Logger.isErrorEnabled() == true) {
        Logger.error("No AWS keyID set in the configuration.");
      }
      return null;
    }
    String awsSecretKey = ConfigFactory.load().getString("dvddb.amazon.aws.secretkey");
    if (StringUtils.isEmpty(awsSecretKey) == true) {
      if (Logger.isErrorEnabled() == true) {
        Logger.error("No AWS keySecretKey set in the configuration.");
      }
      return null;
    }

    try {
      SignedRequestsHelper helper =
          SignedRequestsHelper.getInstance(awsEndPoint, awsKeyId, awsSecretKey);

      if (params == null) {
        params = new HashMap<>();
      }

      params.put("Service", "AWSECommerceService");
      params.put("Version", "2011-08-02");
      params.put("ResponseGroup", "ItemAttributes,Images");
      params.put("AssociateTag", "aztag-20");

      String requestUrl = helper.sign(params);
      if (Logger.isDebugEnabled() == true) {
        Logger.debug("Signed AWS request: " + requestUrl);
      }

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(requestUrl);

      final List<AmazonResult> result = new ArrayList<>();
      NodeList itemNodes = XPath.selectNodes("//Items/Item", doc);
      for (int i = 0; i < itemNodes.getLength(); i++) {
        Node item = itemNodes.item(i);
        final AmazonResult resultFromNode = getResultFromNode(item);
        if (resultFromNode != null) {
          result.add(resultFromNode);
        }
      }

      return result;

    } catch (Exception e) {
      if (Logger.isErrorEnabled() == true) {
        Logger.error("An error happend while looking in the aws.", e);
      }
      return null;
    }
  }