}
}
public void testS3WebsiteConfig() throws Exception {
// Testing takes place in the us-west-1 location
S3Service s3Service = (S3Service) getStorageService(getCredentials());
StorageBucket bucket = createBucketForTest(
"testS3WebsiteConfig",
// Standard US Bucket location
S3Bucket.LOCATION_US_WEST);
String bucketName = bucket.getName();
String s3WebsiteURL = "http://" + bucketName + "."
// Website location must correspond to bucket location, in this case
// the US Standard. For website endpoints see:
// docs.amazonwebservices.com/AmazonS3/latest/dev/WebsiteEndpoints.html
+ "s3-website-us-west-1"
+ ".amazonaws.com";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet getMethod;
// Check no existing website config
try {
s3Service.getWebsiteConfig(bucketName);
fail("Unexpected website config for bucket " + bucketName);
} catch (S3ServiceException e) {
assertEquals(404, e.getResponseCode());
}
// Set index document
s3Service.setWebsiteConfig(bucketName,
new S3WebsiteConfig("index.html"));
Thread.sleep(5000);
// Confirm index document set
S3WebsiteConfig config = s3Service.getWebsiteConfig(bucketName);
assertTrue(config.isWebsiteConfigActive());
assertEquals("index.html", config.getIndexDocumentSuffix());
assertNull(config.getErrorDocumentKey());
// Upload public index document
S3Object indexObject = new S3Object("index.html", "index.html contents");
indexObject.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
s3Service.putObject(bucketName, indexObject);
// Confirm index document is served at explicit path
getMethod = new HttpGet(s3WebsiteURL + "/index.html");
HttpResponse response = httpClient.execute(getMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals("index.html contents", EntityUtils.toString(response.getEntity()));
// Confirm index document is served at root path
// (i.e. website config is effective)
getMethod = new HttpGet(s3WebsiteURL + "/");
response = httpClient.execute(getMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals("index.html contents", EntityUtils.toString(response.getEntity()));
// Set index document and error document
s3Service.setWebsiteConfig(bucketName,
new S3WebsiteConfig("index.html", "error.html"));
Thread.sleep(10000); // Config updates can take a long time... ugh!
// Confirm index document and error document set
config = s3Service.getWebsiteConfig(bucketName);
assertTrue(config.isWebsiteConfigActive());
assertEquals("index.html", config.getIndexDocumentSuffix());
assertEquals("error.html", config.getErrorDocumentKey());
// Upload public error document
S3Object errorObject = new S3Object("error.html", "error.html contents");
errorObject.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
s3Service.putObject(bucketName, errorObject);
// Confirm error document served at explicit path
getMethod = new HttpGet(s3WebsiteURL + "/error.html");
response = httpClient.execute(getMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals("error.html contents", EntityUtils.toString(response.getEntity()));
// Confirm error document served instead of 404 Not Found
getMethod = new HttpGet(s3WebsiteURL + "/does-not-exist");
response = httpClient.execute(getMethod);
assertEquals(403, response.getStatusLine().getStatusCode()); // TODO: Why a 403?
assertEquals("error.html contents", EntityUtils.toString(response.getEntity()));
// Upload private document
S3Object privateObject = new S3Object("private.html", "private.html contents");
s3Service.putObject(bucketName, privateObject);
// Confirm error document served instead for 403 Forbidden
getMethod = new HttpGet(s3WebsiteURL + "/private.html");
response = httpClient.execute(getMethod);
assertEquals(403, response.getStatusLine().getStatusCode());
// Delete website config
s3Service.deleteWebsiteConfig(bucketName);
Thread.sleep(5000);
// Confirm website config deleted
try {
s3Service.getWebsiteConfig(bucketName);
fail("Unexpected website config for bucket " + bucketName);
} catch (S3ServiceException e) { }
} finally {
cleanupBucketForTest("testS3WebsiteConfig");
}