package de.jeha.s3srv.operations.buckets;

import com.codahale.metrics.annotation.Timed;
import de.jeha.s3srv.common.errors.ErrorCodes;
import de.jeha.s3srv.operations.AbstractOperation;
import de.jeha.s3srv.storage.StorageBackend;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

/**
 * @author jenshadlich@googlemail.com
 */
@Path("/")
public class CreateBucket extends AbstractOperation {

    private static final Logger LOG = LoggerFactory.getLogger(CreateBucket.class);

    public CreateBucket(StorageBackend storageBackend) {
        super(storageBackend);
    }

    @PUT
    @Path("/{bucket}/")
    @Timed
    public Response createBucket(@Context HttpHeaders headers,
                                 @Context HttpServletResponse response,
                                 @PathParam("bucket") String bucket) {
        LOG.info("createBucket {}", bucket);

        if (getStorageBackend().existsBucket(bucket)) {
            return createErrorResponse(ErrorCodes.BUCKET_ALREADY_EXISTS, bucket, null);
        } else {
            getStorageBackend().createBucket(bucket);

            return Response.ok()
                    .header("Location", "/" + bucket)
                    .header("Content-Length", "0")
                    .header("Connection", "close")
                    .build();
        }
    }

}

@@ -82,16 +82,65 @@
 .Timed;%0A
+import de.jeha.s3srv.common.BucketNameValidator;%0A
 import d
@@ -379,16 +379,61 @@
 sponse;%0A
+import javax.validation.ValidationException;%0A
 import j
@@ -1187,32 +1187,299 @@
  %7B%7D%22, bucket);%0A%0A
+        try %7B%0A            BucketNameValidator.isValid(bucket);%0A        %7D catch (ValidationException e) %7B%0A            LOG.info(%22Invalid bucket name: %7B%7D%22, e.getMessage());%0A            return createErrorResponse(ErrorCodes.INVALID_BUCKET_NAME, bucket, null);%0A        %7D%0A%0A
         if (getS

validate bucket name on creation