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