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

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

Introduction

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

Prototype

public static void write(String fileName, String s) throws IOException 

Source Link

Usage

From source file:com.liferay.portlet.documentlibrary.antivirus.BaseFileAntivirusScanner.java

License:Open Source License

public void scan(InputStream inputStream) throws AntivirusScannerException, SystemException {

    File file = null;/*from   w  w w.j  a va  2s  .c o  m*/

    try {
        file = FileUtil.createTempFile(_ANTIVIRUS_EXTENSION);

        FileUtil.write(file, inputStream);

        scan(file);
    } catch (IOException ioe) {
        throw new SystemException("Unable to write temporary file", ioe);
    } finally {
        if (file != null) {
            file.delete();
        }
    }
}

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 {// w w w  .  j a  v  a2  s .co  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

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 void putDocument(SharepointRequest sharepointRequest) throws Exception {

    HttpServletRequest request = sharepointRequest.getHttpServletRequest();

    String documentPath = sharepointRequest.getRootPath();
    String parentFolderPath = getParentFolderPath(documentPath);

    long groupId = SharepointUtil.getGroupId(parentFolderPath);
    long parentFolderId = getLastFolderId(groupId, parentFolderPath,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
    String title = getResourceName(documentPath);
    String description = StringPool.BLANK;
    String changeLog = StringPool.BLANK;

    ServiceContext serviceContext = new ServiceContext();

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

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

    String extension = FileUtil.getExtension(title);

    File file = null;//from  ww w . j av a2 s.c  om

    try {
        file = FileUtil.createTempFile(extension);

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

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

        try {
            FileEntry fileEntry = getFileEntry(sharepointRequest);

            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) {
            DLAppServiceUtil.addFileEntry(groupId, parentFolderId, title, contentType, title, description,
                    changeLog, file, serviceContext);
        }
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.portlet.documentlibrary.store.DLStoreImpl.java

License:Open Source License

public void addFile(long companyId, long repositoryId, String fileName, boolean validateFileExtension,
        InputStream is) throws PortalException, SystemException {

    if (is instanceof ByteArrayFileInputStream) {
        ByteArrayFileInputStream byteArrayFileInputStream = (ByteArrayFileInputStream) is;

        File file = byteArrayFileInputStream.getFile();

        addFile(companyId, repositoryId, fileName, validateFileExtension, file);

        return;/*w w  w. ja va2  s .c  om*/
    }

    validate(fileName, validateFileExtension, is);

    if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED || !AntivirusScannerUtil.isActive()) {

        store.addFile(companyId, repositoryId, fileName, is);
    } else {
        File tempFile = null;

        try {
            if (is.markSupported()) {
                is.mark(is.available() + 1);

                AntivirusScannerUtil.scan(is);

                is.reset();

                store.addFile(companyId, repositoryId, fileName, is);
            } else {
                tempFile = FileUtil.createTempFile();

                FileUtil.write(tempFile, is);

                AntivirusScannerUtil.scan(tempFile);

                store.addFile(companyId, repositoryId, fileName, tempFile);
            }
        } catch (IOException ioe) {
            throw new SystemException("Unable to scan file " + fileName, ioe);
        } finally {
            if (tempFile != null) {
                tempFile.delete();
            }
        }
    }
}

From source file:com.liferay.portlet.documentlibrary.store.DLStoreImpl.java

License:Open Source License

public void updateFile(long companyId, long repositoryId, String fileName, String fileExtension,
        boolean validateFileExtension, String versionLabel, String sourceFileName, InputStream is)
        throws PortalException, SystemException {

    if (is instanceof ByteArrayFileInputStream) {
        ByteArrayFileInputStream byteArrayFileInputStream = (ByteArrayFileInputStream) is;

        File file = byteArrayFileInputStream.getFile();

        updateFile(companyId, repositoryId, fileName, fileExtension, validateFileExtension, versionLabel,
                sourceFileName, file);//from   w w w  .  j av  a2 s .c  o  m

        return;
    }

    validate(fileName, fileExtension, sourceFileName, validateFileExtension, is);

    if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED || !AntivirusScannerUtil.isActive()) {

        store.updateFile(companyId, repositoryId, fileName, versionLabel, is);
    } else {
        File tempFile = null;

        try {
            if (is.markSupported()) {
                is.mark(is.available() + 1);

                AntivirusScannerUtil.scan(is);

                is.reset();

                store.updateFile(companyId, repositoryId, fileName, versionLabel, is);
            } else {
                tempFile = FileUtil.createTempFile();

                FileUtil.write(tempFile, is);

                AntivirusScannerUtil.scan(tempFile);

                store.updateFile(companyId, repositoryId, fileName, versionLabel, tempFile);
            }
        } catch (IOException ioe) {
            throw new SystemException("Unable to scan file " + fileName, ioe);
        } finally {
            if (tempFile != null) {
                tempFile.delete();
            }
        }
    }
}

From source file:com.liferay.portlet.documentlibrary.store.FileSystemStore.java

License:Open Source License

@Override
public void addFile(long companyId, long repositoryId, String fileName, InputStream is)
        throws PortalException, SystemException {

    try {//from  w ww.ja  v a  2  s . c o  m
        File fileNameVersionFile = getFileNameVersionFile(companyId, repositoryId, fileName, VERSION_DEFAULT);

        if (fileNameVersionFile.exists()) {
            throw new DuplicateFileException(fileNameVersionFile.getPath());
        }

        FileUtil.write(fileNameVersionFile, is);
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    }
}

From source file:com.liferay.portlet.documentlibrary.store.FileSystemStore.java

License:Open Source License

@Override
public void updateFile(long companyId, long repositoryId, String fileName, String versionLabel, InputStream is)
        throws PortalException, SystemException {

    try {//from w  w w . j a va 2 s. co  m
        File fileNameVersionFile = getFileNameVersionFile(companyId, repositoryId, fileName, versionLabel);

        if (fileNameVersionFile.exists()) {
            throw new DuplicateFileException(fileNameVersionFile.getPath());
        }

        FileUtil.write(fileNameVersionFile, is);
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    }
}

From source file:com.liferay.portlet.documentlibrary.store.S3Store.java

License:Open Source License

@Override
public void updateFile(long companyId, long repositoryId, long newRepositoryId, String fileName)
        throws SystemException {

    try {/*www .  j  a va 2 s .co m*/
        S3Object[] s3Objects = _s3Service.listObjects(_s3Bucket, getKey(companyId, repositoryId, fileName),
                null);

        for (int i = 0; i < s3Objects.length; i++) {
            S3Object oldS3Object = s3Objects[i];

            String oldKey = oldS3Object.getKey();

            oldS3Object = _s3Service.getObject(_s3Bucket, oldKey);

            File tempFile = new File(SystemProperties.get(SystemProperties.TMP_DIR) + File.separator
                    + PortalUUIDUtil.generate());

            FileUtil.write(tempFile, oldS3Object.getDataInputStream());

            InputStream is = new FileInputStream(tempFile);

            String newPrefix = getKey(companyId, newRepositoryId);

            int x = oldKey.indexOf(CharPool.SLASH);

            x = oldKey.indexOf(CharPool.SLASH, x + 1);

            String newKey = newPrefix + oldKey.substring(x + 1, oldKey.length());

            S3Object newS3Object = new S3Object(_s3Bucket, newKey);

            newS3Object.setDataInputStream(is);

            _s3Service.putObject(_s3Bucket, newS3Object);
            _s3Service.deleteObject(_s3Bucket, oldKey);

            FileUtil.delete(tempFile);
        }
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    } catch (S3ServiceException s3se) {
        throw new SystemException(s3se);
    }
}

From source file:com.liferay.portlet.documentlibrary.store.S3Store.java

License:Open Source License

public void updateFile(long companyId, long repositoryId, String fileName, String newFileName)
        throws SystemException {

    try {//from  w w  w.  j  av  a2  s.  com
        S3Object[] s3Objects = _s3Service.listObjects(_s3Bucket, getKey(companyId, repositoryId, fileName),
                null);

        for (int i = 0; i < s3Objects.length; i++) {
            S3Object oldS3Object = s3Objects[i];

            String oldKey = oldS3Object.getKey();

            oldS3Object = _s3Service.getObject(_s3Bucket, oldKey);

            File tempFile = new File(SystemProperties.get(SystemProperties.TMP_DIR) + File.separator
                    + PortalUUIDUtil.generate());

            FileUtil.write(tempFile, oldS3Object.getDataInputStream());

            InputStream is = new FileInputStream(tempFile);

            String newPrefix = getKey(companyId, repositoryId, newFileName);

            int x = oldKey.indexOf(StringPool.SLASH);

            x = oldKey.indexOf(CharPool.SLASH, x + 1);

            x = oldKey.indexOf(CharPool.SLASH, x + 1);

            String newKey = newPrefix + oldKey.substring(x + 1, oldKey.length());

            S3Object newS3Object = new S3Object(_s3Bucket, newKey);

            newS3Object.setDataInputStream(is);

            _s3Service.putObject(_s3Bucket, newS3Object);
            _s3Service.deleteObject(_s3Bucket, oldKey);

            FileUtil.delete(tempFile);
        }
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    } catch (S3ServiceException s3se) {
        throw new SystemException(s3se);
    }
}