package com.coding4people.mosquitoreport.api.buckets;

import java.io.InputStream;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Singleton;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

@Singleton
abstract public class Bucket {
    @Inject AmazonS3Client amazonS3Client;
    
    abstract protected String getBucketName();
    
    @PostConstruct
    protected void postConstruct() {
        createBucket();
        postCreateBucket();
    }
    
    public void put(String path, InputStream fileInputStream, Long contentLenght) {
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(contentLenght);
        
        amazonS3Client.putObject(new PutObjectRequest(getBucketName(), path, fileInputStream, meta));
    }
    
    protected void postCreateBucket() {
    }
    
    protected void createBucket() {
        //TODO create bucket on construct
    }

}

@@ -48,16 +48,119 @@
 ckets;%0A%0A
+import java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0A
 import j
@@ -328,16 +328,127 @@
 Client;%0A
+import com.amazonaws.services.s3.model.AccessControlList;%0Aimport com.amazonaws.services.s3.model.GroupGrantee;%0A
 import c
@@ -494,16 +494,67 @@
 tadata;%0A
+import com.amazonaws.services.s3.model.Permission;%0A
 import c
@@ -656,16 +656,20 @@
  @Inject
+%0A   
  AmazonS
@@ -692,20 +692,16 @@
 Client;%0A
-    
 %0A    abs
@@ -740,20 +740,16 @@
 Name();%0A
-    
 %0A    @Po
@@ -851,28 +851,24 @@
 et();%0A    %7D%0A
-    
 %0A    public 
@@ -867,20 +867,22 @@
  public 
-void
+Object
  put(Str
@@ -938,24 +938,289 @@
 ntLenght) %7B%0A
+        if (contentLenght == null %7C%7C contentLenght %3C= 0) %7B%0A            FindContentLengthResult result = findContentLength(fileInputStream);%0A            contentLenght = result.getContentLength();%0A            fileInputStream = result.getFileInputStream();%0A        %7D%0A%0A
         Obje
@@ -1305,24 +1305,25 @@
 enght);%0A
+%0A
         
 %0A       
@@ -1314,24 +1314,149 @@
         
-%0A       
+AccessControlList acl = new AccessControlList();%0A        acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);%0A%0A        return
  amazonS
@@ -1539,16 +1539,43 @@
 m, meta)
+.withAccessControlList(acl)
 );%0A    %7D
@@ -1571,28 +1571,24 @@
 cl));%0A    %7D%0A
-    
 %0A    protect
@@ -1622,20 +1622,16 @@
 %7B%0A    %7D%0A
-    
 %0A    pro
@@ -1669,16 +1669,17 @@
       //
+ 
 TODO cre
@@ -1708,11 +1708,1566 @@
 t%0A    %7D%0A
+    %0A    private FindContentLengthResult findContentLength(InputStream fileInputStream) %7B%0A        try %7B%0A            Long contentLenght = 0L;%0A            ByteArrayOutputStream buffer = new ByteArrayOutputStream();%0A            int nRead;%0A            byte%5B%5D data = new byte%5B16384%5D;%0A%0A            while ((nRead = fileInputStream.read(data, 0, data.length)) != -1) %7B%0A                contentLenght += nRead;%0A                buffer.write(data, 0, nRead);%0A            %7D%0A%0A            buffer.flush();%0A            %0A            FindContentLengthResult result = new FindContentLengthResult();%0A            result.setContentLength(contentLenght);%0A            result.setFileInputStream(new ByteArrayInputStream(buffer.toByteArray()));%0A            %0A            return result;%0A        %7D catch (IOException e) %7B%0A            // TODO handle and log%0A            e.printStackTrace();%0A%0A            throw new InternalError(%22Error uploading image%22);%0A        %7D%0A        %0A    %7D%0A    %0A    public static class FindContentLengthResult %7B%0A        private InputStream fileInputStream;%0A        private Long contentLength;%0A        %0A        public InputStream getFileInputStream() %7B%0A            return fileInputStream;%0A        %7D%0A        %0A        public void setFileInputStream(InputStream fileInputStream) %7B%0A            this.fileInputStream = fileInputStream;%0A        %7D%0A        %0A        public Long getContentLength() %7B%0A            return contentLength;%0A        %7D%0A        %0A        public void setContentLength(Long contentLenght) %7B%0A            this.contentLength = contentLenght;%0A        %7D%0A    %7D
 %0A%7D%0A

Improve bucket implementation