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