2 * Copyright 2011 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.server.webdav.milton;
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;
27 import java.util.concurrent.Callable;
29 import org.apache.commons.io.IOUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
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;
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;
65 public class GssFileResource extends GssResource implements CopyableResource, DeletableResource, GetableResource, MoveableResource, PropFindableResource, PropPatchableResource {
67 private static final Logger log = LoggerFactory.getLogger(GssFileResource.class);
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;
80 public String checkRedirect(Request arg0) {
81 // TODO Auto-generated method stub
85 public Date getModifiedDate() {
86 return file.getAuditInfo().getModificationDate();
89 public String getName() {
90 return file.getName();
93 public String getUniqueId() {
94 return "file:"+file.getId().toString();
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;
101 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
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);
109 factory.getService().moveFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1);
115 GssFileResource.this.file = factory.getService().getFile(getCurrentUser().getId(), file.getId());
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");
131 throw new RuntimeException("Destination is an unknown type. Must be a Folder, is a: " + newParent.getClass());
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;
140 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
143 public Void call() throws Exception {
144 factory.getService().copyFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1);
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");
164 throw new RuntimeException("Destination is an unknown type. Must be a FsDirectoryResource, is a: " + newParent.getClass());
169 public void delete() throws NotAuthorizedException, ConflictException, BadRequestException {
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);
182 public Long getContentLength() {
183 return file.getFileSize();
186 public String getContentType(String preferredList ) {
187 return file.getMimeType();
190 public Long getMaxAgeSeconds(Auth arg0) {
191 return factory.maxAgeSeconds( this );
194 public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException {
195 InputStream in = null;
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();
203 StreamUtils.readTo(in, out);
207 int bytes = IOUtils.copy( in, out );
210 } catch (ObjectNotFoundException e) {
211 // TODO Auto-generated catch block
213 } catch (InsufficientPermissionsException e) {
214 // TODO Auto-generated catch block
216 } catch (RpcException e) {
217 // TODO Auto-generated catch block
220 IOUtils.closeQuietly( in );
225 public Date getCreateDate() {
226 // TODO Auto-generated method stub
227 return file.getAuditInfo().getCreationDate();
230 public void setProperties(Fields arg0) {
231 // TODO Auto-generated method stub
237 public boolean authorise(Request request, Method method, Auth auth) {
238 boolean result = factory.getSecurityManager().authorise(request, method, auth, this);
240 UserDTO user = getCurrentUser();
243 factory.getService().getFile(user.getId(), file.getId());
244 } catch (ObjectNotFoundException e) {
246 } catch (InsufficientPermissionsException e) {
248 } catch (RpcException e) {