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