Skip to content

Instantly share code, notes, and snippets.

@apetresc
Created December 31, 2010 08:30
Show Gist options
  • Save apetresc/760860 to your computer and use it in GitHub Desktop.
Save apetresc/760860 to your computer and use it in GitHub Desktop.
A modified version of the AWS SDK for Android sample code which fetches credentials from awskeyserver.
package com.amazon.aws.demo;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Properties;
import com.amazon.aws.demo.R;
import com.amazon.aws.demo.s3.S3Menu;
import com.amazon.aws.demo.sdb.SdbMenu;
import com.amazon.aws.demo.sns.SnsMenu;
import com.amazon.aws.demo.sqs.SqsMenu;
import com.amazonaws.auth.BasicAWSCredentials;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AWSDemo extends Activity {
private Handler mHandler;
private static final String success = "Welcome to The AWS Browser Demo!";
private static final String fail = "Load Failed. Please Try Restarting the Application.";
public static BasicAWSCredentials credentials = null;
protected Button snsButton;
protected Button sqsButton;
protected Button s3Button;
protected Button sdbButton;
protected TextView welcomeText;
private boolean credentials_found;
private final Runnable postResults = new Runnable() {
@Override
public void run(){
updateUi();
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
snsButton = (Button) findViewById(R.id.main_notify_button);
sqsButton = (Button) findViewById(R.id.main_queue_button);
s3Button = (Button) findViewById(R.id.main_storage_button);
sdbButton = (Button) findViewById(R.id.main_sdb_button);
welcomeText = (TextView) findViewById(R.id.main_into_text);
startGetCredentials();
}
protected void updateUi(){
if(credentials_found == true){
welcomeText.setText(success);
snsButton.setVisibility(View.VISIBLE);
sqsButton.setVisibility(View.VISIBLE);
s3Button.setVisibility(View.VISIBLE);
sdbButton.setVisibility(View.VISIBLE);
wireButtons();
} else {
displayCredentialsIssueAndExit();
welcomeText.setText(fail);
}
}
private void wireButtons(){
snsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(AWSDemo.this, SnsMenu.class));
}
});
sqsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity( new Intent(AWSDemo.this, SqsMenu.class));
}
});
s3Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(AWSDemo.this, S3Menu.class));
}
});
sdbButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(AWSDemo.this, SdbMenu.class));
}
});
}
private void startGetCredentials() {
final SharedPreferences settings = AWSDemo.this.getSharedPreferences("AWSCredentials", 0);
if (!settings.contains("awsAccessKey")) {
Log.i("Loading AWS Credentials", "No credentials found. Obtaining new ones...");
try {
final String responseString = readStringFromUrl("http://awskeyserver.appspot.com/create_user?group=Users");
if (!responseString.contains(":")) {
// Must have received a reCaptcha code
AlertDialog.Builder ab = new AlertDialog.Builder(AWSDemo.this);
ImageView captchaImage = new ImageView(AWSDemo.this);
captchaImage.setImageDrawable(
Drawable.createFromStream((InputStream) new URL("http://www.google.com/recaptcha/api/image?c=" + responseString).getContent(), "reCaptcha"));
final EditText captchaResponse = new EditText(AWSDemo.this);
LinearLayout captchaView = new LinearLayout(AWSDemo.this);
captchaView.addView(captchaImage);
captchaView.addView(captchaResponse, LinearLayout.LayoutParams.FILL_PARENT);
captchaView.setOrientation(LinearLayout.VERTICAL);
ab.setView(captchaView);
ab.setPositiveButton("Ok", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String captchaResponseText = URLEncoder.encode(captchaResponse.getText().toString());
try {
String credentialString = readStringFromUrl(
String.format("http://awskeyserver.appspot.com/create_user?"
+ "group=Users"
+ "&recaptcha_challenge_field=%s"
+ "&recaptcha_response_field=%s",
responseString, captchaResponseText));
storeCredentials(credentialString.split(":")[0], credentialString.split(":")[1], settings);
} catch (IOException e) {
e.printStackTrace();
}
}});
ab.show();
} else {
// No protection, just credentials
storeCredentials(responseString.split(":")[0], responseString.split(":")[1], settings);
}
} catch (Exception exception) {
Log.e("Loading AWS Credentials", "Message: " + exception.getMessage());
credentials_found = false;
}
} else {
credentials = new BasicAWSCredentials(
settings.getString("awsAccessKey", null),
settings.getString("awsSecretKey", null));
credentials_found = true;
AWSDemo.this.mHandler.post(postResults);
}
}
private void storeCredentials(String accessKey, String secretKey, SharedPreferences settings) {
SharedPreferences.Editor editor = settings.edit();
editor.putString("awsAccessKey", accessKey);
editor.putString("awsSecretKey", secretKey);
editor.commit();
credentials = new BasicAWSCredentials(accessKey, secretKey);
credentials_found = true;
AWSDemo.this.mHandler.post(postResults);
}
private String readStringFromUrl(String urlString) throws IOException {
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
String readString = "";
int byteRead = in.read();
while (byteRead >= 0) {
readString += new String(new byte[] { (byte) byteRead });
byteRead = in.read();
}
return readString;
}
protected void displayCredentialsIssueAndExit() {
AlertDialog.Builder confirm = new AlertDialog.Builder( this );
confirm.setTitle("Credential Problem!");
confirm.setMessage( "AWS Credentials not configured correctly. Please review the README file." );
confirm.setNegativeButton( "OK", new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int which ) {
AWSDemo.this.finish();
}
} );
confirm.show().show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment