Automated merge with https://gss.googlecode.com/hg/
[pithos] / src / gr / ebs / gss / server / webdav / milton / GssFolderResource.java
index 71e37e8..7e0e76e 100644 (file)
  */
 package gr.ebs.gss.server.webdav.milton;
 
-import gr.ebs.gss.client.exceptions.DuplicateNameException;
-import gr.ebs.gss.client.exceptions.GSSIOException;
-import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
-import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
-import gr.ebs.gss.client.exceptions.RpcException;
-import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
-import gr.ebs.gss.server.domain.dto.FolderDTO;
+import gr.ebs.gss.common.exceptions.DuplicateNameException;
+import gr.ebs.gss.common.exceptions.GSSIOException;
+import gr.ebs.gss.common.exceptions.InsufficientPermissionsException;
+import gr.ebs.gss.common.exceptions.ObjectNotFoundException;
+import gr.ebs.gss.common.exceptions.QuotaExceededException;
+import gr.ebs.gss.common.exceptions.RpcException;
+import gr.ebs.gss.server.domain.FileHeader;
+import gr.ebs.gss.server.domain.Folder;
+import gr.ebs.gss.server.domain.User;
 import gr.ebs.gss.server.ejb.TransactionHelper;
 
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -54,10 +57,12 @@ import com.bradmcevoy.http.MakeCollectionableResource;
 import com.bradmcevoy.http.MoveableResource;
 import com.bradmcevoy.http.PropFindableResource;
 import com.bradmcevoy.http.PutableResource;
+import com.bradmcevoy.http.QuotaResource;
 import com.bradmcevoy.http.Range;
 import com.bradmcevoy.http.Request;
 import com.bradmcevoy.http.Resource;
 import com.bradmcevoy.http.XmlWriter;
+import com.bradmcevoy.http.Request.Method;
 import com.bradmcevoy.http.exceptions.BadRequestException;
 import com.bradmcevoy.http.exceptions.ConflictException;
 import com.bradmcevoy.http.exceptions.NotAuthorizedException;
@@ -67,18 +72,19 @@ import com.bradmcevoy.http.exceptions.NotAuthorizedException;
  * @author kman
  *
  */
-public class GssFolderResource extends GssResource implements MakeCollectionableResource, PutableResource, CopyableResource, DeletableResource, MoveableResource, PropFindableResource, LockingCollectionResource, GetableResource{
+public class GssFolderResource extends GssResource implements MakeCollectionableResource, PutableResource, CopyableResource, DeletableResource, MoveableResource, PropFindableResource, LockingCollectionResource, GetableResource, QuotaResource{
         private static final Logger log = LoggerFactory.getLogger(GssFolderResource.class);
-       FolderDTO folder;
+       Folder folder;
        
        /**
         * @param host
         * @param factory
         * @param resource
         */
-       public GssFolderResource(String host, GSSResourceFactory factory, Object resource) {
+       public GssFolderResource(String host, GSSResourceFactory factory, Object resource, User currentUser) {
                super(host, factory, resource);
-               folder=(FolderDTO) resource;
+               folder=(Folder) resource;
+               this.currentUser=currentUser;
        }
        @Override
        public String checkRedirect(Request request) {
@@ -90,7 +96,9 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
        }
        @Override
        public Date getModifiedDate() {
-               return folder.getAuditInfo().getModificationDate();
+               if(folder!=null && folder.getAuditInfo()!=null)
+                       return folder.getAuditInfo().getModificationDate();
+               return null;
        }
        @Override
        public String getName() {
@@ -98,114 +106,81 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
        }
        @Override
        public String getUniqueId() {
-               return folder.getId().toString();
+               return "folder:"+folder.getId().toString();
        }
        @Override
        public void moveTo(final CollectionResource newParent, final String arg1) throws ConflictException, NotAuthorizedException, BadRequestException {
                if( newParent instanceof GssFolderResource ) {
-                       log.info("MOVING:"+arg1);
                        final GssFolderResource newFsParent = (GssFolderResource) newParent;
-                       log.info("NEW PARENT IS:"+newFsParent.folder.getName());
-            /*File dest = new File(newFsParent.getFile(), newName);
-            boolean ok = this.file.renameTo(dest);
-            if( !ok ) throw new RuntimeException("Failed to move to: " + dest.getAbsolutePath());
-            this.file = dest;*/
                        try {
                                if(newFsParent.folder.getName().equals(folder.getParent().getName())){
-                                       new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
-
-                                               @Override
-                                               public Void call() throws Exception {
-                                                       factory.getService().updateFolder(getCurrentUser().getId(), folder.getId(), arg1, null, null);
-                                                       log.info("RENAMING OK:"+arg1);
+                                       if(!folder.getName().equals(arg1))
+                                               new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+       
+                                                       @Override
+                                                       public Void call() throws Exception {
+                                                               getService().updateFolder(getCurrentUser().getId(), folder.getId(), arg1, null, null);
+                                                               return null;
+                                                       }
                                                        
-                                                       return null;
-                                               }
-                                               
-                                       });
+                                               });
                                }
-                               //this.folder = factory.getService().updateFolder(user.getId(), folder.getId(), arg1, null, null);
                                else new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
 
                                        @Override
                                        public Void call() throws Exception {
-                                               factory.getService().moveFolder(getCurrentUser().getId(), folder.getId(), newFsParent.folder.getId(), arg1);
-                                               log.info("MOVING OK:"+arg1);
-                                               
+                                               getService().moveFolder(getCurrentUser().getId(), folder.getId(), newFsParent.folder.getId(), arg1);                                            
                                                return null;
                                        }
                                        
                                });
