Example usage for com.liferay.portal.kernel.webdav WebDAVUtil getResourceName

List of usage examples for com.liferay.portal.kernel.webdav WebDAVUtil getResourceName

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.webdav WebDAVUtil getResourceName.

Prototype

public static String getResourceName(String[] pathArray) 

Source Link

Usage

From source file:com.liferay.portlet.documentlibrary.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public Status makeCollection(WebDAVRequest webDavRequest) throws WebDAVException {

    try {/*from  w  w  w  . j a v  a2s.  co m*/
        HttpServletRequest request = webDavRequest.getHttpServletRequest();

        if (request.getContentLength() > 0) {
            return new Status(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        }

        String[] pathArray = webDavRequest.getPathArray();

        long companyId = webDavRequest.getCompanyId();
        long groupId = webDavRequest.getGroupId();
        long parentFolderId = getParentFolderId(companyId, pathArray);
        String name = WebDAVUtil.getResourceName(pathArray);
        String description = StringPool.BLANK;

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setAddGroupPermissions(isAddGroupPermissions(groupId));
        serviceContext.setAddGuestPermissions(true);

        DLAppServiceUtil.addFolder(groupId, parentFolderId, name, description, serviceContext);

        String location = StringUtil.merge(pathArray, StringPool.SLASH);

        return new Status(location, HttpServletResponse.SC_CREATED);
    } catch (DuplicateFolderNameException dfne) {
        return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    } catch (DuplicateFileException dfe) {
        return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    } catch (NoSuchFolderException nsfe) {
        return new Status(HttpServletResponse.SC_CONFLICT);
    } catch (PrincipalException pe) {
        return new Status(HttpServletResponse.SC_FORBIDDEN);
    } catch (Exception e) {
        throw new WebDAVException(e);
    }
}

From source file:com.liferay.portlet.documentlibrary.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int moveCollectionResource(WebDAVRequest webDavRequest, Resource resource, String destination,
        boolean overwrite) throws WebDAVException {

    try {/*from ww  w  .j  ava2  s.c o m*/
        String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

        Folder folder = (Folder) resource.getModel();

        long companyId = webDavRequest.getCompanyId();
        long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
        long folderId = folder.getFolderId();
        long parentFolderId = getParentFolderId(companyId, destinationArray);
        String name = WebDAVUtil.getResourceName(destinationArray);
        String description = folder.getDescription();

        ServiceContext serviceContext = new ServiceContext();

        int status = HttpServletResponse.SC_CREATED;

        if (overwrite) {
            if (deleteResource(groupId, parentFolderId, name, webDavRequest.getLockUuid())) {

                status = HttpServletResponse.SC_NO_CONTENT;
            }
        }

        if (parentFolderId != folder.getParentFolderId()) {
            DLAppServiceUtil.moveFolder(folderId, parentFolderId, serviceContext);
        }

        if (!name.equals(folder.getName())) {
            DLAppServiceUtil.updateFolder(folderId, name, description, serviceContext);
        }

        return status;
    } catch (PrincipalException pe) {
        return HttpServletResponse.SC_FORBIDDEN;
    } catch (DuplicateFolderNameException dfne) {
        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (Exception e) {
        throw new WebDAVException(e);
    }
}

From source file:com.liferay.portlet.documentlibrary.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int moveSimpleResource(WebDAVRequest webDavRequest, Resource resource, String destination,
        boolean overwrite) throws WebDAVException {

    File file = null;/*from  w  ww. j av a  2 s .  c  om*/

    try {
        String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

        FileEntry fileEntry = (FileEntry) resource.getModel();

        if (!hasLock(fileEntry, webDavRequest.getLockUuid()) && (fileEntry.getLock() != null)) {

            return WebDAVUtil.SC_LOCKED;
        }

        long companyId = webDavRequest.getCompanyId();
        long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
        long newParentFolderId = getParentFolderId(companyId, destinationArray);
        String sourceFileName = WebDAVUtil.getResourceName(destinationArray);
        String title = WebDAVUtil.getResourceName(destinationArray);
        String description = fileEntry.getDescription();
        String changeLog = StringPool.BLANK;

        String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(FileEntry.class.getName(),
                fileEntry.getFileEntryId());

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setAssetTagNames(assetTagNames);

        int status = HttpServletResponse.SC_CREATED;

        if (overwrite) {
            if (deleteResource(groupId, newParentFolderId, title, webDavRequest.getLockUuid())) {

                status = HttpServletResponse.SC_NO_CONTENT;
            }
        }

        // LPS-5415

        if (webDavRequest.isMac()) {
            try {
                FileEntry destFileEntry = DLAppServiceUtil.getFileEntry(groupId, newParentFolderId, title);

                InputStream is = fileEntry.getContentStream();

                file = FileUtil.createTempFile(is);

                DLAppServiceUtil.updateFileEntry(destFileEntry.getFileEntryId(), destFileEntry.getTitle(),
                        destFileEntry.getMimeType(), destFileEntry.getTitle(), destFileEntry.getDescription(),
                        changeLog, false, file, serviceContext);

                DLAppServiceUtil.deleteFileEntry(fileEntry.getFileEntryId());

                return status;
            } catch (NoSuchFileEntryException nsfee) {
            }
        }

        DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), sourceFileName, fileEntry.getMimeType(),
                title, description, changeLog, false, file, serviceContext);

        if (fileEntry.getFolderId() != newParentFolderId) {
            fileEntry = DLAppServiceUtil.moveFileEntry(fileEntry.getFileEntryId(), newParentFolderId,
                    serviceContext);
        }

        return status;
    } catch (PrincipalException pe) {
        return HttpServletResponse.SC_FORBIDDEN;
    } catch (DuplicateFileException dfe) {
        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (DuplicateFolderNameException dfne) {
        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (LockException le) {
        return WebDAVUtil.SC_LOCKED;
    } catch (Exception e) {
        throw new WebDAVException(e);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int putResource(WebDAVRequest webDavRequest) throws WebDAVException {
    File file = null;//w  w  w.j  a va 2  s . c  om

    try {
        HttpServletRequest request = webDavRequest.getHttpServletRequest();

        String[] pathArray = webDavRequest.getPathArray();

        long companyId = webDavRequest.getCompanyId();
        long groupId = webDavRequest.getGroupId();
        long parentFolderId = getParentFolderId(companyId, pathArray);
        String title = WebDAVUtil.getResourceName(pathArray);
        String description = StringPool.BLANK;
        String changeLog = StringPool.BLANK;

        ServiceContext serviceContext = ServiceContextFactory.getInstance(request);

        serviceContext.setAddGroupPermissions(isAddGroupPermissions(groupId));
        serviceContext.setAddGuestPermissions(true);

        String contentType = GetterUtil.get(request.getHeader(HttpHeaders.CONTENT_TYPE),
                ContentTypes.APPLICATION_OCTET_STREAM);

        String extension = FileUtil.getExtension(title);

        file = FileUtil.createTempFile(extension);

        FileUtil.write(file, request.getInputStream());

        if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
            contentType = MimeTypesUtil.getContentType(file, title);
        }

        try {
            FileEntry fileEntry = DLAppServiceUtil.getFileEntry(groupId, parentFolderId, title);

            if (!hasLock(fileEntry, webDavRequest.getLockUuid()) && (fileEntry.getLock() != null)) {

                return WebDAVUtil.SC_LOCKED;
            }

            long fileEntryId = fileEntry.getFileEntryId();

            description = fileEntry.getDescription();

            String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(FileEntry.class.getName(),
                    fileEntry.getFileEntryId());

            serviceContext.setAssetTagNames(assetTagNames);

            DLAppServiceUtil.updateFileEntry(fileEntryId, title, contentType, title, description, changeLog,
                    false, file, serviceContext);
        } catch (NoSuchFileEntryException nsfee) {
            if (file.length() == 0) {
                serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
            }

            DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, contentType, title, description,
                    changeLog, file, serviceContext);
        }

        if (_log.isInfoEnabled()) {
            _log.info("Added " + StringUtil.merge(pathArray, StringPool.SLASH));
        }

        return HttpServletResponse.SC_CREATED;
    } catch (PrincipalException pe) {
        return HttpServletResponse.SC_FORBIDDEN;
    } catch (NoSuchFolderException nsfe) {
        return HttpServletResponse.SC_CONFLICT;
    } catch (PortalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn(pe, pe);
        }

        return HttpServletResponse.SC_CONFLICT;
    } catch (Exception e) {
        throw new WebDAVException(e);
    } finally {
        FileUtil.delete(file);
    }
}