1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.kernel.exception.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.kernel.workflow.StatusConstants;
29  import com.liferay.portal.model.Lock;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.service.ServiceContext;
32  import com.liferay.portal.util.PropsValues;
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.asset.service.AssetTagLocalServiceUtil;
42  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
43  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
44  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
45  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
46  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
47  import com.liferay.portlet.documentlibrary.model.DLFolder;
48  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
49  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
50  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
51  import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
52  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
53  
54  import java.io.File;
55  import java.io.InputStream;
56  
57  import java.util.ArrayList;
58  import java.util.List;
59  
60  import javax.servlet.http.HttpServletRequest;
61  import javax.servlet.http.HttpServletResponse;
62  
63  /**
64   * <a href="DLWebDAVStorageImpl.java.html"><b><i>View Source</i></b></a>
65   *
66   * @author Brian Wing Shun Chan
67   * @author Alexander Chow
68   */
69  public class DLWebDAVStorageImpl extends BaseWebDAVStorageImpl {
70  
71      public int copyCollectionResource(
72              WebDAVRequest webDavRequest, Resource resource, String destination,
73              boolean overwrite, long depth)
74          throws WebDAVException {
75  
76          try {
77              String[] destinationArray = WebDAVUtil.getPathArray(
78                  destination, true);
79  
80              long companyId = webDavRequest.getCompanyId();
81  
82              long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
83  
84              try {
85                  parentFolderId = getParentFolderId(companyId, 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(companyId, destination);
94              String name = WebDAVUtil.getResourceName(destinationArray);
95              String description = folder.getDescription();
96  
97              ServiceContext serviceContext = new ServiceContext();
98  
99              serviceContext.setAddCommunityPermissions(
100                 isAddCommunityPermissions(groupId));
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 companyId = webDavRequest.getCompanyId();
149 
150             long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
151 
152             try {
153                 parentFolderId = getParentFolderId(companyId, destinationArray);
154             }
155             catch (NoSuchFolderException nsfe) {
156                 return HttpServletResponse.SC_CONFLICT;
157             }
158 
159             DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
160 
161             long groupId = WebDAVUtil.getGroupId(companyId, destination);
162             long userId = webDavRequest.getUserId();
163             String name = WebDAVUtil.getResourceName(destinationArray);
164             String title = WebDAVUtil.getResourceName(destinationArray);
165             String description = fileEntry.getDescription();
166             String versionDescription = StringPool.BLANK;
167             String extraSettings = fileEntry.getExtraSettings();
168 
169             file = FileUtil.createTempFile(
170                 FileUtil.getExtension(fileEntry.getName()));
171 
172             InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
173                 fileEntry.getCompanyId(), userId, fileEntry.getGroupId(),
174                 fileEntry.getFolderId(), fileEntry.getName());
175 
176             FileUtil.write(file, is);
177 
178             ServiceContext serviceContext = new ServiceContext();
179 
180             serviceContext.setAddCommunityPermissions(
181                 isAddCommunityPermissions(groupId));
182             serviceContext.setAddGuestPermissions(true);
183 
184             int status = HttpServletResponse.SC_CREATED;
185 
186             if (overwrite) {
187                 if (deleteResource(
188                         groupId, parentFolderId, title,
189                         webDavRequest.getLockUuid())) {
190 
191                     status = HttpServletResponse.SC_NO_CONTENT;
192                 }
193             }
194 
195             DLFileEntryServiceUtil.addFileEntry(
196                 groupId, parentFolderId, name, title, description,
197                 versionDescription, extraSettings, file, serviceContext);
198 
199             return status;
200         }
201         catch (DuplicateFileException dfe) {
202             return HttpServletResponse.SC_PRECONDITION_FAILED;
203         }
204         catch (DuplicateFolderNameException dfne) {
205             return HttpServletResponse.SC_PRECONDITION_FAILED;
206         }
207         catch (LockException le) {
208             return WebDAVUtil.SC_LOCKED;
209         }
210         catch (PrincipalException pe) {
211             return HttpServletResponse.SC_FORBIDDEN;
212         }
213         catch (Exception e) {
214             throw new WebDAVException(e);
215         }
216         finally {
217             if (file != null) {
218                 file.delete();
219             }
220         }
221     }
222 
223     public int deleteResource(WebDAVRequest webDavRequest)
224         throws WebDAVException {
225 
226         try {
227             Resource resource = getResource(webDavRequest);
228 
229             if (resource == null) {
230                 return HttpServletResponse.SC_NOT_FOUND;
231             }
232 
233             Object model = resource.getModel();
234 
235             if (model instanceof DLFolder) {
236                 DLFolder folder = (DLFolder)model;
237 
238                 DLFolderServiceUtil.deleteFolder(folder.getFolderId());
239             }
240             else {
241                 DLFileEntry fileEntry = (DLFileEntry)model;
242 
243                 if (isLocked(fileEntry, webDavRequest.getLockUuid())) {
244                     return WebDAVUtil.SC_LOCKED;
245                 }
246 
247                 DLFileEntryServiceUtil.deleteFileEntry(
248                     fileEntry.getGroupId(), fileEntry.getFolderId(),
249                     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 companyId = webDavRequest.getCompanyId();
269             long parentFolderId = getParentFolderId(companyId, pathArray);
270             String name = WebDAVUtil.getResourceName(pathArray);
271 
272             if (Validator.isNull(name)) {
273                 String path = getRootPath() + webDavRequest.getPath();
274 
275                 return new BaseResourceImpl(path, StringPool.BLANK, getToken());
276             }
277 
278             try {
279                 DLFolder folder = DLFolderServiceUtil.getFolder(
280                     webDavRequest.getGroupId(), parentFolderId, name);
281 
282                 if ((folder.getParentFolderId() != parentFolderId) ||
283                     (webDavRequest.getGroupId() != folder.getGroupId())) {
284 
285                     throw new NoSuchFolderException();
286                 }
287 
288                 return toResource(webDavRequest, folder, false);
289             }
290             catch (NoSuchFolderException nsfe) {
291                 try {
292                     String titleWithExtension = name;
293 
294                     DLFileEntry fileEntry =
295                         DLFileEntryServiceUtil.getFileEntryByTitle(
296                             webDavRequest.getGroupId(), parentFolderId,
297                             titleWithExtension);
298 
299                     return toResource(webDavRequest, fileEntry, false);
300                 }
301                 catch (NoSuchFileEntryException nsfee) {
302                     return null;
303                 }
304             }
305         }
306         catch (Exception e) {
307             throw new WebDAVException(e);
308         }
309     }
310 
311     public List<Resource> getResources(WebDAVRequest webDavRequest)
312         throws WebDAVException {
313 
314         try {
315             long folderId = getFolderId(
316                 webDavRequest.getCompanyId(), webDavRequest.getPathArray());
317 
318             List<Resource> folders = getFolders(webDavRequest, folderId);
319             List<Resource> fileEntries = getFileEntries(
320                 webDavRequest, folderId);
321 
322             List<Resource> resources = new ArrayList<Resource>(
323                 folders.size() + fileEntries.size());
324 
325             resources.addAll(folders);
326             resources.addAll(fileEntries);
327 
328             return resources;
329         }
330         catch (Exception e) {
331             throw new WebDAVException(e);
332         }
333     }
334 
335     public boolean isSupportsClassTwo() {
336         return true;
337     }
338 
339     public Status lockResource(
340             WebDAVRequest webDavRequest, String owner, long timeout)
341         throws WebDAVException {
342 
343         Resource resource = getResource(webDavRequest);
344 
345         Lock lock = null;
346         int status = HttpServletResponse.SC_OK;
347 
348         try {
349             if (resource == null) {
350                 status = HttpServletResponse.SC_CREATED;
351 
352                 String[] pathArray = webDavRequest.getPathArray();
353 
354                 long companyId = webDavRequest.getCompanyId();
355                 long groupId = webDavRequest.getGroupId();
356                 long parentFolderId = getParentFolderId(companyId, pathArray);
357                 String name = WebDAVUtil.getResourceName(pathArray);
358 
359                 String title = name;
360                 String description = StringPool.BLANK;
361                 String versionDescription = StringPool.BLANK;
362                 String extraSettings = StringPool.BLANK;
363 
364                 File file = FileUtil.createTempFile(
365                     FileUtil.getExtension(name));
366 
367                 file.createNewFile();
368 
369                 ServiceContext serviceContext = new ServiceContext();
370 
371                 serviceContext.setAddCommunityPermissions(
372                     isAddCommunityPermissions(groupId));
373                 serviceContext.setAddGuestPermissions(true);
374 
375                 DLFileEntry fileEntry = DLFileEntryServiceUtil.addFileEntry(
376                     groupId, parentFolderId, name, title, description,
377                     versionDescription, extraSettings, file, serviceContext);
378 
379                 resource = toResource(webDavRequest, fileEntry, false);
380             }
381 
382             if (resource instanceof DLFileEntryResourceImpl) {
383                 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
384 
385                 lock = DLFileEntryServiceUtil.lockFileEntry(
386                     fileEntry.getGroupId(), fileEntry.getFolderId(),
387                     fileEntry.getName(), owner, timeout);
388             }
389             else {
390                 boolean inheritable = false;
391 
392                 long depth = WebDAVUtil.getDepth(
393                     webDavRequest.getHttpServletRequest());
394 
395                 if (depth != 0) {
396                     inheritable = true;
397                 }
398 
399                 DLFolder folder = (DLFolder)resource.getModel();
400 
401                 lock = DLFolderServiceUtil.lockFolder(
402                     folder.getFolderId(), owner, inheritable, timeout);
403             }
404         }
405         catch (Exception e) {
406 
407             // DuplicateLock is 423 not 501
408 
409             if (!(e instanceof DuplicateLockException)) {
410                 throw new WebDAVException(e);
411             }
412 
413             status = WebDAVUtil.SC_LOCKED;
414         }
415 
416         return new Status(lock, status);
417     }
418 
419     public Status makeCollection(WebDAVRequest webDavRequest)
420         throws WebDAVException {
421 
422         try {
423             HttpServletRequest request = webDavRequest.getHttpServletRequest();
424 
425             if (request.getContentLength() > 0) {
426                 return new Status(
427                     HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
428             }
429 
430             String[] pathArray = webDavRequest.getPathArray();
431 
432             long companyId = webDavRequest.getCompanyId();
433             long groupId = webDavRequest.getGroupId();
434             long parentFolderId = getParentFolderId(companyId, pathArray);
435             String name = WebDAVUtil.getResourceName(pathArray);
436             String description = StringPool.BLANK;
437 
438             ServiceContext serviceContext = new ServiceContext();
439 
440             serviceContext.setAddCommunityPermissions(
441                 isAddCommunityPermissions(groupId));
442             serviceContext.setAddGuestPermissions(true);
443 
444             DLFolderServiceUtil.addFolder(
445                 groupId, parentFolderId, name, description, serviceContext);
446 
447             String location = StringUtil.merge(pathArray, StringPool.SLASH);
448 
449             return new Status(location, HttpServletResponse.SC_CREATED);
450         }
451         catch (DuplicateFolderNameException dfne) {
452             return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
453         }
454         catch (DuplicateFileException dfe) {
455             return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
456         }
457         catch (NoSuchFolderException nsfe) {
458             return new Status(HttpServletResponse.SC_CONFLICT);
459         }
460         catch (PrincipalException pe) {
461             return new Status(HttpServletResponse.SC_FORBIDDEN);
462         }
463         catch (Exception e) {
464             throw new WebDAVException(e);
465         }
466     }
467 
468     public int moveCollectionResource(
469             WebDAVRequest webDavRequest, Resource resource, String destination,
470             boolean overwrite)
471         throws WebDAVException {
472 
473         try {
474             String[] destinationArray = WebDAVUtil.getPathArray(
475                 destination, true);
476 
477             DLFolder folder = (DLFolder)resource.getModel();
478 
479             long companyId = webDavRequest.getCompanyId();
480             long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
481             long folderId = folder.getFolderId();
482             long parentFolderId = getParentFolderId(
483                 companyId, 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 companyId = webDavRequest.getCompanyId();
532             long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
533             long userId = webDavRequest.getUserId();
534             long parentFolderId = getParentFolderId(
535                 companyId, destinationArray);
536             String name = fileEntry.getName();
537             String sourceFileName = null;
538             String title = WebDAVUtil.getResourceName(destinationArray);
539             String description = fileEntry.getDescription();
540             String versionDescription = StringPool.BLANK;
541             String extraSettings = fileEntry.getExtraSettings();
542             byte[] bytes = null;
543 
544             String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
545                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
546 
547             ServiceContext serviceContext = new ServiceContext();
548 
549             serviceContext.setAssetTagNames(assetTagNames);
550 
551             int status = HttpServletResponse.SC_CREATED;
552 
553             if (overwrite) {
554                 if (deleteResource(
555                         groupId, parentFolderId, title,
556                         webDavRequest.getLockUuid())) {
557 
558                     status = HttpServletResponse.SC_NO_CONTENT;
559                 }
560             }
561 
562             // LPS-5415
563 
564             if (webDavRequest.isMac()) {
565                 try {
566                     DLFileEntry destFileEntry =
567                         DLFileEntryServiceUtil.getFileEntryByTitle(
568                             groupId, parentFolderId, title);
569 
570                     InputStream is =
571                         DLFileEntryLocalServiceUtil.getFileAsStream(
572                             fileEntry.getCompanyId(), userId,
573                             fileEntry.getGroupId(), fileEntry.getFolderId(),
574                             fileEntry.getName());
575 
576                     bytes = FileUtil.getBytes(is);
577 
578                     DLFileEntryServiceUtil.updateFileEntry(
579                         groupId, parentFolderId, parentFolderId,
580                         destFileEntry.getName(), destFileEntry.getTitle(),
581                         destFileEntry.getTitle(),
582                         destFileEntry.getDescription(), versionDescription,
583                         false, destFileEntry.getExtraSettings(), bytes,
584                         serviceContext);
585 
586                     DLFileEntryServiceUtil.deleteFileEntry(
587                         fileEntry.getGroupId(), fileEntry.getFolderId(),
588                         fileEntry.getName());
589 
590                     return status;
591                 }
592                 catch (NoSuchFileEntryException nsfee) {
593                 }
594             }
595 
596             DLFileEntryServiceUtil.updateFileEntry(
597                 fileEntry.getGroupId(), fileEntry.getFolderId(), parentFolderId,
598                 name, sourceFileName, title, description, versionDescription,
599                 false, extraSettings, bytes, serviceContext);
600 
601             return status;
602         }
603         catch (PrincipalException pe) {
604             return HttpServletResponse.SC_FORBIDDEN;
605         }
606         catch (DuplicateFileException dfe) {
607             return HttpServletResponse.SC_PRECONDITION_FAILED;
608         }
609         catch (DuplicateFolderNameException dfne) {
610             return HttpServletResponse.SC_PRECONDITION_FAILED;
611         }
612         catch (LockException le) {
613             return WebDAVUtil.SC_LOCKED;
614         }
615         catch (Exception e) {
616             throw new WebDAVException(e);
617         }
618     }
619 
620     public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
621         File file = null;
622 
623         try {
624             HttpServletRequest request = webDavRequest.getHttpServletRequest();
625 
626             String[] pathArray = webDavRequest.getPathArray();
627 
628             long companyId = webDavRequest.getCompanyId();
629             long groupId = webDavRequest.getGroupId();
630             long parentFolderId = getParentFolderId(companyId, pathArray);
631             String name = WebDAVUtil.getResourceName(pathArray);
632             String title = name;
633             String description = StringPool.BLANK;
634             String versionDescription = StringPool.BLANK;
635             String extraSettings = StringPool.BLANK;
636 
637             ServiceContext serviceContext = new ServiceContext();
638 
639             serviceContext.setAddCommunityPermissions(
640                 isAddCommunityPermissions(groupId));
641             serviceContext.setAddGuestPermissions(true);
642 
643             if (PropsValues.DL_WEBDAV_SAVE_TO_SINGLE_VERSION) {
644                 serviceContext.setStatus(StatusConstants.DRAFT);
645             }
646 
647             try {
648                 DLFileEntry entry = DLFileEntryServiceUtil.getFileEntryByTitle(
649                     groupId, parentFolderId, name);
650 
651                 if (isLocked(entry, webDavRequest.getLockUuid())) {
652                     return WebDAVUtil.SC_LOCKED;
653                 }
654 
655                 name = entry.getName();
656                 description = entry.getDescription();
657                 extraSettings = entry.getExtraSettings();
658 
659                 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
660                     DLFileEntry.class.getName(), entry.getFileEntryId());
661 
662                 serviceContext.setAssetTagNames(assetTagNames);
663 
664                 file = FileUtil.createTempFile(FileUtil.getExtension(name));
665 
666                 FileUtil.write(file, request.getInputStream());
667 
668                 DLFileEntryServiceUtil.updateFileEntry(
669                     groupId, parentFolderId, parentFolderId, name, title, title,
670                     description, versionDescription, false, extraSettings, file,
671                     serviceContext);
672             }
673             catch (NoSuchFileEntryException nsfee) {
674                 file = FileUtil.createTempFile(FileUtil.getExtension(name));
675 
676                 FileUtil.write(file, request.getInputStream());
677 
678                 DLFileEntryServiceUtil.addFileEntry(
679                     groupId, parentFolderId, name, title, description,
680                     versionDescription, extraSettings, file, serviceContext);
681             }
682 
683             return HttpServletResponse.SC_CREATED;
684         }
685         catch (PrincipalException pe) {
686             return HttpServletResponse.SC_FORBIDDEN;
687         }
688         catch (NoSuchFolderException nsfe) {
689             return HttpServletResponse.SC_CONFLICT;
690         }
691         catch (PortalException pe) {
692             if (_log.isWarnEnabled()) {
693                 _log.warn(pe, pe);
694             }
695 
696             return HttpServletResponse.SC_CONFLICT;
697         }
698         catch (Exception e) {
699             throw new WebDAVException(e);
700         }
701         finally {
702             if (file != null) {
703                 file.delete();
704             }
705         }
706     }
707 
708     public Lock refreshResourceLock(
709             WebDAVRequest webDavRequest, String uuid, long timeout)
710         throws WebDAVException {
711 
712         Resource resource = getResource(webDavRequest);
713 
714         Lock lock = null;
715 
716         try {
717             if (resource instanceof DLFileEntryResourceImpl) {
718                 lock = DLFileEntryServiceUtil.refreshFileEntryLock(
719                     uuid, timeout);
720             }
721             else {
722                 lock = DLFolderServiceUtil.refreshFolderLock(uuid, timeout);
723             }
724         }
725         catch (Exception e) {
726             throw new WebDAVException(e);
727         }
728 
729         return lock;
730     }
731 
732     public boolean unlockResource(WebDAVRequest webDavRequest, String token)
733         throws WebDAVException {
734 
735         Resource resource = getResource(webDavRequest);
736 
737         try {
738             if (resource instanceof DLFileEntryResourceImpl) {
739                 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
740 
741                 if (PropsValues.DL_WEBDAV_HOLD_LOCK) {
742                     return true;
743                 }
744 
745                 if (PropsValues.DL_WEBDAV_SAVE_TO_SINGLE_VERSION) {
746                     approveFileEntry(fileEntry);
747                 }
748 
749                 DLFileEntryServiceUtil.unlockFileEntry(
750                     fileEntry.getGroupId(), fileEntry.getFolderId(),
751                     fileEntry.getName(), token);
752             }
753             else {
754                 DLFolder folder = (DLFolder)resource.getModel();
755 
756                 DLFolderServiceUtil.unlockFolder(
757                     folder.getGroupId(), folder.getParentFolderId(),
758                     folder.getName(), token);
759             }
760 
761             return true;
762         }
763         catch (Exception e) {
764             if (e instanceof InvalidLockException) {
765                 if (_log.isWarnEnabled()) {
766                     _log.warn(e.getMessage());
767                 }
768             }
769             else {
770                 if (_log.isWarnEnabled()) {
771                     _log.warn("Unable to unlock file entry", e);
772                 }
773             }
774         }
775 
776         return false;
777     }
778 
779     protected void approveFileEntry(DLFileEntry fileEntry) throws Exception {
780         DLFileVersion fileVersion =
781             DLFileVersionLocalServiceUtil.getLatestFileVersion(
782                 fileEntry.getGroupId(), fileEntry.getFolderId(),
783                 fileEntry.getName());
784 
785         if (fileVersion.getStatus() != StatusConstants.DRAFT) {
786             return;
787         }
788 
789         ServiceContext serviceContext = new ServiceContext();
790 
791         serviceContext.setAddCommunityPermissions(
792             isAddCommunityPermissions(fileEntry.getGroupId()));
793         serviceContext.setAddGuestPermissions(true);
794         serviceContext.setStatus(StatusConstants.APPROVED);
795 
796         String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
797             DLFileEntry.class.getName(), fileEntry.getFileEntryId());
798 
799         serviceContext.setAssetTagNames(assetTagNames);
800 
801         DLFileEntryLocalServiceUtil.updateStatus(
802             fileEntry.getUserId(), fileEntry.getFileEntryId(), serviceContext);
803     }
804 
805     protected boolean deleteResource(
806             long groupId, long parentFolderId, String name, String lockUuid)
807         throws Exception {
808 
809         try {
810             DLFolder folder = DLFolderServiceUtil.getFolder(
811                 groupId, parentFolderId, name);
812 
813             DLFolderServiceUtil.deleteFolder(folder.getFolderId());
814 
815             return true;
816         }
817         catch (NoSuchFolderException nsfe) {
818             try {
819                 DLFileEntry fileEntry =
820                     DLFileEntryServiceUtil.getFileEntryByTitle(
821                         groupId, parentFolderId, name);
822 
823                 if (isLocked(fileEntry, lockUuid)) {
824                     throw new LockException();
825                 }
826 
827                 DLFileEntryServiceUtil.deleteFileEntryByTitle(
828                     groupId, parentFolderId, name);
829 
830                 return true;
831             }
832             catch (NoSuchFileEntryException nsfee) {
833             }
834         }
835 
836         return false;
837     }
838 
839     protected List<Resource> getFileEntries(
840             WebDAVRequest webDavRequest, long parentFolderId)
841         throws Exception {
842 
843         List<Resource> resources = new ArrayList<Resource>();
844 
845         List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
846             webDavRequest.getGroupId(), parentFolderId);
847 
848         for (DLFileEntry fileEntry : fileEntries) {
849             Resource resource = toResource(webDavRequest, fileEntry, true);
850 
851             resources.add(resource);
852         }
853 
854         return resources;
855     }
856 
857     protected long getFolderId(long companyId, String[] pathArray)
858         throws Exception {
859 
860         return getFolderId(companyId, pathArray, false);
861     }
862 
863     protected long getFolderId(
864             long companyId, String[] pathArray, boolean parent)
865         throws Exception {
866 
867         long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
868 
869         if (pathArray.length <= 1) {
870             return folderId;
871         }
872         else {
873             long groupId = WebDAVUtil.getGroupId(companyId, pathArray);
874 
875             int x = pathArray.length;
876 
877             if (parent) {
878                 x--;
879             }
880 
881             for (int i = 2; i < x; i++) {
882                 String name = pathArray[i];
883 
884                 DLFolder folder = DLFolderServiceUtil.getFolder(
885                     groupId, folderId, name);
886 
887                 if (groupId == folder.getGroupId()) {
888                     folderId = folder.getFolderId();
889                 }
890             }
891         }
892 
893         return folderId;
894     }
895 
896     protected List<Resource> getFolders(
897             WebDAVRequest webDavRequest, long parentFolderId)
898         throws Exception {
899 
900         List<Resource> resources = new ArrayList<Resource>();
901 
902         long groupId = webDavRequest.getGroupId();
903 
904         List<DLFolder> folders = DLFolderServiceUtil.getFolders(
905             groupId, parentFolderId);
906 
907         for (DLFolder folder : folders) {
908             Resource resource = toResource(webDavRequest, folder, true);
909 
910             resources.add(resource);
911         }
912 
913         return resources;
914     }
915 
916     protected long getParentFolderId(long companyId, String[] pathArray)
917         throws Exception {
918 
919         return getFolderId(companyId, pathArray, true);
920     }
921 
922     protected boolean isLocked(DLFileEntry fileEntry, String lockUuid)
923         throws Exception {
924 
925         long groupId = fileEntry.getGroupId();
926         long parentFolderId = fileEntry.getFolderId();
927         String fileName = fileEntry.getName();
928 
929         if (Validator.isNull(lockUuid)) {
930 
931             // Client does not claim to know of a lock
932 
933             return DLFileEntryServiceUtil.hasFileEntryLock(
934                 groupId, parentFolderId, fileName);
935         }
936         else {
937 
938             // Client claims to know of a lock. Verify the lock UUID.
939 
940             try {
941                 boolean verified = DLFileEntryServiceUtil.verifyFileEntryLock(
942                     groupId, parentFolderId, fileName, lockUuid);
943 
944                 return !verified;
945             }
946             catch (NoSuchLockException nsle) {
947                 return false;
948             }
949         }
950     }
951 
952     protected Resource toResource(
953         WebDAVRequest webDavRequest, DLFileEntry fileEntry,
954         boolean appendPath) {
955 
956         String parentPath = getRootPath() + webDavRequest.getPath();
957         String name = StringPool.BLANK;
958 
959         if (appendPath) {
960             name = fileEntry.getTitle();
961         }
962 
963         return new DLFileEntryResourceImpl(
964             webDavRequest, fileEntry, parentPath, name);
965     }
966 
967     protected Resource toResource(
968         WebDAVRequest webDavRequest, DLFolder folder, boolean appendPath) {
969 
970         String parentPath = getRootPath() + webDavRequest.getPath();
971         String name = StringPool.BLANK;
972 
973         if (appendPath) {
974             name = folder.getName();
975         }
976 
977         Resource resource = new BaseResourceImpl(
978             parentPath, name, folder.getName(), folder.getCreateDate(),
979             folder.getModifiedDate());
980 
981         resource.setModel(folder);
982         resource.setClassName(DLFolder.class.getName());
983         resource.setPrimaryKey(folder.getPrimaryKey());
984 
985         return resource;
986     }
987 
988     private static Log _log = LogFactoryUtil.getLog(DLWebDAVStorageImpl.class);
989 
990 }