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

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

Introduction

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

Prototype

public static byte[] getBytes(InputStream is) throws IOException 

Source Link

Usage

From source file:com.liferay.blogs.demo.data.creator.internal.BaseBlogsEntryDemoDataCreator.java

License:Open Source License

private byte[] _getRandomImageBytes(long userId, long groupId) throws IOException, PortalException {

    if (_blogsEntryImagesFolder == null) {
        _blogsEntryImagesFolder = rootFolderDemoDataCreator.create(userId, groupId, "Blogs Images");
    }// www  .ja  va 2s .co m

    FileEntry fileEntry = fileEntryDemoDataCreator.create(userId, _blogsEntryImagesFolder.getFolderId());

    FileVersion fileVersion = fileEntry.getFileVersion();

    return FileUtil.getBytes(fileVersion.getContentStream(false));
}

From source file:com.liferay.blogs.internal.exportimport.data.handler.BlogsEntryStagedModelDataHandler.java

License:Open Source License

@Override
protected void doImportStagedModel(PortletDataContext portletDataContext, BlogsEntry entry) throws Exception {

    long userId = portletDataContext.getUserId(entry.getUserUuid());

    Element entryElement = portletDataContext.getImportDataStagedModelElement(entry);

    String content = _exportImportContentProcessorController.replaceImportContentReferences(portletDataContext,
            entry, entry.getContent());/* w w  w. j  a  v  a  2s  .  c o m*/

    entry.setContent(content);

    Calendar displayDateCal = CalendarFactoryUtil.getCalendar();

    displayDateCal.setTime(entry.getDisplayDate());

    int displayDateMonth = displayDateCal.get(Calendar.MONTH);
    int displayDateDay = displayDateCal.get(Calendar.DATE);
    int displayDateYear = displayDateCal.get(Calendar.YEAR);
    int displayDateHour = displayDateCal.get(Calendar.HOUR);
    int displayDateMinute = displayDateCal.get(Calendar.MINUTE);

    if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
        displayDateHour += 12;
    }

    boolean allowPingbacks = entry.isAllowPingbacks();
    boolean allowTrackbacks = entry.isAllowTrackbacks();
    String[] trackbacks = StringUtil.split(entry.getTrackbacks());

    ServiceContext serviceContext = portletDataContext.createServiceContext(entry);

    BlogsEntry importedEntry = null;

    if (portletDataContext.isDataStrategyMirror()) {
        BlogsEntry existingEntry = fetchStagedModelByUuidAndGroupId(entry.getUuid(),
                portletDataContext.getScopeGroupId());

        if (existingEntry == null) {
            serviceContext.setUuid(entry.getUuid());

            importedEntry = _blogsEntryLocalService.addEntry(userId, entry.getTitle(), entry.getSubtitle(),
                    entry.getDescription(), entry.getContent(), displayDateMonth, displayDateDay,
                    displayDateYear, displayDateHour, displayDateMinute, allowPingbacks, allowTrackbacks,
                    trackbacks, entry.getCoverImageCaption(), null, null, serviceContext);
        } else {
            importedEntry = _blogsEntryLocalService.updateEntry(userId, existingEntry.getEntryId(),
                    entry.getTitle(), entry.getSubtitle(), entry.getDescription(), entry.getContent(),
                    displayDateMonth, displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
                    allowPingbacks, allowTrackbacks, trackbacks, entry.getCoverImageCaption(),
                    new ImageSelector(), new ImageSelector(), serviceContext);
        }
    } else {
        importedEntry = _blogsEntryLocalService.addEntry(userId, entry.getTitle(), entry.getSubtitle(),
                entry.getDescription(), entry.getContent(), displayDateMonth, displayDateDay, displayDateYear,
                displayDateHour, displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
                entry.getCoverImageCaption(), null, null, serviceContext);
    }

    if ((entry.getCoverImageFileEntryId() == 0) && Validator.isNull(entry.getCoverImageURL())
            && (entry.getSmallImageFileEntryId() == 0) && Validator.isNull(entry.getSmallImageURL())
            && !entry.isSmallImage()) {

        portletDataContext.importClassedModel(entry, importedEntry);

        return;
    }

    // Cover image

    ImageSelector coverImageSelector = null;

    List<Element> attachmentElements = portletDataContext.getReferenceDataElements(entry, DLFileEntry.class,
            PortletDataContext.REFERENCE_TYPE_WEAK);

    if (Validator.isNotNull(entry.getCoverImageURL())) {
        coverImageSelector = new ImageSelector(entry.getCoverImageURL());
    } else if (entry.getCoverImageFileEntryId() != 0) {
        coverImageSelector = _getImageSelector(portletDataContext, entry.getCoverImageFileEntryId(),
                attachmentElements);
    }

    if (coverImageSelector != null) {
        _blogsEntryLocalService.addCoverImage(importedEntry.getEntryId(), coverImageSelector);
    }

    // Small image

    ImageSelector smallImageSelector = null;

    if (entry.isSmallImage()) {
        String smallImagePath = entryElement.attributeValue("small-image-path");

        if (Validator.isNotNull(entry.getSmallImageURL())) {
            smallImageSelector = new ImageSelector(entry.getSmallImageURL());
        } else if (Validator.isNotNull(smallImagePath)) {
            String smallImageFileName = entry.getSmallImageId() + StringPool.PERIOD + entry.getSmallImageType();

            InputStream inputStream = null;

            try {
                inputStream = portletDataContext.getZipEntryAsInputStream(smallImagePath);

                smallImageSelector = new ImageSelector(FileUtil.getBytes(inputStream), smallImageFileName,
                        MimeTypesUtil.getContentType(smallImageFileName), null);
            } finally {
                StreamUtil.cleanUp(inputStream);
            }
        } else if (entry.getSmallImageFileEntryId() != 0) {
            smallImageSelector = _getImageSelector(portletDataContext, entry.getSmallImageFileEntryId(),
                    attachmentElements);
        }
    }

    if (smallImageSelector != null) {
        _blogsEntryLocalService.addSmallImage(importedEntry.getEntryId(), smallImageSelector);
    }

    if ((coverImageSelector != null) || (smallImageSelector != null)) {
        importedEntry = _blogsEntryLocalService.getEntry(importedEntry.getEntryId());
    }

    portletDataContext.importClassedModel(entry, importedEntry);
}

