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