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:ch.inofix.contact.service.impl.ContactLocalServiceImpl.java

License:Open Source License

@Override
public void importContacts(ExportImportConfiguration exportImportConfiguration, InputStream inputStream)
        throws PortalException {

    File file = null;//from www.j a  v a 2 s . com

    try {
        file = FileUtil.createTempFile("lar");

        FileUtil.write(file, inputStream);

        importContacts(exportImportConfiguration, file);

    } catch (IOException ioe) {
        throw new SystemException(ioe);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:ch.inofix.contact.service.impl.ContactLocalServiceImpl.java

License:Open Source License

@Override
public long importContactsInBackground(long userId, ExportImportConfiguration exportImportConfiguration,
        InputStream inputStream, String extension) throws PortalException {

    File file = null;/*  w ww .ja  v  a 2 s.co  m*/

    try {

        file = FileUtil.createTempFile(extension);

        FileUtil.write(file, inputStream);

        return importContactsInBackground(userId, exportImportConfiguration, file);

    } catch (IOException ioe) {
        throw new SystemException(ioe);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:ch.inofix.referencemanager.service.impl.ReferenceLocalServiceImpl.java

License:Open Source License

/**
 * Imports the references from the input stream.
 *
 * @param userId// w w  w . jav a 2  s.co  m
 *            the primary key of the user
 * @param groupId
 *            the primary key of the group
 * @param privateLayout
 *            whether the layout is private to the group
 * @param parameterMap
 *            the mapping of parameters indicating which information will be
 *            imported.
 * @param inputStream
 *            the input stream
 * @param serviceContext
 * @since 1.0.2
 * @throws PortalException
 *             if a group or user with the primary key could not be found,
 *             or if some other portal exception occurred
 * @throws SystemException
 *             if a system exception occurred
 */
public void importReferences(long userId, long groupId, boolean privateLayout,
        Map<String, String[]> parameterMap, InputStream inputStream, ServiceContext serviceContext)
        throws PortalException, SystemException {

    File file = null;

    try {
        file = FileUtil.createTempFile("bib");

        FileUtil.write(file, inputStream);

        importReferences(userId, groupId, privateLayout, parameterMap, file, serviceContext);

    } catch (IOException ioe) {

        _log.error(ioe);

        throw new SystemException(ioe);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.alloy.mvc.jsonwebservice.AlloyControllerInvokerManager.java

License:Open Source License

protected Class<? extends AlloyControllerInvoker> createAlloyControllerInvokerClass(
        Class<? extends AlloyController> controllerClass) throws NoClassNecessaryException {

    ClassLoader classLoader = controllerClass.getClassLoader();

    String alloyControllerInvokerClassName = getAlloyControllerInvokerClassName(controllerClass);

    Class<? extends AlloyControllerInvoker> alloyControllerInvokerClass = null;

    synchronized (classLoader) {
        try {// w  ww  .  jav a 2s.c  om
            Method defineClassMethod = ReflectionUtil.getDeclaredMethod(ClassLoader.class, "defineClass",
                    String.class, byte[].class, int.class, int.class);

            final byte[] classData = generateAlloyControllerInvokerClassData(controllerClass,
                    alloyControllerInvokerClassName);

            final String fileName = PropsUtil.get(PropsKeys.LIFERAY_HOME) + "/data/alloy/"
                    + getClassBinaryName(alloyControllerInvokerClassName) + ".class";

            ClassLoader customClassLoader = new ClassLoader(classLoader) {

                @Override
                public URL getResource(String name) {
                    if (fileName.contains(name)) {
                        File file = new File(fileName);

                        try {
                            FileUtil.write(file, classData);

                            URI uri = file.toURI();

                            return uri.toURL();
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }

                    return super.getResource(name);
                }

            };

            alloyControllerInvokerClass = (Class<? extends AlloyControllerInvoker>) defineClassMethod
                    .invoke(customClassLoader, alloyControllerInvokerClassName, classData, 0, classData.length);

            return alloyControllerInvokerClass;
        } catch (NoClassNecessaryException ncne) {
            throw ncne;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.liferay.appadder.portlet.AppAdderPortlet.java

License:Open Source License

protected File getFile(String path, String fileName) throws Exception {
    String tempDir = SystemProperties.get(SystemProperties.TMP_DIR);

    File file = new File(tempDir + File.separator + fileName);

    ClassLoader classLoader = getClass().getClassLoader();

    FileUtil.write(file, classLoader.getResourceAsStream(path + fileName));

    return file;/*www  .j a  v  a 2 s. c o m*/
}

From source file:com.liferay.document.library.web.internal.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int putResource(WebDAVRequest webDAVRequest) throws WebDAVException {
    File file = null;// w  w  w. ja  v a  2 s.c o  m

    try {
        HttpServletRequest request = webDAVRequest.getHttpServletRequest();

        String[] pathArray = webDAVRequest.getPathArray();

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

        ServiceContext serviceContext = ServiceContextFactory.getInstance(request);

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

        String extension = FileUtil.getExtension(title);

        file = FileUtil.createTempFile(extension);

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

        String contentType = getContentType(request, file, title, extension);

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

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

                return WebDAVUtil.SC_LOCKED;
            }

            long fileEntryId = fileEntry.getFileEntryId();

            description = fileEntry.getDescription();

            populateServiceContext(serviceContext, fileEntry);

            serviceContext.setCommand(Constants.UPDATE_WEBDAV);

            _dlAppService.updateFileEntry(fileEntryId, title, contentType, title, description, changeLog, false,
                    file, serviceContext);
        } catch (NoSuchFileEntryException nsfee) {
            if (_log.isDebugEnabled()) {
                _log.debug(nsfee, nsfee);
            }

            serviceContext.setCommand(Constants.ADD_WEBDAV);

            _dlAppService.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 (FileSizeException fse) {
        if (_log.isDebugEnabled()) {
            _log.debug(fse, fse);
        }

        return HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE;
    } catch (NoSuchFolderException nsfe) {
        if (_log.isDebugEnabled()) {
            _log.debug(nsfe, nsfe);
        }

        return HttpServletResponse.SC_CONFLICT;
    } catch (PrincipalException pe) {
        if (_log.isDebugEnabled()) {
            _log.debug(pe, pe);
        }

        return HttpServletResponse.SC_FORBIDDEN;
    } 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);
    }
}

From source file:com.liferay.dynamic.data.lists.exporter.test.DDLExporterTest.java

License:Open Source License

@Test
public void testCSVExport() throws Exception {
    DDMForm ddmForm = DDMFormTestUtil.createDDMForm(_availableLocales, _defaultLocale);

    createDDMFormFields(ddmForm);/*from  w  w w .  j  a  va  2s  .co m*/

    DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues(ddmForm, _availableLocales,
            _defaultLocale);

    createDDMFormFieldValues(ddmFormValues);

    DDLRecordSetTestHelper recordSetTestHelper = new DDLRecordSetTestHelper(_group);

    DDLRecordSet recordSet = recordSetTestHelper.addRecordSet(ddmForm);

    DDLRecordTestHelper recordTestHelper = new DDLRecordTestHelper(_group, recordSet);

    recordTestHelper.addRecord(ddmFormValues, WorkflowConstants.ACTION_PUBLISH);

    DDLExporter ddlExporter = _ddlExporterFactory.getDDLExporter("csv");

    byte[] bytes = ddlExporter.export(recordSet.getRecordSetId());

    File file = new File("record-set.csv");

    FileUtil.write(file, bytes);

    String expectedFileContent = read("test-record-set-export.csv");
    String actualFileContent = FileUtil.read(file);

    Assert.assertEquals(expectedFileContent, actualFileContent);
}

From source file:com.liferay.dynamic.data.lists.exporter.test.DDLExporterTest.java

License:Open Source License

@Test
public void testXMLExport() throws Exception {
    DDMForm ddmForm = DDMFormTestUtil.createDDMForm(_availableLocales, _defaultLocale);

    createDDMFormFields(ddmForm);//  w  w w . ja  va2s . com

    DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues(ddmForm, _availableLocales,
            _defaultLocale);

    createDDMFormFieldValues(ddmFormValues);

    DDLRecordSetTestHelper recordSetTestHelper = new DDLRecordSetTestHelper(_group);

    DDLRecordSet recordSet = recordSetTestHelper.addRecordSet(ddmForm);

    DDLRecordTestHelper recordTestHelper = new DDLRecordTestHelper(_group, recordSet);

    recordTestHelper.addRecord(ddmFormValues, WorkflowConstants.ACTION_PUBLISH);

    DDLExporter ddlExporter = _ddlExporterFactory.getDDLExporter("xml");

    byte[] bytes = ddlExporter.export(recordSet.getRecordSetId());

    File file = new File("record-set.xml");

    FileUtil.write(file, bytes);

    String expectedFileContent = read("test-record-set-export.xml");
    String actualFileContent = FileUtil.read(file);

    Assert.assertEquals(expectedFileContent, actualFileContent);
}

From source file:com.liferay.dynamic.data.mapping.service.impl.DDMTemplateLocalServiceImpl.java

License:Open Source License

protected File getSmallImageFile(DDMTemplate template) {
    File smallImageFile = null;//  ww w .  j  ava2 s .  c  om

    if (template.isSmallImage() && Validator.isNull(template.getSmallImageURL())) {

        Image smallImage = imageLocalService.fetchImage(template.getSmallImageId());

        if (smallImage != null) {
            smallImageFile = FileUtil.createTempFile(smallImage.getType());

            try {
                FileUtil.write(smallImageFile, smallImage.getTextObj());
            } catch (IOException ioe) {
                _log.error(ioe, ioe);
            }
        }
    }

    return smallImageFile;
}

From source file:com.liferay.dynamic.data.mapping.web.internal.exportimport.data.handler.DDMTemplateStagedModelDataHandler.java

License:Open Source License

@Override
protected void doImportStagedModel(PortletDataContext portletDataContext, DDMTemplate template)
        throws Exception {

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

    long classPK = template.getClassPK();

    if (classPK > 0) {
        Map<Long, Long> structureIds = (Map<Long, Long>) portletDataContext
                .getNewPrimaryKeysMap(DDMStructure.class);

        classPK = MapUtil.getLong(structureIds, classPK, classPK);
    }//  w  ww  .j a  va  2s. c o  m

    Element element = portletDataContext.getImportDataStagedModelElement(template);

    File smallFile = null;

    try {
        if (template.isSmallImage()) {
            String smallImagePath = element.attributeValue("small-image-path");

            if (Validator.isNotNull(template.getSmallImageURL())) {
                String smallImageURL = _ddmTemplateExportImportContentProcessor.replaceImportContentReferences(
                        portletDataContext, template, template.getSmallImageURL());

                template.setSmallImageURL(smallImageURL);
            } else if (Validator.isNotNull(smallImagePath)) {
                byte[] bytes = portletDataContext.getZipEntryAsByteArray(smallImagePath);

                if (bytes != null) {
                    smallFile = FileUtil.createTempFile(template.getSmallImageType());

                    FileUtil.write(smallFile, bytes);
                }
            }
        }

        String script = _ddmTemplateExportImportContentProcessor
                .replaceImportContentReferences(portletDataContext, template, template.getScript());

        template.setScript(script);

        long resourceClassNameId = 0L;

        if (template.getResourceClassNameId() > 0) {
            String resourceClassName = element.attributeValue("resource-class-name");

            if (Validator.isNotNull(resourceClassName)) {
                resourceClassNameId = _portal.getClassNameId(resourceClassName);
            }
        }

        ServiceContext serviceContext = portletDataContext.createServiceContext(template);

        DDMTemplate importedTemplate = null;

        if (portletDataContext.isDataStrategyMirror()) {
            boolean preloaded = GetterUtil.getBoolean(element.attributeValue("preloaded"));

            DDMTemplate existingTemplate = fetchExistingTemplate(template.getUuid(),
                    portletDataContext.getScopeGroupId(), template.getClassNameId(), template.getTemplateKey(),
                    preloaded);

            if (existingTemplate == null) {
                serviceContext.setUuid(template.getUuid());

                // Force a new template key if a template with the same key
                // already exists

                existingTemplate = _ddmTemplateLocalService.fetchTemplate(portletDataContext.getScopeGroupId(),
                        template.getClassNameId(), template.getTemplateKey());

                if (existingTemplate != null) {
                    template.setTemplateKey(null);
                }

                importedTemplate = _ddmTemplateLocalService.addTemplate(userId,
                        portletDataContext.getScopeGroupId(), template.getClassNameId(), classPK,
                        resourceClassNameId, template.getTemplateKey(), template.getNameMap(),
                        template.getDescriptionMap(), template.getType(), template.getMode(),
                        template.getLanguage(), template.getScript(), template.isCacheable(),
                        template.isSmallImage(), template.getSmallImageURL(), smallFile, serviceContext);
            } else {
                importedTemplate = _ddmTemplateLocalService.updateTemplate(userId,
                        existingTemplate.getTemplateId(), classPK, template.getNameMap(),
                        template.getDescriptionMap(), template.getType(), template.getMode(),
                        template.getLanguage(), template.getScript(), template.isCacheable(),
                        template.isSmallImage(), template.getSmallImageURL(), smallFile, serviceContext);
            }
        } else {
            importedTemplate = _ddmTemplateLocalService.addTemplate(userId,
                    portletDataContext.getScopeGroupId(), template.getClassNameId(), classPK,
                    resourceClassNameId, null, template.getNameMap(), template.getDescriptionMap(),
                    template.getType(), template.getMode(), template.getLanguage(), template.getScript(),
                    template.isCacheable(), template.isSmallImage(), template.getSmallImageURL(), smallFile,
                    serviceContext);
        }

        portletDataContext.importClassedModel(template, importedTemplate);

        Map<String, String> ddmTemplateKeys = (Map<String, String>) portletDataContext
                .getNewPrimaryKeysMap(DDMTemplate.class + ".ddmTemplateKey");

        ddmTemplateKeys.put(template.getTemplateKey(), importedTemplate.getTemplateKey());
    } finally {
        if (smallFile != null) {
            smallFile.delete();
        }
    }
}