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