From source file:com.liferay.blogs.internal.exportimport.data.handler.BlogsEntryStagedModelDataHandler.java

License:Open Source License

private ImageSelector _getImageSelector(PortletDataContext portletDataContext, long fileEntryId,
        List<Element> attachmentElements) throws Exception {

    for (Element attachmentElement : attachmentElements) {
        String path = attachmentElement.attributeValue("path");

        FileEntry fileEntry = (FileEntry) portletDataContext.getZipEntryAsObject(path);

        if (fileEntryId == fileEntry.getFileEntryId()) {
            InputStream inputStream = null;

            try {
                String binPath = attachmentElement.attributeValue("bin-path");

                if (Validator.isNull(binPath) && portletDataContext.isPerformDirectBinaryImport()) {

                    try {
                        inputStream = FileEntryUtil.getContentStream(fileEntry);
                    } catch (NoSuchFileException nsfe) {
                    }//ww w  .  j a  va2  s . c  o  m
                } else {
                    inputStream = portletDataContext.getZipEntryAsInputStream(binPath);
                }

                if (inputStream == null) {
                    if (_log.isWarnEnabled()) {
                        _log.warn("Unable to import attachment for file entry " + fileEntry.getFileEntryId());
                    }

                    continue;
                }

                return new ImageSelector(FileUtil.getBytes(inputStream), fileEntry.getFileName(),
                        fileEntry.getMimeType(), null);
            } finally {
                StreamUtil.cleanUp(inputStream);
            }
        }
    }

    return null;
}

From source file:com.liferay.blogs.internal.exportimport.data.handler.test.BlogsEntryStagedModelDataHandlerTest.java

License:Open Source License

protected BlogsEntry addBlogsEntryWithCoverImage() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(stagingGroup.getGroupId(),
            TestPropsValues.getUserId());

    InputStream inputStream = getInputStream();

    String mimeType = MimeTypesUtil.getContentType(_IMAGE_TITLE);

    ImageSelector imageSelector = new ImageSelector(FileUtil.getBytes(inputStream), _IMAGE_TITLE, mimeType,
            _IMAGE_CROP_REGION);//from   w  ww .j  av  a2 s. co m

    return addBlogsEntry(imageSelector, null, serviceContext);
}

From source file:com.liferay.blogs.internal.exportimport.data.handler.test.BlogsEntryStagedModelDataHandlerTest.java

License:Open Source License

protected BlogsEntry addBlogsEntryWithSmallImage() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(stagingGroup.getGroupId(),
            TestPropsValues.getUserId());

    InputStream inputStream = getInputStream();

    String mimeType = MimeTypesUtil.getContentType(_IMAGE_TITLE);

    ImageSelector imageSelector = new ImageSelector(FileUtil.getBytes(inputStream), _IMAGE_TITLE, mimeType,
            StringPool.BLANK);//from  ww  w.  ja  v a2s  .  com

    return addBlogsEntry(null, imageSelector, serviceContext);
}

