Example usage for com.liferay.portal.kernel.zip ZipWriter addEntry

List of usage examples for com.liferay.portal.kernel.zip ZipWriter addEntry

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.zip ZipWriter addEntry.

Prototype

public void addEntry(String name, StringBuilder sb) throws IOException;

Source Link

Usage

From source file:com.liferay.configuration.admin.web.internal.portlet.action.ExportConfigurationMVCResourceCommand.java

License:Open Source License

protected void exportAll(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String languageId = themeDisplay.getLanguageId();

    ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();

    Map<String, ConfigurationModel> configurationModels = _configurationModelRetriever
            .getConfigurationModels(themeDisplay.getLanguageId());

    for (ConfigurationModel configurationModel : configurationModels.values()) {

        if (configurationModel.isFactory()) {
            String curFactoryPid = configurationModel.getFactoryPid();

            List<ConfigurationModel> factoryInstances = _configurationModelRetriever
                    .getFactoryInstances(configurationModel);

            for (ConfigurationModel factoryInstance : factoryInstances) {
                String curPid = factoryInstance.getID();

                String curFileName = getFileName(curFactoryPid, curPid);

                zipWriter.addEntry(curFileName, ConfigurationExporter
                        .getPropertiesAsBytes(getProperties(languageId, curFactoryPid, curPid)));
            }/* w w w .  j a v  a 2  s.co m*/
        } else if (configurationModel.hasConfiguration()) {
            String curPid = configurationModel.getID();

            String curFileName = getFileName(null, curPid);

            zipWriter.addEntry(curFileName,
                    ConfigurationExporter.getPropertiesAsBytes(getProperties(languageId, curPid, curPid)));
        }
    }

    String fileName = "liferay-system-settings.zip";

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, fileName,
            new FileInputStream(zipWriter.getFile()), ContentTypes.APPLICATION_ZIP);
}

From source file:com.liferay.configuration.admin.web.internal.portlet.action.ExportConfigurationMVCResourceCommand.java

License:Open Source License

protected void exportFactoryPid(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String languageId = themeDisplay.getLanguageId();

    ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();

    String factoryPid = ParamUtil.getString(resourceRequest, "factoryPid");

    Map<String, ConfigurationModel> configurationModels = _configurationModelRetriever
            .getConfigurationModels(themeDisplay.getLanguageId());

    ConfigurationModel factoryConfigurationModel = configurationModels.get(factoryPid);

    List<ConfigurationModel> factoryInstances = _configurationModelRetriever
            .getFactoryInstances(factoryConfigurationModel);

    for (ConfigurationModel factoryInstance : factoryInstances) {
        String curPid = factoryInstance.getID();

        String curFileName = getFileName(factoryPid, curPid);

        zipWriter.addEntry(curFileName,
                ConfigurationExporter.getPropertiesAsBytes(getProperties(languageId, factoryPid, curPid)));
    }//from  w w w.j  a  v  a 2 s  .  com

    String fileName = "liferay-system-settings-" + factoryConfigurationModel.getFactoryPid() + ".zip";

    PortletResponseUtil.sendFile(resourceRequest, resourceResponse, fileName,
            new FileInputStream(zipWriter.getFile()), ContentTypes.APPLICATION_ZIP);
}

From source file:com.liferay.document.library.web.internal.portlet.action.DownloadEntriesMVCResourceCommand.java

License:Open Source License

protected void zipFileEntry(FileEntry fileEntry, String path, ZipWriter zipWriter) throws Exception {

    zipWriter.addEntry(path + StringPool.SLASH + fileEntry.getFileName(), fileEntry.getContentStream());
}

From source file:com.liferay.exportimport.lar.PortletDataContextImpl.java

License:Open Source License

@Override
public void addZipEntry(String path, byte[] bytes) {
    if (isPathProcessed(path)) {
        return;/*  ww w .  j a  va2 s  .  c  o  m*/
    }

    try {
        ZipWriter zipWriter = getZipWriter();

        zipWriter.addEntry(path, bytes);
    } catch (IOException ioe) {
        throw new SystemException("Unable to add data bytes to the LAR file with path: " + path, ioe);
    }
}

