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