milton webdav support - TODO: get rid of spring dependencies
[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
28 import org.apache.commons.io.IOUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.bradmcevoy.common.ContentTypeUtils;
33 import com.bradmcevoy.http.Auth;
34 import com.bradmcevoy.http.CollectionResource;
35 import com.bradmcevoy.http.CopyableResource;
36 import com.bradmcevoy.http.DeletableResource;
37 import com.bradmcevoy.http.GetableResource;
38 import com.bradmcevoy.http.MoveableResource;
39 import com.bradmcevoy.http.PropFindableResource;
40 import com.bradmcevoy.http.PropPatchableResource;
41 import com.bradmcevoy.http.Range;
42 import com.bradmcevoy.http.Request;
43 import com.bradmcevoy.http.exceptions.BadRequestException;
44 import com.bradmcevoy.http.exceptions.ConflictException;
45 import com.bradmcevoy.http.exceptions.NotAuthorizedException;
46 import com.bradmcevoy.http.webdav.PropPatchHandler.Fields;
47
48 import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
49 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
50 import gr.ebs.gss.client.exceptions.RpcException;
51 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
52 import gr.ebs.gss.server.domain.dto.UserDTO;
53
54
55 /**
56  * @author kman
57  *
58  */
59 public class GssFileResource extends GssResource implements CopyableResource, DeletableResource, GetableResource, MoveableResource, PropFindableResource, PropPatchableResource {
60         FileHeaderDTO file;
61         private static final Logger log = LoggerFactory.getLogger(GssFileResource.class);
62         /**
63          * @param host
64          * @param factory
65          * @param resource
66          */
67         public GssFileResource(String host, GSSResourceFactory factory, Object resource) {
68                 super(host, factory, resource);
69                 this.file = (FileHeaderDTO)resource;
70                 
71         }
72         @Override
73         public String checkRedirect(Request arg0) {
74                 // TODO Auto-generated method stub
75                 return null;
76         }
77         @Override
78         public Date getModifiedDate() {
79                 return file.getAuditInfo().getModificationDate();
80         }
81         @Override
82         public String getName() {
83                 return file.getName();
84         }
85         @Override
86         public String getUniqueId() {
87                 return file.getId().toString();
88         }
89         @Override
90         public void moveTo(CollectionResource arg0, String arg1) throws ConflictException, NotAuthorizedException, BadRequestException {
91                 // TODO Auto-generated method stub
92                 
93         }
94         @Override
95         public void copyTo(CollectionResource arg0, String arg1) throws NotAuthorizedException, BadRequestException, ConflictException {
96                 log.info("COPY FILE:"+arg0);
97                 
98         }
99         @Override
100         public void delete() throws NotAuthorizedException, ConflictException, BadRequestException {
101                 try {
102                         factory.getService().deleteFile(getCurrentUser().getId(), file.getId());
103                 } catch (ObjectNotFoundException e) {
104                         // TODO Auto-generated catch block
105                         e.printStackTrace();
106                 } catch (InsufficientPermissionsException e) {
107                         // TODO Auto-generated catch block
108                         e.printStackTrace();
109                 } catch (RpcException e) {
110                         // TODO Auto-generated catch block
111                         e.printStackTrace();
112                 }
113                 
114         }
115         @Override
116         public Long getContentLength() {
117                 return file.getFileSize();
118         }
119         @Override
120         public String getContentType(String preferredList ) {
121         return file.getMimeType();
122         }
123         @Override
124         public Long getMaxAgeSeconds(Auth arg0) {
125                 return factory.maxAgeSeconds( this );
126         }
127         @Override
128         public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException {
129         InputStream in = null;
130         try {
131             in = factory.getService().getFileContents(getCurrentUser().getId(), file.getId());
132             //        if( range != null ) {
133             //            long start = range.getStart();
134             //            if( start > 0 ) in.skip(start);
135             //            long finish = range.getFinish();
136             //            if( finish > 0 ) {
137             //                StreamToStream.readTo(in, out);
138             //            }
139             //        } else {
140             int bytes = IOUtils.copy( in, out );
141             
142             out.flush();
143             //        }
144         } catch (ObjectNotFoundException e) {
145                         // TODO Auto-generated catch block
146                         e.printStackTrace();
147                 } catch (InsufficientPermissionsException e) {
148                         // TODO Auto-generated catch block
149                         e.printStackTrace();
150                 } catch (RpcException e) {
151                         // TODO Auto-generated catch block
152                         e.printStackTrace();
153                 } finally {
154             IOUtils.closeQuietly( in );
155         }
156                 
157         }
158         @Override
159         public Date getCreateDate() {
160                 // TODO Auto-generated method stub
161                 return file.getAuditInfo().getCreationDate();
162         }
163         @Override
164         public void setProperties(Fields arg0) {
165                 // TODO Auto-generated method stub
166                 
167         }
168
169 }