Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / webdav / milton / GssFileResource.java @ 15f4b8ea

History | View | Annotate | Download (7.7 kB)

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.exceptions.BadRequestException;
45
import com.bradmcevoy.http.exceptions.ConflictException;
46
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
47
import com.bradmcevoy.http.webdav.PropPatchHandler.Fields;
48

    
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;
57

    
58

    
59
/**
60
 * @author kman
61
 *
62
 */
63
public class GssFileResource extends GssResource implements CopyableResource, DeletableResource, GetableResource, MoveableResource, PropFindableResource, PropPatchableResource {
64
        FileHeaderDTO file;
65
        private static final Logger log = LoggerFactory.getLogger(GssFileResource.class);
66
        /**
67
         * @param host
68
         * @param factory
69
         * @param resource
70
         */
71
        public GssFileResource(String host, GSSResourceFactory factory, Object resource) {
72
                super(host, factory, resource);
73
                this.file = (FileHeaderDTO)resource;
74
                
75
        }
76
        @Override
77
        public String checkRedirect(Request arg0) {
78
                // TODO Auto-generated method stub
79
                return null;
80
        }
81
        @Override
82
        public Date getModifiedDate() {
83
                return file.getAuditInfo().getModificationDate();
84
        }
85
        @Override
86
        public String getName() {
87
                return file.getName();
88
        }
89
        @Override
90
        public String getUniqueId() {
91
                return "file:"+file.getId().toString();
92
        }
93
        @Override
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;
97
                        try {
98
                                 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
99

    
100
                                        @Override
101
                                        public Void call() throws Exception {
102
                                                factory.getService().moveFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1);
103
                                                return null;
104
                                        }
105
                                        
106
                                });
107
                                GssFileResource.this.file = factory.getService().getFile(getCurrentUser().getId(), file.getId());
108
                                
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");
121
                        }
122
        } else {
123
            throw new RuntimeException("Destination is an unknown type. Must be a Folder, is a: " + newParent.getClass());
124
        }
125
                
126
        }
127
        @Override
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;
131
                        try {
132
                                 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
133

    
134
                                        @Override
135
                                        public Void call() throws Exception {
136
                                                factory.getService().copyFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1);
137
                                                return null;
138
                                        }
139
                                        
140
                                });
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");
154
                        }
155
        } else {
156
            throw new RuntimeException("Destination is an unknown type. Must be a FsDirectoryResource, is a: " + newParent.getClass());
157
        }
158
                
159
        }
160
        @Override
161
        public void delete() throws NotAuthorizedException, ConflictException, BadRequestException {
162
                try {
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);
170
                }
171
                
172
        }
173
        @Override
174
        public Long getContentLength() {
175
                return file.getFileSize();
176
        }
177
        @Override
178
        public String getContentType(String preferredList ) {
179
        return file.getMimeType();
180
        }
181
        @Override
182
        public Long getMaxAgeSeconds(Auth arg0) {
183
                return factory.maxAgeSeconds( this );
184
        }
185
        @Override
186
        public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException {
187
        InputStream in = null;
188
        try {
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);
196
            //            }
197
            //        } else {
198
            int bytes = IOUtils.copy( in, out );
199
            
200
            out.flush();
201
            //        }
202
        } catch (ObjectNotFoundException e) {
203
                        // TODO Auto-generated catch block
204
                        e.printStackTrace();
205
                } catch (InsufficientPermissionsException e) {
206
                        // TODO Auto-generated catch block
207
                        e.printStackTrace();
208
                } catch (RpcException e) {
209
                        // TODO Auto-generated catch block
210
                        e.printStackTrace();
211
                } finally {
212
            IOUtils.closeQuietly( in );
213
        }
214
                
215
        }
216
        @Override
217
        public Date getCreateDate() {
218
                // TODO Auto-generated method stub
219
                return file.getAuditInfo().getCreationDate();
220
        }
221
        @Override
222
        public void setProperties(Fields arg0) {
223
                // TODO Auto-generated method stub
224
                
225
        }
226

    
227
}