feat: implement S3 static website hosting with index document redirection (#507)

Co-authored-by: Shivam Srivastav <shivamsrivastav@192.168.1.6>
This commit is contained in:
iam-ssrivastavandShivam Srivastav authored and GitHub committed 2026-04-18 01:34:38 -05:00
1 parent f4ce6ef445
commit b013588037
5 files changed
+253

No files matched your search

@@ -19,6 +19,7 @@ import io.github.hectorvent.floci.services.s3.model.Part;
import io.github.hectorvent.floci.services.s3.model.S3Checksum;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import io.github.hectorvent.floci.services.s3.model.TopicNotification;
import io.github.hectorvent.floci.services.s3.model.WebsiteConfiguration;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Context;
@@ -143,6 +144,9 @@ public class S3Controller {
if (hasQueryParam(uriInfo, "object-lock")) {
return handlePutObjectLockConfiguration(bucket, body);
}
if (hasQueryParam(uriInfo, "website")) {
return handlePutBucketWebsite(bucket, body);
}
if (hasQueryParam(uriInfo, "policy")) {
s3Service.putBucketPolicy(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
@@ -210,6 +214,10 @@ public class S3Controller {
s3Service.deleteBucketTagging(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "website")) {
s3Service.deleteBucketWebsite(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "policy")) {
s3Service.deleteBucketPolicy(bucket);
return Response.noContent().build();
@@ -276,6 +284,9 @@ public class S3Controller {
if (hasQueryParam(uriInfo, "object-lock")) {
return handleGetObjectLockConfiguration(bucket);
}
if (hasQueryParam(uriInfo, "website")) {
return handleGetBucketWebsite(bucket);
}
if (hasQueryParam(uriInfo, "policy")) {
return Response.ok(s3Service.getBucketPolicy(bucket)).build();
}
@@ -298,6 +309,28 @@ public class S3Controller {
return Response.ok(s3Service.getBucketOwnershipControls(bucket)).build();
}
// --- Website Hosting Redirection Logic ---
if (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type"))) {
try {
WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);
if (webConfig.getIndexDocument() != null) {
try {
S3Object indexObj = s3Service.getObject(bucket, webConfig.getIndexDocument());
return Response.ok(indexObj.getData())
.type(indexObj.getContentType())
.header("Content-Length", indexObj.getSize())
.header("ETag", indexObj.getETag())
.header("x-amz-website-redirect-location", "index")
.build();
} catch (AwsException e) {
// If index.html is missing, we could serve ErrorDocument, but for now we fall back to listObjects
}
}
} catch (AwsException e) {
// Bucket is not a website, continue to listObjects
}
}
int max = (maxKeys != null && maxKeys > 0) ? maxKeys : 1000;
S3Service.ListObjectsResult result = s3Service.listObjectsWithPrefixes(
bucket, prefix, delimiter, max, continuationToken, startAfter);
@@ -1957,4 +1990,32 @@ public class S3Controller {
String rawKey = rawPath.substring(prefixIndex + bucketPrefix.length());
return URLDecoder.decode(rawKey, StandardCharsets.UTF_8);
}
private Response handleGetBucketWebsite(String bucket) {
WebsiteConfiguration config = s3Service.getBucketWebsite(bucket);
XmlBuilder xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("WebsiteConfiguration", AwsNamespaces.S3)
.start("IndexDocument")
.elem("Suffix", config.getIndexDocument())
.end("IndexDocument");
if (config.getErrorDocument() != null) {
xml.start("ErrorDocument")
.elem("Key", config.getErrorDocument())
.end("ErrorDocument");
}
xml.end("WebsiteConfiguration");
return Response.ok(xml.build()).build();
}
private Response handlePutBucketWebsite(String bucket, byte[] body) {
String xml = new String(body, StandardCharsets.UTF_8);
String indexDoc = XmlParser.extractFirst(xml, "Suffix", null);
String errorDoc = XmlParser.extractFirst(xml, "Key", null);
if (indexDoc == null) {
throw new AwsException("MalformedXML", "IndexDocument.Suffix is required.", 400);
}
s3Service.putBucketWebsite(bucket, new WebsiteConfiguration(indexDoc, errorDoc));
return Response.ok().build();
}
}
@@ -809,6 +809,34 @@ public class S3Service {
return bucket.getTags() != null ? bucket.getTags() : Map.of();
}
public void putBucketWebsite(String bucketName, WebsiteConfiguration config) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
bucket.setWebsiteConfiguration(config);
bucketStore.put(bucketName, bucket);
LOG.infov("Set website configuration for bucket: {0}", bucketName);
}
public WebsiteConfiguration getBucketWebsite(String bucketName) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
if (bucket.getWebsiteConfiguration() == null) {
throw new AwsException("NoSuchWebsiteConfiguration", "The specified bucket does not have a website configuration.", 404);
}
return bucket.getWebsiteConfiguration();
}
public void deleteBucketWebsite(String bucketName) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
bucket.setWebsiteConfiguration(null);
bucketStore.put(bucketName, bucket);
LOG.infov("Deleted website configuration for bucket: {0}", bucketName);
}
public void deleteBucketTagging(String bucketName) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
@@ -26,6 +26,7 @@ public class Bucket {
private String publicAccessBlockConfiguration; // XML string
private String ownershipControlsConfiguration; // XML string
private String region;
private WebsiteConfiguration websiteConfiguration;
public Bucket() {
this.tags = new HashMap<>();
@@ -90,4 +91,7 @@ public class Bucket {
public String getRegion() { return region; }
public void setRegion(String region) { this.region = region; }
public WebsiteConfiguration getWebsiteConfiguration() { return websiteConfiguration; }
public void setWebsiteConfiguration(WebsiteConfiguration websiteConfiguration) { this.websiteConfiguration = websiteConfiguration; }
}
@@ -0,0 +1,22 @@
package io.github.hectorvent.floci.services.s3.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class WebsiteConfiguration {
private String indexDocument;
private String errorDocument;
public WebsiteConfiguration() {}
public WebsiteConfiguration(String indexDocument, String errorDocument) {
this.indexDocument = indexDocument;
this.errorDocument = errorDocument;
}
public String getIndexDocument() { return indexDocument; }
public void setIndexDocument(String indexDocument) { this.indexDocument = indexDocument; }
public String getErrorDocument() { return errorDocument; }
public void setErrorDocument(String errorDocument) { this.errorDocument = errorDocument; }
}