Java tutorial
package com.runtimecollective.influence.metrics; /* * Copyright 2010-2011 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URLEncoder; import java.security.SignatureException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Properties; import java.util.Set; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.PropertiesCredentials; import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2Client; import com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult; import com.amazonaws.services.ec2.model.DescribeInstancesResult; import com.amazonaws.services.ec2.model.Instance; import com.amazonaws.services.ec2.model.Reservation; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.Bucket; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.S3ObjectSummary; import com.amazonaws.services.simpledb.AmazonSimpleDB; import com.amazonaws.services.simpledb.AmazonSimpleDBClient; import com.amazonaws.services.simpledb.model.DomainMetadataRequest; import com.amazonaws.services.simpledb.model.DomainMetadataResult; import com.amazonaws.services.simpledb.model.ListDomainsRequest; import com.amazonaws.services.simpledb.model.ListDomainsResult; import com.runtimecollective.influence.metrics.accounts.HttpRequestor; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; /** * Welcome to your new AWS Java SDK based project! * * This class is meant as a starting point for your console-based application that * makes one or more calls to the AWS services supported by the Java SDK, such as EC2, * SimpleDB, and S3. * * In order to use the services in this sample, you need: * * - A valid Amazon Web Services account. You can register for AWS at: * https://aws-portal.amazon.com/gp/aws/developer/registration/index.html * * - Your account's Access Key ID and Secret Access Key: * http://aws.amazon.com/security-credentials * * - A subscription to Amazon EC2. You can sign up for EC2 at: * http://aws.amazon.com/ec2/ * * - A subscription to Amazon SimpleDB. You can sign up for Simple DB at: * http://aws.amazon.com/simpledb/ * * - A subscription to Amazon S3. You can sign up for S3 at: * http://aws.amazon.com/s3/ */ public class Alexa extends HttpRequestor implements Metric { private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; Properties properties; public Alexa() throws IOException { properties = new Properties(); FileInputStream fis = new FileInputStream("alexa.properties"); properties.load(fis); } public void getDetails() throws Exception { Date timestamp = new Date(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String formattedTime = URLEncoder.encode(format.format(timestamp), "UTF-8"); String action = "UrlInfo"; String search = "AWSAccessKeyId=" + properties.getProperty("accessKey") + "&Timestamp=" + formattedTime + "&Signature=" + URLEncoder.encode( calculateRFC2104HMAC(action + formattedTime, properties.getProperty("secretKey")), "UTF-8") + "&Version=2005-07-11" + "&Action=" + action + "&ResponseGroup=Rank" + "&Url=yahoo.com"; HttpResponse response = getRequest("http://awis.amazonaws.com/?" + search); System.out.println(EntityUtils.toString(response.getEntity())); int a = 5; } /** * Computes RFC 2104-compliant HMAC signature. * * @param data * The data to be signed. * @param key * The signing key. * @return * The Base64-encoded RFC 2104-compliant HMAC signature. * @throws * java.security.SignatureException when signature generation fails */ private String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException { String result; try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes()); // base64-encode the hmac result = new sun.misc.BASE64Encoder().encode(rawHmac); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; } @Override public double getWeightedScore() { return 0; } }