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.exceptions.BadRequestException;
45 import com.bradmcevoy.http.exceptions.ConflictException;
46 import com.bradmcevoy.http.exceptions.NotAuthorizedException;
47 import com.bradmcevoy.http.webdav.PropPatchHandler.Fields;
49 import gr.ebs.gss.client.exceptions.DuplicateNameException;
50 import gr.ebs.gss.client.exceptions.GSSIOException;
51 import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
52 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
53 import gr.ebs.gss.client.exceptions.RpcException;
54 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
55 import gr.ebs.gss.server.domain.dto.UserDTO;
56 import gr.ebs.gss.server.ejb.TransactionHelper;
63 public class GssFileResource extends GssResource implements CopyableResource, DeletableResource, GetableResource, MoveableResource, PropFindableResource, PropPatchableResource {
65 private static final Logger log = LoggerFactory.getLogger(GssFileResource.class);
71 public GssFileResource(String host, GSSResourceFactory factory, Object resource) {
72 super(host, factory, resource);
73 this.file = (FileHeaderDTO)resource;
77 public String checkRedirect(Request arg0) {
78 // TODO Auto-generated method stub
82 public Date getModifiedDate() {
83 return file.getAuditInfo().getModificationDate();
86 public String getName() {
87 return file.getName();
90 public String getUniqueId() {
91 return "file:"+file.getId().toString();
94 public void moveTo(final CollectionResource newParent, final String arg1) throws ConflictException, NotAuthorizedException, BadRequestException {
95 if( newParent instanceof GssFolderResource ) {
96 final GssFolderResource newFsParent = (GssFolderResource) newParent;
98 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
101 public Void call() throws Exception {
102 factory.getService().moveFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1);
107 GssFileResource.this.file = factory.getService().getFile(getCurrentUser().getId(), file.getId());
109 } catch (InsufficientPermissionsException e) {
110 throw new NotAuthorizedException(this);
111 } catch (ObjectNotFoundException e) {
112 throw new BadRequestException(this);
113 } catch (DuplicateNameException e) {
114 throw new ConflictException(this);
115 } catch (RpcException e) {
116 throw new RuntimeException("System error");
117 } catch (GSSIOException e) {
118 throw new RuntimeException("Unable to Move");
119 } catch (Exception e) {
120 throw new RuntimeException("Unable to Move");
123 throw new RuntimeException("Destination is an unknown type. Must be a Folder, is a: " + newParent.getClass());
128 public void copyTo(final CollectionResource newParent, final String arg1) throws NotAuthorizedException, BadRequestException, ConflictException {
129 if( newParent instanceof GssFolderResource ) {
130 final GssFolderResource newFsParent = (GssFolderResource) newParent;
132 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
135 public Void call() throws Exception {
136 factory.getService().copyFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1);
141 GssFileResource.this.file = factory.getService().getFile(getCurrentUser().getId(), file.getId());
142 } catch (InsufficientPermissionsException e) {
143 throw new NotAuthorizedException(this);
144 } catch (ObjectNotFoundException e) {
145 throw new BadRequestException(this);
146 } catch (DuplicateNameException e) {
147 throw new ConflictException(this);
148 } catch (RpcException e) {
149 throw new RuntimeException("System error");
150 } catch (GSSIOException e) {
151 throw new RuntimeException("Unable to Move");
152 } catch (Exception e) {
153 throw new RuntimeException("Unable to Move");
156 throw new RuntimeException("Destination is an unknown type. Must be a FsDirectoryResource, is a: " + newParent.getClass());
161 public void delete() throws NotAuthorizedException, ConflictException, BadRequestException {
163 factory.getService().deleteFile(getCurrentUser().getId(), file.getId());
164 } catch (ObjectNotFoundException e) {
165 throw new BadRequestException(this);
166 } catch (InsufficientPermissionsException e) {
167 throw new NotAuthorizedException(this);
168 } catch (RpcException e) {
169 throw new BadRequestException(this);
174 public Long getContentLength() {
175 return file.getFileSize();
178 public String getContentType(String preferredList ) {
179 return file.getMimeType();
182 public Long getMaxAgeSeconds(Auth arg0) {
183 return factory.maxAgeSeconds( this );
186 public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException {
187 InputStream in = null;
189 in = factory.getService().getFileContents(getCurrentUser().getId(), file.getId());
190 // if( range != null ) {
191 // long start = range.getStart();
192 // if( start > 0 ) in.skip(start);
193 // long finish = range.getFinish();
194 // if( finish > 0 ) {
195 // StreamToStream.readTo(in, out);
198 int bytes = IOUtils.copy( in, out );
202 } catch (ObjectNotFoundException e) {
203 // TODO Auto-generated catch block
205 } catch (InsufficientPermissionsException e) {
206 // TODO Auto-generated catch block
208 } catch (RpcException e) {
209 // TODO Auto-generated catch block
212 IOUtils.closeQuietly( in );
217 public Date getCreateDate() {
218 // TODO Auto-generated method stub
219 return file.getAuditInfo().getCreationDate();
222 public void setProperties(Fields arg0) {
223 // TODO Auto-generated method stub