package de.jeha.s3srv.operations.buckets;

import com.codahale.metrics.annotation.Timed;
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.HEAD;
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 ExistsBucket extends AbstractOperation {

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

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

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

        if (!getStorageBackend().existsBucket(bucket)) {
            return Response.status(Response.Status.NOT_FOUND)
                    .build();
        }

        return Response.ok()
                .build();
    }

}

@@ -82,16 +82,122 @@
 .Timed;%0A
+import de.jeha.s3srv.common.errors.ErrorCodes;%0Aimport de.jeha.s3srv.common.security.AuthorizationContext;%0A
 import d
@@ -381,22 +381,21 @@
 ervletRe
-sponse
+quest
 ;%0Aimport
@@ -505,45 +505,8 @@
 xt;%0A
-import javax.ws.rs.core.HttpHeaders;%0A
 impo
@@ -922,23 +922,30 @@
 Http
-Headers headers
+ServletRequest request
 ,%0A  
@@ -980,69 +980,385 @@
    @
-Context HttpServletResponse response,%0A               
+PathParam(%22bucket%22) String bucket) %7B%0A        LOG.info(%22existsBucket '%7B%7D'%22, bucket);%0A        final String resource = %22/%22 + bucket + %22/%22;%0A%0A        AuthorizationContext authorizationContext = checkAuthorization(request, resource);%0A        if (!authorizationContext.isUserValid()) %7B%0A            return createErrorResponse(ErrorCodes.INVALID_ACCESS_KEY_ID, resource, null);%0A
         
     
@@ -1349,32 +1349,34 @@
 l);%0A        
+%7D%0A
         
   @PathParam
@@ -1367,95 +1367,317 @@
     
-  @PathParam(%22bucket%22) String bucket) %7B%0A        LOG.info(%22existsBucket '%7B%7D'%22, bucket);%0A
+if (!authorizationContext.isSignatureValid()) %7B%0A            return createErrorResponse(ErrorCodes.SIGNATURE_DOES_NOT_MATCH, resource, null);%0A        %7D%0A        if (!getStorageBackend().existsBucket(bucket)) %7B%0A            return Response.status(Response.Status.NOT_FOUND)%0A                    .build();%0A        %7D
 %0A   
@@ -1698,38 +1698,35 @@
 torageBackend().
-exists
+get
 Bucket(bucket)) 
@@ -1715,32 +1715,74 @@
 etBucket(bucket)
+.isOwnedBy(authorizationContext.getUser())
 ) %7B%0A            
@@ -1816,32 +1816,35 @@
 onse.Status.
-NOT_FOUN
+UNAUTHORIZE
 D)%0A         

check authorization when checking bucket existence