Automated merge with https://gss.googlecode.com/hg/
[pithos] / src / gr / ebs / gss / server / webdav / milton / GssFolderResource.java
index d239e89..8e7cc61 100644 (file)
@@ -22,11 +22,10 @@ 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.QuotaExceededException;
 import gr.ebs.gss.client.exceptions.RpcException;
-import gr.ebs.gss.server.domain.User;
 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
 import gr.ebs.gss.server.domain.dto.FolderDTO;
-import gr.ebs.gss.server.domain.dto.UserDTO;
 import gr.ebs.gss.server.ejb.TransactionHelper;
 
 import java.io.File;
@@ -47,8 +46,8 @@ import com.bradmcevoy.http.CollectionResource;
 import com.bradmcevoy.http.CopyableResource;
 import com.bradmcevoy.http.DeletableResource;
 import com.bradmcevoy.http.GetableResource;
-import com.bradmcevoy.http.HttpManager;
 import com.bradmcevoy.http.LockInfo;
+import com.bradmcevoy.http.LockResult;
 import com.bradmcevoy.http.LockTimeout;
 import com.bradmcevoy.http.LockToken;
 import com.bradmcevoy.http.LockingCollectionResource;
@@ -100,18 +99,12 @@ 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>() {
@@ -119,14 +112,11 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
                                                @Override
                                                public Void call() throws Exception {
                                                        factory.getService().updateFolder(getCurrentUser().getId(), folder.getId(), arg1, null, null);
-                                                       log.info("RENAMING OK:"+arg1);
-                                                       
                                                        return null;
                                                }
                                                
                                        });
                                }
-                               //this.folder = factory.getService().updateFolder(user.getId(), folder.getId(), arg1, null, null);
                                else new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
 
                                        @Override
@@ -142,72 +132,50 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
                                log.info("MOVING:"+folder.getName());
                                
                        } 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);
-                                               
                                                return null;
                                        }
                                        
                                });
                                GssFolderResource.this.folder = factory.getService().getFolder(getCurrentUser().getId(), folder.getId());
-                               log.info("MOVING:"+folder.getName());
-                               
                        } 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());
@@ -240,10 +208,10 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
                        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
@@ -311,25 +279,58 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
                } 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 {
-                       FileHeaderDTO kmfileDTO = new TransactionHelper<FileHeaderDTO>().tryExecute(new Callable<FileHeaderDTO>() {
+                       String pathFolder = folder.getPath();
+                       if(!pathFolder.endsWith("/"))
+                               pathFolder=pathFolder+"/";
+                       String fname = pathFolder+name;
+                       Object ff2;
+                       try{
+                               ff2 = factory.getService().getResourceAtPath(folder.getOwner().getId(), fname, true);
+                       }
+                       catch(ObjectNotFoundException ex){
+                               ff2=null;
+                       }
+                       final Object ff = ff2;
+                       FileHeaderDTO kmfileDTO=null;
+                       if(ff!=null && ff instanceof FileHeaderDTO){
+                               kmfileDTO = new TransactionHelper<FileHeaderDTO>().tryExecute(new Callable<FileHeaderDTO>() {
+                                       @Override
+                                       public FileHeaderDTO call()  throws Exception{
+                                               return factory.getService().updateFileContents(getCurrentUser().getId(), ((FileHeaderDTO)ff).getId(),  contentType, uf.length(), uf.getAbsolutePath());
+                                       }
+                               });
+                       }
+                       else
+                               kmfileDTO = new TransactionHelper<FileHeaderDTO>().tryExecute(new Callable<FileHeaderDTO>() {
                                        @Override
-                                       public FileHeaderDTO call() throws Exception {
+                                       public FileHeaderDTO call() throws Exception{
                                                return factory.getService().createFile(getCurrentUser().getId(), folder.getId(), name, contentType, uf.length(), uf.getAbsolutePath());
                                        }
                                });
                        return new GssFileResource(host, factory, kmfileDTO);
-               } catch (Exception e) {
+               } catch (ObjectNotFoundException e) {
+                       throw new BadRequestException(this);
+               } catch (InsufficientPermissionsException e) {
+                       throw new NotAuthorizedException(this);
+               }
+               catch (DuplicateNameException e) {
+                       // TODO Auto-generated catch block
                        e.printStackTrace();
+                       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 {
@@ -354,13 +355,28 @@ public class GssFolderResource extends GssResource implements MakeCollectionable
                return null;
        }
        @Override
-       public LockToken createAndLock( String name, LockTimeout timeout, LockInfo lockInfo ) throws NotAuthorizedException {
-               //File dest = new File( this.getFile(), name );
+       public LockToken createAndLock(final String name, LockTimeout timeout, LockInfo lockInfo ) throws NotAuthorizedException {
+               log.info("CREATE AND LOCK");
+               final File tmp =  new File("/tmp/"+new java.util.Random().nextInt());
+               FileHeaderDTO kmfileDTO=null;
+               try {
+                       kmfileDTO = new TransactionHelper<FileHeaderDTO>().tryExecute(new Callable<FileHeaderDTO>() {
+                               @Override
+                               public FileHeaderDTO call() throws Exception {
+                                       return factory.getService().createFile(getCurrentUser().getId(), folder.getId(), name, "text/html", tmp.length(), tmp.getAbsolutePath());
+                               }
+                       });
+               } catch (Exception e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+               log.info("CREATE AND LOCK:"+kmfileDTO.getId());
+               //File dest = new File( this.getFile(), name );
                //createEmptyFile(  );
-               //FsFileResource newRes = new FsFileResource( host, factory, dest );
-               //LockResult res = newRes.lock( timeout, lockInfo );
-               //return res.getLockToken();
-               return null;
+               GssFileResource newRes = new GssFileResource( host, factory, kmfileDTO );
+               LockResult res = newRes.lock( timeout, lockInfo );
+               return res.getLockToken();
+               
        }
        @Override
        public Long getContentLength() {