2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
\r
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
5 * of this software and associated documentation files (the "Software"), to deal
\r
6 * in the Software without restriction, including without limitation the rights
\r
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
8 * copies of the Software, and to permit persons to whom the Software is
\r
9 * furnished to do so, subject to the following conditions:
\r
11 * The above copyright notice and this permission notice shall be included in
\r
12 * all copies or substantial portions of the Software.
\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
23 package com.liferay.portlet.documentlibrary.webdav;
\r
25 import java.io.File;
\r
26 import java.io.InputStream;
\r
27 import java.util.ArrayList;
\r
28 import java.util.List;
\r
30 import javax.servlet.http.HttpServletRequest;
\r
31 import javax.servlet.http.HttpServletResponse;
\r
33 import com.liferay.portal.DuplicateLockException;
\r
34 import com.liferay.portal.InvalidLockException;
\r
35 import com.liferay.portal.NoSuchLockException;
\r
36 import com.liferay.portal.kernel.exception.PortalException;
\r
37 import com.liferay.portal.kernel.log.Log;
\r
38 import com.liferay.portal.kernel.log.LogFactoryUtil;
\r
39 import com.liferay.portal.kernel.repository.model.FileEntry;
\r
40 import com.liferay.portal.kernel.repository.model.Folder;
\r
41 import com.liferay.portal.kernel.search.Indexer;
\r
42 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
\r
43 import com.liferay.portal.kernel.servlet.HttpHeaders;
\r
44 import com.liferay.portal.kernel.util.ContentTypes;
\r
45 import com.liferay.portal.kernel.util.FileUtil;
\r
46 import com.liferay.portal.kernel.util.GetterUtil;
\r
47 import com.liferay.portal.kernel.util.MimeTypesUtil;
\r
48 import com.liferay.portal.kernel.util.StringPool;
\r
49 import com.liferay.portal.kernel.util.StringUtil;
\r
50 import com.liferay.portal.kernel.util.Validator;
\r
51 import com.liferay.portal.kernel.webdav.BaseResourceImpl;
\r
52 import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
\r
53 import com.liferay.portal.kernel.webdav.Resource;
\r
54 import com.liferay.portal.kernel.webdav.Status;
\r
55 import com.liferay.portal.kernel.webdav.WebDAVException;
\r
56 import com.liferay.portal.kernel.webdav.WebDAVRequest;
\r
57 import com.liferay.portal.kernel.webdav.WebDAVUtil;
\r
58 import com.liferay.portal.kernel.workflow.WorkflowConstants;
\r
59 import com.liferay.portal.model.Lock;
\r
60 import com.liferay.portal.security.auth.PrincipalException;
\r
61 import com.liferay.portal.service.ServiceContext;
\r
62 import com.liferay.portal.service.ServiceContextFactory;
\r
63 import com.liferay.portal.webdav.LockException;
\r
64 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
\r
65 import com.liferay.portlet.documentlibrary.DuplicateFileException;
\r
66 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
\r
67 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
\r
68 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
\r
69 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
\r
70 import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
\r
71 import com.liferay.portlet.documentlibrary.model.DLFolder;
\r
72 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
\r
73 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
\r
74 import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
\r
75 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
\r
76 import com.pentila.entSavoie.utils.DLAppUtil;
\r
77 import com.pentila.entSavoie.utils.PermissionsUtils;
\r
80 * <a href="DLWebDAVStorageImpl.java.html"><b><i>View Source</i></b></a>
\r
82 * @author Brian Wing Shun Chan
\r
83 * @author Alexander Chow
\r
86 public class DLWebDAVStorageImpl extends BaseWebDAVStorageImpl {
\r
89 public int copyCollectionResource(
\r
90 WebDAVRequest webDavRequest, Resource resource, String destination,
\r
91 boolean overwrite, long depth)
\r
92 throws WebDAVException {
\r
95 String[] destinationArray = WebDAVUtil.getPathArray(
\r
98 long companyId = webDavRequest.getCompanyId();
\r
100 long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
\r
103 parentFolderId = getParentFolderId(companyId, destinationArray);
\r
105 catch (NoSuchFolderException nsfe) {
\r
106 return HttpServletResponse.SC_CONFLICT;
\r
109 Folder parentFolder = DLAppLocalServiceUtil.getFolder(parentFolderId);
\r
111 Folder folder = (Folder)resource.getModel();
\r
113 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), parentFolder, "ADD_OBJECT")) {
\r
114 long groupId = WebDAVUtil.getGroupId(companyId, destination);
\r
115 String name = WebDAVUtil.getResourceName(destinationArray);
\r
116 String description = folder.getDescription();
\r
118 ServiceContext serviceContext = new ServiceContext();
\r
120 serviceContext.setAddGroupPermissions(
\r
121 isAddGroupPermissions(groupId));
\r
122 serviceContext.setAddGuestPermissions(true);
\r
123 serviceContext.setAddGuestPermissions(true);
\r
125 int status = HttpServletResponse.SC_CREATED;
\r
128 if (deleteResource(
\r
129 groupId, parentFolderId, name,
\r
130 webDavRequest.getLockUuid())) {
\r
132 status = HttpServletResponse.SC_NO_CONTENT;
\r
138 Folder newFolder = DLAppServiceUtil.addFolder(
\r
139 groupId, parentFolderId, name, description, serviceContext);
\r
141 // Apply parent folder permissions
\r
142 PermissionsUtils.setParentPermissionToFolder(newFolder);
\r
145 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFolder.class);
\r
146 indexer.reindex(newFolder);
\r
150 Folder copiedFolder = DLAppServiceUtil.copyFolder(
\r
151 groupId, folder.getFolderId(), parentFolderId, name,
\r
152 description, serviceContext);
\r
154 // Apply parent folder permissions
\r
155 PermissionsUtils.setParentPermissionToFolder(copiedFolder);
\r
158 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFolder.class);
\r
159 indexer.reindex(copiedFolder);
\r
163 return HttpServletResponse.SC_FORBIDDEN;
\r
166 catch (DuplicateFolderNameException dfne) {
\r
167 return HttpServletResponse.SC_PRECONDITION_FAILED;
\r
169 catch (PrincipalException pe) {
\r
170 return HttpServletResponse.SC_FORBIDDEN;
\r
172 catch (Exception e) {
\r
174 throw new WebDAVException(e);
\r
179 public int copySimpleResource(
\r
180 WebDAVRequest webDavRequest, Resource resource, String destination,
\r
182 throws WebDAVException {
\r
187 String[] destinationArray = WebDAVUtil.getPathArray(
\r
188 destination, true);
\r
190 long companyId = webDavRequest.getCompanyId();
\r
192 long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
\r
195 parentFolderId = getParentFolderId(companyId, destinationArray);
\r
197 catch (NoSuchFolderException nsfe) {
\r
198 return HttpServletResponse.SC_CONFLICT;
\r
201 Folder parentFolder = DLAppLocalServiceUtil.getFolder(parentFolderId);
\r
203 FileEntry fileEntry = (FileEntry)resource.getModel();
\r
205 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), parentFolder, "ADD_OBJECT")) {
\r
207 long groupId = WebDAVUtil.getGroupId(companyId, destination);
\r
208 String mimeType = fileEntry.getMimeType();
\r
209 String title = WebDAVUtil.getResourceName(destinationArray);
\r
210 String description = fileEntry.getDescription();
\r
211 String changeLog = StringPool.BLANK;
\r
213 InputStream is = fileEntry.getContentStream();
\r
215 file = FileUtil.createTempFile(is);
\r
217 ServiceContext serviceContext = new ServiceContext();
\r
219 serviceContext.setAddGroupPermissions(
\r
220 isAddGroupPermissions(groupId));
\r
221 serviceContext.setAddGuestPermissions(true);
\r
223 int status = HttpServletResponse.SC_CREATED;
\r
226 if (deleteResource(
\r
227 groupId, parentFolderId, title,
\r
228 webDavRequest.getLockUuid())) {
\r
230 status = HttpServletResponse.SC_NO_CONTENT;
\r
234 FileEntry newFileEntry = DLAppServiceUtil.addFileEntry(
\r
235 groupId, parentFolderId, title, mimeType, title, description,
\r
236 changeLog, file, serviceContext);
\r
238 // Apply parent folder permissions
\r
239 PermissionsUtils.setParentPermissionToFile(newFileEntry);
\r
242 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);
\r
243 indexer.reindex(newFileEntry);
\r
247 return HttpServletResponse.SC_FORBIDDEN;
\r
250 catch (DuplicateFileException dfe) {
\r
251 return HttpServletResponse.SC_PRECONDITION_FAILED;
\r
253 catch (DuplicateFolderNameException dfne) {
\r
254 return HttpServletResponse.SC_PRECONDITION_FAILED;
\r
256 catch (LockException le) {
\r
257 return WebDAVUtil.SC_LOCKED;
\r
259 catch (PrincipalException pe) {
\r
260 return HttpServletResponse.SC_FORBIDDEN;
\r
262 catch (Exception e) {
\r
264 throw new WebDAVException(e);
\r
267 FileUtil.delete(file);
\r
272 public int deleteResource(WebDAVRequest webDavRequest)
\r
273 throws WebDAVException {
\r
275 Resource resource = getResource(webDavRequest);
\r
277 if (resource == null) {
\r
278 if (webDavRequest.isAppleDoubleRequest()) {
\r
279 return HttpServletResponse.SC_NO_CONTENT;
\r
282 return HttpServletResponse.SC_NOT_FOUND;
\r
286 Object model = resource.getModel();
\r
288 if (model instanceof Folder) {
\r
289 Folder folder = (Folder)model;
\r
290 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), folder, "DELETE")) {
\r
291 DLAppServiceUtil.deleteFolder(folder.getFolderId());
\r
293 return HttpServletResponse.SC_FORBIDDEN;
\r
297 FileEntry fileEntry = (FileEntry)model;
\r
299 if (!hasLock(fileEntry, webDavRequest.getLockUuid()) &&
\r
300 (fileEntry.getLock() != null)) {
\r
301 return WebDAVUtil.SC_LOCKED;
\r
304 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), fileEntry, "DELETE")) {
\r
305 DLAppServiceUtil.deleteFileEntry(fileEntry.getFileEntryId());
\r
307 return HttpServletResponse.SC_FORBIDDEN;
\r
311 return HttpServletResponse.SC_NO_CONTENT;
\r
313 catch (PrincipalException pe) {
\r
314 return HttpServletResponse.SC_FORBIDDEN;
\r
316 catch (Exception e) {
\r
318 throw new WebDAVException(e);
\r
322 public Resource getResource(WebDAVRequest webDavRequest)
\r
323 throws WebDAVException {
\r
326 String[] pathArray = webDavRequest.getPathArray();
\r
328 long companyId = webDavRequest.getCompanyId();
\r
329 long parentFolderId = getParentFolderId(companyId, pathArray);
\r
330 String name = WebDAVUtil.getResourceName(pathArray);
\r
332 if (Validator.isNull(name)) {
\r
333 String path = getRootPath() + webDavRequest.getPath();
\r
335 return new BaseResourceImpl(path, StringPool.BLANK, getToken());
\r
339 Folder folder = DLAppServiceUtil.getFolder(
\r
340 webDavRequest.getGroupId(), parentFolderId, name);
\r
342 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), folder, "VIEW")) {
\r
344 if ((folder.getParentFolderId() != parentFolderId) ||
\r
345 (webDavRequest.getGroupId() != folder.getRepositoryId())) {
\r
347 throw new NoSuchFolderException();
\r
350 return toResource(webDavRequest, folder, false);
\r
355 catch (NoSuchFolderException nsfe) {
\r
357 String titleWithExtension = name;
\r
359 FileEntry fileEntry = DLAppServiceUtil.getFileEntry(
\r
360 webDavRequest.getGroupId(), parentFolderId,
\r
361 titleWithExtension);
\r
363 // Si le file a une taille nulle, on la recalcule
\r
364 if(fileEntry.getSize() == 0){
\r
365 InputStream is = fileEntry.getContentStream();
\r
366 byte[] bytes = FileUtil.getBytes(is);
\r
367 DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getDLFileEntry(fileEntry.getFileEntryId());
\r
368 dlFileEntry.setSize(bytes.length);
\r
369 DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry, false);
\r
372 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), fileEntry, "VIEW")) {
\r
373 return toResource(webDavRequest, fileEntry, false);
\r
378 catch (NoSuchFileEntryException nsfee) {
\r
383 catch (Exception e) {
\r
384 throw new WebDAVException(e);
\r
388 public List<Resource> getResources(WebDAVRequest webDavRequest)
\r
389 throws WebDAVException {
\r
392 long folderId = getFolderId(
\r
393 webDavRequest.getCompanyId(), webDavRequest.getPathArray());
\r
395 List<Resource> folders = getFolders(webDavRequest, folderId);
\r
396 List<Resource> fileEntries = getFileEntries(
\r
397 webDavRequest, folderId);
\r
399 List<Resource> resources = new ArrayList<Resource>(
\r
400 folders.size() + fileEntries.size());
\r
402 resources.addAll(folders);
\r
403 resources.addAll(fileEntries);
\r
407 catch (Exception e) {
\r
408 throw new WebDAVException(e);
\r
413 public boolean isSupportsClassTwo() {
\r
418 public Status lockResource(
\r
419 WebDAVRequest webDavRequest, String owner, long timeout)
\r
420 throws WebDAVException {
\r
422 Resource resource = getResource(webDavRequest);
\r
425 int status = HttpServletResponse.SC_OK;
\r
428 if (resource == null) {
\r
429 status = HttpServletResponse.SC_CREATED;
\r
431 HttpServletRequest request =
\r
432 webDavRequest.getHttpServletRequest();
\r
434 String[] pathArray = webDavRequest.getPathArray();
\r
436 long companyId = webDavRequest.getCompanyId();
\r
437 long groupId = webDavRequest.getGroupId();
\r
438 long parentFolderId = getParentFolderId(companyId, pathArray);
\r
439 String title = WebDAVUtil.getResourceName(pathArray);
\r
441 String contentType = GetterUtil.get(
\r
442 request.getHeader(HttpHeaders.CONTENT_TYPE),
\r
443 ContentTypes.APPLICATION_OCTET_STREAM);
\r
445 if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
\r
446 contentType = MimeTypesUtil.getContentType(
\r
447 request.getInputStream(), title);
\r
450 String description = StringPool.BLANK;
\r
451 String changeLog = StringPool.BLANK;
\r
453 File file = FileUtil.createTempFile(
\r
454 FileUtil.getExtension(title));
\r
456 file.createNewFile();
\r
458 ServiceContext serviceContext = new ServiceContext();
\r
460 serviceContext.setAddGroupPermissions(
\r
461 isAddGroupPermissions(groupId));
\r
462 serviceContext.setAddGuestPermissions(true);
\r
464 FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
\r
465 groupId, parentFolderId, title, contentType, title,
\r
466 description, changeLog, file, serviceContext);
\r
468 // Apply parent folder permissions
\r
469 PermissionsUtils.setParentPermissionToFile(fileEntry);
\r
472 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);
\r
473 indexer.reindex(fileEntry);
\r
475 resource = toResource(webDavRequest, fileEntry, false);
\r
478 if (resource instanceof DLFileEntryResourceImpl) {
\r
479 FileEntry fileEntry = (FileEntry)resource.getModel();
\r
481 DLAppServiceUtil.checkOutFileEntry(
\r
482 fileEntry.getFileEntryId(), owner, timeout,
\r
483 new ServiceContext());
\r
485 lock = fileEntry.getLock();
\r
488 boolean inheritable = false;
\r
490 long depth = WebDAVUtil.getDepth(
\r
491 webDavRequest.getHttpServletRequest());
\r
494 inheritable = true;
\r
497 Folder folder = (Folder)resource.getModel();
\r
499 lock = DLAppServiceUtil.lockFolder(
\r
500 folder.getRepositoryId(), folder.getFolderId(), owner,
\r
501 inheritable, timeout);
\r
504 catch (Exception e) {
\r
506 // DuplicateLock is 423 not 501
\r
508 if (!(e instanceof DuplicateLockException)) {
\r
509 throw new WebDAVException(e);
\r
512 status = WebDAVUtil.SC_LOCKED;
\r
515 resource.setLock(lock);
\r
517 return new Status(lock, status);
\r
521 public Status makeCollection(WebDAVRequest webDavRequest)
\r
522 throws WebDAVException {
\r
525 HttpServletRequest request = webDavRequest.getHttpServletRequest();
\r
527 if (request.getContentLength() > 0) {
\r
529 HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
\r
532 String[] pathArray = webDavRequest.getPathArray();
\r
534 long companyId = webDavRequest.getCompanyId();
\r
535 long groupId = webDavRequest.getGroupId();
\r
536 long parentFolderId = getParentFolderId(companyId, pathArray);
\r
537 String name = WebDAVUtil.getResourceName(pathArray);
\r
538 String description = StringPool.BLANK;
\r
540 ServiceContext serviceContext = new ServiceContext();
\r
542 serviceContext.setAddGroupPermissions(
\r
543 isAddGroupPermissions(groupId));
\r
544 serviceContext.setAddGuestPermissions(true);
\r
546 Folder folder = DLAppServiceUtil.addFolder(
\r
547 groupId, parentFolderId, name, description, serviceContext);
\r
549 // Apply parent folder permissions to the created folder
\r
550 PermissionsUtils.setParentPermissionToFolder(folder);
\r
553 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFolder.class);
\r
554 indexer.reindex(folder);
\r
556 String location = StringUtil.merge(pathArray, StringPool.SLASH);
\r
558 return new Status(location, HttpServletResponse.SC_CREATED);
\r
560 catch (DuplicateFolderNameException dfne) {
\r
561 return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
563 catch (DuplicateFileException dfe) {
\r
564 return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
\r
566 catch (NoSuchFolderException nsfe) {
\r
567 return new Status(HttpServletResponse.SC_CONFLICT);
\r
569 catch (PrincipalException pe) {
\r
570 return new Status(HttpServletResponse.SC_FORBIDDEN);
\r
572 catch (Exception e) {
\r
573 throw new WebDAVException(e);
\r
578 public int moveCollectionResource(
\r
579 WebDAVRequest webDavRequest, Resource resource, String destination,
\r
581 throws WebDAVException {
\r
584 String[] destinationArray = WebDAVUtil.getPathArray(
\r
585 destination, true);
\r
587 Folder folder = (Folder)resource.getModel();
\r
589 long companyId = webDavRequest.getCompanyId();
\r
590 long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
\r
591 long folderId = folder.getFolderId();
\r
592 long parentFolderId = getParentFolderId(
\r
593 companyId, destinationArray);
\r
595 Folder parentFolder = DLAppLocalServiceUtil.getFolder(parentFolderId);
\r
597 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), parentFolder, "ADD_OBJECT")) {
\r
599 String name = WebDAVUtil.getResourceName(destinationArray);
\r
600 String description = folder.getDescription();
\r
602 ServiceContext serviceContext = new ServiceContext();
\r
604 int status = HttpServletResponse.SC_CREATED;
\r
607 if (deleteResource(
\r
608 groupId, parentFolderId, name,
\r
609 webDavRequest.getLockUuid())) {
\r
611 status = HttpServletResponse.SC_NO_CONTENT;
\r
615 if (parentFolderId != folder.getParentFolderId()) {
\r
616 DLAppServiceUtil.moveFolder(
\r
617 folderId, parentFolderId, serviceContext);
\r
620 if (!name.equals(folder.getName())) {
\r
621 folder = DLAppServiceUtil.updateFolder(
\r
622 folderId, name, description, serviceContext);
\r
625 // Apply new parent folder permissions to the moved folder
\r
626 PermissionsUtils.setParentPermissionToFolder(folder);
\r
629 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFolder.class);
\r
630 indexer.reindex(folder);
\r
635 return HttpServletResponse.SC_FORBIDDEN;
\r
638 catch (PrincipalException pe) {
\r
639 return HttpServletResponse.SC_FORBIDDEN;
\r
641 catch (DuplicateFolderNameException dfne) {
\r
642 return HttpServletResponse.SC_PRECONDITION_FAILED;
\r
644 catch (Exception e) {
\r
645 throw new WebDAVException(e);
\r
650 public int moveSimpleResource(
\r
651 WebDAVRequest webDavRequest, Resource resource, String destination,
\r
653 throws WebDAVException {
\r
658 String[] destinationArray = WebDAVUtil.getPathArray(
\r
659 destination, true);
\r
661 FileEntry fileEntry = (FileEntry)resource.getModel();
\r
663 if (!hasLock(fileEntry, webDavRequest.getLockUuid()) &&
\r
664 (fileEntry.getLock() != null)) {
\r
666 return WebDAVUtil.SC_LOCKED;
\r
669 long companyId = webDavRequest.getCompanyId();
\r
670 long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
\r
671 long newParentFolderId = getParentFolderId(
\r
672 companyId, destinationArray);
\r
674 Folder parentFolder = DLAppLocalServiceUtil.getFolder(newParentFolderId);
\r
676 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), parentFolder, "ADD_OBJECT")) {
\r
678 String sourceFileName = WebDAVUtil.getResourceName(
\r
680 String title = WebDAVUtil.getResourceName(destinationArray);
\r
681 String description = fileEntry.getDescription();
\r
682 String changeLog = StringPool.BLANK;
\r
684 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
\r
685 DLFileEntryConstants.getClassName(),
\r
686 fileEntry.getFileEntryId());
\r
688 ServiceContext serviceContext = new ServiceContext();
\r
690 serviceContext.setAssetTagNames(assetTagNames);
\r
692 int status = HttpServletResponse.SC_CREATED;
\r
695 if (deleteResource(
\r
696 groupId, newParentFolderId, title,
\r
697 webDavRequest.getLockUuid())) {
\r
699 status = HttpServletResponse.SC_NO_CONTENT;
\r
705 if (webDavRequest.isMac()) {
\r
707 FileEntry destFileEntry = DLAppServiceUtil.getFileEntry(
\r
708 groupId, newParentFolderId, title);
\r
710 InputStream is = fileEntry.getContentStream();
\r
712 file = FileUtil.createTempFile(is);
\r
714 DLAppServiceUtil.updateFileEntry(
\r
715 destFileEntry.getFileEntryId(),
\r
716 destFileEntry.getTitle(), destFileEntry.getMimeType(),
\r
717 destFileEntry.getTitle(),
\r
718 destFileEntry.getDescription(), changeLog, false, file,
\r
721 DLAppServiceUtil.deleteFileEntry(
\r
722 fileEntry.getFileEntryId());
\r
726 catch (NoSuchFileEntryException nsfee) {
\r
730 DLAppServiceUtil.updateFileEntry(
\r
731 fileEntry.getFileEntryId(), sourceFileName,
\r
732 fileEntry.getMimeType(), title, description, changeLog, false,
\r
733 file, serviceContext);
\r
735 if (fileEntry.getFolderId() != newParentFolderId) {
\r
736 fileEntry = DLAppServiceUtil.moveFileEntry(
\r
737 fileEntry.getFileEntryId(), newParentFolderId,
\r
741 // Apply new parent folder permissions
\r
742 PermissionsUtils.setParentPermissionToFile(fileEntry);
\r
747 return HttpServletResponse.SC_FORBIDDEN;
\r
750 catch (PrincipalException pe) {
\r
751 return HttpServletResponse.SC_FORBIDDEN;
\r
753 catch (DuplicateFileException dfe) {
\r
754 return HttpServletResponse.SC_PRECONDITION_FAILED;
\r
756 catch (DuplicateFolderNameException dfne) {
\r
757 return HttpServletResponse.SC_PRECONDITION_FAILED;
\r
759 catch (LockException le) {
\r
760 return WebDAVUtil.SC_LOCKED;
\r
762 catch (Exception e) {
\r
763 throw new WebDAVException(e);
\r
766 FileUtil.delete(file);
\r
771 public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
\r
775 HttpServletRequest request = webDavRequest.getHttpServletRequest();
\r
777 String[] pathArray = webDavRequest.getPathArray();
\r
779 long companyId = webDavRequest.getCompanyId();
\r
780 long groupId = webDavRequest.getGroupId();
\r
781 long parentFolderId = getParentFolderId(companyId, pathArray);
\r
783 Folder parentFolder = DLAppLocalServiceUtil.getFolder(parentFolderId);
\r
785 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), parentFolder, "ADD_OBJECT")) {
\r
787 String title = WebDAVUtil.getResourceName(pathArray);
\r
788 String description = StringPool.BLANK;
\r
789 String changeLog = StringPool.BLANK;
\r
791 ServiceContext serviceContext = ServiceContextFactory.getInstance(
\r
794 serviceContext.setAddGroupPermissions(
\r
795 isAddGroupPermissions(groupId));
\r
796 serviceContext.setAddGuestPermissions(true);
\r
798 String contentType = GetterUtil.get(
\r
799 request.getHeader(HttpHeaders.CONTENT_TYPE),
\r
800 ContentTypes.APPLICATION_OCTET_STREAM);
\r
802 String extension = FileUtil.getExtension(title);
\r
804 file = FileUtil.createTempFile(extension);
\r
806 FileUtil.write(file, request.getInputStream());
\r
808 if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
\r
809 contentType = MimeTypesUtil.getContentType(file, title);
\r
813 FileEntry fileEntry = DLAppServiceUtil.getFileEntry(
\r
814 groupId, parentFolderId, title);
\r
816 if (!hasLock(fileEntry, webDavRequest.getLockUuid()) &&
\r
817 (fileEntry.getLock() != null)) {
\r
819 return WebDAVUtil.SC_LOCKED;
\r
822 long fileEntryId = fileEntry.getFileEntryId();
\r
824 description = fileEntry.getDescription();
\r
826 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
\r
827 DLFileEntryConstants.getClassName(),
\r
828 fileEntry.getFileEntryId());
\r
830 serviceContext.setAssetTagNames(assetTagNames);
\r
832 /*DLAppServiceUtil.updateFileEntry(
\r
833 fileEntryId, title, contentType, title, description,
\r
834 changeLog, false, file, serviceContext);*/
\r
836 DLAppUtil.updateFileEntry(fileEntry, title, file);
\r
839 catch (NoSuchFileEntryException nsfee) {
\r
840 if (file.length() == 0) {
\r
841 serviceContext.setWorkflowAction(
\r
842 WorkflowConstants.ACTION_SAVE_DRAFT);
\r
845 FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
\r
846 groupId, parentFolderId, title, contentType, title,
\r
847 description, changeLog, file, serviceContext);
\r
849 // Apply parent folder permissions to this file
\r
850 PermissionsUtils.setParentPermissionToFile(fileEntry);
\r
853 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);
\r
854 indexer.reindex(fileEntry);
\r
858 if (_log.isInfoEnabled()) {
\r
860 "Added " + StringUtil.merge(pathArray, StringPool.SLASH));
\r
863 return HttpServletResponse.SC_CREATED;
\r
866 return HttpServletResponse.SC_FORBIDDEN;
\r
869 catch (PrincipalException pe) {
\r
870 return HttpServletResponse.SC_FORBIDDEN;
\r
872 catch (NoSuchFolderException nsfe) {
\r
873 return HttpServletResponse.SC_CONFLICT;
\r
875 catch (PortalException pe) {
\r
876 if (_log.isWarnEnabled()) {
\r
879 return HttpServletResponse.SC_CONFLICT;
\r
881 catch (Exception e) {
\r
883 throw new WebDAVException(e);
\r
886 FileUtil.delete(file);
\r
891 public Lock refreshResourceLock(
\r
892 WebDAVRequest webDavRequest, String uuid, long timeout)
\r
893 throws WebDAVException {
\r
895 Resource resource = getResource(webDavRequest);
\r
900 if (resource instanceof DLFileEntryResourceImpl) {
\r
901 lock = DLAppServiceUtil.refreshFileEntryLock(uuid, timeout);
\r
904 lock = DLAppServiceUtil.refreshFolderLock(uuid, timeout);
\r
907 resource.setLock(lock);
\r
909 catch (Exception e) {
\r
910 throw new WebDAVException(e);
\r
917 public boolean unlockResource(WebDAVRequest webDavRequest, String token)
\r
918 throws WebDAVException {
\r
920 Resource resource = getResource(webDavRequest);
\r
923 if (resource instanceof DLFileEntryResourceImpl) {
\r
924 FileEntry fileEntry = (FileEntry)resource.getModel();
\r
926 DLAppServiceUtil.checkInFileEntry(
\r
927 fileEntry.getFileEntryId(), token);
\r
929 if (webDavRequest.isAppleDoubleRequest()) {
\r
930 DLAppServiceUtil.deleteFileEntry(
\r
931 fileEntry.getFileEntryId());
\r
935 Folder folder = (Folder)resource.getModel();
\r
937 DLAppServiceUtil.unlockFolder(
\r
938 folder.getRepositoryId(), folder.getParentFolderId(),
\r
939 folder.getName(), token);
\r
942 resource.setLock(null);
\r
946 catch (Exception e) {
\r
947 if (e instanceof InvalidLockException) {
\r
948 if (_log.isWarnEnabled()) {
\r
949 _log.warn(e.getMessage());
\r
953 if (_log.isWarnEnabled()) {
\r
954 _log.warn("Unable to unlock file entry", e);
\r
962 protected boolean deleteResource(
\r
963 long groupId, long parentFolderId, String name, String lockUuid)
\r
968 Folder folder = DLAppServiceUtil.getFolder(
\r
969 groupId, parentFolderId, name);
\r
971 DLAppServiceUtil.deleteFolder(folder.getFolderId());
\r
975 catch (NoSuchFolderException nsfe) {
\r
977 FileEntry fileEntry = DLAppServiceUtil.getFileEntry(
\r
978 groupId, parentFolderId, name);
\r
980 if (!hasLock(fileEntry, lockUuid) &&
\r
981 (fileEntry.getLock() != null)) {
\r
982 throw new LockException();
\r
985 DLAppServiceUtil.deleteFileEntryByTitle(
\r
986 groupId, parentFolderId, name);
\r
990 catch (NoSuchFileEntryException nsfee) {
\r
998 protected List<Resource> getFileEntries(
\r
999 WebDAVRequest webDavRequest, long parentFolderId)
\r
1000 throws Exception {
\r
1002 List<Resource> resources = new ArrayList<Resource>();
\r
1004 List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
\r
1005 webDavRequest.getGroupId(), parentFolderId);
\r
1007 for (FileEntry fileEntry : fileEntries) {
\r
1008 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), fileEntry, "VIEW")) {
\r
1009 Resource resource = toResource(webDavRequest, fileEntry, true);
\r
1010 resources.add(resource);
\r
1017 protected long getFolderId(long companyId, String[] pathArray)
\r
1018 throws Exception {
\r
1020 return getFolderId(companyId, pathArray, false);
\r
1023 protected long getFolderId(
\r
1024 long companyId, String[] pathArray, boolean parent)
\r
1025 throws Exception {
\r
1027 long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
\r
1029 if (pathArray.length <= 1) {
\r
1033 long groupId = WebDAVUtil.getGroupId(companyId, pathArray);
\r
1035 int x = pathArray.length;
\r
1041 for (int i = 2; i < x; i++) {
\r
1042 String name = pathArray[i];
\r
1044 Folder folder = DLAppServiceUtil.getFolder(
\r
1045 groupId, folderId, name);
\r
1047 if (groupId == folder.getRepositoryId()) {
\r
1048 folderId = folder.getFolderId();
\r
1056 protected List<Resource> getFolders(
\r
1057 WebDAVRequest webDavRequest, long parentFolderId)
\r
1058 throws Exception {
\r
1060 List<Resource> resources = new ArrayList<Resource>();
\r
1062 long groupId = webDavRequest.getGroupId();
\r
1064 List<Folder> folders = DLAppServiceUtil.getFolders(
\r
1065 groupId, parentFolderId, false);
\r
1067 for (Folder folder : folders) {
\r
1068 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), folder, "VIEW")) {
\r
1069 Resource resource = toResource(webDavRequest, folder, true);
\r
1070 resources.add(resource);
\r
1077 protected long getParentFolderId(long companyId, String[] pathArray)
\r
1078 throws Exception {
\r
1080 return getFolderId(companyId, pathArray, true);
\r
1083 protected boolean hasLock(FileEntry fileEntry, String lockUuid)
\r
1084 throws Exception {
\r
1086 if (Validator.isNull(lockUuid)) {
\r
1088 // Client does not claim to know of a lock
\r
1090 return fileEntry.hasLock();
\r
1094 // Client claims to know of a lock. Verify the lock UUID.
\r
1097 return DLAppServiceUtil.verifyFileEntryLock(
\r
1098 fileEntry.getRepositoryId(), fileEntry.getFileEntryId(),
\r
1101 catch (NoSuchLockException nsle) {
\r
1107 protected Resource toResource(
\r
1108 WebDAVRequest webDavRequest, FileEntry fileEntry, boolean appendPath) throws Exception {
\r
1110 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), fileEntry, "VIEW")) {
\r
1111 String parentPath = getRootPath() + webDavRequest.getPath();
\r
1112 String name = StringPool.BLANK;
\r
1115 name = fileEntry.getTitle();
\r
1118 return new DLFileEntryResourceImpl(
\r
1119 webDavRequest, fileEntry, parentPath, name);
\r
1125 protected Resource toResource(
\r
1126 WebDAVRequest webDavRequest, Folder folder, boolean appendPath) {
\r
1129 if (PermissionsUtils.hasUserPermissionForRessourceInGroup(webDavRequest.getPermissionChecker(), folder, "VIEW")) {
\r
1130 String parentPath = getRootPath() + webDavRequest.getPath();
\r
1131 String name = StringPool.BLANK;
\r
1134 name = folder.getName();
\r
1137 Resource resource = new BaseResourceImpl(
\r
1138 parentPath, name, folder.getName(), folder.getCreateDate(),
\r
1139 folder.getModifiedDate());
\r
1141 resource.setModel(folder);
\r
1142 resource.setClassName(Folder.class.getName());
\r
1143 resource.setPrimaryKey(folder.getPrimaryKey());
\r
1149 } catch (Exception e) {
\r
1154 private static Log _log = LogFactoryUtil.getLog(DLWebDAVStorageImpl.class);
\r