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