From source file:com.liferay.exportimport.lar.PortletDataContextImpl.java

License:Open Source License

@Override
public void addZipEntry(String path, InputStream is) {
    if (isPathProcessed(path)) {
        return;/*from   w ww. ja va 2s  .  c o m*/
    }

    try {
        ZipWriter zipWriter = getZipWriter();

        zipWriter.addEntry(path, is);
    } catch (IOException ioe) {
        throw new SystemException("Unable to add data stream to the LAR file with path: " + path, ioe);
    }
}

From source file:com.liferay.exportimport.lar.PortletDataContextImpl.java

License:Open Source License

@Override
public void addZipEntry(String path, String s) {
    if (isPathProcessed(path)) {
        return;/* w w  w.  jav  a  2  s .  co  m*/
    }

    try {
        ZipWriter zipWriter = getZipWriter();

        zipWriter.addEntry(path, s);
    } catch (IOException ioe) {
        throw new SystemException("Unable to add data string to the LAR file with path: " + path, ioe);
    }
}

From source file:com.liferay.exportimport.test.ExportImportHelperUtilTest.java

License:Open Source License

@Test
public void testValidateMissingReferences() throws Exception {
    String xml = replaceParameters(getContent("missing_references.txt"), getFileEntry());

    ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();

    zipWriter.addEntry("/manifest.xml", xml);

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(zipWriter.getFile());

    PortletDataContext portletDataContextImport = PortletDataContextFactoryUtil.createImportPortletDataContext(
            _liveGroup.getCompanyId(), _liveGroup.getGroupId(), new HashMap<String, String[]>(),
            new TestUserIdStrategy(), zipReader);

    MissingReferences missingReferences = ExportImportHelperUtil
            .validateMissingReferences(portletDataContextImport);

    Map<String, MissingReference> dependencyMissingReferences = missingReferences
            .getDependencyMissingReferences();

    Map<String, MissingReference> weakMissingReferences = missingReferences.getWeakMissingReferences();

    Assert.assertEquals(dependencyMissingReferences.toString(), 2, dependencyMissingReferences.size());
    Assert.assertEquals(weakMissingReferences.toString(), 1, weakMissingReferences.size());

    FileUtil.delete(zipWriter.getFile());

    zipReader.close();// w ww. java 2 s  .c  om
}

From source file:com.liferay.samplelar.plugin.LARPlugin.java

License:Open Source License

@Override
public String exportData(PortletDataContext context, String portletId, PortletPreferences preferences)
        throws PortletDataException {

    Map parameterMap = context.getParameterMap();

    boolean exportData = MapUtil.getBoolean(parameterMap, "export-sample-lar-portlet-data",
            _enableExport.getDefaultState());

    if (_log.isDebugEnabled()) {
        if (exportData) {
            _log.debug("Exporting data is enabled");
        } else {//from w  w w  .  ja v a 2 s .c om
            _log.debug("Exporting data is disabled");
        }
    }

    if (!exportData) {
        return null;
    }

    try {
        long exportDate = System.currentTimeMillis();

        if (_log.isInfoEnabled()) {
            _log.info("Exporting LAR on " + new Date(exportDate));
        }

        preferences.setValue("last-export-date", String.valueOf(exportDate));

        preferences.store();

        String data = "<data-file />";

        ZipWriter zipWriter = context.getZipWriter();

        if (zipWriter != null) {
            boolean createReadMe = MapUtil.getBoolean(parameterMap, "create-readme",
                    _createReadme.getDefaultState());

            if (createReadMe) {
                if (_log.isInfoEnabled()) {
                    _log.info("Writing to zip");
                }

                zipWriter.addEntry(portletId + "/README.txt", "Test writing to zip.");
            }

            String dataType = MapUtil.getString(parameterMap, "data-type", _dataType.getDefaultChoice());

            if (Validator.equals(dataType, "csv")) {
                StringBuilder csv = new StringBuilder();

                csv.append("data 1," + new Date() + "\n");
                csv.append("data 2," + new Date() + "\n");

                String filePath = portletId + "/data.csv";

                data = "<data-file>" + filePath + "</data-file>";

                zipWriter.addEntry(filePath, csv.toString());
            } else if (Validator.equals(dataType, "xml")) {
                StringBuilder xml = new StringBuilder();

                xml.append("<?xml version=\"1.0\"?>\n\n");
                xml.append("<records>\n");
                xml.append("\t<record>\n");
                xml.append("\t\t<field>data 1</field>\n");
                xml.append("\t\t<field>" + new Date() + "</field>\n");
                xml.append("\t</record>\n");
                xml.append("\t<record>\n");
                xml.append("\t\t<field>data 2</field>\n");
                xml.append("\t\t<field>" + new Date() + "</field>\n");
                xml.append("\t</record>\n");
                xml.append("</records>");

                String filePath = portletId + "/data.xml";

                data = "<data-file>" + filePath + "</data-file>";

                zipWriter.addEntry(filePath, xml.toString());
            }
        }

        return data;
    } catch (Exception e) {
        throw new PortletDataException(e);
    }
}