-                               GssFolderResource.this.folder = factory.getService().getFolder(getCurrentUser().getId(), folder.getId());
-                               log.info("MOVING:"+folder.getName());
+                               GssFolderResource.this.folder = getService().getFolder(getCurrentUser().getId(), folder.getId());
                                
                        } catch (InsufficientPermissionsException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new NotAuthorizedException(this);
                        } catch (ObjectNotFoundException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new BadRequestException(this);
                        } catch (DuplicateNameException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new ConflictException(this);
                        } catch (RpcException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new RuntimeException("System error");
                        } catch (GSSIOException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new RuntimeException("Unable to Move");
                        } catch (Exception e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new RuntimeException("Unable to Move");
                        }
         } else {
-            throw new RuntimeException("Destination is an unknown type. Must be a FsDirectoryResource, is a: " + newParent.getClass());
+            throw new RuntimeException("Destination is an unknown type. Must be a Folder, is a: " + newParent.getClass());
         }
                
        }
        @Override
        public void copyTo(final CollectionResource newParent, final String arg1) throws NotAuthorizedException, BadRequestException, ConflictException {
-               if( newParent instanceof GssFolderResource ) {
-                       log.info("COPYING:"+arg1);
+               if( newParent instanceof GssFolderResource ) {                  
                        final GssFolderResource newFsParent = (GssFolderResource) newParent;
-                       log.info("NEW PARENT IS:"+newFsParent.folder.getName());
-            /*File dest = new File(newFsParent.getFile(), newName);
-            boolean ok = this.file.renameTo(dest);
-            if( !ok ) throw new RuntimeException("Failed to move to: " + dest.getAbsolutePath());
-            this.file = dest;*/
                        try {
                                 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
 
                                        @Override
                                        public Void call() throws Exception {
-                                               factory.getService().copyFolder(getCurrentUser().getId(), folder.getId(), newFsParent.folder.getId(), arg1);
-                                               log.info("COPYING OK:"+arg1);
-                                               
+                                               getService().copyFolder(getCurrentUser().getId(), folder.getId(), newFsParent.folder.getId(), arg1);
                                                return null;
                                        }
                                        
                                });
-                               GssFolderResource.this.folder = factory.getService().getFolder(getCurrentUser().getId(), folder.getId());
-                               log.info("MOVING:"+folder.getName());
-                               
+                               GssFolderResource.this.folder = getService().getFolder(getCurrentUser().getId(), folder.getId());
                        } catch (InsufficientPermissionsException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new NotAuthorizedException(this);
                        } catch (ObjectNotFoundException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new BadRequestException(this);
                        } catch (DuplicateNameException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new ConflictException(this);
                        } catch (RpcException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new RuntimeException("System error");
                        } catch (GSSIOException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new RuntimeException("Unable to Move");
                        } catch (Exception e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new RuntimeException("Unable to Move");
                        }
         } else {
             throw new RuntimeException("Destination is an unknown type. Must be a FsDirectoryResource, is a: " + newParent.getClass());
@@ -214,89 +189,78 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
        }
        @Override
        public CollectionResource createCollection(final String name) throws NotAuthorizedException, ConflictException, BadRequestException {
+               ////log.info("CALLING CREATECOLLECTION:"+name);
                try {
-                       final FolderDTO folderParent = folder;
-                       FolderDTO created = new TransactionHelper<FolderDTO>().tryExecute(new Callable<FolderDTO>() {
+                       final Folder folderParent = folder;
+                       Folder created = new TransactionHelper<Folder>().tryExecute(new Callable<Folder>() {
                                @Override
-                               public FolderDTO call() throws Exception {
-                                       FolderDTO f = factory.getService().createFolder(getCurrentUser().getId(), folder.getId(), name);
+                               public Folder call() throws Exception {
+                                       Folder f = getService().createFolder(getCurrentUser().getId(), folder.getId(), name);
                                        return f;
                                }
                        });
-                       return new GssFolderResource(host, factory, created);
+                       return new GssFolderResource(host, factory, created, getCurrentUser());
                } catch (DuplicateNameException e) {
-                       e.printStackTrace();
                        // XXX If the existing name is a folder we should be returning
                        // SC_METHOD_NOT_ALLOWED, or even better, just do the createFolder
                        // without checking first and then deal with the exceptions.
                        throw new ConflictException(this);
                } catch (InsufficientPermissionsException e) {
-                       e.printStackTrace();
                        throw new NotAuthorizedException(this);
                } catch (ObjectNotFoundException e) {
-                       e.printStackTrace();
                        return null;
                } catch (RpcException e) {
-                       e.printStackTrace();
-                       return null;
+                       throw new RuntimeException("System Error");
                } catch (Exception e) {
-                       e.printStackTrace();
-                       return null;
+                       throw new RuntimeException("System Error");
+                       
                }
        }
        @Override
        public Resource child(String name) {
-               for(FolderDTO f : folder.getSubfolders())
+               for(Folder f : folder.getSubfolders())
                        if(f.getName().equals(name))
-                               return new GssFolderResource(host, factory, f);
+                               return new GssFolderResource(host, factory, f, getCurrentUser());
                
                        try {
-                               for(FileHeaderDTO f : factory.getService().getFiles(folder.getOwner().getId(), folder.getId(), true))
+                               for(FileHeader f : getService().getFiles(folder.getOwner().getId(), folder.getId(), true))
                                        if(f.getName().equals(name))
-                                               return new GssFileResource(host, factory, f);
+                                               return new GssFileResource(host, factory, f,getCurrentUser());
                        } catch (ObjectNotFoundException e) {
                                // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               return null;
                        } catch (InsufficientPermissionsException e) {
                                // TODO Auto-generated catch block
-                               e.printStackTrace();
                        } catch (RpcException e) {
                                // TODO Auto-generated catch block
-                               e.printStackTrace();
                        }
-               
+           ////log.info("CALLING CHILD return null");
                return null;
        }
        @Override
        public List<? extends Resource> getChildren() {
                try {
-                       this.folder = factory.getService().getFolder(getCurrentUser().getId(), folder.getId());
+                       this.folder = getService().getFolder(getCurrentUser().getId(), folder.getId());
                } catch (ObjectNotFoundException e) {
                        // TODO Auto-generated catch block
-                       e.printStackTrace();
                } catch (InsufficientPermissionsException e) {
                        // TODO Auto-generated catch block
-                       e.printStackTrace();
                } catch (RpcException e) {
                        // TODO Auto-generated catch block
-                       e.printStackTrace();
                }
                List<GssResource> result = new ArrayList<GssResource>();
-               for(FolderDTO f : folder.getSubfolders())
+               for(Folder f : folder.getSubfolders())
                        if(!f.isDeleted())
-                               result.add(new GssFolderResource(host, factory, f));
+                               result.add(new GssFolderResource(host, factory, f, getCurrentUser()));
                try {
-                       for(FileHeaderDTO f : factory.getService().getFiles(getCurrentUser().getId(), folder.getId(), true))
-                               result.add(new GssFileResource(host, factory, f));
+                       for(FileHeader f : getService().getFiles(getCurrentUser().getId(), folder.getId(), true))
+                               result.add(new GssFileResource(host, factory, f,getCurrentUser()));
                } catch (ObjectNotFoundException e) {
                        // TODO Auto-generated catch block
-                       e.printStackTrace();
                } catch (InsufficientPermissionsException e) {
                        // TODO Auto-generated catch block
-                       e.printStackTrace();
                } catch (RpcException e) {
                        // TODO Auto-generated catch block
-                       e.printStackTrace();
                }
                return result;
        }
@@ -305,15 +269,13 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
                
        File uploadedFile = null;
        try {
-                       uploadedFile = factory.getService().uploadFile(in, getCurrentUser().getId());
+                       uploadedFile = getService().uploadFile(in, getCurrentUser().getId());
                } catch (IOException ex) {
                        throw new IOException(ex);
                } catch (ObjectNotFoundException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
+                       throw new BadRequestException(this);
                } catch (RpcException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
+                       throw new RuntimeException("Unable to upload file");                    
                }
                final File uf = uploadedFile;
                try {
@@ -321,54 +283,71 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
                        if(!pathFolder.endsWith("/"))
                                pathFolder=pathFolder+"/";
                        String fname = pathFolder+name;
+                       ////log.info("fname:"+fname+" "+URLDecoder.decode(fname));
                        Object ff2;
                        try{
-                               ff2 = factory.getService().getResourceAtPath(folder.getOwner().getId(), fname, true);
+                               ff2 = getService().getResourceAtPath(folder.getOwner().getId(), URLDecoder.decode(fname), true);
                        }
                        catch(ObjectNotFoundException ex){
                                ff2=null;
                        }
                        final Object ff = ff2;
-                       log.info("**************FOUND FILE:"+ff);
-                       FileHeaderDTO kmfileDTO=null;
-                       if(ff!=null && ff instanceof FileHeaderDTO){
-                               kmfileDTO = new TransactionHelper<FileHeaderDTO>().tryExecute(new Callable<FileHeaderDTO>() {
+                       FileHeader kmfile = null;
+                       if(ff != null && ff instanceof FileHeader){
+                               kmfile = new TransactionHelper<FileHeader>().tryExecute(new Callable<FileHeader>() {
                                        @Override
-                                       public FileHeaderDTO call() throws Exception {
-                                               log.info("**************UPDATING:"+ff);
-                                               return factory.getService().updateFileContents(getCurrentUser().getId(), ((FileHeaderDTO)ff).getId(),  contentType, uf.length(), uf.getAbsolutePath());
+                                       public FileHeader call()  throws Exception{
+                                               return getService().updateFileContents(getCurrentUser().getId(), ((FileHeader)ff).getId(),  contentType, uf.length(), uf.getAbsolutePath());
                                        }
                                });
                        }
                        else
-                               kmfileDTO = new TransactionHelper<FileHeaderDTO>().tryExecute(new Callable<FileHeaderDTO>() {
+                               kmfile = new TransactionHelper<FileHeader>().tryExecute(new Callable<FileHeader>() {
                                        @Override
-                                       public FileHeaderDTO call() throws Exception {
-                                               log.info("**************CREATING:"+ff);
-                                               return factory.getService().createFile(getCurrentUser().getId(), folder.getId(), name, contentType, uf.length(), uf.getAbsolutePath());
+                                       public FileHeader call() throws Exception{
+                                               return getService().createFile(getCurrentUser().getId(), folder.getId(), name, contentType, uf.length(), uf.getAbsolutePath());
                                        }
                                });
-                       return new GssFileResource(host, factory, kmfileDTO);
-               } catch (Exception e) {
-                       e.printStackTrace();
+                       return new GssFileResource(host, factory, kmfile, getCurrentUser());
+               } catch (ObjectNotFoundException e) {
+                       throw new BadRequestException(this);
+               } catch (InsufficientPermissionsException e) {
+                       throw new NotAuthorizedException(this);
+               }
+               catch (DuplicateNameException e) {
+                       // TODO Auto-generated catch block
+                       throw new ConflictException(this);
+               }
+               catch(QuotaExceededException e){
+                       throw new ConflictException(this);
+               }
+               catch(Exception e){
+                       throw new RuntimeException("System Error");
                }
-               return null;
        }
        @Override
        public void delete() throws NotAuthorizedException, ConflictException, BadRequestException {
                try {
-                       factory.getService().deleteFolder(getCurrentUser().getId(), folder.getId());
+                       
+                               new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
+
+                                       @Override
+                                       public Void call() throws Exception {
+                                               getService().deleteFolder(getCurrentUser().getId(), folder.getId());
+                                               return  null;
+                                       }
+                               });
+                        
                } catch (InsufficientPermissionsException e) {
-                       e.printStackTrace();
                        throw new NotAuthorizedException(this);
                } catch (ObjectNotFoundException e) {
-                       e.printStackTrace();
                        throw new BadRequestException(this);
                } catch (RpcException e) {
-                       e.printStackTrace();
                        throw new BadRequestException(this);
                }
-               
+               catch (Exception e) {
+                       throw new BadRequestException(this);
+               }
        }
        @Override
        public Date getCreateDate() {
@@ -378,24 +357,20 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
        }
        @Override
        public LockToken createAndLock(final String name, LockTimeout timeout, LockInfo lockInfo ) throws NotAuthorizedException {
-               final File tmp =  new File("/tmp/"+new java.util.Random().nextInt());
-               FileHeaderDTO kmfileDTO=null;
+               FileHeader kmfile=null;
                try {
-                       kmfileDTO = new TransactionHelper<FileHeaderDTO>().tryExecute(new Callable<FileHeaderDTO>() {
+                       kmfile = new TransactionHelper<FileHeader>().tryExecute(new Callable<FileHeader>() {
                                @Override
-                               public FileHeaderDTO call() throws Exception {
-                                       return factory.getService().createFile(getCurrentUser().getId(), folder.getId(), name, "text/html", tmp.length(), tmp.getAbsolutePath());
+                               public FileHeader call() throws Exception {
+                                       return getService().createEmptyFile(getCurrentUser().getId(), folder.getId(), name);
                                }
                        });
                } catch (Exception e) {
                        // TODO Auto-generated catch block
-                       e.printStackTrace();
                }
-               //File dest = new File( this.getFile(), name );
-               //createEmptyFile(  );
-               GssFileResource newRes = new GssFileResource( host, factory, kmfileDTO );
-               LockResult res = newRes.lock( timeout, lockInfo );
-               return res.getLockToken();
+        GssFileResource newRes = new GssFileResource( host, factory, kmfile ,getCurrentUser());
+        LockResult res = newRes.lock( timeout, lockInfo );
+        return res.getLockToken();
                
        }
        @Override
@@ -446,6 +421,50 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
        w.close( "body" );
        w.close( "html" );
        w.flush();
+       
    }
-
+       @Override
+       public Long getQuotaAvailable() {
+               if(getCurrentUser()!=null)
+                       try {
+                               return getService().getUserStatistics(getCurrentUser().getId()).getQuotaLeftSize();
+                       } catch (ObjectNotFoundException e) {
+                               // TODO Auto-generated catch block
+                       } catch (RpcException e) {
+                               // TODO Auto-generated catch block
+                       }
+               return null;
+       }
+       @Override
+       public Long getQuotaUsed() {
+               if(getCurrentUser()!=null)
+                       try {
+                               return getService().getUserStatistics(getCurrentUser().getId()).getFileSize();
+                       } catch (ObjectNotFoundException e) {
+                               // TODO Auto-generated catch block
+                       } catch (RpcException e) {
+                               // TODO Auto-generated catch block
+                       }
+               return null;
+       }
+       
+       @Override
+       public boolean authorise(Request request, Method method, Auth auth) {
+        boolean result = factory.getSecurityManager().authorise(request, method, auth, this);
+        if(result){
+               User user = getCurrentUser();
+               //check permission
+               try {
+                               this.folder=getService().getFolder(user.getId(), folder.getId());
+                       } catch (ObjectNotFoundException e) {
+                               return false;
+                       } catch (InsufficientPermissionsException e) {
+                               return false;
+                       } catch (RpcException e) {
+                               return false;
+                       }
+                       return true;
+        }
+        return result;
+    }
 }