use milton authentication manager, working on windows, need to check loss of auth...
[pithos] / src / gr / ebs / gss / server / webdav / milton / GssFileResource.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 java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.Date;
26 import java.util.Map;
27 import java.util.concurrent.Callable;
28
29 import org.apache.commons.io.IOUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.bradmcevoy.common.ContentTypeUtils;
34 import com.bradmcevoy.http.Auth;
35 import com.bradmcevoy.http.CollectionResource;
36 import com.bradmcevoy.http.CopyableResource;
37 import com.bradmcevoy.http.DeletableResource;
38 import com.bradmcevoy.http.GetableResource;
39 import com.bradmcevoy.http.MoveableResource;
40 import com.bradmcevoy.http.PropFindableResource;
41 import com.bradmcevoy.http.PropPatchableResource;
42 import com.bradmcevoy.http.Range;
43 import com.bradmcevoy.http.Request;
44 import com.bradmcevoy.http.Request.Method;
45 import com.bradmcevoy.http.exceptions.BadRequestException;
46 import com.bradmcevoy.http.exceptions.ConflictException;
47 import com.bradmcevoy.http.exceptions.NotAuthorizedException;
48 import com.bradmcevoy.http.webdav.PropPatchHandler.Fields;
49 import com.bradmcevoy.io.StreamUtils;
50
51 import gr.ebs.gss.client.exceptions.DuplicateNameException;
52 import gr.ebs.gss.client.exceptions.GSSIOException;
53 import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
54 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
55 import gr.ebs.gss.client.exceptions.RpcException;
56 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
57 import gr.ebs.gss.server.domain.dto.UserDTO;
58 import gr.ebs.gss.server.ejb.TransactionHelper;
59
60
61 /**
62  * @author kman
63  *
64  */
65 public class GssFileResource extends GssResource implements CopyableResource, DeletableResource, GetableResource, MoveableResource, PropFindableResource, PropPatchableResource {
66         FileHeaderDTO file;
67         private static final Logger log = LoggerFactory.getLogger(GssFileResource.class);
68         /**
69          * @param host
70          * @param factory
71          * @param resource
72          */
73         public GssFileResource(String host, GSSResourceFactory factory, Object resource, UserDTO currentUser) {
74                 super(host, factory, resource);
75                 this.file = (FileHeaderDTO)resource;
76                 this.currentUser=currentUser;
77                 
78         }
79         @Override
80         public String checkRedirect(Request arg0) {
81                 // TODO Auto-generated method stub
82                 return null;
83         }
84         @Override
85         public Date getModifiedDate() {
86                 return file.getAuditInfo().getModificationDate();
87         }
88         @Override
89         public String getName() {
90                 return file.getName();
91         }
92         @Override
93         public String getUniqueId() {
94                 return "file:"+file.getId().toString();
95         }
96         @Override
97         public void moveTo(final CollectionResource newParent, final String arg1) throws ConflictException, NotAuthorizedException, BadRequestException {
98                 if( newParent instanceof GssFolderResource ) {
99                         final GssFolderResource newFsParent = (GssFolderResource) newParent;
100                         try {
101                                  new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
102
103                                         @Override
104                                         public Void call() throws Exception {
105                                                 if(newFsParent.folder.getId().equals(file.getFolder().getId())){
106                                                         factory.getService().updateFile(getCurrentUser().getId(), file.getId(), arg1, null, new Date(), null, null, null);
107                                                 }
108                                                 else{
109                                                         factory.getService().moveFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1);
110                                                 }
111                                                 return null;
112                                         }
113                                         
114                                 });
115                                 GssFileResource.this.file = factory.getService().getFile(getCurrentUser().getId(), file.getId());
116                                 
117                         } catch (InsufficientPermissionsException e) {
118                                 throw new NotAuthorizedException(this);
119                         } catch (ObjectNotFoundException e) {
120                                 throw new BadRequestException(this);
121                         } catch (DuplicateNameException e) {
122                                 throw new ConflictException(this);
123                         } catch (RpcException e) {
124                                 throw new RuntimeException("System error");
125                         } catch (GSSIOException e) {
126                                 throw new RuntimeException("Unable to Move");
127                         } catch (Exception e) {
128                                 throw new RuntimeException("Unable to Move");
129                         }
130         } else {
131             throw new RuntimeException("Destination is an unknown type. Must be a Folder, is a: " + newParent.getClass());
132         }
133                 
134         }
135         @Override
136         public void copyTo(final CollectionResource newParent, final String arg1) throws NotAuthorizedException, BadRequestException, ConflictException {
137                 if( newParent instanceof GssFolderResource ) {                  
138                         final GssFolderResource newFsParent = (GssFolderResource) newParent;
139                         try {
140                                  new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
141
142                                         @Override
143                                         public Void call() throws Exception {
144                                                 factory.getService().copyFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1);
145                                                 return null;
146                                         }
147                                         
148                                 });
149                                  GssFileResource.this.file = factory.getService().getFile(getCurrentUser().getId(), file.getId());
150                         } catch (InsufficientPermissionsException e) {
151                                 throw new NotAuthorizedException(this);
152                         } catch (ObjectNotFoundException e) {
153                                 throw new BadRequestException(this);
154                         } catch (DuplicateNameException e) {
155                                 throw new ConflictException(this);
156                         } catch (RpcException e) {
157                                 throw new RuntimeException("System error");
158                         } catch (GSSIOException e) {
159                                 throw new RuntimeException("Unable to Move");
160                         } catch (Exception e) {
161                                 throw new RuntimeException("Unable to Move");
162                         }
163         } else {
164             throw new RuntimeException("Destination is an unknown type. Must be a FsDirectoryResource, is a: " + newParent.getClass());
165         }
166                 
167         }
168         @Override
169         public void delete() throws NotAuthorizedException, ConflictException, BadRequestException {
170                 try {
171                         factory.getService().deleteFile(getCurrentUser().getId(), file.getId());
172                 } catch (ObjectNotFoundException e) {
173                         throw new BadRequestException(this);                    
174                 } catch (InsufficientPermissionsException e) {
175                         throw new NotAuthorizedException(this);
176                 } catch (RpcException e) {
177                         throw new BadRequestException(this);
178                 }
179                 
180         }
181         @Override
182         public Long getContentLength() {
183                 return file.getFileSize();
184         }
185         @Override
186         public String getContentType(String preferredList ) {
187         return file.getMimeType();
188         }
189         @Override
190         public Long getMaxAgeSeconds(Auth arg0) {
191                 return factory.maxAgeSeconds( this );
192         }
193         @Override
194         public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException {
195         InputStream in = null;
196         try {
197             in = factory.getService().getFileContents(getCurrentUser().getId(), file.getId());
198             /*if( range != null ) {
199                 long start = range.getStart();
200                 if( start > 0 ) in.skip(start);
201                 long finish = range.getFinish();
202                 if( finish > 0 ) {
203                         StreamUtils.readTo(in, out);
204                 }
205             } else {
206             */
207                 int bytes = IOUtils.copy( in, out );
208                 out.flush();
209             //}
210         } catch (ObjectNotFoundException e) {
211                         // TODO Auto-generated catch block
212                         e.printStackTrace();
213                 } catch (InsufficientPermissionsException e) {
214                         // TODO Auto-generated catch block
215                         e.printStackTrace();
216                 } catch (RpcException e) {
217                         // TODO Auto-generated catch block
218                         e.printStackTrace();
219                 } finally {
220             IOUtils.closeQuietly( in );
221         }
222                 
223         }
224         @Override
225         public Date getCreateDate() {
226                 // TODO Auto-generated method stub
227                 return file.getAuditInfo().getCreationDate();
228         }
229         @Override
230         public void setProperties(Fields arg0) {
231                 // TODO Auto-generated method stub
232                 
233         }
234         
235         
236         @Override
237         public boolean authorise(Request request, Method method, Auth auth) {
238         boolean result = factory.getSecurityManager().authorise(request, method, auth, this);
239         if(result){
240                 UserDTO user = getCurrentUser();
241                 //check permission
242                 try {
243                                 factory.getService().getFile(user.getId(), file.getId());
244                         } catch (ObjectNotFoundException e) {
245                                 return false;
246                         } catch (InsufficientPermissionsException e) {
247                                 return false;
248                         } catch (RpcException e) {
249                                 return false;
250                         }
251                         return true;
252         }
253         return result;
254     }
255 }