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