From source file:com.liferay.sync.internal.servlet.SyncDownloadServlet.java

License:Open Source License

protected void addZipFolderEntry(long userId, long repositoryId, long folderId, String folderPath,
        ZipWriter zipWriter) throws Exception {

    List<FileEntry> fileEntries = _dlAppService.getFileEntries(repositoryId, folderId);

    for (FileEntry fileEntry : fileEntries) {
        try (InputStream inputStream = _dlFileEntryLocalService.getFileAsStream(userId,
                fileEntry.getFileEntryId(), fileEntry.getVersion(), false)) {

            String filePath = folderPath + fileEntry.getTitle();

            zipWriter.addEntry(filePath, inputStream);
        }// w  w w.  j a  v  a 2  s.  c o m
    }

    List<Folder> childFolders = _dlAppService.getFolders(repositoryId, folderId);

    for (Folder childFolder : childFolders) {
        String childFolderPath = folderPath + childFolder.getName() + StringPool.FORWARD_SLASH;

        addZipFolderEntry(userId, repositoryId, childFolder.getFolderId(), childFolderPath, zipWriter);
    }
}

From source file:com.liferay.sync.internal.servlet.SyncDownloadServlet.java

License:Open Source License

protected void sendZipFile(HttpServletResponse response, long userId, JSONArray zipFileIdsJSONArray)
        throws Exception {

    ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();

    JSONObject errorsJSONObject = JSONFactoryUtil.createJSONObject();

    for (int i = 0; i < zipFileIdsJSONArray.length(); i++) {
        JSONObject zipObjectJSONObject = zipFileIdsJSONArray.getJSONObject(i);

        long groupId = zipObjectJSONObject.getLong("groupId");
        String zipFileId = zipObjectJSONObject.getString("zipFileId");

        Group group = _groupLocalService.fetchGroup(groupId);

        if ((group == null) || !_syncUtil.isSyncEnabled(group)) {
            processException(zipFileId, SyncSiteUnavailableException.class.getName(), errorsJSONObject);

            continue;
        }/*  w ww  . j  a v a2  s .  c om*/

        try (InputStream inputStream = _getZipFileInputStream(zipObjectJSONObject, userId, groupId)) {

            zipWriter.addEntry(zipFileId, inputStream);
        } catch (Exception e) {
            Class<?> clazz = e.getClass();

            processException(zipFileId, clazz.getName(), errorsJSONObject);
        }
    }

    zipWriter.addEntry("errors.json", errorsJSONObject.toString());

    File file = zipWriter.getFile();

    ServletResponseUtil.write(response, new FileInputStream(file), file.length());
}