package de.jeha.s3srv.operations.objects;

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

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
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 GetObject extends AbstractOperation {

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

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

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

        if (getStorageBackend().existsObject(bucket, key)) {
            S3Object object = getStorageBackend().getObject(bucket, key);
            return Response.ok(object.getInputStream()).type(object.getContentType()).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    }

}

@@ -1283,16 +1283,17 @@
 , key);%0A
+%0A
         
@@ -1339,16 +1339,37 @@
 tream())
+%0A                    
 .type(ob
@@ -1390,16 +1390,173 @@
 tType())
+%0A                    .header(%22ETag%22, object.getETag())%0A                    .header(%22Content-Length%22, Integer.toString(object.getSize()))%0A                    
 .build()

set headers