[jbosscache-commits] JBoss Cache SVN: r5332 - in amazon-s3/trunk/src: test/java/com/amazon/s3 and 1 other directory.
jbosscache-commits at lists.jboss.org
jbosscache-commits at lists.jboss.org
Fri Feb 8 04:32:51 EST 2008
Author: genman
Date: 2008-02-08 04:32:51 -0500 (Fri, 08 Feb 2008)
New Revision: 5332
Added:
amazon-s3/trunk/src/test/java/com/amazon/s3/CallingFormatTest.java
amazon-s3/trunk/src/test/java/com/amazon/s3/CanonicalStringTest.java
Modified:
amazon-s3/trunk/src/main/java/com/amazon/s3/CallingFormat.java
amazon-s3/trunk/src/main/java/com/amazon/s3/CanonicalString.java
amazon-s3/trunk/src/main/java/com/amazon/s3/Connection.java
amazon-s3/trunk/src/main/java/com/amazon/s3/Entry.java
amazon-s3/trunk/src/main/java/com/amazon/s3/Headers.java
amazon-s3/trunk/src/main/java/com/amazon/s3/ListAllBucketsResponse.java
amazon-s3/trunk/src/main/java/com/amazon/s3/Method.java
amazon-s3/trunk/src/main/java/com/amazon/s3/QueryGenerator.java
amazon-s3/trunk/src/main/java/com/amazon/s3/Response.java
amazon-s3/trunk/src/test/java/com/amazon/s3/S3Test.java
Log:
Additional code cleanup, more tests
Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/CallingFormat.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/CallingFormat.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/CallingFormat.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -23,8 +23,20 @@
*/
public abstract class CallingFormat {
+ /**
+ * Call by using a path.
+ */
public final static CallingFormat PATH = new PathCallingFormat();
+
+ /**
+ * Call by using a sub-domain of the bucket name.
+ */
public final static CallingFormat SUBDOMAIN = new SubdomainCallingFormat();
+
+ /**
+ * Call using a "vanity" or user-provided hostname.
+ * The bucket name is in fact the domain name.
+ */
public final static CallingFormat VANITY = new VanityCallingFormat();
public abstract boolean supportsLocatedBuckets();
Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/CanonicalString.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/CanonicalString.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/CanonicalString.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -1,5 +1,6 @@
package com.amazon.s3;
+import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
@@ -25,6 +26,7 @@
private static final String AMAZON_HEADER_PREFIX = "x-amz-";
private static final String ALTERNATIVE_DATE_HEADER = "x-amz-date";
+ private static final Charset UTF8 = Charset.forName("UTF-8");
/**
* HMAC/SHA1 Algorithm per RFC 2104.
@@ -159,15 +161,13 @@
new SecretKeySpec(awsSecretAccessKey.getBytes(), HMAC_SHA1_ALGORITHM);
return signingKey;
}
-
+
+
/**
* Calculate the HMAC/SHA1 on a string.
- * @param data Data to sign
- * @param passcode Passcode to sign it with
* @return Signature
*/
- static String encode(Key signingKey, String canonicalString,
- boolean urlencode)
+ static String encode(Key signingKey, String canonicalString)
{
Mac mac;
try {
@@ -181,15 +181,9 @@
throw new RuntimeException("Could not initialize the MAC algorithm", e);
}
- byte[] b = mac.doFinal(canonicalString.getBytes());
- byte[] encode = Base64.encodeBase64(b);
- String b64 = EncodingUtil.getAsciiString(encode);
-
- if (urlencode) {
- return UrlEncoder.encode(b64);
- } else {
- return b64;
- }
+ mac.update(UTF8.encode(canonicalString));
+ byte[] encode = Base64.encodeBase64(mac.doFinal());
+ return EncodingUtil.getAsciiString(encode);
}
}
Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/Connection.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/Connection.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/Connection.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -39,15 +39,34 @@
*/
public class Connection {
+ /**
+ * Location default.
+ */
public static final String LOCATION_DEFAULT = null;
+
+ /**
+ * Location in Europe.
+ */
public static final String LOCATION_EU = "EU";
+ /**
+ * Default hostname.
+ */
public static final String DEFAULT_HOST = "s3.amazonaws.com";
+
+ /**
+ * HTTP port.
+ */
public static final int INSECURE_PORT = 80;
+
+ /**
+ * HTTPS port.
+ */
public static final int SECURE_PORT = 443;
/**
* Data larger than 1024 bytes will use expect headers.
+ * See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html 14.20
*/
public static final int EXPECT_SIZE = 1024;
@@ -69,14 +88,23 @@
throw new Error("URI charset must be UTF-8: " + charset);
}
+ /**
+ * Constructs a new Connection.
+ */
public Connection(String awsAccessKeyId, String awsSecretAccessKey) {
this(awsAccessKeyId, awsSecretAccessKey, true);
}
+ /**
+ * Constructs a new Connection.
+ */
public Connection(String awsAccessKeyId, String awsSecretAccessKey, boolean isSecure) {
this(awsAccessKeyId, awsSecretAccessKey, isSecure, DEFAULT_HOST);
}
+ /**
+ * Constructs a new Connection.
+ */
public Connection(String awsAccessKeyId, String awsSecretAccessKey, boolean isSecure,
String server)
{
@@ -84,12 +112,18 @@
isSecure ? SECURE_PORT : INSECURE_PORT);
}
+ /**
+ * Constructs a new Connection.
+ */
public Connection(String awsAccessKeyId, String awsSecretAccessKey, boolean isSecure,
String server, int port) {
this(awsAccessKeyId, awsSecretAccessKey, isSecure, server, port, CallingFormat.SUBDOMAIN);
}
+ /**
+ * Constructs a new Connection.
+ */
public Connection(String awsAccessKeyId, String awsSecretAccessKey, boolean isSecure,
String server, CallingFormat format) {
this(awsAccessKeyId, awsSecretAccessKey, isSecure, server,
@@ -174,7 +208,7 @@
}
/**
- * Creates a new bucket.
+ * Creates a new bucket with a location.
*/
public Response create(Bucket bucket, String location) throws IOException {
return create(bucket, location, null);
@@ -182,7 +216,6 @@
/**
* Creates a new bucket.
- * @throws IOException
*/
public Response create(Bucket bucket) throws IOException {
return create(bucket, null);
@@ -569,8 +602,6 @@
/**
* Lists all the buckets created by this account.
- * @param headers A Map of String to List of Strings representing the http
- * headers to pass (can be null).
*/
public ListAllBucketsResponse listAllBuckets(Headers headers)
throws IOException
@@ -705,7 +736,7 @@
Headers prop = new Headers(httpMethod.getRequestHeaders());
String enckey = UrlEncoder.encode(key);
String canonicalString = CanonicalString.make(method, bucket, enckey, pathArgs, prop);
- String encodedCanonical = CanonicalString.encode(this.awsSecretAccessKey, canonicalString, false);
+ String encodedCanonical = CanonicalString.encode(this.awsSecretAccessKey, canonicalString);
httpMethod.setRequestHeader("Authorization",
"AWS " + this.awsAccessKeyId + ":" + encodedCanonical);
}
Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/Entry.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/Entry.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/Entry.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -48,6 +48,10 @@
return (Date) lastModified.clone();
}
+ /**
+ * Returns the key
+ */
+ @Override
public String toString() {
return getKey();
}
Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/Headers.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/Headers.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/Headers.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -32,7 +32,10 @@
*/
public Headers(Headers headers) {
this();
- this.headers.putAll(headers.headers);
+ for (Map.Entry<String, List<String>> me : headers.headers.entrySet()) {
+ for (String v : me.getValue())
+ put(me.getKey(), v);
+ }
}
/**
@@ -42,6 +45,9 @@
this(new HashMap<String, List<String>>());
}
+ /**
+ * Constructs a new Headers object.
+ */
public Headers(Header[] requestHeaders) {
this();
for (Header h : requestHeaders) {
Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/ListAllBucketsResponse.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/ListAllBucketsResponse.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/ListAllBucketsResponse.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -57,14 +57,6 @@
this.currText = new StringBuilder();
}
- public void startDocument() {
- // ignore
- }
-
- public void endDocument() {
- // ignore
- }
-
public void startElement(String uri, String name, String qName,
Attributes attrs) {
if (name.equals("Bucket")) {
Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/Method.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/Method.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/Method.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -14,7 +14,10 @@
public enum Method {
GET, PUT, DELETE, HEAD;
- public HttpMethod createHttpMethod() {
+ /**
+ * Returns a new HTTP method for processing.
+ */
+ HttpMethod createHttpMethod() {
switch (this) {
case PUT: return new PutMethod();
case GET: return new GetMethod();
Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/QueryGenerator.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/QueryGenerator.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/QueryGenerator.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -227,7 +227,7 @@
String enckey = UrlEncoder.encode(key);
String canonicalString = CanonicalString.make(method, bucket, enckey, pathArgs, headers, ""+expires);
- String encodedCanonical = CanonicalString.encode(this.awsSecretAccessKey, canonicalString, false);
+ String encodedCanonical = CanonicalString.encode(this.awsSecretAccessKey, canonicalString);
pathArgs.put("Signature", encodedCanonical);
pathArgs.put("Expires", Long.toString(expires));
Modified: amazon-s3/trunk/src/main/java/com/amazon/s3/Response.java
===================================================================
--- amazon-s3/trunk/src/main/java/com/amazon/s3/Response.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/main/java/com/amazon/s3/Response.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -29,7 +29,7 @@
public class Response {
private HttpMethod method;
-
+
Response(HttpMethod method) {
this.method = method;
}
@@ -61,7 +61,13 @@
public void assertOk() {
if (isOk())
return;
- throw new IllegalStateException("Unexpected response: " + this);
+ String msg;
+ try {
+ msg = method.getResponseBodyAsString();
+ } catch (IOException e) {
+ msg = "?";
+ }
+ throw new IllegalStateException("Unexpected response: " + this + " Message: " + msg);
}
/**
@@ -148,7 +154,7 @@
throw new RuntimeException("Unexpected error parsing ListBucket xml", e);
}
}
-
+
static XMLReader createXMLReader() {
try {
return XMLReaderFactory.createXMLReader();
Added: amazon-s3/trunk/src/test/java/com/amazon/s3/CallingFormatTest.java
===================================================================
--- amazon-s3/trunk/src/test/java/com/amazon/s3/CallingFormatTest.java (rev 0)
+++ amazon-s3/trunk/src/test/java/com/amazon/s3/CallingFormatTest.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -0,0 +1,26 @@
+package com.amazon.s3;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.httpclient.URI;
+import org.junit.Test;
+
+public class CallingFormatTest {
+
+ @Test
+ public void testCalling() throws Exception {
+ Bucket bucket = new Bucket("bucket");
+ String key = "/HI";
+ Map<String, String> args = new HashMap<String, String>();
+ args.put("one", "1");
+ URI uri;
+ uri = CallingFormat.PATH.getURI(true, "foo", 444, bucket, key, args);
+ assertEquals("https://foo:444/bucket/%2FHI?one=1", uri.toString());
+ uri = CallingFormat.SUBDOMAIN.getURI(true, "foo", 444, bucket, key, args);
+ assertEquals("https://bucket.foo:444/%2FHI?one=1", uri.toString());
+ }
+
+}
Added: amazon-s3/trunk/src/test/java/com/amazon/s3/CanonicalStringTest.java
===================================================================
--- amazon-s3/trunk/src/test/java/com/amazon/s3/CanonicalStringTest.java (rev 0)
+++ amazon-s3/trunk/src/test/java/com/amazon/s3/CanonicalStringTest.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -0,0 +1,40 @@
+package com.amazon.s3;
+
+import static org.junit.Assert.assertEquals;
+
+import java.security.Key;
+import java.util.Collections;
+import java.util.Map;
+
+import org.junit.Test;
+
+public class CanonicalStringTest {
+
+ @Test
+ public void testMacOlder() throws Exception {
+ String access = "213321324ksadjfkasjfdasfdjksadf";
+ String canon = "XXYasfajkjaslkfdjalksjflkasjflkajskfjasjflksadjflksajfdkljsadlkfjaslkfd";
+ Key key = CanonicalString.key(access);
+ String encode = CanonicalString.encode(key, canon);
+ assertEquals("hAu+ibd/CZIw6/5OR69i2+40bfc=", encode);
+ }
+
+ @Test
+ public void testMac2() throws Exception {
+ String access = "213321324ksadjfkasjfdasfdjksadf";
+ String canon = "XXYasfajkjaslkfdjalksjflkasjflkajskfjasjflksadjflksajfdkljsadlkfjaslkfd";
+ Key key = CanonicalString.key(access);
+ String encode = CanonicalString.encode(key, canon);
+ assertEquals("p5p5Y89Qhhaitcesa/l03whnQhw=", encode);
+ }
+
+ @Test
+ public void testMake() throws Exception {
+ Map<String, String> path = Collections.singletonMap("auth", "");
+ Headers h = new Headers();
+ h.put("x", "y");
+ String expires = "whenever";
+ String make = CanonicalString.make(Method.PUT, new Bucket("xyz"), "key", path, h, expires);
+ assertEquals("PUT\n\n\nwhenever\n/xyz/key", make);
+ }
+}
Modified: amazon-s3/trunk/src/test/java/com/amazon/s3/S3Test.java
===================================================================
--- amazon-s3/trunk/src/test/java/com/amazon/s3/S3Test.java 2008-02-07 22:57:14 UTC (rev 5331)
+++ amazon-s3/trunk/src/test/java/com/amazon/s3/S3Test.java 2008-02-08 09:32:51 UTC (rev 5332)
@@ -126,7 +126,7 @@
assertEquals("Unexpected common prefix size", 1, listBucketResponse.getCommonPrefixEntries().size());
verifyBucketResponseParameters(listBucketResponse, bucket, "", "", 2, "/", true, "test/");
String marker = listBucketResponse.getNextMarker();
- listBucketResponse = conn.list(bucket, null, marker, new Integer( 2 ), "/", null);
+ listBucketResponse = conn.list(bucket, null, marker, 2, "/", null);
listBucketResponse.assertOk();
assertEquals("Unexpected list size", 1, listBucketResponse.getEntries().size());
assertEquals("Unexpected common prefix size", 0, listBucketResponse.getCommonPrefixEntries().size());
More information about the jbosscache-commits
mailing list