root / src / gr / ebs / gss / server / webdav / milton / GssFolderResource.java @ 21a76038
History | View | Annotate | Download (16.1 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 gr.ebs.gss.client.exceptions.DuplicateNameException; |
22 |
import gr.ebs.gss.client.exceptions.GSSIOException; |
23 |
import gr.ebs.gss.client.exceptions.InsufficientPermissionsException; |
24 |
import gr.ebs.gss.client.exceptions.ObjectNotFoundException; |
25 |
import gr.ebs.gss.client.exceptions.QuotaExceededException; |
26 |
import gr.ebs.gss.client.exceptions.RpcException; |
27 |
import gr.ebs.gss.server.domain.FileHeader; |
28 |
import gr.ebs.gss.server.domain.Folder; |
29 |
import gr.ebs.gss.server.domain.User; |
30 |
import gr.ebs.gss.server.ejb.TransactionHelper; |
31 |
|
32 |
import java.io.File; |
33 |
import java.io.IOException; |
34 |
import java.io.InputStream; |
35 |
import java.io.OutputStream; |
36 |
import java.net.URLDecoder; |
37 |
import java.util.ArrayList; |
38 |
import java.util.Date; |
39 |
import java.util.List; |
40 |
import java.util.Map; |
41 |
import java.util.concurrent.Callable; |
42 |
|
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
|
46 |
import com.bradmcevoy.http.Auth; |
47 |
import com.bradmcevoy.http.CollectionResource; |
48 |
import com.bradmcevoy.http.CopyableResource; |
49 |
import com.bradmcevoy.http.DeletableResource; |
50 |
import com.bradmcevoy.http.GetableResource; |
51 |
import com.bradmcevoy.http.LockInfo; |
52 |
import com.bradmcevoy.http.LockResult; |
53 |
import com.bradmcevoy.http.LockTimeout; |
54 |
import com.bradmcevoy.http.LockToken; |
55 |
import com.bradmcevoy.http.LockingCollectionResource; |
56 |
import com.bradmcevoy.http.MakeCollectionableResource; |
57 |
import com.bradmcevoy.http.MoveableResource; |
58 |
import com.bradmcevoy.http.PropFindableResource; |
59 |
import com.bradmcevoy.http.PutableResource; |
60 |
import com.bradmcevoy.http.QuotaResource; |
61 |
import com.bradmcevoy.http.Range; |
62 |
import com.bradmcevoy.http.Request; |
63 |
import com.bradmcevoy.http.Resource; |
64 |
import com.bradmcevoy.http.XmlWriter; |
65 |
import com.bradmcevoy.http.Request.Method; |
66 |
import com.bradmcevoy.http.exceptions.BadRequestException; |
67 |
import com.bradmcevoy.http.exceptions.ConflictException; |
68 |
import com.bradmcevoy.http.exceptions.NotAuthorizedException; |
69 |
|
70 |
|
71 |
/**
|
72 |
* @author kman
|
73 |
*
|
74 |
*/
|
75 |
public class GssFolderResource extends GssResource implements MakeCollectionableResource, PutableResource, CopyableResource, DeletableResource, MoveableResource, PropFindableResource, LockingCollectionResource, GetableResource, QuotaResource{ |
76 |
private static final Logger log = LoggerFactory.getLogger(GssFolderResource.class); |
77 |
Folder folder; |
78 |
|
79 |
/**
|
80 |
* @param host
|
81 |
* @param factory
|
82 |
* @param resource
|
83 |
*/
|
84 |
public GssFolderResource(String host, GSSResourceFactory factory, Object resource, User currentUser) { |
85 |
super(host, factory, resource);
|
86 |
folder=(Folder) resource; |
87 |
this.currentUser=currentUser;
|
88 |
} |
89 |
@Override
|
90 |
public String checkRedirect(Request request) { |
91 |
if( factory.getDefaultPage() != null ) { |
92 |
return request.getAbsoluteUrl() + "/" + factory.getDefaultPage(); |
93 |
} else {
|
94 |
return null; |
95 |
} |
96 |
} |
97 |
@Override
|
98 |
public Date getModifiedDate() { |
99 |
if(folder!=null && folder.getAuditInfo()!=null) |
100 |
return folder.getAuditInfo().getModificationDate();
|
101 |
return null; |
102 |
} |
103 |
@Override
|
104 |
public String getName() { |
105 |
return folder.getName();
|
106 |
} |
107 |
@Override
|
108 |
public String getUniqueId() { |
109 |
return "folder:"+folder.getId().toString(); |
110 |
} |
111 |
@Override
|
112 |
public void moveTo(final CollectionResource newParent, final String arg1) throws ConflictException, NotAuthorizedException, BadRequestException { |
113 |
if( newParent instanceof GssFolderResource ) { |
114 |
final GssFolderResource newFsParent = (GssFolderResource) newParent;
|
115 |
try {
|
116 |
if(newFsParent.folder.getName().equals(folder.getParent().getName())){
|
117 |
if(!folder.getName().equals(arg1))
|
118 |
new TransactionHelper<Void>().tryExecute(new Callable<Void>() { |
119 |
|
120 |
@Override
|
121 |
public Void call() throws Exception { |
122 |
getService().updateFolder(getCurrentUser().getId(), folder.getId(), arg1, null, null); |
123 |
return null; |
124 |
} |
125 |
|
126 |
}); |
127 |
} |
128 |
else new TransactionHelper<Void>().tryExecute(new Callable<Void>() { |
129 |
|
130 |
@Override
|
131 |
public Void call() throws Exception { |
132 |
getService().moveFolder(getCurrentUser().getId(), folder.getId(), newFsParent.folder.getId(), arg1); |
133 |
return null; |
134 |
} |
135 |
|
136 |
}); |
137 |
GssFolderResource.this.folder = getService().getFolder(getCurrentUser().getId(), folder.getId()); |
138 |
|
139 |
} catch (InsufficientPermissionsException e) {
|
140 |
throw new NotAuthorizedException(this); |
141 |
} catch (ObjectNotFoundException e) {
|
142 |
throw new BadRequestException(this); |
143 |
} catch (DuplicateNameException e) {
|
144 |
throw new ConflictException(this); |
145 |
} catch (RpcException e) {
|
146 |
throw new RuntimeException("System error"); |
147 |
} catch (GSSIOException e) {
|
148 |
throw new RuntimeException("Unable to Move"); |
149 |
} catch (Exception e) { |
150 |
throw new RuntimeException("Unable to Move"); |
151 |
} |
152 |
} else {
|
153 |
throw new RuntimeException("Destination is an unknown type. Must be a Folder, is a: " + newParent.getClass()); |
154 |
} |
155 |
|
156 |
} |
157 |
@Override
|
158 |
public void copyTo(final CollectionResource newParent, final String arg1) throws NotAuthorizedException, BadRequestException, ConflictException { |
159 |
if( newParent instanceof GssFolderResource ) { |
160 |
final GssFolderResource newFsParent = (GssFolderResource) newParent;
|
161 |
try {
|
162 |
new TransactionHelper<Void>().tryExecute(new Callable<Void>() { |
163 |
|
164 |
@Override
|
165 |
public Void call() throws Exception { |
166 |
getService().copyFolder(getCurrentUser().getId(), folder.getId(), newFsParent.folder.getId(), arg1); |
167 |
return null; |
168 |
} |
169 |
|
170 |
}); |
171 |
GssFolderResource.this.folder = getService().getFolder(getCurrentUser().getId(), folder.getId()); |
172 |
} catch (InsufficientPermissionsException e) {
|
173 |
throw new NotAuthorizedException(this); |
174 |
} catch (ObjectNotFoundException e) {
|
175 |
throw new BadRequestException(this); |
176 |
} catch (DuplicateNameException e) {
|
177 |
throw new ConflictException(this); |
178 |
} catch (RpcException e) {
|
179 |
throw new RuntimeException("System error"); |
180 |
} catch (GSSIOException e) {
|
181 |
throw new RuntimeException("Unable to Move"); |
182 |
} catch (Exception e) { |
183 |
throw new RuntimeException("Unable to Move"); |
184 |
} |
185 |
} else {
|
186 |
throw new RuntimeException("Destination is an unknown type. Must be a FsDirectoryResource, is a: " + newParent.getClass()); |
187 |
} |
188 |
|
189 |
} |
190 |
@Override
|
191 |
public CollectionResource createCollection(final String name) throws NotAuthorizedException, ConflictException, BadRequestException { |
192 |
////log.info("CALLING CREATECOLLECTION:"+name);
|
193 |
try {
|
194 |
final Folder folderParent = folder;
|
195 |
Folder created = new TransactionHelper<Folder>().tryExecute(new Callable<Folder>() { |
196 |
@Override
|
197 |
public Folder call() throws Exception { |
198 |
Folder f = getService().createFolder(getCurrentUser().getId(), folder.getId(), name); |
199 |
return f;
|
200 |
} |
201 |
}); |
202 |
return new GssFolderResource(host, factory, created, getCurrentUser()); |
203 |
} catch (DuplicateNameException e) {
|
204 |
// XXX If the existing name is a folder we should be returning
|
205 |
// SC_METHOD_NOT_ALLOWED, or even better, just do the createFolder
|
206 |
// without checking first and then deal with the exceptions.
|
207 |
throw new ConflictException(this); |
208 |
} catch (InsufficientPermissionsException e) {
|
209 |
throw new NotAuthorizedException(this); |
210 |
} catch (ObjectNotFoundException e) {
|
211 |
return null; |
212 |
} catch (RpcException e) {
|
213 |
throw new RuntimeException("System Error"); |
214 |
} catch (Exception e) { |
215 |
throw new RuntimeException("System Error"); |
216 |
|
217 |
} |
218 |
} |
219 |
@Override
|
220 |
public Resource child(String name) { |
221 |
for(Folder f : folder.getSubfolders())
|
222 |
if(f.getName().equals(name))
|
223 |
return new GssFolderResource(host, factory, f, getCurrentUser()); |
224 |
|
225 |
try {
|
226 |
for(FileHeader f : getService().getFiles(folder.getOwner().getId(), folder.getId(), true)) |
227 |
if(f.getName().equals(name))
|
228 |
return new GssFileResource(host, factory, f,getCurrentUser()); |
229 |
} catch (ObjectNotFoundException e) {
|
230 |
// TODO Auto-generated catch block
|
231 |
return null; |
232 |
} catch (InsufficientPermissionsException e) {
|
233 |
// TODO Auto-generated catch block
|
234 |
} catch (RpcException e) {
|
235 |
// TODO Auto-generated catch block
|
236 |
} |
237 |
////log.info("CALLING CHILD return null");
|
238 |
return null; |
239 |
} |
240 |
@Override
|
241 |
public List<? extends Resource> getChildren() { |
242 |
try {
|
243 |
this.folder = getService().getFolder(getCurrentUser().getId(), folder.getId());
|
244 |
} catch (ObjectNotFoundException e) {
|
245 |
// TODO Auto-generated catch block
|
246 |
} catch (InsufficientPermissionsException e) {
|
247 |
// TODO Auto-generated catch block
|
248 |
} catch (RpcException e) {
|
249 |
// TODO Auto-generated catch block
|
250 |
} |
251 |
List<GssResource> result = new ArrayList<GssResource>(); |
252 |
for(Folder f : folder.getSubfolders())
|
253 |
if(!f.isDeleted())
|
254 |
result.add(new GssFolderResource(host, factory, f, getCurrentUser()));
|
255 |
try {
|
256 |
for(FileHeader f : getService().getFiles(getCurrentUser().getId(), folder.getId(), true)) |
257 |
result.add(new GssFileResource(host, factory, f,getCurrentUser()));
|
258 |
} catch (ObjectNotFoundException e) {
|
259 |
// TODO Auto-generated catch block
|
260 |
} catch (InsufficientPermissionsException e) {
|
261 |
// TODO Auto-generated catch block
|
262 |
} catch (RpcException e) {
|
263 |
// TODO Auto-generated catch block
|
264 |
} |
265 |
return result;
|
266 |
} |
267 |
@Override
|
268 |
public Resource createNew(final String name, InputStream in, Long length, final String contentType ) throws IOException, ConflictException, NotAuthorizedException, BadRequestException { |
269 |
|
270 |
File uploadedFile = null; |
271 |
try {
|
272 |
uploadedFile = getService().uploadFile(in, getCurrentUser().getId()); |
273 |
} catch (IOException ex) { |
274 |
throw new IOException(ex); |
275 |
} catch (ObjectNotFoundException e) {
|
276 |
throw new BadRequestException(this); |
277 |
} catch (RpcException e) {
|
278 |
throw new RuntimeException("Unable to upload file"); |
279 |
} |
280 |
final File uf = uploadedFile; |
281 |
try {
|
282 |
String pathFolder = folder.getPath();
|
283 |
if(!pathFolder.endsWith("/")) |
284 |
pathFolder=pathFolder+"/";
|
285 |
String fname = pathFolder+name;
|
286 |
////log.info("fname:"+fname+" "+URLDecoder.decode(fname));
|
287 |
Object ff2;
|
288 |
try{
|
289 |
ff2 = getService().getResourceAtPath(folder.getOwner().getId(), URLDecoder.decode(fname), true); |
290 |
} |
291 |
catch(ObjectNotFoundException ex){
|
292 |
ff2=null;
|
293 |
} |
294 |
final Object ff = ff2; |
295 |
FileHeader kmfile = null;
|
296 |
if(ff != null && ff instanceof FileHeader){ |
297 |
kmfile = new TransactionHelper<FileHeader>().tryExecute(new Callable<FileHeader>() { |
298 |
@Override
|
299 |
public FileHeader call() throws Exception{ |
300 |
return getService().updateFileContents(getCurrentUser().getId(), ((FileHeader)ff).getId(), contentType, uf.length(), uf.getAbsolutePath());
|
301 |
} |
302 |
}); |
303 |
} |
304 |
else
|
305 |
kmfile = new TransactionHelper<FileHeader>().tryExecute(new Callable<FileHeader>() { |
306 |
@Override
|
307 |
public FileHeader call() throws Exception{ |
308 |
return getService().createFile(getCurrentUser().getId(), folder.getId(), name, contentType, uf.length(), uf.getAbsolutePath());
|
309 |
} |
310 |
}); |
311 |
return new GssFileResource(host, factory, kmfile, getCurrentUser()); |
312 |
} catch (ObjectNotFoundException e) {
|
313 |
throw new BadRequestException(this); |
314 |
} catch (InsufficientPermissionsException e) {
|
315 |
throw new NotAuthorizedException(this); |
316 |
} |
317 |
catch (DuplicateNameException e) {
|
318 |
// TODO Auto-generated catch block
|
319 |
throw new ConflictException(this); |
320 |
} |
321 |
catch(QuotaExceededException e){
|
322 |
throw new ConflictException(this); |
323 |
} |
324 |
catch(Exception e){ |
325 |
throw new RuntimeException("System Error"); |
326 |
} |
327 |
} |
328 |
@Override
|
329 |
public void delete() throws NotAuthorizedException, ConflictException, BadRequestException { |
330 |
try {
|
331 |
|
332 |
new TransactionHelper<Void>().tryExecute(new Callable<Void>() { |
333 |
|
334 |
@Override
|
335 |
public Void call() throws Exception { |
336 |
getService().deleteFolder(getCurrentUser().getId(), folder.getId()); |
337 |
return null; |
338 |
} |
339 |
}); |
340 |
|
341 |
} catch (InsufficientPermissionsException e) {
|
342 |
throw new NotAuthorizedException(this); |
343 |
} catch (ObjectNotFoundException e) {
|
344 |
throw new BadRequestException(this); |
345 |
} catch (RpcException e) {
|
346 |
throw new BadRequestException(this); |
347 |
} |
348 |
catch (Exception e) { |
349 |
throw new BadRequestException(this); |
350 |
} |
351 |
} |
352 |
@Override
|
353 |
public Date getCreateDate() { |
354 |
if(folder!=null && folder.getAuditInfo()!=null) |
355 |
return folder.getAuditInfo().getCreationDate();
|
356 |
return null; |
357 |
} |
358 |
@Override
|
359 |
public LockToken createAndLock(final String name, LockTimeout timeout, LockInfo lockInfo ) throws NotAuthorizedException { |
360 |
FileHeader kmfile=null;
|
361 |
try {
|
362 |
kmfile = new TransactionHelper<FileHeader>().tryExecute(new Callable<FileHeader>() { |
363 |
@Override
|
364 |
public FileHeader call() throws Exception { |
365 |
return getService().createEmptyFile(getCurrentUser().getId(), folder.getId(), name);
|
366 |
} |
367 |
}); |
368 |
} catch (Exception e) { |
369 |
// TODO Auto-generated catch block
|
370 |
} |
371 |
GssFileResource newRes = new GssFileResource( host, factory, kmfile ,getCurrentUser());
|
372 |
LockResult res = newRes.lock( timeout, lockInfo ); |
373 |
return res.getLockToken();
|
374 |
|
375 |
} |
376 |
@Override
|
377 |
public Long getContentLength() { |
378 |
return null; |
379 |
} |
380 |
@Override
|
381 |
public String getContentType(String arg0) { |
382 |
return "text/html"; |
383 |
} |
384 |
@Override
|
385 |
public Long getMaxAgeSeconds(Auth arg0) { |
386 |
// TODO Auto-generated method stub
|
387 |
return null; |
388 |
} |
389 |
/**
|
390 |
* Will generate a listing of the contents of this directory, unless
|
391 |
* the factory's allowDirectoryBrowsing has been set to false.
|
392 |
*
|
393 |
* If so it will just output a message saying that access has been disabled.
|
394 |
*
|
395 |
* @param out
|
396 |
* @param range
|
397 |
* @param params
|
398 |
* @param contentType
|
399 |
* @throws IOException
|
400 |
* @throws NotAuthorizedException
|
401 |
*/
|
402 |
public void sendContent( OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException, NotAuthorizedException { |
403 |
String subpath = folder.getPath();//getFile().getCanonicalPath().substring( factory.getRoot().getCanonicalPath().length() ).replace( '\\', '/' ); |
404 |
String uri = "/" + factory.getContextPath() + subpath; |
405 |
XmlWriter w = new XmlWriter( out ); |
406 |
w.open( "html" );
|
407 |
w.open( "body" );
|
408 |
w.begin( "h1" ).open().writeText( this.getName() ).close(); |
409 |
w.open( "table" );
|
410 |
for( Resource r : getChildren() ) {
|
411 |
w.open( "tr" );
|
412 |
|
413 |
w.open( "td" );
|
414 |
w.begin( "a" ).writeAtt( "href", uri + "/" + r.getName() ).open().writeText( r.getName() ).close(); |
415 |
w.close( "td" );
|
416 |
|
417 |
w.begin( "td" ).open().writeText( r.getModifiedDate() + "" ).close(); |
418 |
w.close( "tr" );
|
419 |
} |
420 |
w.close( "table" );
|
421 |
w.close( "body" );
|
422 |
w.close( "html" );
|
423 |
w.flush(); |
424 |
|
425 |
} |
426 |
@Override
|
427 |
public Long getQuotaAvailable() { |
428 |
if(getCurrentUser()!=null) |
429 |
try {
|
430 |
return getService().getUserStatistics(getCurrentUser().getId()).getQuotaLeftSize();
|
431 |
} catch (ObjectNotFoundException e) {
|
432 |
// TODO Auto-generated catch block
|
433 |
} catch (RpcException e) {
|
434 |
// TODO Auto-generated catch block
|
435 |
} |
436 |
return null; |
437 |
} |
438 |
@Override
|
439 |
public Long getQuotaUsed() { |
440 |
if(getCurrentUser()!=null) |
441 |
try {
|
442 |
return getService().getUserStatistics(getCurrentUser().getId()).getFileSize();
|
443 |
} catch (ObjectNotFoundException e) {
|
444 |
// TODO Auto-generated catch block
|
445 |
} catch (RpcException e) {
|
446 |
// TODO Auto-generated catch block
|
447 |
} |
448 |
return null; |
449 |
} |
450 |
|
451 |
@Override
|
452 |
public boolean authorise(Request request, Method method, Auth auth) { |
453 |
boolean result = factory.getSecurityManager().authorise(request, method, auth, this); |
454 |
if(result){
|
455 |
User user = getCurrentUser(); |
456 |
//check permission
|
457 |
try {
|
458 |
this.folder=getService().getFolder(user.getId(), folder.getId());
|
459 |
} catch (ObjectNotFoundException e) {
|
460 |
return false; |
461 |
} catch (InsufficientPermissionsException e) {
|
462 |
return false; |
463 |
} catch (RpcException e) {
|
464 |
return false; |
465 |
} |
466 |
return true; |
467 |
} |
468 |
return result;
|
469 |
} |
470 |
} |