Example usage for com.liferay.portal.kernel.util FileUtil delete

List of usage examples for com.liferay.portal.kernel.util FileUtil delete

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util FileUtil delete.

Prototype

public static boolean delete(String file) 

Source Link

Usage

From source file:com.liferay.portlet.calendar.action.ExportEventsAction.java

License:Open Source License

@Override
public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
        ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    File file = null;//from   www  .j ava  2s . co  m

    try {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        long eventId = ParamUtil.getLong(actionRequest, "eventId");

        String exportFileName = ParamUtil.getString(actionRequest, "exportFileName");

        if (Validator.isNull(exportFileName)) {
            exportFileName = "liferay.ics";
        } else {
            exportFileName = FileUtil.getShortFileName(exportFileName);
        }

        if (eventId > 0) {
            file = CalEventServiceUtil.exportEvent(eventId);
        } else {
            file = CalEventServiceUtil.exportGroupEvents(themeDisplay.getScopeGroupId(), exportFileName);
        }

        HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
        HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

        ServletResponseUtil.sendFile(request, response, exportFileName, new FileInputStream(file),
                ContentTypes.TEXT_CALENDAR);

        setForward(actionRequest, ActionConstants.COMMON_NULL);
    } catch (Exception e) {
        _log.error(e, e);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.service.DLAppServiceTest.java

License:Open Source License

public void testAddNullFileEntry() throws Exception {
    long folderId = _folder.getFolderId();
    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    ServiceContext serviceContext = new ServiceContext();

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

    try {/*  w  w  w .j a  va  2  s. co  m*/
        String name = "Bytes-null.txt";
        byte[] bytes = null;

        FileEntry fileEntry = DLAppServiceUtil.addFileEntry(TestPropsValues.getGroupId(), folderId, name,
                ContentTypes.TEXT_PLAIN, name, description, changeLog, bytes, serviceContext);

        DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), name, ContentTypes.TEXT_PLAIN, name,
                description, changeLog, true, bytes, serviceContext);

        String newName = "Bytes-changed.txt";

        bytes = _CONTENT.getBytes();

        DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), newName, ContentTypes.TEXT_PLAIN, newName,
                description, changeLog, true, bytes, serviceContext);
    } catch (Exception e) {
        fail("Unable to pass null byte[] " + StackTraceUtil.getStackTrace(e));
    }

    try {
        String name = "File-null.txt";
        File file = null;

        FileEntry fileEntry = DLAppServiceUtil.addFileEntry(TestPropsValues.getGroupId(), folderId, name,
                ContentTypes.TEXT_PLAIN, name, description, changeLog, file, serviceContext);

        DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), name, ContentTypes.TEXT_PLAIN, name,
                description, changeLog, true, file, serviceContext);

        try {
            String newName = "File-changed.txt";

            file = FileUtil.createTempFile(_CONTENT.getBytes());

            DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), newName, ContentTypes.TEXT_PLAIN,
                    newName, description, changeLog, true, file, serviceContext);
        } finally {
            FileUtil.delete(file);
        }
    } catch (Exception e) {
        fail("Unable to pass null File " + StackTraceUtil.getStackTrace(e));
    }

    try {
        String name = "IS-null.txt";
        InputStream is = null;

        FileEntry fileEntry = DLAppServiceUtil.addFileEntry(TestPropsValues.getGroupId(), folderId, name,
                ContentTypes.TEXT_PLAIN, name, description, changeLog, is, 0, serviceContext);

        DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), name, ContentTypes.TEXT_PLAIN, name,
                description, changeLog, true, is, 0, serviceContext);

        try {
            String newName = "IS-changed.txt";

            is = new ByteArrayInputStream(_CONTENT.getBytes());

            DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(), newName, ContentTypes.TEXT_PLAIN,
                    newName, description, changeLog, true, is, 0, serviceContext);
        } finally {
            if (is != null) {
                is.close();
            }
        }
    } catch (Exception e) {
        fail("Unable to pass null InputStream " + StackTraceUtil.getStackTrace(e));
    }
}

From source file:com.liferay.portlet.documentlibrary.service.impl.DLAppLocalServiceImpl.java

License:Open Source License