From source file:com.liferay.blogs.service.impl.BlogsEntryLocalServiceImpl.java

License:Open Source License

/**
 * @deprecated As of 7.0.0, replaced by {@link #addEntry(long, String,
 *             String, String, String, int, int, int, int, int, boolean,
 *             boolean, String[], String, ImageSelector, ImageSelector,
 *             ServiceContext)}//from w  w  w  .jav a2  s.  c  om
 */
@Deprecated
@Override
public BlogsEntry addEntry(long userId, String title, String description, String content, int displayDateMonth,
        int displayDateDay, int displayDateYear, int displayDateHour, int displayDateMinute,
        boolean allowPingbacks, boolean allowTrackbacks, String[] trackbacks, boolean smallImage,
        String smallImageURL, String smallImageFileName, InputStream smallImageInputStream,
        ServiceContext serviceContext) throws PortalException {

    ImageSelector coverImageImageSelector = null;
    ImageSelector smallImageImageSelector = null;

    if (smallImage) {
        if (Validator.isNotNull(smallImageFileName) && (smallImageInputStream != null)) {

            try {
                byte[] bytes = FileUtil.getBytes(smallImageInputStream);

                smallImageImageSelector = new ImageSelector(bytes, smallImageFileName,
                        MimeTypesUtil.getContentType(smallImageFileName), null);
            } catch (IOException ioe) {
                _log.error("Unable to create image selector", ioe);
            }
        } else if (Validator.isNotNull(smallImageURL)) {
            smallImageImageSelector = new ImageSelector(smallImageURL);
        }
    }

    return addEntry(userId, title, StringPool.BLANK, description, content, displayDateMonth, displayDateDay,
            displayDateYear, displayDateHour, displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
            StringPool.BLANK, coverImageImageSelector, smallImageImageSelector, serviceContext);
}

From source file:com.liferay.blogs.service.impl.BlogsEntryLocalServiceImpl.java

License:Open Source License

/**
 * @deprecated As of 7.0.0, replaced by {@link #updateEntry(long, long,
 *             String, String, String, String, int, int, int, int, int,
 *             boolean, boolean, String[], String, ImageSelector,
 *             ImageSelector, ServiceContext)}
 *//*from  w  ww.j av  a 2 s .  com*/
@Deprecated
@Override
public BlogsEntry updateEntry(long userId, long entryId, String title, String description, String content,
        int displayDateMonth, int displayDateDay, int displayDateYear, int displayDateHour,
        int displayDateMinute, boolean allowPingbacks, boolean allowTrackbacks, String[] trackbacks,
        boolean smallImage, String smallImageURL, String smallImageFileName, InputStream smallImageInputStream,
        ServiceContext serviceContext) throws PortalException {

    ImageSelector coverImageImageSelector = null;
    ImageSelector smallImageImageSelector = null;

    if (smallImage) {
        if (Validator.isNotNull(smallImageFileName) && (smallImageInputStream != null)) {

            try {
                byte[] bytes = FileUtil.getBytes(smallImageInputStream);

                smallImageImageSelector = new ImageSelector(bytes, smallImageFileName,
                        MimeTypesUtil.getContentType(smallImageFileName), null);
            } catch (IOException ioe) {
                _log.error("Unable to create image selector", ioe);
            }
        } else if (Validator.isNotNull(smallImageURL)) {
            smallImageImageSelector = new ImageSelector(smallImageURL);
        }
    } else {
        smallImageImageSelector = new ImageSelector();
    }

    return updateEntry(userId, entryId, title, StringPool.BLANK, description, content, displayDateMonth,
            displayDateDay, displayDateYear, displayDateHour, displayDateMinute, allowPingbacks,
            allowTrackbacks, trackbacks, StringPool.BLANK, coverImageImageSelector, smallImageImageSelector,
            serviceContext);
}

From source file:com.liferay.blogs.service.impl.BlogsEntryServiceImpl.java

License:Open Source License

/**
 * @deprecated As of 7.0.0, replaced by {@link #addEntry(String, String,
 *             String, String, int, int, int, int, int, boolean, boolean,
 *             String[], String, ImageSelector, ImageSelector,
 *             ServiceContext)}/*  www  .j a  v a  2  s. c  o m*/
 */
