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