/**
 * Adds a file entry and associated metadata based on a byte array.
 *
 * <p>/*from  w  w  w . j  a va2 s .co  m*/
 * This method takes two file names, the <code>sourceFileName</code> and the
 * <code>title</code>. The <code>sourceFileName</code> corresponds to the
 * name of the actual file being uploaded. The <code>title</code>
 * corresponds to a name the client wishes to assign this file after it has
 * been uploaded to the portal. If it is <code>null</code>, the <code>
 * sourceFileName</code> will be used.
 * </p>
 *
 * @param  userId the primary key of the file entry's creator/owner
 * @param  repositoryId the primary key of the file entry's repository
 * @param  folderId the primary key of the file entry's parent folder
 * @param  sourceFileName the original file's name
 * @param  mimeType the file's MIME type
 * @param  title the name to be assigned to the file (optionally <code>null
 *         </code>)
 * @param  description the file's description
 * @param  changeLog the file's version change log
 * @param  bytes the file's data (optionally <code>null</code>)
 * @param  serviceContext the service context to be applied. Can set the
 *         asset category IDs, asset tag names, and expando bridge
 *         attributes for the file entry. In a Liferay repository, it may
 *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
 *         type </li> <li> fieldsMap - mapping for fields associated with a
 *         custom file entry type </li> </ul>
 * @return the file entry
 * @throws PortalException if the parent folder could not be found or if the
 *         file entry's information was invalid
 * @throws SystemException if a system exception occurred
 */