@Deprecated
@Override
public BlogsEntry addEntry(String title, String description, String content, int displayDateMonth,
        int displayDateDay, int displayDateYear, int displayDateHour, int displayDateMinute,
        boolean allowPingbacks, boolean allowTrackbacks, String[] trackbacks, boolean smallImage,
        String smallImageURL, String smallImageFileName, InputStream smallImageInputStream,
        ServiceContext serviceContext) throws PortalException {

    BlogsPermission.check(getPermissionChecker(), serviceContext.getScopeGroupId(), ActionKeys.ADD_ENTRY);

    ImageSelector coverImageImageSelector = null;
    ImageSelector smallImageImageSelector = null;

    if (smallImage) {
        if (Validator.isNotNull(smallImageFileName) && (smallImageInputStream != null)) {

            try {
                byte[] bytes = FileUtil.getBytes(smallImageInputStream);

                smallImageImageSelector = new ImageSelector(bytes, smallImageFileName,
                        MimeTypesUtil.getContentType(smallImageFileName), null);
            } catch (IOException ioe) {
                _log.error("Unable to create image selector", ioe);
            }
        } else if (Validator.isNotNull(smallImageURL)) {
            smallImageImageSelector = new ImageSelector(smallImageURL);
        }
    }

    return addEntry(title, StringPool.BLANK, description, content, displayDateMonth, displayDateDay,
            displayDateYear, displayDateHour, displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
            StringPool.BLANK, coverImageImageSelector, smallImageImageSelector, serviceContext);
}

From source file:com.liferay.blogs.service.impl.BlogsEntryServiceImpl.java

License:Open Source License

/**
 * @deprecated As of 7.0.0, replaced by {@link #updateEntry(long, String,
 *             String, String, String, int, int, int, int, int, boolean,
 *             boolean, String[], String, ImageSelector, ImageSelector,
 *             ServiceContext)}//from ww w  .  j av  a 2  s . c o  m
 */
@Deprecated
@Override
public BlogsEntry updateEntry(long entryId, String title, String description, String content,
        int displayDateMonth, int displayDateDay, int displayDateYear, int displayDateHour,
        int displayDateMinute, boolean allowPingbacks, boolean allowTrackbacks, String[] trackbacks,
        boolean smallImage, String smallImageURL, String smallImageFileName, InputStream smallImageInputStream,
        ServiceContext serviceContext) throws PortalException {

    BlogsPermission.check(getPermissionChecker(), serviceContext.getScopeGroupId(), ActionKeys.UPDATE);

    ImageSelector coverImageImageSelector = null;
    ImageSelector smallImageImageSelector = null;

    if (smallImage) {
        if (Validator.isNotNull(smallImageFileName) && (smallImageInputStream != null)) {

            try {
                byte[] bytes = FileUtil.getBytes(smallImageInputStream);

                smallImageImageSelector = new ImageSelector(bytes, smallImageFileName,
                        MimeTypesUtil.getContentType(smallImageFileName), null);
            } catch (IOException ioe) {
                _log.error("Unable to create image selector", ioe);
            }
        } else if (Validator.isNotNull(smallImageURL)) {
            smallImageImageSelector = new ImageSelector(smallImageURL);
        }
    } else {
        smallImageImageSelector = new ImageSelector();
    }

    return updateEntry(entryId, title, StringPool.BLANK, description, content, displayDateMonth, displayDateDay,
            displayDateYear, displayDateHour, displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
            StringPool.BLANK, coverImageImageSelector, smallImageImageSelector, serviceContext);
}

From source file:com.liferay.blogs.service.test.BlogsEntryLocalServiceTest.java

License:Open Source License

@Test
public void testAddOriginalImageInVisibleImageFolder() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId(),
            _user.getUserId());//w  ww  .ja v  a  2  s .com

    BlogsEntry blogsEntry = BlogsEntryLocalServiceUtil.addEntry(_user.getUserId(),
            RandomTestUtil.randomString(), RandomTestUtil.randomString(), serviceContext);

    FileEntry tempFileEntry = getTempFileEntry(_user.getUserId(), _group.getGroupId(), "image.jpg");

    ImageSelector imageSelector = new ImageSelector(FileUtil.getBytes(tempFileEntry.getContentStream()),
            tempFileEntry.getTitle(), tempFileEntry.getMimeType(), StringPool.BLANK);

    long originalImageFileEntryId = BlogsEntryLocalServiceUtil.addOriginalImageFileEntry(_user.getUserId(),
            _group.getGroupId(), blogsEntry.getEntryId(), imageSelector);

    FileEntry portletFileEntry = PortletFileRepositoryUtil.getPortletFileEntry(originalImageFileEntryId);

    Folder folder = portletFileEntry.getFolder();

    Assert.assertEquals(BlogsConstants.SERVICE_NAME, folder.getName());
}