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.InvalidLockException;
28  import com.liferay.portal.NoSuchLockException;
29  import com.liferay.portal.PortalException;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.kernel.util.FileUtil;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.kernel.util.Validator;
36  import com.liferay.portal.model.Lock;
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  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              String name = WebDAVUtil.getResourceName(destinationArray);
97              String description = folder.getDescription();
98  
99              ServiceContext serviceContext = new ServiceContext();
100 
101             serviceContext.setAddCommunityPermissions(true);
102             serviceContext.setAddGuestPermissions(true);
103 
104             int status = HttpServletResponse.SC_CREATED;
105 
106             if (overwrite) {
107                 if (deleteResource(
108                         groupId, parentFolderId, name,
109                         webDavRequest.getLockUuid())) {
110 
111                     status = HttpServletResponse.SC_NO_CONTENT;
112                 }
113             }
114 
115             if (depth == 0) {
116                 DLFolderServiceUtil.addFolder(
117                     groupId, parentFolderId, name, description, serviceContext);
118             }
119             else {
120                 DLFolderServiceUtil.copyFolder(
121                     groupId, folder.getFolderId(), parentFolderId, name,
122                     description, serviceContext);
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 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             ServiceContext serviceContext = new ServiceContext();
177 
178             serviceContext.setAddCommunityPermissions(true);
179             serviceContext.setAddGuestPermissions(true);
180 
181             int status = HttpServletResponse.SC_CREATED;
182 
183             if (overwrite) {
184                 if (deleteResource(
185                         groupId, parentFolderId, title,
186                         webDavRequest.getLockUuid())) {
187 
188                     status = HttpServletResponse.SC_NO_CONTENT;
189                 }
190             }
191 
192             DLFileEntryServiceUtil.addFileEntry(
193                 parentFolderId, name, title, description, extraSettings, file,
194                 serviceContext);
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 extraSettings = StringPool.BLANK;
353 
354                 File file = FileUtil.createTempFile(
355                     FileUtil.getExtension(name));
356 
357                 file.createNewFile();
358 
359                 ServiceContext serviceContext = new ServiceContext();
360 
361                 serviceContext.setAddCommunityPermissions(true);
362                 serviceContext.setAddGuestPermissions(true);
363 
364                 DLFileEntry fileEntry = DLFileEntryServiceUtil.addFileEntry(
365                     parentFolderId, name, title, description, extraSettings,
366                     file, serviceContext);
367 
368                 resource = toResource(webDavRequest, fileEntry, false);
369             }
370 
371             if (resource instanceof DLFileEntryResourceImpl) {
372                 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
373 
374                 lock = DLFileEntryServiceUtil.lockFileEntry(
375                     fileEntry.getFolderId(), fileEntry.getName(), owner,
376                     timeout);
377             }
378             else {
379                 boolean inheritable = false;
380 
381                 long depth = WebDAVUtil.getDepth(
382                     webDavRequest.getHttpServletRequest());
383 
384                 if (depth != 0) {
385                     inheritable = true;
386                 }
387 
388                 DLFolder folder = (DLFolder)resource.getModel();
389 
390                 lock = DLFolderServiceUtil.lockFolder(
391                     folder.getFolderId(), owner, inheritable, timeout);
392             }
393         }
394         catch (Exception e) {
395 
396             // DuplicateLock is 423 not 501
397 
398             if (!(e instanceof DuplicateLockException)) {
399                 throw new WebDAVException(e);
400             }
401 
402             status = WebDAVUtil.SC_LOCKED;
403         }
404 
405         return new Status(lock, status);
406     }
407 
408     public Status makeCollection(WebDAVRequest webDavRequest)
409         throws WebDAVException {
410 
411         try {
412             HttpServletRequest request = webDavRequest.getHttpServletRequest();
413 
414             if (request.getContentLength() > 0) {
415                 return new Status(
416                     HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
417             }
418 
419             String[] pathArray = webDavRequest.getPathArray();
420 
421             long parentFolderId = getParentFolderId(pathArray);
422             String name = WebDAVUtil.getResourceName(pathArray);
423             String description = StringPool.BLANK;
424 
425             ServiceContext serviceContext = new ServiceContext();
426 
427             serviceContext.setAddCommunityPermissions(true);
428             serviceContext.setAddGuestPermissions(true);
429 
430             DLFolderServiceUtil.addFolder(
431                 webDavRequest.getGroupId(), parentFolderId, name, description,
432                 serviceContext);
433 
434             String location = StringUtil.merge(pathArray, StringPool.SLASH);
435 
436             return new Status(location, HttpServletResponse.SC_CREATED);
437         }
438         catch (DuplicateFolderNameException dfne) {
439             return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
440         }
441         catch (NoSuchFolderException nsfe) {
442             return new Status(HttpServletResponse.SC_CONFLICT);
443         }
444         catch (PrincipalException pe) {
445             return new Status(HttpServletResponse.SC_FORBIDDEN);
446         }
447         catch (Exception e) {
448             throw new WebDAVException(e);
449         }
450     }
451 
452     public int moveCollectionResource(
453             WebDAVRequest webDavRequest, Resource resource, String destination,
454             boolean overwrite)
455         throws WebDAVException {
456 
457         try {
458             String[] destinationArray = WebDAVUtil.getPathArray(
459                 destination, true);
460 
461             DLFolder folder = (DLFolder)resource.getModel();
462 
463             long groupId = WebDAVUtil.getGroupId(destinationArray);
464             long folderId = folder.getFolderId();
465             long parentFolderId = getParentFolderId(destinationArray);
466             String name = WebDAVUtil.getResourceName(destinationArray);
467             String description = folder.getDescription();
468 
469             ServiceContext serviceContext = new ServiceContext();
470 
471             int status = HttpServletResponse.SC_CREATED;
472 
473             if (overwrite) {
474                 if (deleteResource(
475                         groupId, parentFolderId, name,
476                         webDavRequest.getLockUuid())) {
477 
478                     status = HttpServletResponse.SC_NO_CONTENT;
479                 }
480             }
481 
482             DLFolderServiceUtil.updateFolder(
483                 folderId, parentFolderId, name, description, serviceContext);
484 
485             return status;
486         }
487         catch (PrincipalException pe) {
488             return HttpServletResponse.SC_FORBIDDEN;
489         }
490         catch (DuplicateFolderNameException dfne) {
491             return HttpServletResponse.SC_PRECONDITION_FAILED;
492         }
493         catch (Exception e) {
494             throw new WebDAVException(e);
495         }
496     }
497 
498     public int moveSimpleResource(
499             WebDAVRequest webDavRequest, Resource resource, String destination,
500             boolean overwrite)
501         throws WebDAVException {
502 
503         try {
504             String[] destinationArray = WebDAVUtil.getPathArray(
505                 destination, true);
506 
507             DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
508 
509             if (isLocked(fileEntry, webDavRequest.getLockUuid())) {
510                 return WebDAVUtil.SC_LOCKED;
511             }
512 
513             long groupId = WebDAVUtil.getGroupId(destinationArray);
514             long parentFolderId = getParentFolderId(destinationArray);
515             String name = fileEntry.getName();
516             String sourceFileName = null;
517             String title = WebDAVUtil.getResourceName(destinationArray);
518             String description = fileEntry.getDescription();
519             String extraSettings = fileEntry.getExtraSettings();
520             byte[] bytes = null;
521 
522             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
523                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
524 
525             ServiceContext serviceContext = new ServiceContext();
526 
527             serviceContext.setTagsEntries(tagsEntries);
528 
529             int status = HttpServletResponse.SC_CREATED;
530 
531             if (overwrite) {
532                 if (deleteResource(
533                         groupId, parentFolderId, title,
534                         webDavRequest.getLockUuid())) {
535 
536                     status = HttpServletResponse.SC_NO_CONTENT;
537                 }
538             }
539 
540             DLFileEntryServiceUtil.updateFileEntry(
541                 fileEntry.getFolderId(), parentFolderId, name, sourceFileName,
542                 title, description, extraSettings, bytes, serviceContext);
543 
544             return status;
545         }
546         catch (PrincipalException pe) {
547             return HttpServletResponse.SC_FORBIDDEN;
548         }
549         catch (DuplicateFileException dfe) {
550             return HttpServletResponse.SC_PRECONDITION_FAILED;
551         }
552         catch (DuplicateFolderNameException dfne) {
553             return HttpServletResponse.SC_PRECONDITION_FAILED;
554         }
555         catch (LockException le) {
556             return WebDAVUtil.SC_LOCKED;
557         }
558         catch (Exception e) {
559             throw new WebDAVException(e);
560         }
561     }
562 
563     public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
564         File file = null;
565 
566         try {
567             HttpServletRequest request = webDavRequest.getHttpServletRequest();
568 
569             String[] pathArray = webDavRequest.getPathArray();
570 
571             long parentFolderId = getParentFolderId(pathArray);
572             String name = WebDAVUtil.getResourceName(pathArray);
573             String title = name;
574             String description = StringPool.BLANK;
575             String extraSettings = StringPool.BLANK;
576 
577             ServiceContext serviceContext = new ServiceContext();
578 
579             serviceContext.setAddCommunityPermissions(true);
580             serviceContext.setAddGuestPermissions(true);
581 
582             try {
583                 DLFileEntry entry = DLFileEntryServiceUtil.getFileEntryByTitle(
584                     parentFolderId, name);
585 
586                 if (isLocked(entry, webDavRequest.getLockUuid())) {
587                     return WebDAVUtil.SC_LOCKED;
588                 }
589 
590                 name = entry.getName();
591                 description = entry.getDescription();
592                 extraSettings = entry.getExtraSettings();
593 
594                 String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
595                     DLFileEntry.class.getName(), entry.getFileEntryId());
596 
597                 serviceContext.setTagsEntries(tagsEntries);
598 
599                 DLFileEntryServiceUtil.updateFileEntry(
600                     parentFolderId, parentFolderId, name, title, title,
601                     description, extraSettings,
602                     FileUtil.getBytes(request.getInputStream()),
603                     serviceContext);
604             }
605             catch (NoSuchFileEntryException nsfee) {
606                 file = FileUtil.createTempFile(FileUtil.getExtension(name));
607 
608                 FileUtil.write(file, request.getInputStream());
609 
610                 DLFileEntryServiceUtil.addFileEntry(
611                     parentFolderId, name, title, description, extraSettings,
612                     file, serviceContext);
613             }
614 
615             return HttpServletResponse.SC_CREATED;
616         }
617         catch (PrincipalException pe) {
618             return HttpServletResponse.SC_FORBIDDEN;
619         }
620         catch (PortalException pe) {
621             if (_log.isWarnEnabled()) {
622                 _log.warn(pe, pe);
623             }
624 
625             return HttpServletResponse.SC_CONFLICT;
626         }
627         catch (Exception e) {
628             throw new WebDAVException(e);
629         }
630         finally {
631             if (file != null) {
632                 file.delete();
633             }
634         }
635     }
636 
637     public Lock refreshResourceLock(
638             WebDAVRequest webDavRequest, String uuid, long timeout)
639         throws WebDAVException {
640 
641         Resource resource = getResource(webDavRequest);
642 
643         Lock lock = null;
644 
645         try {
646             if (resource instanceof DLFileEntryResourceImpl) {
647                 lock = DLFileEntryServiceUtil.refreshFileEntryLock(
648                     uuid, timeout);
649             }
650             else {
651                 lock = DLFolderServiceUtil.refreshFolderLock(uuid, timeout);
652             }
653         }
654         catch (Exception e) {
655             throw new WebDAVException(e);
656         }
657 
658         return lock;
659     }
660 
661     public boolean unlockResource(WebDAVRequest webDavRequest, String token)
662         throws WebDAVException {
663 
664         Resource resource = getResource(webDavRequest);
665 
666         try {
667             if (resource instanceof DLFileEntryResourceImpl) {
668                 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
669 
670                 DLFileEntryServiceUtil.unlockFileEntry(
671                     fileEntry.getFolderId(), fileEntry.getName(), token);
672             }
673             else {
674                 DLFolder folder = (DLFolder)resource.getModel();
675 
676                 DLFolderServiceUtil.unlockFolder(
677                     folder.getGroupId(), folder.getParentFolderId(),
678                     folder.getName(), token);
679             }
680 
681             return true;
682         }
683         catch (Exception e) {
684             if (e instanceof InvalidLockException) {
685                 if (_log.isWarnEnabled()) {
686                     _log.warn(e.getMessage());
687                 }
688             }
689             else {
690                 if (_log.isWarnEnabled()) {
691                     _log.warn("Unable to unlock file entry", e);
692                 }
693             }
694         }
695 
696         return false;
697     }
698 
699     protected boolean deleteResource(
700             long groupId, long parentFolderId, String name, String lockUuid)
701         throws Exception {
702 
703         try {
704             DLFolder folder = DLFolderServiceUtil.getFolder(
705                 groupId, parentFolderId, name);
706 
707             DLFolderServiceUtil.deleteFolder(folder.getFolderId());
708 
709             return true;
710         }
711         catch (NoSuchFolderException nsfe) {
712             try {
713                 DLFileEntry fileEntry =
714                     DLFileEntryServiceUtil.getFileEntryByTitle(
715                         parentFolderId, name);
716 
717                 if (isLocked(fileEntry, lockUuid)) {
718                     throw new LockException();
719                 }
720 
721                 DLFileEntryServiceUtil.deleteFileEntryByTitle(
722                     parentFolderId, name);
723 
724                 return true;
725             }
726             catch (NoSuchFileEntryException nsfee) {
727             }
728         }
729 
730         return false;
731     }
732 
733     protected List<Resource> getFileEntries(
734             WebDAVRequest webDavRequest, long parentFolderId)
735         throws Exception {
736 
737         List<Resource> resources = new ArrayList<Resource>();
738 
739         List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
740             parentFolderId);
741 
742         for (DLFileEntry fileEntry : fileEntries) {
743             Resource resource = toResource(webDavRequest, fileEntry, true);
744 
745             resources.add(resource);
746         }
747 
748         return resources;
749     }
750 
751     protected long getFolderId(String[] pathArray) throws Exception {
752         return getFolderId(pathArray, false);
753     }
754 
755     protected long getFolderId(String[] pathArray, boolean parent)
756         throws Exception {
757 
758         long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
759 
760         if (pathArray.length <= 2) {
761             return folderId;
762         }
763         else {
764             long groupId = WebDAVUtil.getGroupId(pathArray);
765 
766             int x = pathArray.length;
767 
768             if (parent) {
769                 x--;
770             }
771 
772             for (int i = 3; i < x; i++) {
773                 String name = pathArray[i];
774 
775                 DLFolder folder = DLFolderServiceUtil.getFolder(
776                     groupId, folderId, name);
777 
778                 if (groupId == folder.getGroupId()) {
779                     folderId = folder.getFolderId();
780                 }
781             }
782         }
783 
784         return folderId;
785     }
786 
787     protected List<Resource> getFolders(
788             WebDAVRequest webDavRequest, long parentFolderId)
789         throws Exception {
790 
791         List<Resource> resources = new ArrayList<Resource>();
792 
793         long groupId = webDavRequest.getGroupId();
794 
795         List<DLFolder> folders = DLFolderServiceUtil.getFolders(
796             groupId, parentFolderId);
797 
798         for (DLFolder folder : folders) {
799             Resource resource = toResource(webDavRequest, folder, true);
800 
801             resources.add(resource);
802         }
803 
804         return resources;
805     }
806 
807     protected long getParentFolderId(String[] pathArray) throws Exception {
808         return getFolderId(pathArray, true);
809     }
810 
811     protected boolean isLocked(DLFileEntry fileEntry, String lockUuid)
812         throws Exception {
813 
814         long parentFolderId = fileEntry.getFolderId();
815         String fileName = fileEntry.getName();
816 
817         if (Validator.isNull(lockUuid)) {
818 
819             // Client does not claim to know of a lock
820 
821             return DLFileEntryServiceUtil.hasFileEntryLock(
822                 parentFolderId, fileName);
823         }
824         else {
825 
826             // Client claims to know of a lock. Verify the lock UUID.
827 
828             try {
829                 boolean verified = DLFileEntryServiceUtil.verifyFileEntryLock(
830                     parentFolderId, fileName, lockUuid);
831 
832                 return !verified;
833             }
834             catch (NoSuchLockException nsle) {
835                 return false;
836             }
837         }
838     }
839 
840     protected Resource toResource(
841         WebDAVRequest webDavRequest, DLFileEntry fileEntry,
842         boolean appendPath) {
843 
844         String parentPath = getRootPath() + webDavRequest.getPath();
845         String name = StringPool.BLANK;
846 
847         if (appendPath) {
848             name = fileEntry.getTitleWithExtension();
849         }
850 
851         return new DLFileEntryResourceImpl(
852             webDavRequest, fileEntry, parentPath, name);
853     }
854 
855     protected Resource toResource(
856         WebDAVRequest webDavRequest, DLFolder folder, boolean appendPath) {
857 
858         String parentPath = getRootPath() + webDavRequest.getPath();
859         String name = StringPool.BLANK;
860 
861         if (appendPath) {
862             name = folder.getName();
863         }
864 
865         Resource resource = new BaseResourceImpl(
866             parentPath, name, folder.getName(), folder.getCreateDate(),
867             folder.getModifiedDate());
868 
869         resource.setModel(folder);
870         resource.setClassName(DLFolder.class.getName());
871         resource.setPrimaryKey(folder.getPrimaryKey());
872 
873         return resource;
874     }
875 
876     private static Log _log = LogFactoryUtil.getLog(DLWebDAVStorageImpl.class);
877 
878 }