001
014
015 package com.liferay.portlet.documentlibrary.webdav;
016
017 import com.liferay.documentlibrary.DuplicateFileException;
018 import com.liferay.portal.DuplicateLockException;
019 import com.liferay.portal.InvalidLockException;
020 import com.liferay.portal.NoSuchLockException;
021 import com.liferay.portal.kernel.exception.PortalException;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.util.FileUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.kernel.webdav.BaseResourceImpl;
029 import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
030 import com.liferay.portal.kernel.webdav.Resource;
031 import com.liferay.portal.kernel.webdav.Status;
032 import com.liferay.portal.kernel.webdav.WebDAVException;
033 import com.liferay.portal.kernel.webdav.WebDAVRequest;
034 import com.liferay.portal.kernel.webdav.WebDAVUtil;
035 import com.liferay.portal.kernel.workflow.WorkflowConstants;
036 import com.liferay.portal.model.Lock;
037 import com.liferay.portal.security.auth.PrincipalException;
038 import com.liferay.portal.service.ServiceContext;
039 import com.liferay.portal.util.PropsValues;
040 import com.liferay.portal.webdav.LockException;
041 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
042 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
043 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
044 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
045 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
046 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
047 import com.liferay.portlet.documentlibrary.model.DLFolder;
048 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
049 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
050 import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
051 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
052 import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
053
054 import java.io.File;
055 import java.io.InputStream;
056
057 import java.util.ArrayList;
058 import java.util.List;
059
060 import javax.servlet.http.HttpServletRequest;
061 import javax.servlet.http.HttpServletResponse;
062
063
067 public class DLWebDAVStorageImpl extends BaseWebDAVStorageImpl {
068
069 public int copyCollectionResource(
070 WebDAVRequest webDavRequest, Resource resource, String destination,
071 boolean overwrite, long depth)
072 throws WebDAVException {
073
074 try {
075 String[] destinationArray = WebDAVUtil.getPathArray(
076 destination, true);
077
078 long companyId = webDavRequest.getCompanyId();
079
080 long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
081
082 try {
083 parentFolderId = getParentFolderId(companyId, destinationArray);
084 }
085 catch (NoSuchFolderException nsfe) {
086 return HttpServletResponse.SC_CONFLICT;
087 }
088
089 DLFolder folder = (DLFolder)resource.getModel();
090
091 long groupId = WebDAVUtil.getGroupId(companyId, destination);
092 String name = WebDAVUtil.getResourceName(destinationArray);
093 String description = folder.getDescription();
094
095 ServiceContext serviceContext = new ServiceContext();
096
097 serviceContext.setAddCommunityPermissions(
098 isAddCommunityPermissions(groupId));
099 serviceContext.setAddGuestPermissions(true);
100
101 int status = HttpServletResponse.SC_CREATED;
102
103 if (overwrite) {
104 if (deleteResource(
105 groupId, parentFolderId, name,
106 webDavRequest.getLockUuid())) {
107
108 status = HttpServletResponse.SC_NO_CONTENT;
109 }
110 }
111
112 if (depth == 0) {
113 DLFolderServiceUtil.addFolder(
114 groupId, parentFolderId, name, description, serviceContext);
115 }
116 else {
117 DLFolderServiceUtil.copyFolder(
118 groupId, folder.getFolderId(), parentFolderId, name,
119 description, serviceContext);
120 }
121
122 return status;
123 }
124 catch (DuplicateFolderNameException dfne) {
125 return HttpServletResponse.SC_PRECONDITION_FAILED;
126 }
127 catch (PrincipalException pe) {
128 return HttpServletResponse.SC_FORBIDDEN;
129 }
130 catch (Exception e) {
131 throw new WebDAVException(e);
132 }
133 }
134
135 public int copySimpleResource(
136 WebDAVRequest webDavRequest, Resource resource, String destination,
137 boolean overwrite)
138 throws WebDAVException {
139
140 File file = null;
141
142 try {
143 String[] destinationArray = WebDAVUtil.getPathArray(
144 destination, true);
145
146 long companyId = webDavRequest.getCompanyId();
147
148 long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
149
150 try {
151 parentFolderId = getParentFolderId(companyId, 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(companyId, 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 changeLog = StringPool.BLANK;
165 String extraSettings = fileEntry.getExtraSettings();
166
167 file = FileUtil.createTempFile(
168 FileUtil.getExtension(fileEntry.getName()));
169
170 InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
171 fileEntry.getCompanyId(), userId, fileEntry.getGroupId(),
172 fileEntry.getFolderId(), fileEntry.getName());
173
174 FileUtil.write(file, is);
175
176 ServiceContext serviceContext = new ServiceContext();
177
178 serviceContext.setAddCommunityPermissions(
179 isAddCommunityPermissions(groupId));
180 serviceContext.setAddGuestPermissions(true);
181
182 int status = HttpServletResponse.SC_CREATED;
183
184 if (overwrite) {
185 if (deleteResource(
186 groupId, parentFolderId, title,
187 webDavRequest.getLockUuid())) {
188
189 status = HttpServletResponse.SC_NO_CONTENT;
190 }
191 }
192
193 DLFileEntryServiceUtil.addFileEntry(
194 groupId, parentFolderId, name, title, description, changeLog,
195 extraSettings, file, serviceContext);
196
197 return status;
198 }
199 catch (DuplicateFileException dfe) {
200 return HttpServletResponse.SC_PRECONDITION_FAILED;
201 }
202 catch (DuplicateFolderNameException dfne) {
203 return HttpServletResponse.SC_PRECONDITION_FAILED;
204 }
205 catch (LockException le) {
206 return WebDAVUtil.SC_LOCKED;
207 }
208 catch (PrincipalException pe) {
209 return HttpServletResponse.SC_FORBIDDEN;
210 }
211 catch (Exception e) {
212 throw new WebDAVException(e);
213 }
214 finally {
215 if (file != null) {
216 file.delete();
217 }
218 }
219 }
220
221 public int deleteResource(WebDAVRequest webDavRequest)
222 throws WebDAVException {
223
224 try {
225 Resource resource = getResource(webDavRequest);
226
227 if (resource == null) {
228 return HttpServletResponse.SC_NOT_FOUND;
229 }
230
231 Object model = resource.getModel();
232
233 if (model instanceof DLFolder) {
234 DLFolder folder = (DLFolder)model;
235
236 DLFolderServiceUtil.deleteFolder(folder.getFolderId());
237 }
238 else {
239 DLFileEntry fileEntry = (DLFileEntry)model;
240
241 if (isLocked(fileEntry, webDavRequest.getLockUuid())) {
242 return WebDAVUtil.SC_LOCKED;
243 }
244
245 DLFileEntryServiceUtil.deleteFileEntry(
246 fileEntry.getGroupId(), fileEntry.getFolderId(),
247 fileEntry.getName());
248 }
249
250 return HttpServletResponse.SC_NO_CONTENT;
251 }
252 catch (PrincipalException pe) {
253 return HttpServletResponse.SC_FORBIDDEN;
254 }
255 catch (Exception e) {
256 throw new WebDAVException(e);
257 }
258 }
259
260 public Resource getResource(WebDAVRequest webDavRequest)
261 throws WebDAVException {
262
263 try {
264 String[] pathArray = webDavRequest.getPathArray();
265
266 long companyId = webDavRequest.getCompanyId();
267 long parentFolderId = getParentFolderId(companyId, pathArray);
268 String name = WebDAVUtil.getResourceName(pathArray);
269
270 if (Validator.isNull(name)) {
271 String path = getRootPath() + webDavRequest.getPath();
272
273 return new BaseResourceImpl(path, StringPool.BLANK, getToken());
274 }
275
276 try {
277 DLFolder folder = DLFolderServiceUtil.getFolder(
278 webDavRequest.getGroupId(), parentFolderId, name);
279
280 if ((folder.getParentFolderId() != parentFolderId) ||
281 (webDavRequest.getGroupId() != folder.getGroupId())) {
282
283 throw new NoSuchFolderException();
284 }
285
286 return toResource(webDavRequest, folder, false);
287 }
288 catch (NoSuchFolderException nsfe) {
289 try {
290 String titleWithExtension = name;
291
292 DLFileEntry fileEntry =
293 DLFileEntryServiceUtil.getFileEntryByTitle(
294 webDavRequest.getGroupId(), parentFolderId,
295 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(
314 webDavRequest.getCompanyId(), webDavRequest.getPathArray());
315
316 List<Resource> folders = getFolders(webDavRequest, folderId);
317 List<Resource> fileEntries = getFileEntries(
318 webDavRequest, folderId);
319
320 List<Resource> resources = new ArrayList<Resource>(
321 folders.size() + fileEntries.size());
322
323 resources.addAll(folders);
324 resources.addAll(fileEntries);
325
326 return resources;
327 }
328 catch (Exception e) {
329 throw new WebDAVException(e);
330 }
331 }
332
333 public boolean isSupportsClassTwo() {
334 return true;
335 }
336
337 public Status lockResource(
338 WebDAVRequest webDavRequest, String owner, long timeout)
339 throws WebDAVException {
340
341 Resource resource = getResource(webDavRequest);
342
343 Lock lock = null;
344 int status = HttpServletResponse.SC_OK;
345
346 try {
347 if (resource == null) {
348 status = HttpServletResponse.SC_CREATED;
349
350 String[] pathArray = webDavRequest.getPathArray();
351
352 long companyId = webDavRequest.getCompanyId();
353 long groupId = webDavRequest.getGroupId();
354 long parentFolderId = getParentFolderId(companyId, pathArray);
355 String name = WebDAVUtil.getResourceName(pathArray);
356
357 String title = name;
358 String description = StringPool.BLANK;
359 String changeLog = StringPool.BLANK;
360 String extraSettings = StringPool.BLANK;
361
362 File file = FileUtil.createTempFile(
363 FileUtil.getExtension(name));
364
365 file.createNewFile();
366
367 ServiceContext serviceContext = new ServiceContext();
368
369 serviceContext.setAddCommunityPermissions(
370 isAddCommunityPermissions(groupId));
371 serviceContext.setAddGuestPermissions(true);
372
373 DLFileEntry fileEntry = DLFileEntryServiceUtil.addFileEntry(
374 groupId, parentFolderId, name, title, description,
375 changeLog, extraSettings, file, serviceContext);
376
377 resource = toResource(webDavRequest, fileEntry, false);
378 }
379
380 if (resource instanceof DLFileEntryResourceImpl) {
381 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
382
383 lock = DLFileEntryServiceUtil.lockFileEntry(
384 fileEntry.getGroupId(), fileEntry.getFolderId(),
385 fileEntry.getName(), owner, timeout);
386 }
387 else {
388 boolean inheritable = false;
389
390 long depth = WebDAVUtil.getDepth(
391 webDavRequest.getHttpServletRequest());
392
393 if (depth != 0) {
394 inheritable = true;
395 }
396
397 DLFolder folder = (DLFolder)resource.getModel();
398
399 lock = DLFolderServiceUtil.lockFolder(
400 folder.getFolderId(), owner, inheritable, timeout);
401 }
402 }
403 catch (Exception e) {
404
405
406
407 if (!(e instanceof DuplicateLockException)) {
408 throw new WebDAVException(e);
409 }
410
411 status = WebDAVUtil.SC_LOCKED;
412 }
413
414 return new Status(lock, status);
415 }
416
417 public Status makeCollection(WebDAVRequest webDavRequest)
418 throws WebDAVException {
419
420 try {
421 HttpServletRequest request = webDavRequest.getHttpServletRequest();
422
423 if (request.getContentLength() > 0) {
424 return new Status(
425 HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
426 }
427
428 String[] pathArray = webDavRequest.getPathArray();
429
430 long companyId = webDavRequest.getCompanyId();
431 long groupId = webDavRequest.getGroupId();
432 long parentFolderId = getParentFolderId(companyId, pathArray);
433 String name = WebDAVUtil.getResourceName(pathArray);
434 String description = StringPool.BLANK;
435
436 ServiceContext serviceContext = new ServiceContext();
437
438 serviceContext.setAddCommunityPermissions(
439 isAddCommunityPermissions(groupId));
440 serviceContext.setAddGuestPermissions(true);
441
442 DLFolderServiceUtil.addFolder(
443 groupId, parentFolderId, name, description, serviceContext);
444
445 String location = StringUtil.merge(pathArray, StringPool.SLASH);
446
447 return new Status(location, HttpServletResponse.SC_CREATED);
448 }
449 catch (DuplicateFolderNameException dfne) {
450 return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
451 }
452 catch (DuplicateFileException dfe) {
453 return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
454 }
455 catch (NoSuchFolderException nsfe) {
456 return new Status(HttpServletResponse.SC_CONFLICT);
457 }
458 catch (PrincipalException pe) {
459 return new Status(HttpServletResponse.SC_FORBIDDEN);
460 }
461 catch (Exception e) {
462 throw new WebDAVException(e);
463 }
464 }
465
466 public int moveCollectionResource(
467 WebDAVRequest webDavRequest, Resource resource, String destination,
468 boolean overwrite)
469 throws WebDAVException {
470
471 try {
472 String[] destinationArray = WebDAVUtil.getPathArray(
473 destination, true);
474
475 DLFolder folder = (DLFolder)resource.getModel();
476
477 long companyId = webDavRequest.getCompanyId();
478 long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
479 long folderId = folder.getFolderId();
480 long parentFolderId = getParentFolderId(
481 companyId, destinationArray);
482 String name = WebDAVUtil.getResourceName(destinationArray);
483 String description = folder.getDescription();
484
485 ServiceContext serviceContext = new ServiceContext();
486
487 int status = HttpServletResponse.SC_CREATED;
488
489 if (overwrite) {
490 if (deleteResource(
491 groupId, parentFolderId, name,
492 webDavRequest.getLockUuid())) {
493
494 status = HttpServletResponse.SC_NO_CONTENT;
495 }
496 }
497
498 DLFolderServiceUtil.updateFolder(
499 folderId, parentFolderId, name, description, serviceContext);
500
501 return status;
502 }
503 catch (PrincipalException pe) {
504 return HttpServletResponse.SC_FORBIDDEN;
505 }
506 catch (DuplicateFolderNameException dfne) {
507 return HttpServletResponse.SC_PRECONDITION_FAILED;
508 }
509 catch (Exception e) {
510 throw new WebDAVException(e);
511 }
512 }
513
514 public int moveSimpleResource(
515 WebDAVRequest webDavRequest, Resource resource, String destination,
516 boolean overwrite)
517 throws WebDAVException {
518
519 try {
520 String[] destinationArray = WebDAVUtil.getPathArray(
521 destination, true);
522
523 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
524
525 if (isLocked(fileEntry, webDavRequest.getLockUuid())) {
526 return WebDAVUtil.SC_LOCKED;
527 }
528
529 long companyId = webDavRequest.getCompanyId();
530 long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
531 long userId = webDavRequest.getUserId();
532 long parentFolderId = getParentFolderId(
533 companyId, destinationArray);
534 String name = fileEntry.getName();
535 String sourceFileName = WebDAVUtil.getResourceName(
536 destinationArray);
537 String title = WebDAVUtil.getResourceName(destinationArray);
538 String description = fileEntry.getDescription();
539 String changeLog = StringPool.BLANK;
540 String extraSettings = fileEntry.getExtraSettings();
541 byte[] bytes = null;
542
543 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
544 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
545
546 ServiceContext serviceContext = new ServiceContext();
547
548 serviceContext.setAssetTagNames(assetTagNames);
549
550 int status = HttpServletResponse.SC_CREATED;
551
552 if (overwrite) {
553 if (deleteResource(
554 groupId, parentFolderId, title,
555 webDavRequest.getLockUuid())) {
556
557 status = HttpServletResponse.SC_NO_CONTENT;
558 }
559 }
560
561
562
563 if (webDavRequest.isMac()) {
564 try {
565 DLFileEntry destFileEntry =
566 DLFileEntryServiceUtil.getFileEntryByTitle(
567 groupId, parentFolderId, title);
568
569 InputStream is =
570 DLFileEntryLocalServiceUtil.getFileAsStream(
571 fileEntry.getCompanyId(), userId,
572 fileEntry.getGroupId(), fileEntry.getFolderId(),
573 fileEntry.getName());
574
575 bytes = FileUtil.getBytes(is);
576
577 DLFileEntryServiceUtil.updateFileEntry(
578 groupId, parentFolderId, destFileEntry.getName(),
579 destFileEntry.getTitle(), destFileEntry.getTitle(),
580 destFileEntry.getDescription(), changeLog, false,
581 destFileEntry.getExtraSettings(), bytes,
582 serviceContext);
583
584 DLFileEntryServiceUtil.deleteFileEntry(
585 fileEntry.getGroupId(), fileEntry.getFolderId(),
586 fileEntry.getName());
587
588 return status;
589 }
590 catch (NoSuchFileEntryException nsfee) {
591 }
592 }
593
594 DLFileEntryServiceUtil.updateFileEntry(
595 fileEntry.getGroupId(), fileEntry.getFolderId(), name,
596 sourceFileName, title, description, changeLog, false,
597 extraSettings, bytes, serviceContext);
598
599 if (fileEntry.getFolderId() != parentFolderId) {
600 fileEntry = DLFileEntryServiceUtil.moveFileEntry(
601 groupId, fileEntry.getFolderId(), parentFolderId, name,
602 serviceContext);
603 }
604
605 return status;
606 }
607 catch (PrincipalException pe) {
608 return HttpServletResponse.SC_FORBIDDEN;
609 }
610 catch (DuplicateFileException dfe) {
611 return HttpServletResponse.SC_PRECONDITION_FAILED;
612 }
613 catch (DuplicateFolderNameException dfne) {
614 return HttpServletResponse.SC_PRECONDITION_FAILED;
615 }
616 catch (LockException le) {
617 return WebDAVUtil.SC_LOCKED;
618 }
619 catch (Exception e) {
620 throw new WebDAVException(e);
621 }
622 }
623
624 public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
625 File file = null;
626
627 try {
628 HttpServletRequest request = webDavRequest.getHttpServletRequest();
629
630 String[] pathArray = webDavRequest.getPathArray();
631
632 long companyId = webDavRequest.getCompanyId();
633 long groupId = webDavRequest.getGroupId();
634 long parentFolderId = getParentFolderId(companyId, pathArray);
635 String name = WebDAVUtil.getResourceName(pathArray);
636 String title = name;
637 String description = StringPool.BLANK;
638 String changeLog = StringPool.BLANK;
639 String extraSettings = StringPool.BLANK;
640
641 ServiceContext serviceContext = new ServiceContext();
642
643 serviceContext.setAddCommunityPermissions(
644 isAddCommunityPermissions(groupId));
645 serviceContext.setAddGuestPermissions(true);
646
647 if (PropsValues.DL_WEBDAV_SAVE_TO_SINGLE_VERSION) {
648 serviceContext.setWorkflowAction(
649 WorkflowConstants.ACTION_SAVE_DRAFT);
650 }
651
652 try {
653 DLFileEntry fileEntry =
654 DLFileEntryServiceUtil.getFileEntryByTitle(
655 groupId, parentFolderId, name);
656
657 if (isLocked(fileEntry, webDavRequest.getLockUuid())) {
658 return WebDAVUtil.SC_LOCKED;
659 }
660
661 name = fileEntry.getName();
662 description = fileEntry.getDescription();
663 extraSettings = fileEntry.getExtraSettings();
664
665 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
666 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
667
668 serviceContext.setAssetTagNames(assetTagNames);
669
670 file = FileUtil.createTempFile(FileUtil.getExtension(name));
671
672 FileUtil.write(file, request.getInputStream());
673
674 DLFileEntryServiceUtil.updateFileEntry(
675 groupId, parentFolderId, name, title, title, description,
676 changeLog, false, extraSettings, file, serviceContext);
677 }
678 catch (NoSuchFileEntryException nsfee) {
679 file = FileUtil.createTempFile(FileUtil.getExtension(name));
680
681 FileUtil.write(file, request.getInputStream());
682
683 DLFileEntryServiceUtil.addFileEntry(
684 groupId, parentFolderId, name, title, description,
685 changeLog, extraSettings, file, serviceContext);
686 }
687
688 if (_log.isInfoEnabled()) {
689 _log.info(
690 "Added " + StringUtil.merge(pathArray, StringPool.SLASH));
691 }
692
693 return HttpServletResponse.SC_CREATED;
694 }
695 catch (PrincipalException pe) {
696 return HttpServletResponse.SC_FORBIDDEN;
697 }
698 catch (NoSuchFolderException nsfe) {
699 return HttpServletResponse.SC_CONFLICT;
700 }
701 catch (PortalException pe) {
702 if (_log.isWarnEnabled()) {
703 _log.warn(pe, pe);
704 }
705
706 return HttpServletResponse.SC_CONFLICT;
707 }
708 catch (Exception e) {
709 throw new WebDAVException(e);
710 }
711 finally {
712 if (file != null) {
713 file.delete();
714 }
715 }
716 }
717
718 public Lock refreshResourceLock(
719 WebDAVRequest webDavRequest, String uuid, long timeout)
720 throws WebDAVException {
721
722 Resource resource = getResource(webDavRequest);
723
724 Lock lock = null;
725
726 try {
727 if (resource instanceof DLFileEntryResourceImpl) {
728 lock = DLFileEntryServiceUtil.refreshFileEntryLock(
729 uuid, timeout);
730 }
731 else {
732 lock = DLFolderServiceUtil.refreshFolderLock(uuid, timeout);
733 }
734 }
735 catch (Exception e) {
736 throw new WebDAVException(e);
737 }
738
739 return lock;
740 }
741
742 public boolean unlockResource(WebDAVRequest webDavRequest, String token)
743 throws WebDAVException {
744
745 Resource resource = getResource(webDavRequest);
746
747 try {
748 if (resource instanceof DLFileEntryResourceImpl) {
749 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
750
751 if (PropsValues.DL_WEBDAV_HOLD_LOCK) {
752 return true;
753 }
754
755 if (PropsValues.DL_WEBDAV_SAVE_TO_SINGLE_VERSION) {
756 publishFileEntry(fileEntry);
757 }
758
759 DLFileEntryServiceUtil.unlockFileEntry(
760 fileEntry.getGroupId(), fileEntry.getFolderId(),
761 fileEntry.getName(), token);
762 }
763 else {
764 DLFolder folder = (DLFolder)resource.getModel();
765
766 DLFolderServiceUtil.unlockFolder(
767 folder.getGroupId(), folder.getParentFolderId(),
768 folder.getName(), token);
769 }
770
771 return true;
772 }
773 catch (Exception e) {
774 if (e instanceof InvalidLockException) {
775 if (_log.isWarnEnabled()) {
776 _log.warn(e.getMessage());
777 }
778 }
779 else {
780 if (_log.isWarnEnabled()) {
781 _log.warn("Unable to unlock file entry", e);
782 }
783 }
784 }
785
786 return false;
787 }
788
789 protected boolean deleteResource(
790 long groupId, long parentFolderId, String name, String lockUuid)
791 throws Exception {
792
793 try {
794 DLFolder folder = DLFolderServiceUtil.getFolder(
795 groupId, parentFolderId, name);
796
797 DLFolderServiceUtil.deleteFolder(folder.getFolderId());
798
799 return true;
800 }
801 catch (NoSuchFolderException nsfe) {
802 try {
803 DLFileEntry fileEntry =
804 DLFileEntryServiceUtil.getFileEntryByTitle(
805 groupId, parentFolderId, name);
806
807 if (isLocked(fileEntry, lockUuid)) {
808 throw new LockException();
809 }
810
811 DLFileEntryServiceUtil.deleteFileEntryByTitle(
812 groupId, parentFolderId, name);
813
814 return true;
815 }
816 catch (NoSuchFileEntryException nsfee) {
817 }
818 }
819
820 return false;
821 }
822
823 protected List<Resource> getFileEntries(
824 WebDAVRequest webDavRequest, long parentFolderId)
825 throws Exception {
826
827 List<Resource> resources = new ArrayList<Resource>();
828
829 List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
830 webDavRequest.getGroupId(), parentFolderId);
831
832 for (DLFileEntry fileEntry : fileEntries) {
833 Resource resource = toResource(webDavRequest, fileEntry, true);
834
835 resources.add(resource);
836 }
837
838 return resources;
839 }
840
841 protected long getFolderId(long companyId, String[] pathArray)
842 throws Exception {
843
844 return getFolderId(companyId, pathArray, false);
845 }
846
847 protected long getFolderId(
848 long companyId, String[] pathArray, boolean parent)
849 throws Exception {
850
851 long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
852
853 if (pathArray.length <= 1) {
854 return folderId;
855 }
856 else {
857 long groupId = WebDAVUtil.getGroupId(companyId, pathArray);
858
859 int x = pathArray.length;
860
861 if (parent) {
862 x--;
863 }
864
865 for (int i = 2; i < x; i++) {
866 String name = pathArray[i];
867
868 DLFolder folder = DLFolderServiceUtil.getFolder(
869 groupId, folderId, name);
870
871 if (groupId == folder.getGroupId()) {
872 folderId = folder.getFolderId();
873 }
874 }
875 }
876
877 return folderId;
878 }
879
880 protected List<Resource> getFolders(
881 WebDAVRequest webDavRequest, long parentFolderId)
882 throws Exception {
883
884 List<Resource> resources = new ArrayList<Resource>();
885
886 long groupId = webDavRequest.getGroupId();
887
888 List<DLFolder> folders = DLFolderServiceUtil.getFolders(
889 groupId, parentFolderId);
890
891 for (DLFolder folder : folders) {
892 Resource resource = toResource(webDavRequest, folder, true);
893
894 resources.add(resource);
895 }
896
897 return resources;
898 }
899
900 protected long getParentFolderId(long companyId, String[] pathArray)
901 throws Exception {
902
903 return getFolderId(companyId, pathArray, true);
904 }
905
906 protected boolean isLocked(DLFileEntry fileEntry, String lockUuid)
907 throws Exception {
908
909 long groupId = fileEntry.getGroupId();
910 long parentFolderId = fileEntry.getFolderId();
911 String fileName = fileEntry.getName();
912
913 if (Validator.isNull(lockUuid)) {
914
915
916
917 return DLFileEntryServiceUtil.hasFileEntryLock(
918 groupId, parentFolderId, fileName);
919 }
920 else {
921
922
923
924 try {
925 boolean verified = DLFileEntryServiceUtil.verifyFileEntryLock(
926 groupId, parentFolderId, fileName, lockUuid);
927
928 return !verified;
929 }
930 catch (NoSuchLockException nsle) {
931 return false;
932 }
933 }
934 }
935
936 protected void publishFileEntry(DLFileEntry fileEntry) throws Exception {
937 DLFileVersion latestFileVersion =
938 DLFileVersionLocalServiceUtil.getLatestFileVersion(
939 fileEntry.getGroupId(), fileEntry.getFolderId(),
940 fileEntry.getName());
941
942 if (latestFileVersion.getStatus() ==
943 WorkflowConstants.STATUS_APPROVED) {
944
945 return;
946 }
947
948 ServiceContext serviceContext = new ServiceContext();
949
950 serviceContext.setAddCommunityPermissions(
951 isAddCommunityPermissions(fileEntry.getGroupId()));
952 serviceContext.setAddGuestPermissions(true);
953
954 DLFileEntryLocalServiceUtil.updateFileEntry(
955 latestFileVersion.getUserId(), latestFileVersion.getGroupId(),
956 latestFileVersion.getFolderId(), latestFileVersion.getName(),
957 fileEntry.getTitle(), fileEntry.getTitle(),
958 fileEntry.getDescription(), latestFileVersion.getDescription(),
959 true, fileEntry.getExtraSettings(), null, 0, serviceContext);
960 }
961
962 protected Resource toResource(
963 WebDAVRequest webDavRequest, DLFileEntry fileEntry,
964 boolean appendPath) {
965
966 String parentPath = getRootPath() + webDavRequest.getPath();
967 String name = StringPool.BLANK;
968
969 if (appendPath) {
970 name = fileEntry.getTitle();
971 }
972
973 return new DLFileEntryResourceImpl(
974 webDavRequest, fileEntry, parentPath, name);
975 }
976
977 protected Resource toResource(
978 WebDAVRequest webDavRequest, DLFolder folder, boolean appendPath) {
979
980 String parentPath = getRootPath() + webDavRequest.getPath();
981 String name = StringPool.BLANK;
982
983 if (appendPath) {
984 name = folder.getName();
985 }
986
987 Resource resource = new BaseResourceImpl(
988 parentPath, name, folder.getName(), folder.getCreateDate(),
989 folder.getModifiedDate());
990
991 resource.setModel(folder);
992 resource.setClassName(DLFolder.class.getName());
993 resource.setPrimaryKey(folder.getPrimaryKey());
994
995 return resource;
996 }
997
998 private static Log _log = LogFactoryUtil.getLog(DLWebDAVStorageImpl.class);
999
1000 }