1
14
15 package com.liferay.portlet.documentlibrary.webdav;
16
17 import com.liferay.documentlibrary.DuplicateFileException;
18 import com.liferay.portal.DuplicateLockException;
19 import com.liferay.portal.InvalidLockException;
20 import com.liferay.portal.NoSuchLockException;
21 import com.liferay.portal.kernel.exception.PortalException;
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.FileUtil;
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.kernel.util.StringUtil;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.model.Lock;
29 import com.liferay.portal.security.auth.PrincipalException;
30 import com.liferay.portal.service.ServiceContext;
31 import com.liferay.portal.webdav.BaseResourceImpl;
32 import com.liferay.portal.webdav.BaseWebDAVStorageImpl;
33 import com.liferay.portal.webdav.LockException;
34 import com.liferay.portal.webdav.Resource;
35 import com.liferay.portal.webdav.Status;
36 import com.liferay.portal.webdav.WebDAVException;
37 import com.liferay.portal.webdav.WebDAVRequest;
38 import com.liferay.portal.webdav.WebDAVUtil;
39 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
40 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
41 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
42 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
43 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
44 import com.liferay.portlet.documentlibrary.model.DLFolder;
45 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
46 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
47 import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
48 import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
49
50 import java.io.File;
51 import java.io.InputStream;
52
53 import java.util.ArrayList;
54 import java.util.List;
55
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
58
59
65 public class DLWebDAVStorageImpl extends BaseWebDAVStorageImpl {
66
67 public int copyCollectionResource(
68 WebDAVRequest webDavRequest, Resource resource, String destination,
69 boolean overwrite, long depth)
70 throws WebDAVException {
71
72 try {
73 String[] destinationArray = WebDAVUtil.getPathArray(
74 destination, true);
75
76 long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
77
78 try {
79 parentFolderId = getParentFolderId(destinationArray);
80 }
81 catch (NoSuchFolderException nsfe) {
82 return HttpServletResponse.SC_CONFLICT;
83 }
84
85 DLFolder folder = (DLFolder)resource.getModel();
86
87 long groupId = WebDAVUtil.getGroupId(destination);
88 String name = WebDAVUtil.getResourceName(destinationArray);
89 String description = folder.getDescription();
90
91 ServiceContext serviceContext = new ServiceContext();
92
93 serviceContext.setAddCommunityPermissions(
94 isAddCommunityPermissions(groupId));
95 serviceContext.setAddGuestPermissions(true);
96
97 int status = HttpServletResponse.SC_CREATED;
98
99 if (overwrite) {
100 if (deleteResource(
101 groupId, parentFolderId, name,
102 webDavRequest.getLockUuid())) {
103
104 status = HttpServletResponse.SC_NO_CONTENT;
105 }
106 }
107
108 if (depth == 0) {
109 DLFolderServiceUtil.addFolder(
110 groupId, parentFolderId, name, description, serviceContext);
111 }
112 else {
113 DLFolderServiceUtil.copyFolder(
114 groupId, folder.getFolderId(), parentFolderId, name,
115 description, serviceContext);
116 }
117
118 return status;
119 }
120 catch (DuplicateFolderNameException dfne) {
121 return HttpServletResponse.SC_PRECONDITION_FAILED;
122 }
123 catch (PrincipalException pe) {
124 return HttpServletResponse.SC_FORBIDDEN;
125 }
126 catch (Exception e) {
127 throw new WebDAVException(e);
128 }
129 }
130
131 public int copySimpleResource(
132 WebDAVRequest webDavRequest, Resource resource, String destination,
133 boolean overwrite)
134 throws WebDAVException {
135
136 File file = null;
137
138 try {
139 String[] destinationArray = WebDAVUtil.getPathArray(
140 destination, true);
141
142 long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
143
144 try {
145 parentFolderId = getParentFolderId(destinationArray);
146 }
147 catch (NoSuchFolderException nsfe) {
148 return HttpServletResponse.SC_CONFLICT;
149 }
150
151 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
152
153 long groupId = WebDAVUtil.getGroupId(destination);
154 long userId = webDavRequest.getUserId();
155 String name = WebDAVUtil.getResourceName(destinationArray);
156 String title = WebDAVUtil.getResourceName(destinationArray);
157 String description = fileEntry.getDescription();
158 String versionDescription = StringPool.BLANK;
159 String extraSettings = fileEntry.getExtraSettings();
160
161 file = FileUtil.createTempFile(
162 FileUtil.getExtension(fileEntry.getName()));
163
164 InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
165 fileEntry.getCompanyId(), userId, fileEntry.getGroupId(),
166 fileEntry.getFolderId(), fileEntry.getName());
167
168 FileUtil.write(file, is);
169
170 ServiceContext serviceContext = new ServiceContext();
171
172 serviceContext.setAddCommunityPermissions(
173 isAddCommunityPermissions(groupId));
174 serviceContext.setAddGuestPermissions(true);
175
176 int status = HttpServletResponse.SC_CREATED;
177
178 if (overwrite) {
179 if (deleteResource(
180 groupId, parentFolderId, title,
181 webDavRequest.getLockUuid())) {
182
183 status = HttpServletResponse.SC_NO_CONTENT;
184 }
185 }
186
187 DLFileEntryServiceUtil.addFileEntry(
188 groupId, parentFolderId, name, title, description,
189 versionDescription, extraSettings, file, serviceContext);
190
191 return status;
192 }
193 catch (DuplicateFileException dfe) {
194 return HttpServletResponse.SC_PRECONDITION_FAILED;
195 }
196 catch (DuplicateFolderNameException dfne) {
197 return HttpServletResponse.SC_PRECONDITION_FAILED;
198 }
199 catch (LockException le) {
200 return WebDAVUtil.SC_LOCKED;
201 }
202 catch (PrincipalException pe) {
203 return HttpServletResponse.SC_FORBIDDEN;
204 }
205 catch (Exception e) {
206 throw new WebDAVException(e);
207 }
208 finally {
209 if (file != null) {
210 file.delete();
211 }
212 }
213 }
214
215 public int deleteResource(WebDAVRequest webDavRequest)
216 throws WebDAVException {
217
218 try {
219 Resource resource = getResource(webDavRequest);
220
221 if (resource == null) {
222 return HttpServletResponse.SC_NOT_FOUND;
223 }
224
225 Object model = resource.getModel();
226
227 if (model instanceof DLFolder) {
228 DLFolder folder = (DLFolder)model;
229
230 DLFolderServiceUtil.deleteFolder(folder.getFolderId());
231 }
232 else {
233 DLFileEntry fileEntry = (DLFileEntry)model;
234
235 if (isLocked(fileEntry, webDavRequest.getLockUuid())) {
236 return WebDAVUtil.SC_LOCKED;
237 }
238
239 DLFileEntryServiceUtil.deleteFileEntry(
240 fileEntry.getGroupId(), fileEntry.getFolderId(),
241 fileEntry.getName());
242 }
243
244 return HttpServletResponse.SC_NO_CONTENT;
245 }
246 catch (PrincipalException pe) {
247 return HttpServletResponse.SC_FORBIDDEN;
248 }
249 catch (Exception e) {
250 throw new WebDAVException(e);
251 }
252 }
253
254 public Resource getResource(WebDAVRequest webDavRequest)
255 throws WebDAVException {
256
257 try {
258 String[] pathArray = webDavRequest.getPathArray();
259
260 long parentFolderId = getParentFolderId(pathArray);
261 String name = WebDAVUtil.getResourceName(pathArray);
262
263 if (Validator.isNull(name)) {
264 String path = getRootPath() + webDavRequest.getPath();
265
266 return new BaseResourceImpl(path, StringPool.BLANK, getToken());
267 }
268
269 try {
270 DLFolder folder = DLFolderServiceUtil.getFolder(
271 webDavRequest.getGroupId(), parentFolderId, name);
272
273 if ((folder.getParentFolderId() != parentFolderId) ||
274 (webDavRequest.getGroupId() != folder.getGroupId())) {
275
276 throw new NoSuchFolderException();
277 }
278
279 return toResource(webDavRequest, folder, false);
280 }
281 catch (NoSuchFolderException nsfe) {
282 try {
283 String titleWithExtension = name;
284
285 DLFileEntry fileEntry =
286 DLFileEntryServiceUtil.getFileEntryByTitle(
287 webDavRequest.getGroupId(), parentFolderId,
288 titleWithExtension);
289
290 return toResource(webDavRequest, fileEntry, false);
291 }
292 catch (NoSuchFileEntryException nsfee) {
293 return null;
294 }
295 }
296 }
297 catch (Exception e) {
298 throw new WebDAVException(e);
299 }
300 }
301
302 public List<Resource> getResources(WebDAVRequest webDavRequest)
303 throws WebDAVException {
304
305 try {
306 long folderId = getFolderId(webDavRequest.getPathArray());
307
308 List<Resource> folders = getFolders(webDavRequest, folderId);
309 List<Resource> fileEntries = getFileEntries(
310 webDavRequest, folderId);
311
312 List<Resource> resources = new ArrayList<Resource>(
313 folders.size() + fileEntries.size());
314
315 resources.addAll(folders);
316 resources.addAll(fileEntries);
317
318 return resources;
319 }
320 catch (Exception e) {
321 throw new WebDAVException(e);
322 }
323 }
324
325 public boolean isSupportsClassTwo() {
326 return true;
327 }
328
329 public Status lockResource(
330 WebDAVRequest webDavRequest, String owner, long timeout)
331 throws WebDAVException {
332
333 Resource resource = getResource(webDavRequest);
334
335 Lock lock = null;
336 int status = HttpServletResponse.SC_OK;
337
338 try {
339 if (resource == null) {
340 status = HttpServletResponse.SC_CREATED;
341
342 String[] pathArray = webDavRequest.getPathArray();
343
344 long groupId = webDavRequest.getGroupId();
345 long parentFolderId = getParentFolderId(pathArray);
346 String name = WebDAVUtil.getResourceName(pathArray);
347
348 String title = name;
349 String description = StringPool.BLANK;
350 String versionDescription = StringPool.BLANK;
351 String extraSettings = StringPool.BLANK;
352
353 File file = FileUtil.createTempFile(
354 FileUtil.getExtension(name));
355
356 file.createNewFile();
357
358 ServiceContext serviceContext = new ServiceContext();
359
360 serviceContext.setAddCommunityPermissions(
361 isAddCommunityPermissions(groupId));
362 serviceContext.setAddGuestPermissions(true);
363
364 DLFileEntry fileEntry = DLFileEntryServiceUtil.addFileEntry(
365 groupId, parentFolderId, name, title, description,
366 versionDescription, extraSettings, file, serviceContext);
367
368 resource = toResource(webDavRequest, fileEntry, false);
369 }
370
371 if (resource instanceof DLFileEntryResourceImpl) {
372 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
373
374 lock = DLFileEntryServiceUtil.lockFileEntry(
375 fileEntry.getGroupId(), fileEntry.getFolderId(),
376 fileEntry.getName(), owner, timeout);
377 }
378 else {
379 boolean inheritable = false;
380
381 long depth = WebDAVUtil.getDepth(
382 webDavRequest.getHttpServletRequest());
383
384 if (depth != 0) {
385 inheritable = true;
386 }
387
388 DLFolder folder = (DLFolder)resource.getModel();
389
390 lock = DLFolderServiceUtil.lockFolder(
391 folder.getFolderId(), owner, inheritable, timeout);
392 }
393 }
394 catch (Exception e) {
395
396
398 if (!(e instanceof DuplicateLockException)) {
399 throw new WebDAVException(e);
400 }
401
402 status = WebDAVUtil.SC_LOCKED;
403 }
404
405 return new Status(lock, status);
406 }
407
408 public Status makeCollection(WebDAVRequest webDavRequest)
409 throws WebDAVException {
410
411 try {
412 HttpServletRequest request = webDavRequest.getHttpServletRequest();
413
414 if (request.getContentLength() > 0) {
415 return new Status(
416 HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
417 }
418
419 String[] pathArray = webDavRequest.getPathArray();
420
421 long groupId = webDavRequest.getGroupId();
422 long parentFolderId = getParentFolderId(pathArray);
423 String name = WebDAVUtil.getResourceName(pathArray);
424 String description = StringPool.BLANK;
425
426 ServiceContext serviceContext = new ServiceContext();
427
428 serviceContext.setAddCommunityPermissions(
429 isAddCommunityPermissions(groupId));
430 serviceContext.setAddGuestPermissions(true);
431
432 DLFolderServiceUtil.addFolder(
433 groupId, parentFolderId, name, description, serviceContext);
434
435 String location = StringUtil.merge(pathArray, StringPool.SLASH);
436
437 return new Status(location, HttpServletResponse.SC_CREATED);
438 }
439 catch (DuplicateFolderNameException dfne) {
440 return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
441 }
442 catch (DuplicateFileException dfe) {
443 return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
444 }
445 catch (NoSuchFolderException nsfe) {
446 return new Status(HttpServletResponse.SC_CONFLICT);
447 }
448 catch (PrincipalException pe) {
449 return new Status(HttpServletResponse.SC_FORBIDDEN);
450 }
451 catch (Exception e) {
452 throw new WebDAVException(e);
453 }
454 }
455
456 public int moveCollectionResource(
457 WebDAVRequest webDavRequest, Resource resource, String destination,
458 boolean overwrite)
459 throws WebDAVException {
460
461 try {
462 String[] destinationArray = WebDAVUtil.getPathArray(
463 destination, true);
464
465 DLFolder folder = (DLFolder)resource.getModel();
466
467 long groupId = WebDAVUtil.getGroupId(destinationArray);
468 long folderId = folder.getFolderId();
469 long parentFolderId = getParentFolderId(destinationArray);
470 String name = WebDAVUtil.getResourceName(destinationArray);
471 String description = folder.getDescription();
472
473 ServiceContext serviceContext = new ServiceContext();
474
475 int status = HttpServletResponse.SC_CREATED;
476
477 if (overwrite) {
478 if (deleteResource(
479 groupId, parentFolderId, name,
480 webDavRequest.getLockUuid())) {
481
482 status = HttpServletResponse.SC_NO_CONTENT;
483 }
484 }
485
486 DLFolderServiceUtil.updateFolder(
487 folderId, parentFolderId, name, description, serviceContext);
488
489 return status;
490 }
491 catch (PrincipalException pe) {
492 return HttpServletResponse.SC_FORBIDDEN;
493 }
494 catch (DuplicateFolderNameException dfne) {
495 return HttpServletResponse.SC_PRECONDITION_FAILED;
496 }
497 catch (Exception e) {
498 throw new WebDAVException(e);
499 }
500 }
501
502 public int moveSimpleResource(
503 WebDAVRequest webDavRequest, Resource resource, String destination,
504 boolean overwrite)
505 throws WebDAVException {
506
507 try {
508 String[] destinationArray = WebDAVUtil.getPathArray(
509 destination, true);
510
511 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
512
513 if (isLocked(fileEntry, webDavRequest.getLockUuid())) {
514 return WebDAVUtil.SC_LOCKED;
515 }
516
517 long groupId = WebDAVUtil.getGroupId(destinationArray);
518 long userId = webDavRequest.getUserId();
519 long parentFolderId = getParentFolderId(destinationArray);
520 String name = fileEntry.getName();
521 String sourceFileName = null;
522 String title = WebDAVUtil.getResourceName(destinationArray);
523 String description = fileEntry.getDescription();
524 String versionDescription = StringPool.BLANK;
525 String extraSettings = fileEntry.getExtraSettings();
526 byte[] bytes = null;
527
528 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
529 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
530
531 ServiceContext serviceContext = new ServiceContext();
532
533 serviceContext.setAssetTagNames(assetTagNames);
534
535 int status = HttpServletResponse.SC_CREATED;
536
537 if (overwrite) {
538 if (deleteResource(
539 groupId, parentFolderId, title,
540 webDavRequest.getLockUuid())) {
541
542 status = HttpServletResponse.SC_NO_CONTENT;
543 }
544 }
545
546
548 if (webDavRequest.isMac()) {
549 try {
550 DLFileEntry destFileEntry =
551 DLFileEntryServiceUtil.getFileEntryByTitle(
552 groupId, parentFolderId, title);
553
554 InputStream is =
555 DLFileEntryLocalServiceUtil.getFileAsStream(
556 fileEntry.getCompanyId(), userId,
557 fileEntry.getGroupId(), fileEntry.getFolderId(),
558 fileEntry.getName());
559
560 bytes = FileUtil.getBytes(is);
561
562 DLFileEntryServiceUtil.updateFileEntry(
563 groupId, parentFolderId, parentFolderId,
564 destFileEntry.getName(), destFileEntry.getTitle(),
565 destFileEntry.getTitle(),
566 destFileEntry.getDescription(), versionDescription,
567 false, destFileEntry.getExtraSettings(), bytes,
568 serviceContext);
569
570 DLFileEntryServiceUtil.deleteFileEntry(
571 fileEntry.getGroupId(), fileEntry.getFolderId(),
572 fileEntry.getName());
573
574 return status;
575 }
576 catch (NoSuchFileEntryException nsfee) {
577 }
578 }
579
580 DLFileEntryServiceUtil.updateFileEntry(
581 fileEntry.getGroupId(), fileEntry.getFolderId(), parentFolderId,
582 name, sourceFileName, title, description, versionDescription,
583 false, extraSettings, bytes, serviceContext);
584
585 return status;
586 }
587 catch (PrincipalException pe) {
588 return HttpServletResponse.SC_FORBIDDEN;
589 }
590 catch (DuplicateFileException dfe) {
591 return HttpServletResponse.SC_PRECONDITION_FAILED;
592 }
593 catch (DuplicateFolderNameException dfne) {
594 return HttpServletResponse.SC_PRECONDITION_FAILED;
595 }
596 catch (LockException le) {
597 return WebDAVUtil.SC_LOCKED;
598 }
599 catch (Exception e) {
600 throw new WebDAVException(e);
601 }
602 }
603
604 public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
605 File file = null;
606
607 try {
608 HttpServletRequest request = webDavRequest.getHttpServletRequest();
609
610 String[] pathArray = webDavRequest.getPathArray();
611
612 long groupId = webDavRequest.getGroupId();
613 long parentFolderId = getParentFolderId(pathArray);
614 String name = WebDAVUtil.getResourceName(pathArray);
615 String title = name;
616 String description = StringPool.BLANK;
617 String versionDescription = StringPool.BLANK;
618 String extraSettings = StringPool.BLANK;
619
620 ServiceContext serviceContext = new ServiceContext();
621
622 serviceContext.setAddCommunityPermissions(
623 isAddCommunityPermissions(groupId));
624 serviceContext.setAddGuestPermissions(true);
625
626 try {
627 DLFileEntry entry = DLFileEntryServiceUtil.getFileEntryByTitle(
628 groupId, parentFolderId, name);
629
630 if (isLocked(entry, webDavRequest.getLockUuid())) {
631 return WebDAVUtil.SC_LOCKED;
632 }
633
634 name = entry.getName();
635 description = entry.getDescription();
636 extraSettings = entry.getExtraSettings();
637
638 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
639 DLFileEntry.class.getName(), entry.getFileEntryId());
640
641 serviceContext.setAssetTagNames(assetTagNames);
642
643 file = FileUtil.createTempFile(FileUtil.getExtension(name));
644
645 FileUtil.write(file, request.getInputStream());
646
647 DLFileEntryServiceUtil.updateFileEntry(
648 groupId, parentFolderId, parentFolderId, name, title, title,
649 description, versionDescription, false, extraSettings, file,
650 serviceContext);
651 }
652 catch (NoSuchFileEntryException nsfee) {
653 file = FileUtil.createTempFile(FileUtil.getExtension(name));
654
655 FileUtil.write(file, request.getInputStream());
656
657 DLFileEntryServiceUtil.addFileEntry(
658 groupId, parentFolderId, name, title, description,
659 versionDescription, extraSettings, file, serviceContext);
660 }
661
662 return HttpServletResponse.SC_CREATED;
663 }
664 catch (PrincipalException pe) {
665 return HttpServletResponse.SC_FORBIDDEN;
666 }
667 catch (NoSuchFolderException nsfe) {
668 return HttpServletResponse.SC_CONFLICT;
669 }
670 catch (PortalException pe) {
671 if (_log.isWarnEnabled()) {
672 _log.warn(pe, pe);
673 }
674
675 return HttpServletResponse.SC_CONFLICT;
676 }
677 catch (Exception e) {
678 throw new WebDAVException(e);
679 }
680 finally {
681 if (file != null) {
682 file.delete();
683 }
684 }
685 }
686
687 public Lock refreshResourceLock(
688 WebDAVRequest webDavRequest, String uuid, long timeout)
689 throws WebDAVException {
690
691 Resource resource = getResource(webDavRequest);
692
693 Lock lock = null;
694
695 try {
696 if (resource instanceof DLFileEntryResourceImpl) {
697 lock = DLFileEntryServiceUtil.refreshFileEntryLock(
698 uuid, timeout);
699 }
700 else {
701 lock = DLFolderServiceUtil.refreshFolderLock(uuid, timeout);
702 }
703 }
704 catch (Exception e) {
705 throw new WebDAVException(e);
706 }
707
708 return lock;
709 }
710
711 public boolean unlockResource(WebDAVRequest webDavRequest, String token)
712 throws WebDAVException {
713
714 Resource resource = getResource(webDavRequest);
715
716 try {
717 if (resource instanceof DLFileEntryResourceImpl) {
718 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
719
720 DLFileEntryServiceUtil.unlockFileEntry(
721 fileEntry.getGroupId(), fileEntry.getFolderId(),
722 fileEntry.getName(), token);
723 }
724 else {
725 DLFolder folder = (DLFolder)resource.getModel();
726
727 DLFolderServiceUtil.unlockFolder(
728 folder.getGroupId(), folder.getParentFolderId(),
729 folder.getName(), token);
730 }
731
732 return true;
733 }
734 catch (Exception e) {
735 if (e instanceof InvalidLockException) {
736 if (_log.isWarnEnabled()) {
737 _log.warn(e.getMessage());
738 }
739 }
740 else {
741 if (_log.isWarnEnabled()) {
742 _log.warn("Unable to unlock file entry", e);
743 }
744 }
745 }
746
747 return false;
748 }
749
750 protected boolean deleteResource(
751 long groupId, long parentFolderId, String name, String lockUuid)
752 throws Exception {
753
754 try {
755 DLFolder folder = DLFolderServiceUtil.getFolder(
756 groupId, parentFolderId, name);
757
758 DLFolderServiceUtil.deleteFolder(folder.getFolderId());
759
760 return true;
761 }
762 catch (NoSuchFolderException nsfe) {
763 try {
764 DLFileEntry fileEntry =
765 DLFileEntryServiceUtil.getFileEntryByTitle(
766 groupId, parentFolderId, name);
767
768 if (isLocked(fileEntry, lockUuid)) {
769 throw new LockException();
770 }
771
772 DLFileEntryServiceUtil.deleteFileEntryByTitle(
773 groupId, parentFolderId, name);
774
775 return true;
776 }
777 catch (NoSuchFileEntryException nsfee) {
778 }
779 }
780
781 return false;
782 }
783
784 protected List<Resource> getFileEntries(
785 WebDAVRequest webDavRequest, long parentFolderId)
786 throws Exception {
787
788 List<Resource> resources = new ArrayList<Resource>();
789
790 List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
791 webDavRequest.getGroupId(), parentFolderId);
792
793 for (DLFileEntry fileEntry : fileEntries) {
794 Resource resource = toResource(webDavRequest, fileEntry, true);
795
796 resources.add(resource);
797 }
798
799 return resources;
800 }
801
802 protected long getFolderId(String[] pathArray) throws Exception {
803 return getFolderId(pathArray, false);
804 }
805
806 protected long getFolderId(String[] pathArray, boolean parent)
807 throws Exception {
808
809 long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
810
811 if (pathArray.length <= 2) {
812 return folderId;
813 }
814 else {
815 long groupId = WebDAVUtil.getGroupId(pathArray);
816
817 int x = pathArray.length;
818
819 if (parent) {
820 x--;
821 }
822
823 for (int i = 3; i < x; i++) {
824 String name = pathArray[i];
825
826 DLFolder folder = DLFolderServiceUtil.getFolder(
827 groupId, folderId, name);
828
829 if (groupId == folder.getGroupId()) {
830 folderId = folder.getFolderId();
831 }
832 }
833 }
834
835 return folderId;
836 }
837
838 protected List<Resource> getFolders(
839 WebDAVRequest webDavRequest, long parentFolderId)
840 throws Exception {
841
842 List<Resource> resources = new ArrayList<Resource>();
843
844 long groupId = webDavRequest.getGroupId();
845
846 List<DLFolder> folders = DLFolderServiceUtil.getFolders(
847 groupId, parentFolderId);
848
849 for (DLFolder folder : folders) {
850 Resource resource = toResource(webDavRequest, folder, true);
851
852 resources.add(resource);
853 }
854
855 return resources;
856 }
857
858 protected long getParentFolderId(String[] pathArray) throws Exception {
859 return getFolderId(pathArray, true);
860 }
861
862 protected boolean isLocked(DLFileEntry fileEntry, String lockUuid)
863 throws Exception {
864
865 long groupId = fileEntry.getGroupId();
866 long parentFolderId = fileEntry.getFolderId();
867 String fileName = fileEntry.getName();
868
869 if (Validator.isNull(lockUuid)) {
870
871
873 return DLFileEntryServiceUtil.hasFileEntryLock(
874 groupId, parentFolderId, fileName);
875 }
876 else {
877
878
880 try {
881 boolean verified = DLFileEntryServiceUtil.verifyFileEntryLock(
882 groupId, parentFolderId, fileName, lockUuid);
883
884 return !verified;
885 }
886 catch (NoSuchLockException nsle) {
887 return false;
888 }
889 }
890 }
891
892 protected Resource toResource(
893 WebDAVRequest webDavRequest, DLFileEntry fileEntry,
894 boolean appendPath) {
895
896 String parentPath = getRootPath() + webDavRequest.getPath();
897 String name = StringPool.BLANK;
898
899 if (appendPath) {
900 name = fileEntry.getTitle();
901 }
902
903 return new DLFileEntryResourceImpl(
904 webDavRequest, fileEntry, parentPath, name);
905 }
906
907 protected Resource toResource(
908 WebDAVRequest webDavRequest, DLFolder folder, boolean appendPath) {
909
910 String parentPath = getRootPath() + webDavRequest.getPath();
911 String name = StringPool.BLANK;
912
913 if (appendPath) {
914 name = folder.getName();
915 }
916
917 Resource resource = new BaseResourceImpl(
918 parentPath, name, folder.getName(), folder.getCreateDate(),
919 folder.getModifiedDate());
920
921 resource.setModel(folder);
922 resource.setClassName(DLFolder.class.getName());
923 resource.setPrimaryKey(folder.getPrimaryKey());
924
925 return resource;
926 }
927
928 private static Log _log = LogFactoryUtil.getLog(DLWebDAVStorageImpl.class);
929
930 }