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