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 =
586                                                    DLAppServiceUtil.getFileEntry(
587                                                            groupId, newParentFolderId, title);
588    
589                                            InputStream is = fileEntry.getContentStream();
590    
591                                            file = FileUtil.createTempFile(is);
592    
593                                            DLAppServiceUtil.updateFileEntry(
594                                                    destFileEntry.getFileEntryId(),
595                                                    destFileEntry.getTitle(), destFileEntry.getMimeType(),
596                                                    destFileEntry.getTitle(),
597                                                    destFileEntry.getDescription(), changeLog, false, file,
598                                                    serviceContext);
599    
600                                            DLAppServiceUtil.deleteFileEntry(
601                                                    fileEntry.getFileEntryId());
602    
603                                            return status;
604                                    }
605                                    catch (NoSuchFileEntryException nsfee) {
606                                    }
607                            }
608    
609                            DLAppServiceUtil.updateFileEntry(
610                                    fileEntry.getFileEntryId(), sourceFileName,
611                                    fileEntry.getMimeType(), title, description, changeLog, false,
612                                    file, serviceContext);
613    
614                            if (fileEntry.getFolderId() != newParentFolderId) {
615                                    fileEntry = DLAppServiceUtil.moveFileEntry(
616                                            fileEntry.getFileEntryId(), newParentFolderId,
617                                            serviceContext);
618                            }
619    
620                            return status;
621                    }
622                    catch (PrincipalException pe) {
623                            return HttpServletResponse.SC_FORBIDDEN;
624                    }
625                    catch (DuplicateFileException dfe) {
626                            return HttpServletResponse.SC_PRECONDITION_FAILED;
627                    }
628                    catch (DuplicateFolderNameException dfne) {
629                            return HttpServletResponse.SC_PRECONDITION_FAILED;
630                    }
631                    catch (LockException le) {
632                            return WebDAVUtil.SC_LOCKED;
633                    }
634                    catch (Exception e) {
635                            throw new WebDAVException(e);
636                    }
637                    finally {
638                            FileUtil.delete(file);
639                    }
640            }
641    
642            @Override
643            public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
644                    File file = null;
645    
646                    try {
647                            HttpServletRequest request = webDavRequest.getHttpServletRequest();
648    
649                            String[] pathArray = webDavRequest.getPathArray();
650    
651                            long companyId = webDavRequest.getCompanyId();
652                            long groupId = webDavRequest.getGroupId();
653                            long parentFolderId = getParentFolderId(companyId, pathArray);
654                            String title = WebDAVUtil.getResourceName(pathArray);
655                            String description = StringPool.BLANK;
656                            String changeLog = StringPool.BLANK;
657    
658                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
659                                    request);
660    
661                            serviceContext.setAddGroupPermissions(
662                                    isAddGroupPermissions(groupId));
663                            serviceContext.setAddGuestPermissions(true);
664    
665                            String contentType = GetterUtil.get(
666                                    request.getHeader(HttpHeaders.CONTENT_TYPE),
667                                    ContentTypes.APPLICATION_OCTET_STREAM);
668    
669                            String extension = FileUtil.getExtension(title);
670    
671                            file = FileUtil.createTempFile(extension);
672    
673                            FileUtil.write(file, request.getInputStream());
674    
675                            if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
676                                    contentType = MimeTypesUtil.getContentType(file, title);
677                            }
678    
679                            try {
680                                    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(
681                                            groupId, parentFolderId, title);
682    
683                                    if (!hasLock(fileEntry, webDavRequest.getLockUuid()) &&
684                                            (fileEntry.getLock() != null)) {
685    
686                                            return WebDAVUtil.SC_LOCKED;
687                                    }
688    
689                                    long fileEntryId = fileEntry.getFileEntryId();
690    
691                                    description = fileEntry.getDescription();
692    
693                                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
694                                            FileEntry.class.getName(), fileEntry.getFileEntryId());
695    
696                                    serviceContext.setAssetTagNames(assetTagNames);
697    
698                                    DLAppServiceUtil.updateFileEntry(
699                                            fileEntryId, title, contentType, title, description,
700                                            changeLog, false, file, serviceContext);
701                            }
702                            catch (NoSuchFileEntryException nsfee) {
703                                    if (file.length() == 0) {
704                                            serviceContext.setWorkflowAction(
705                                                    WorkflowConstants.ACTION_SAVE_DRAFT);
706                                    }
707    
708                                    DLAppServiceUtil.addFileEntry(
709                                            groupId, parentFolderId, title, contentType, title,
710                                            description, changeLog, file, serviceContext);
711                            }
712    
713                            if (_log.isInfoEnabled()) {
714                                    _log.info(
715                                            "Added " + StringUtil.merge(pathArray, StringPool.SLASH));
716                            }
717    
718                            return HttpServletResponse.SC_CREATED;
719                    }
720                    catch (PrincipalException pe) {
721                            return HttpServletResponse.SC_FORBIDDEN;
722                    }
723                    catch (NoSuchFolderException nsfe) {
724                            return HttpServletResponse.SC_CONFLICT;
725                    }
726                    catch (PortalException pe) {
727                            if (_log.isWarnEnabled()) {
728                                    _log.warn(pe, pe);
729                            }
730    
731                            return HttpServletResponse.SC_CONFLICT;
732                    }
733                    catch (Exception e) {
734                            throw new WebDAVException(e);
735                    }
736                    finally {
737                            FileUtil.delete(file);
738                    }
739            }
740    
741            @Override
742            public Lock refreshResourceLock(
743                            WebDAVRequest webDavRequest, String uuid, long timeout)
744                    throws WebDAVException {
745    
746                    Resource resource = getResource(webDavRequest);
747    
748                    Lock lock = null;
749    
750                    try {
751                            if (resource instanceof DLFileEntryResourceImpl) {
752                                    lock = DLAppServiceUtil.refreshFileEntryLock(uuid, timeout);
753                            }
754                            else {
755                                    lock = DLAppServiceUtil.refreshFolderLock(uuid, timeout);
756                            }
757                    }
758                    catch (Exception e) {
759                            throw new WebDAVException(e);
760                    }
761    
762                    return lock;
763            }
764    
765            @Override
766            public boolean unlockResource(WebDAVRequest webDavRequest, String token)
767                    throws WebDAVException {
768    
769                    Resource resource = getResource(webDavRequest);
770    
771                    try {
772                            if (resource instanceof DLFileEntryResourceImpl) {
773                                    FileEntry fileEntry = (FileEntry)resource.getModel();
774    
775                                    DLAppServiceUtil.unlockFileEntry(
776                                            fileEntry.getFileEntryId(), token);
777    
778                                    if (webDavRequest.isAppleDoubleRequest()) {
779                                            DLAppServiceUtil.deleteFileEntry(
780                                                    fileEntry.getFileEntryId());
781                                    }
782                            }
783                            else {
784                                    Folder folder = (Folder)resource.getModel();
785    
786                                    DLAppServiceUtil.unlockFolder(
787                                            folder.getRepositoryId(), folder.getParentFolderId(),
788                                            folder.getName(), token);
789                            }
790    
791                            return true;
792                    }
793                    catch (Exception e) {
794                            if (e instanceof InvalidLockException) {
795                                    if (_log.isWarnEnabled()) {
796                                            _log.warn(e.getMessage());
797                                    }
798                            }
799                            else {
800                                    if (_log.isWarnEnabled()) {
801                                            _log.warn("Unable to unlock file entry", e);
802                                    }
803                            }
804                    }
805    
806                    return false;
807            }
808    
809            protected boolean deleteResource(
810                            long groupId, long parentFolderId, String name, String lockUuid)
811                    throws Exception {
812    
813                    try {
814                            Folder folder = DLAppServiceUtil.getFolder(
815                                    groupId, parentFolderId, name);
816    
817                            DLAppServiceUtil.deleteFolder(folder.getFolderId());
818    
819                            return true;
820                    }
821                    catch (NoSuchFolderException nsfe) {
822                            try {
823                                    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(
824                                            groupId, parentFolderId, name);
825    
826                                    if (!hasLock(fileEntry, lockUuid) &&
827                                            (fileEntry.getLock() != null)) {
828    
829                                            throw new LockException();
830                                    }
831    
832                                    DLAppServiceUtil.deleteFileEntryByTitle(
833                                            groupId, parentFolderId, name);
834    
835                                    return true;
836                            }
837                            catch (NoSuchFileEntryException nsfee) {
838                            }
839                    }
840    
841                    return false;
842            }
843    
844            protected List<Resource> getFileEntries(
845                            WebDAVRequest webDavRequest, long parentFolderId)
846                    throws Exception {
847    
848                    List<Resource> resources = new ArrayList<Resource>();
849    
850                    List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
851                            webDavRequest.getGroupId(), parentFolderId);
852    
853                    for (FileEntry fileEntry : fileEntries) {
854                            Resource resource = toResource(webDavRequest, fileEntry, true);
855    
856                            resources.add(resource);
857                    }
858    
859                    return resources;
860            }
861    
862            protected long getFolderId(long companyId, String[] pathArray)
863                    throws Exception {
864    
865                    return getFolderId(companyId, pathArray, false);
866            }
867    
868            protected long getFolderId(
869                            long companyId, String[] pathArray, boolean parent)
870                    throws Exception {
871    
872                    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
873    
874                    if (pathArray.length <= 1) {
875                            return folderId;
876                    }
877                    else {
878                            long groupId = WebDAVUtil.getGroupId(companyId, pathArray);
879    
880                            int x = pathArray.length;
881    
882                            if (parent) {
883                                    x--;
884                            }
885    
886                            for (int i = 2; i < x; i++) {
887                                    String name = pathArray[i];
888    
889                                    Folder folder = DLAppServiceUtil.getFolder(
890                                            groupId, folderId, name);
891    
892                                    if (groupId == folder.getRepositoryId()) {
893                                            folderId = folder.getFolderId();
894                                    }
895                            }
896                    }
897    
898                    return folderId;
899            }
900    
901            protected List<Resource> getFolders(
902                            WebDAVRequest webDavRequest, long parentFolderId)
903                    throws Exception {
904    
905                    List<Resource> resources = new ArrayList<Resource>();
906    
907                    long groupId = webDavRequest.getGroupId();
908    
909                    List<Folder> folders = DLAppServiceUtil.getFolders(
910                            groupId, parentFolderId, false);
911    
912                    for (Folder folder : folders) {
913                            Resource resource = toResource(webDavRequest, folder, true);
914    
915                            resources.add(resource);
916                    }
917    
918                    return resources;
919            }
920    
921            protected long getParentFolderId(long companyId, String[] pathArray)
922                    throws Exception {
923    
924                    return getFolderId(companyId, pathArray, true);
925            }
926    
927            protected boolean hasLock(FileEntry fileEntry, String lockUuid)
928                    throws Exception {
929    
930                    if (Validator.isNull(lockUuid)) {
931    
932                            // Client does not claim to know of a lock
933    
934                            return fileEntry.hasLock();
935                    }
936                    else {
937    
938                            // Client claims to know of a lock. Verify the lock UUID.
939    
940                            try {
941                                    return DLAppServiceUtil.verifyFileEntryLock(
942                                            fileEntry.getRepositoryId(), fileEntry.getFileEntryId(),
943                                            lockUuid);
944                            }
945                            catch (NoSuchLockException nsle) {
946                                    return false;
947                            }
948                    }
949            }
950    
951            protected Resource toResource(
952                    WebDAVRequest webDavRequest, FileEntry fileEntry,
953                    boolean appendPath) {
954    
955                    String parentPath = getRootPath() + webDavRequest.getPath();
956                    String name = StringPool.BLANK;
957    
958                    if (appendPath) {
959                            name = fileEntry.getTitle();
960                    }
961    
962                    return new DLFileEntryResourceImpl(
963                            webDavRequest, fileEntry, parentPath, name);
964            }
965    
966            protected Resource toResource(
967                    WebDAVRequest webDavRequest, Folder folder, boolean appendPath) {
968    
969                    String parentPath = getRootPath() + webDavRequest.getPath();
970                    String name = StringPool.BLANK;
971    
972                    if (appendPath) {
973                            name = folder.getName();
974                    }
975    
976                    Resource resource = new BaseResourceImpl(
977                            parentPath, name, folder.getName(), folder.getCreateDate(),
978                            folder.getModifiedDate());
979    
980                    resource.setModel(folder);
981                    resource.setClassName(Folder.class.getName());
982                    resource.setPrimaryKey(folder.getPrimaryKey());
983    
984                    return resource;
985            }
986    
987            private static Log _log = LogFactoryUtil.getLog(DLWebDAVStorageImpl.class);
988    
989    }