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