public FileEntry addFileEntry(long userId, long repositoryId, long folderId, String sourceFileName,
        String mimeType, String title, String description, String changeLog, byte[] bytes,
        ServiceContext serviceContext) throws PortalException, SystemException {

    File file = null;

    try {
        if ((bytes != null) && (bytes.length > 0)) {
            file = FileUtil.createTempFile(bytes);
        }

        return addFileEntry(userId, repositoryId, folderId, sourceFileName, mimeType, title, description,
                changeLog, file, serviceContext);
    } catch (IOException ioe) {
        throw new SystemException("Unable to write temporary file", ioe);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.service.impl.DLAppLocalServiceImpl.java

License:Open Source License

/**
 * Updates a file entry and associated metadata based on a byte array
 * object. If the file data is <code>null</code>, then only the associated
 * metadata (i.e., <code>title</code>, <code>description</code>, and
 * parameters in the <code>serviceContext</code>) will be updated.
 *
 * <p>//from   w  ww .j  a  va 2s .c o  m
 * This method takes two file names, the <code>sourceFileName</code> and the
 * <code>title</code>. The <code>sourceFileName</code> corresponds to the
 * name of the actual file being uploaded. The <code>title</code>
 * corresponds to a name the client wishes to assign this file after it has
 * been uploaded to the portal.
 * </p>
 *
 * @param  userId the primary key of the user
 * @param  fileEntryId the primary key of the file entry
 * @param  sourceFileName the original file's name (optionally
 *         <code>null</code>)
 * @param  mimeType the file's MIME type (optionally <code>null</code>)
 * @param  title the new name to be assigned to the file (optionally <code>
 *         <code>null</code></code>)
 * @param  description the file's new description
 * @param  changeLog the file's version change log (optionally
 *         <code>null</code>)
 * @param  majorVersion whether the new file version is a major version
 * @param  bytes the file's data (optionally <code>null</code>)
 * @param  serviceContext the service context to be applied. Can set the
 *         asset category IDs, asset tag names, and expando bridge
 *         attributes for the file entry. In a Liferay repository, it may
 *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
 *         type </li> <li> fieldsMap - mapping for fields associated with a
 *         custom file entry type </li> </ul>
 * @return the file entry
 * @throws PortalException if the file entry could not be found
 * @throws SystemException if a system exception occurred
 */
public FileEntry updateFileEntry(long userId, long fileEntryId, String sourceFileName, String mimeType,
        String title, String description, String changeLog, boolean majorVersion, byte[] bytes,
        ServiceContext serviceContext) throws PortalException, SystemException {

    File file = null;

    try {
        if ((bytes != null) && (bytes.length > 0)) {
            file = FileUtil.createTempFile(bytes);
        }

        return updateFileEntry(userId, fileEntryId, sourceFileName, mimeType, title, description, changeLog,
                majorVersion, file, serviceContext);
    } catch (IOException ioe) {
        throw new SystemException("Unable to write temporary file", ioe);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.service.impl.DLAppServiceImpl.java

License:Open Source License

/**
 * Adds a file entry and associated metadata. It is created based on a byte
 * array./* w w  w. ja v a2  s  .c om*/
 *
 * <p>
 * This method takes two file names, the <code>sourceFileName</code> and the
 * <code>title</code>. The <code>sourceFileName</code> corresponds to the
 * name of the actual file being uploaded. The <code>title</code>
 * corresponds to a name the client wishes to assign this file after it has
 * been uploaded to the portal. If it is <code>null</code>, the <code>
 * sourceFileName</code> will be used.
 * </p>
 *
 * @param  repositoryId the primary key of the repository
 * @param  folderId the primary key of the file entry's parent folder
 * @param  sourceFileName the original file's name
 * @param  mimeType the file's MIME type
 * @param  title the name to be assigned to the file (optionally <code>null
 *         </code>)
 * @param  description the file's description
 * @param  changeLog the file's version change log
 * @param  bytes the file's data (optionally <code>null</code>)
 * @param  serviceContext the service context to be applied. Can set the
 *         asset category IDs, asset tag names, and expando bridge
 *         attributes for the file entry. In a Liferay repository, it may
 *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
 *         type </li> <li> fieldsMap - mapping for fields associated with a
 *         custom file entry type </li> </ul>
 * @return the file entry
 * @throws PortalException if the parent folder could not be found or if the
 *         file entry's information was invalid
 * @throws SystemException if a system exception occurred
 */
public FileEntry addFileEntry(long repositoryId, long folderId, String sourceFileName, String mimeType,
        String title, String description, String changeLog, byte[] bytes, ServiceContext serviceContext)
        throws PortalException, SystemException {

    File file = null;

    try {
        if ((bytes != null) && (bytes.length > 0)) {
            file = FileUtil.createTempFile(bytes);
        }

        return addFileEntry(repositoryId, folderId, sourceFileName, mimeType, title, description, changeLog,
                file, serviceContext);
    } catch (IOException ioe) {
        throw new SystemException("Unable to write temporary file", ioe);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.service.impl.DLAppServiceImpl.java

License:Open Source License

/**
 * Updates a file entry and associated metadata based on a byte array
 * object. If the file data is <code>null</code>, then only the associated
 * metadata (i.e., <code>title</code>, <code>description</code>, and
 * parameters in the <code>serviceContext</code>) will be updated.
 *
 * <p>/*w ww.  j a v  a  2 s  .c o m*/
 * This method takes two file names, the <code>sourceFileName</code> and the
 * <code>title</code>. The <code>sourceFileName</code> corresponds to the
 * name of the actual file being uploaded. The <code>title</code>
 * corresponds to a name the client wishes to assign this file after it has
 * been uploaded to the portal.
 * </p>
 *
 * @param  fileEntryId the primary key of the file entry
 * @param  sourceFileName the original file's name (optionally
 *         <code>null</code>)
 * @param  mimeType the file's MIME type (optionally <code>null</code>)
 * @param  title the new name to be assigned to the file (optionally <code>
 *         <code>null</code></code>)
 * @param  description the file's new description
 * @param  changeLog the file's version change log (optionally
 *         <code>null</code>)
 * @param  majorVersion whether the new file version is a major version
 * @param  bytes the file's data (optionally <code>null</code>)
 * @param  serviceContext the service context to be applied. Can set the
 *         asset category IDs, asset tag names, and expando bridge
 *         attributes for the file entry. In a Liferay repository, it may
 *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
 *         type </li> <li> fieldsMap - mapping for fields associated with a
 *         custom file entry type </li> </ul>
 * @return the file entry
 * @throws PortalException if the file entry could not be found
 * @throws SystemException if a system exception occurred
 */
public FileEntry updateFileEntry(long fileEntryId, String sourceFileName, String mimeType, String title,
        String description, String changeLog, boolean majorVersion, byte[] bytes, ServiceContext serviceContext)
        throws PortalException, SystemException {

    File file = null;

    try {
        if ((bytes != null) && (bytes.length > 0)) {
            file = FileUtil.createTempFile(bytes);
        }

        return updateFileEntry(fileEntryId, sourceFileName, mimeType, title, description, changeLog,
                majorVersion, file, serviceContext);
    } catch (IOException ioe) {
        throw new SystemException("Unable to write temporary file", ioe);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.service.impl.DLSyncServiceImpl.java

License:Open Source License

public InputStream getFileDeltaAsStream(long fileEntryId, String sourceVersion, String destinationVersion)
        throws PortalException, SystemException {

    InputStream deltaInputStream = null;

    FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

    InputStream sourceInputStream = null;
    File sourceFile = FileUtil.createTempFile();
    FileInputStream sourceFileInputStream = null;
    FileChannel sourceFileChannel = null;
    File checksumsFile = FileUtil.createTempFile();
    OutputStream checksumsOutputStream = null;
    WritableByteChannel checksumsWritableByteChannel = null;

    try {//from w w w .ja v  a2  s  .  c  o  m
        sourceInputStream = fileEntry.getContentStream(sourceVersion);

        FileUtil.write(sourceFile, sourceInputStream);

        sourceFileInputStream = new FileInputStream(sourceFile);

        sourceFileChannel = sourceFileInputStream.getChannel();

        checksumsOutputStream = new FileOutputStream(checksumsFile);

        checksumsWritableByteChannel = Channels.newChannel(checksumsOutputStream);

        ByteChannelWriter checksumsByteChannelWriter = new ByteChannelWriter(checksumsWritableByteChannel);

        DeltaUtil.checksums(sourceFileChannel, checksumsByteChannelWriter);

        checksumsByteChannelWriter.finish();
    } catch (Exception e) {
        throw new PortalException(e);
    } finally {
        StreamUtil.cleanUp(sourceFileInputStream);
        StreamUtil.cleanUp(sourceFileChannel);
        StreamUtil.cleanUp(checksumsOutputStream);
        StreamUtil.cleanUp(checksumsWritableByteChannel);

        FileUtil.delete(sourceFile);
    }

    InputStream destinationInputStream = null;
    ReadableByteChannel destinationReadableByteChannel = null;
    InputStream checksumsInputStream = null;
    ReadableByteChannel checksumsReadableByteChannel = null;
    OutputStream deltaOutputStream = null;
    WritableByteChannel deltaOutputStreamWritableByteChannel = null;

    try {
        destinationInputStream = fileEntry.getContentStream(destinationVersion);

        destinationReadableByteChannel = Channels.newChannel(destinationInputStream);

        checksumsInputStream = new FileInputStream(checksumsFile);

        checksumsReadableByteChannel = Channels.newChannel(checksumsInputStream);

        ByteChannelReader checksumsByteChannelReader = new ByteChannelReader(checksumsReadableByteChannel);

        File deltaFile = FileUtil.createTempFile();

        deltaOutputStream = new FileOutputStream(deltaFile);

        deltaOutputStreamWritableByteChannel = Channels.newChannel(deltaOutputStream);

        ByteChannelWriter deltaByteChannelWriter = new ByteChannelWriter(deltaOutputStreamWritableByteChannel);

        DeltaUtil.delta(destinationReadableByteChannel, checksumsByteChannelReader, deltaByteChannelWriter);

        deltaByteChannelWriter.finish();

        deltaInputStream = new FileInputStream(deltaFile);
    } catch (Exception e) {
        throw new PortalException(e);
    } finally {
        StreamUtil.cleanUp(destinationInputStream);
        StreamUtil.cleanUp(destinationReadableByteChannel);
        StreamUtil.cleanUp(checksumsInputStream);
        StreamUtil.cleanUp(checksumsReadableByteChannel);
        StreamUtil.cleanUp(deltaOutputStream);
        StreamUtil.cleanUp(deltaOutputStreamWritableByteChannel);

        FileUtil.delete(checksumsFile);
    }

    return deltaInputStream;
}

From source file:com.liferay.portlet.documentlibrary.service.impl.DLSyncServiceImpl.java

License:Open Source License

public FileEntry updateFileEntry(long fileEntryId, String sourceFileName, String mimeType, String title,
        String description, String changeLog, boolean majorVersion, InputStream deltaInputStream, long size,
        ServiceContext serviceContext) throws PortalException, SystemException {

    FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

    InputStream originalInputStream = null;
    File patchedFile = null;//from   www.ja  v  a 2s. c  om
    InputStream patchedInputStream = null;

    try {
        originalInputStream = fileEntry.getContentStream();

        patchedFile = FileUtil.createTempFile();

        patchFile(originalInputStream, deltaInputStream, patchedFile);

        patchedInputStream = new FileInputStream(patchedFile);

        return dlAppService.updateFileEntry(fileEntryId, sourceFileName, mimeType, title, description,
                changeLog, majorVersion, patchedInputStream, size, serviceContext);
    } catch (Exception e) {
        throw new PortalException(e);
    } finally {
        StreamUtil.cleanUp(originalInputStream);
        StreamUtil.cleanUp(patchedInputStream);

        FileUtil.delete(patchedFile);
    }
}

From source file:com.liferay.portlet.documentlibrary.service.impl.DLSyncServiceImpl.java

License:Open Source License

protected void patchFile(InputStream originalInputStream, InputStream deltaInputStream, File patchedFile)
        throws PortalException {

    File originalFile = null;/*from w  ww.j  av  a 2s .  c om*/
    FileInputStream originalFileInputStream = null;
    FileChannel originalFileChannel = null;
    FileOutputStream patchedFileOutputStream = null;
    WritableByteChannel patchedWritableByteChannel = null;
    ReadableByteChannel deltaReadableByteChannel = null;

    try {
        originalFile = FileUtil.createTempFile();

        FileUtil.write(originalFile, originalInputStream);

        originalFileInputStream = new FileInputStream(originalFile);

        originalFileChannel = originalFileInputStream.getChannel();

        patchedFileOutputStream = new FileOutputStream(patchedFile);

        patchedWritableByteChannel = Channels.newChannel(patchedFileOutputStream);

        deltaReadableByteChannel = Channels.newChannel(deltaInputStream);

        ByteChannelReader deltaByteChannelReader = new ByteChannelReader(deltaReadableByteChannel);

        DeltaUtil.patch(originalFileChannel, patchedWritableByteChannel, deltaByteChannelReader);
    } catch (Exception e) {
        throw new PortalException(e);
    } finally {
        StreamUtil.cleanUp(originalFileInputStream);
        StreamUtil.cleanUp(originalFileChannel);
        StreamUtil.cleanUp(patchedFileOutputStream);
        StreamUtil.cleanUp(patchedWritableByteChannel);
        StreamUtil.cleanUp(deltaReadableByteChannel);

        FileUtil.delete(originalFile);
    }
}

From source file:com.liferay.portlet.documentlibrary.sharepoint.DLSharepointStorageImpl.java

License:Open Source License

@Override
public Tree[] moveDocument(SharepointRequest sharepointRequest) throws Exception {

    String parentFolderPath = sharepointRequest.getRootPath();

    long groupId = SharepointUtil.getGroupId(parentFolderPath);

    Folder folder = null;//from w w  w.  j a  va 2 s. c o  m
    FileEntry fileEntry = null;

    try {
        long parentFolderId = getLastFolderId(groupId, parentFolderPath,
                DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);

        folder = DLAppServiceUtil.getFolder(parentFolderId);
    } catch (Exception e1) {
        if (e1 instanceof NoSuchFolderException) {
            try {
                fileEntry = getFileEntry(sharepointRequest);
            } catch (Exception e2) {
            }
        }
    }

    Tree movedDocsTree = new Tree();
    Tree movedDirsTree = new Tree();

    String newPath = sharepointRequest.getParameterValue("newUrl");
    String newParentFolderPath = getParentFolderPath(newPath);

    long newGroupId = SharepointUtil.getGroupId(newParentFolderPath);

    long newParentFolderId = getLastFolderId(newGroupId, newParentFolderPath,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);

    String newName = getResourceName(newPath);

    ServiceContext serviceContext = new ServiceContext();

    if (fileEntry != null) {
        File file = null;

        try {
            long fileEntryId = fileEntry.getFileEntryId();

            long folderId = fileEntry.getFolderId();
            String mimeType = fileEntry.getMimeType();
            String description = fileEntry.getDescription();
            String changeLog = StringPool.BLANK;

            InputStream is = fileEntry.getContentStream();

            file = FileUtil.createTempFile(is);

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

            serviceContext.setAssetTagNames(assetTagNames);

            fileEntry = DLAppServiceUtil.updateFileEntry(fileEntryId, newName, mimeType, newName, description,
                    changeLog, false, file, serviceContext);

            if (folderId != newParentFolderId) {
                fileEntry = DLAppServiceUtil.moveFileEntry(fileEntryId, newParentFolderId, serviceContext);
            }

            Tree documentTree = getFileEntryTree(fileEntry, newParentFolderPath);

            movedDocsTree.addChild(documentTree);
        } finally {
            FileUtil.delete(file);
        }
    } else if (folder != null) {
        long folderId = folder.getFolderId();

        folder = DLAppServiceUtil.moveFolder(folderId, newParentFolderId, serviceContext);

        Tree folderTree = getFolderTree(folder, newParentFolderPath);

        movedDirsTree.addChild(folderTree);
    }

    return new Tree[] { movedDocsTree, movedDirsTree };
}