package de.jeha.s3srv.operations.buckets;

import com.codahale.metrics.annotation.Timed;
import de.jeha.s3srv.common.BucketNameValidator;
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.validation.ValidationException;
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);

        final String date = headers.getHeaderString("Date");
        final String authorization = headers.getHeaderString("Authorization");

        LOG.info("Date: {}", date);
        LOG.info("Authorization: {}", authorization);

        try {
            BucketNameValidator.isValid(bucket);
        } catch (ValidationException e) {
            LOG.info("Invalid bucket name: {}", e.getMessage());
            return createErrorResponse(ErrorCodes.INVALID_BUCKET_NAME, "/" + bucket, null);
        }

        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();
        }
    }

}

@@ -178,16 +178,182 @@
 rCodes;%0A
+import de.jeha.s3srv.common.security.AuthorizationContext;%0Aimport de.jeha.s3srv.common.security.AuthorizationUtils;%0Aimport de.jeha.s3srv.common.security.Credentials;%0A
 import d
@@ -537,22 +537,21 @@
 ervletRe
-sponse
+quest
 ;%0Aimport
@@ -705,45 +705,8 @@
 xt;%0A
-import javax.ws.rs.core.HttpHeaders;%0A
 impo
@@ -1121,95 +1121,30 @@
 Http
-Headers headers,%0A                                 @Context HttpServletResponse response
+ServletRequest request
 ,%0A  
@@ -1243,18 +1243,20 @@
 eBucket 
+'
 %7B%7D
+'
 %22, bucke
@@ -1251,33 +1251,32 @@
 '%7B%7D'%22, bucket);%0A
-%0A
         final St
@@ -1284,68 +1284,68 @@
 ing 
-date = headers.getHeaderString(%22Date%22);%0A        final String
+resource = %22/%22 + bucket + %22/%22;%0A%0A        AuthorizationContext
  aut
@@ -1358,143 +1358,371 @@
 tion
+Context
  = 
+c
 he
-aders.getHeaderString(%22Authorization%22);%0A%0A        LOG.info(%22Date: %7B%7D%22, date);%0A        LOG.info(%22Authorization: %7B%7D%22, authorization);
+ckAuthorization(request, resource);%0A        if (!authorizationContext.isAccessKeyValid()) %7B%0A            return createErrorResponse(ErrorCodes.INVALID_ACCESS_KEY_ID, resource, null);%0A        %7D%0A        if (!authorizationContext.isSignatureValid()) %7B%0A            return createErrorResponse(ErrorCodes.SIGNATURE_DOES_NOT_MATCH, resource, null);%0A        %7D
 %0A%0A  
@@ -1960,28 +1960,24 @@
 T_NAME, 
-%22/%22 + bucket
+resource
 , null);
@@ -2117,28 +2117,24 @@
 EXISTS, 
-%22/%22 + bucket
+resource
 , null);
@@ -2442,10 +2442,891 @@
 %0A    %7D%0A%0A
+    private AuthorizationContext checkAuthorization(HttpServletRequest request, String resource) %7B%0A        final String date = request.getHeader(%22Date%22);%0A        final String authorization = request.getHeader(%22Authorization%22);%0A        final String contentMD5 = request.getHeader(%22Content-MD5%22);%0A        final String contentType = request.getHeader(%22Content-Type%22);%0A%0A        LOG.info(%22Date: %7B%7D%22, date);%0A        LOG.info(%22Authorization: %7B%7D%22, authorization);%0A%0A        Credentials credentials = new Credentials(%22foo%22, %22bar%22); // TODO%0A%0A        boolean valid = AuthorizationUtils.checkAuthorization(%0A                authorization,%0A                credentials,%0A                request.getMethod(),%0A                contentMD5,%0A                contentType,%0A                date,%0A                resource);%0A%0A        return new AuthorizationContext(credentials.getAccessKey(), valid);%0A    %7D%0A%0A
 %7D%0A

check access key and signature on bucket creation