Removed all DTO where possible
[pithos] / src / gr / ebs / gss / server / webdav / milton / GssRootFolderResource.java
1 /*
2  * Copyright 2011 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.server.webdav.milton;
20
21 import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
22 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 import gr.ebs.gss.client.exceptions.RpcException;
24 import gr.ebs.gss.server.domain.FileHeader;
25 import gr.ebs.gss.server.domain.Folder;
26 import gr.ebs.gss.server.domain.User;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.bradmcevoy.http.Auth;
35 import com.bradmcevoy.http.LockInfo;
36 import com.bradmcevoy.http.LockResult;
37 import com.bradmcevoy.http.LockTimeout;
38 import com.bradmcevoy.http.LockToken;
39 import com.bradmcevoy.http.Request;
40 import com.bradmcevoy.http.Resource;
41 import com.bradmcevoy.http.Request.Method;
42 import com.bradmcevoy.http.exceptions.NotAuthorizedException;
43 import com.bradmcevoy.http.http11.auth.DigestResponse;
44
45
46 /**
47  * @author kman
48  *
49  */
50 public class GssRootFolderResource extends GssFolderResource{
51         private static final Logger log = LoggerFactory.getLogger(GssFolderResource.class);
52         String path;
53         /**
54          * @param host
55          * @param factory
56          * @param resource
57          */
58         public GssRootFolderResource(String host, GSSResourceFactory factory, Object resource,String path) {
59                 super(host, factory, resource,null);
60                 this.path=path;
61                 try {
62                         this.folder = (Folder) factory.getResourceGss(path,getCurrentUser());
63                         //log.info("ROOT FOLDER:"+folder);
64                 } catch (RpcException e) {
65                         // TODO Auto-generated catch block
66                         e.printStackTrace();
67                 }
68         }
69         
70         @Override
71         public boolean authorise(Request request, Method method, Auth auth) {
72                 // TODO Auto-generated method stub
73                 boolean result = factory.getSecurityManager().authorise(request, method, auth, this);
74         if(result){
75                 User user = getCurrentUser();
76                 if(user==null)
77                         //log.info("AUTH USER NULL");
78                 if(this.folder==null){
79                         try {
80                                         this.folder= (Folder) factory.getResourceGss(path,getCurrentUser());//getService().getFolder(user.getId(), folder.getId());
81                                 } catch (RpcException e) {
82                                         //log.info("*****AUTH1:"+false+" "+getCurrentUser());
83                                         return false;
84                                 }
85                 }
86                         //log.info("*****AUTH2:"+true+" "+getCurrentUser());
87                         return true;
88         }
89         //log.info("*****AUTH3:"+result+" "+getCurrentUser()+" "+method);
90         return result;
91     }
92         
93         
94         @Override
95         public Object authenticate(DigestResponse digestRequest) {
96                 // TODO Auto-generated method stub
97                 return super.authenticate(digestRequest);
98         }
99         
100         @Override
101         public String getName() {
102                 return path;
103         }
104         @Override
105         public String getUniqueId() {
106                 if(folder!=null)
107                         return "folder:"+folder.getId().toString();
108                 return "folder:"+path;
109         }
110         @Override
111         public Resource child(String name) {
112                 log.info("CALLING ROOT GET CHILD:"+getCurrentUser());
113                 if(this.folder==null)
114                         try {
115                                 this.folder = (Folder) factory.getResourceGss(path,getCurrentUser());
116                         } catch (RpcException e) {
117                                 // TODO Auto-generated catch block
118                                 e.printStackTrace();
119                         }
120                 return super.child(name);
121         }
122         @Override
123         public List<? extends Resource> getChildren() {
124                 //log.info("CALLING ROOT GET CHILDREN:"+getCurrentUser());
125                 if(this.folder==null)
126                         try {
127                                 this.folder = (Folder) factory.getResourceGss(path,getCurrentUser());
128                         } catch (RpcException e) {
129                                 // TODO Auto-generated catch block
130                                 e.printStackTrace();
131                         }
132                 List<Resource> result = new ArrayList<Resource>();
133                 for(Folder f : folder.getSubfolders())
134                         if(!f.isDeleted())
135                                 result.add(new GssFolderResource(host, factory, f,getCurrentUser()));
136                 try {
137                         for(FileHeader f : factory.getService().getFiles(getCurrentUser().getId(), folder.getId(), true))
138                                 result.add(new GssFileResource(host, factory, f,getCurrentUser()));
139                 } catch (ObjectNotFoundException e) {
140                         // TODO Auto-generated catch block
141                         e.printStackTrace();
142                 } catch (InsufficientPermissionsException e) {
143                         // TODO Auto-generated catch block
144                         e.printStackTrace();
145                 } catch (RpcException e) {
146                         // TODO Auto-generated catch block
147                         e.printStackTrace();
148                 }
149                 //result.add(new GssOthersResource(host, factory));
150                 return result;
151         }
152         
153         /*Disable Locks if folder is null*/
154         public LockResult lock(LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException {
155                 if(folder==null)
156                         throw new NotAuthorizedException(this);
157         return factory.getLockManager().lock(timeout, lockInfo, this);
158     }
159
160     public LockResult refreshLock(String token) throws NotAuthorizedException {
161         if(folder==null)
162                         throw new NotAuthorizedException(this);
163         return factory.getLockManager().refresh(token, this);
164     }
165
166     public void unlock(String tokenId) throws NotAuthorizedException {
167         if(folder==null)
168                         throw new NotAuthorizedException(this);
169         factory.getLockManager().unlock(tokenId, this);
170     }
171
172     public LockToken getCurrentLock() {
173         if(folder==null)
174                         return null;
175         if( factory.getLockManager() != null ) {
176             return factory.getLockManager().getCurrentToken( this );
177         } else {
178             log.warn("getCurrentLock called, but no lock manager: file: " + resource);
179             return null;
180         }
181     }
182
183 }