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