root / src / gr / ebs / gss / server / webdav / milton / GssFileResource.java @ 19388118
History | View | Annotate | Download (8.5 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.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) { |
74 |
super(host, factory, resource);
|
75 |
this.file = (FileHeaderDTO)resource;
|
76 |
|
77 |
} |
78 |
@Override
|
79 |
public String checkRedirect(Request arg0) { |
80 |
// TODO Auto-generated method stub
|
81 |
return null; |
82 |
} |
83 |
@Override
|
84 |
public Date getModifiedDate() { |
85 |
return file.getAuditInfo().getModificationDate();
|
86 |
} |
87 |
@Override
|
88 |
public String getName() { |
89 |
return file.getName();
|
90 |
} |
91 |
@Override
|
92 |
public String getUniqueId() { |
93 |
return "file:"+file.getId().toString(); |
94 |
} |
95 |
@Override
|
96 |
public void moveTo(final CollectionResource newParent, final String arg1) throws ConflictException, NotAuthorizedException, BadRequestException { |
97 |
if( newParent instanceof GssFolderResource ) { |
98 |
final GssFolderResource newFsParent = (GssFolderResource) newParent;
|
99 |
try {
|
100 |
new TransactionHelper<Void>().tryExecute(new Callable<Void>() { |
101 |
|
102 |
@Override
|
103 |
public Void call() throws Exception { |
104 |
if(newFsParent.folder.getId().equals(file.getFolder().getId())){
|
105 |
factory.getService().updateFile(getCurrentUser().getId(), file.getId(), arg1, null, new Date(), null, null, null); |
106 |
} |
107 |
else{
|
108 |
factory.getService().moveFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1); |
109 |
} |
110 |
return null; |
111 |
} |
112 |
|
113 |
}); |
114 |
GssFileResource.this.file = factory.getService().getFile(getCurrentUser().getId(), file.getId()); |
115 |
|
116 |
} catch (InsufficientPermissionsException e) {
|
117 |
throw new NotAuthorizedException(this); |
118 |
} catch (ObjectNotFoundException e) {
|
119 |
throw new BadRequestException(this); |
120 |
} catch (DuplicateNameException e) {
|
121 |
throw new ConflictException(this); |
122 |
} catch (RpcException e) {
|
123 |
throw new RuntimeException("System error"); |
124 |
} catch (GSSIOException e) {
|
125 |
throw new RuntimeException("Unable to Move"); |
126 |
} catch (Exception e) { |
127 |
throw new RuntimeException("Unable to Move"); |
128 |
} |
129 |
} else {
|
130 |
throw new RuntimeException("Destination is an unknown type. Must be a Folder, is a: " + newParent.getClass()); |
131 |
} |
132 |
|
133 |
} |
134 |
@Override
|
135 |
public void copyTo(final CollectionResource newParent, final String arg1) throws NotAuthorizedException, BadRequestException, ConflictException { |
136 |
if( newParent instanceof GssFolderResource ) { |
137 |
final GssFolderResource newFsParent = (GssFolderResource) newParent;
|
138 |
try {
|
139 |
new TransactionHelper<Void>().tryExecute(new Callable<Void>() { |
140 |
|
141 |
@Override
|
142 |
public Void call() throws Exception { |
143 |
factory.getService().copyFile(getCurrentUser().getId(), file.getId(), newFsParent.folder.getId(), arg1); |
144 |
return null; |
145 |
} |
146 |
|
147 |
}); |
148 |
GssFileResource.this.file = factory.getService().getFile(getCurrentUser().getId(), file.getId()); |
149 |
} catch (InsufficientPermissionsException e) {
|
150 |
throw new NotAuthorizedException(this); |
151 |
} catch (ObjectNotFoundException e) {
|
152 |
throw new BadRequestException(this); |
153 |
} catch (DuplicateNameException e) {
|
154 |
throw new ConflictException(this); |
155 |
} catch (RpcException e) {
|
156 |
throw new RuntimeException("System error"); |
157 |
} catch (GSSIOException e) {
|
158 |
throw new RuntimeException("Unable to Move"); |
159 |
} catch (Exception e) { |
160 |
throw new RuntimeException("Unable to Move"); |
161 |
} |
162 |
} else {
|
163 |
throw new RuntimeException("Destination is an unknown type. Must be a FsDirectoryResource, is a: " + newParent.getClass()); |
164 |
} |
165 |
|
166 |
} |
167 |
@Override
|
168 |
public void delete() throws NotAuthorizedException, ConflictException, BadRequestException { |
169 |
try {
|
170 |
factory.getService().deleteFile(getCurrentUser().getId(), file.getId()); |
171 |
} catch (ObjectNotFoundException e) {
|
172 |
throw new BadRequestException(this); |
173 |
} catch (InsufficientPermissionsException e) {
|
174 |
throw new NotAuthorizedException(this); |
175 |
} catch (RpcException e) {
|
176 |
throw new BadRequestException(this); |
177 |
} |
178 |
|
179 |
} |
180 |
@Override
|
181 |
public Long getContentLength() { |
182 |
return file.getFileSize();
|
183 |
} |
184 |
@Override
|
185 |
public String getContentType(String preferredList ) { |
186 |
return file.getMimeType();
|
187 |
} |
188 |
@Override
|
189 |
public Long getMaxAgeSeconds(Auth arg0) { |
190 |
return factory.maxAgeSeconds( this ); |
191 |
} |
192 |
@Override
|
193 |
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException { |
194 |
InputStream in = null; |
195 |
try {
|
196 |
in = factory.getService().getFileContents(getCurrentUser().getId(), file.getId()); |
197 |
/*if( range != null ) {
|
198 |
long start = range.getStart();
|
199 |
if( start > 0 ) in.skip(start);
|
200 |
long finish = range.getFinish();
|
201 |
if( finish > 0 ) {
|
202 |
StreamUtils.readTo(in, out);
|
203 |
}
|
204 |
} else {
|
205 |
*/
|
206 |
int bytes = IOUtils.copy( in, out );
|
207 |
out.flush(); |
208 |
//}
|
209 |
} catch (ObjectNotFoundException e) {
|
210 |
// TODO Auto-generated catch block
|
211 |
e.printStackTrace(); |
212 |
} catch (InsufficientPermissionsException e) {
|
213 |
// TODO Auto-generated catch block
|
214 |
e.printStackTrace(); |
215 |
} catch (RpcException e) {
|
216 |
// TODO Auto-generated catch block
|
217 |
e.printStackTrace(); |
218 |
} finally {
|
219 |
IOUtils.closeQuietly( in ); |
220 |
} |
221 |
|
222 |
} |
223 |
@Override
|
224 |
public Date getCreateDate() { |
225 |
// TODO Auto-generated method stub
|
226 |
return file.getAuditInfo().getCreationDate();
|
227 |
} |
228 |
@Override
|
229 |
public void setProperties(Fields arg0) { |
230 |
// TODO Auto-generated method stub
|
231 |
|
232 |
} |
233 |
|
234 |
|
235 |
@Override
|
236 |
public boolean authorise(Request request, Method method, Auth auth) { |
237 |
boolean result = factory.getSecurityManager().authorise(request, method, auth, this); |
238 |
if(result){
|
239 |
UserDTO user = (UserDTO) auth.getTag(); |
240 |
//check permission
|
241 |
try {
|
242 |
factory.getService().getFile(user.getId(), file.getId()); |
243 |
} catch (ObjectNotFoundException e) {
|
244 |
return false; |
245 |
} catch (InsufficientPermissionsException e) {
|
246 |
return false; |
247 |
} catch (RpcException e) {
|
248 |
return false; |
249 |
} |
250 |
return true; |
251 |
} |
252 |
return result;
|
253 |
} |
254 |
} |