Search in sources :

Example 6 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project winery by eclipse.

the class CsarExporter method addDummyRepositoryFileReferenceForGeneratedXSD.

/**
 * Adds a dummy file to the archive
 *
 * @param zos         Output stream of the archive
 * @param transformer Given transformer to transform the {@link DummyRepositoryFileReferenceForGeneratedXSD} to a
 *                    {@link ArchiveOutputStream}
 * @param ref         The dummy document that should be exported as an archive
 * @param archivePath The output path of the archive
 */
private void addDummyRepositoryFileReferenceForGeneratedXSD(ArchiveOutputStream zos, Transformer transformer, DummyRepositoryFileReferenceForGeneratedXSD ref, String archivePath) throws IOException {
    ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath);
    zos.putArchiveEntry(archiveEntry);
    CsarExporter.LOGGER.trace("Special treatment for generated XSDs");
    Document document = ref.getDocument();
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(zos);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        CsarExporter.LOGGER.debug("Could not serialize generated xsd", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

Example 7 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project winery by eclipse.

the class CsarExporter method addNamespacePrefixes.

/**
 * Writes the configured mapping namespaceprefix -> namespace to the archive
 * <p>
 * This is kind of a quick hack. TODO: during the import, the prefixes should be extracted using JAXB and stored in
 * the NamespacesResource
 */
private void addNamespacePrefixes(ArchiveOutputStream zos, IRepository repository) throws IOException {
    Configuration configuration = repository.getConfiguration(new NamespacesId());
    if (configuration instanceof PropertiesConfiguration) {
        // Quick hack: direct serialization only works for PropertiesConfiguration
        PropertiesConfiguration pconf = (PropertiesConfiguration) configuration;
        ArchiveEntry archiveEntry = new ZipArchiveEntry(CsarExporter.PATH_TO_NAMESPACES_PROPERTIES);
        zos.putArchiveEntry(archiveEntry);
        try {
            pconf.save(zos);
        } catch (ConfigurationException e) {
            CsarExporter.LOGGER.debug(e.getMessage(), e);
            zos.write("#Could not export properties".getBytes());
            zos.write(("#" + e.getMessage()).getBytes());
        }
        zos.closeArchiveEntry();
    }
}
Also used : Configuration(org.apache.commons.configuration.Configuration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) NamespacesId(org.eclipse.winery.common.ids.admin.NamespacesId) ConfigurationException(org.apache.commons.configuration.ConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration)

Example 8 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project winery by eclipse.

the class CsarExporter method addWorkingTreeToArchive.

/**
 * Adds a working tree to an archive
 *
 * @param file        The current directory to add
 * @param zos         Output stream of the archive
 * @param template    Template of the artifact
 * @param rootDir     The root of the working tree
 * @param archivePath The path inside the archive to the working tree
 */
private void addWorkingTreeToArchive(File file, ArchiveOutputStream zos, TArtifactTemplate template, Path rootDir, String archivePath) {
    if (file.isDirectory()) {
        if (file.getName().equals(".git")) {
            return;
        }
        File[] files = file.listFiles();
        if (files != null) {
            for (File f : files) {
                addWorkingTreeToArchive(f, zos, template, rootDir, archivePath);
            }
        }
    } else {
        boolean foundInclude = false;
        boolean included = false;
        boolean excluded = false;
        for (TArtifactReference artifactReference : template.getArtifactReferences().getArtifactReference()) {
            for (Object includeOrExclude : artifactReference.getIncludeOrExclude()) {
                if (includeOrExclude instanceof TArtifactReference.Include) {
                    foundInclude = true;
                    TArtifactReference.Include include = (TArtifactReference.Include) includeOrExclude;
                    String reference = artifactReference.getReference();
                    if (reference.endsWith("/")) {
                        reference += include.getPattern();
                    } else {
                        reference += "/" + include.getPattern();
                    }
                    reference = reference.substring(1);
                    included |= BackendUtils.isGlobMatch(reference, rootDir.relativize(file.toPath()));
                } else if (includeOrExclude instanceof TArtifactReference.Exclude) {
                    TArtifactReference.Exclude exclude = (TArtifactReference.Exclude) includeOrExclude;
                    String reference = artifactReference.getReference();
                    if (reference.endsWith("/")) {
                        reference += exclude.getPattern();
                    } else {
                        reference += "/" + exclude.getPattern();
                    }
                    reference = reference.substring(1);
                    excluded |= BackendUtils.isGlobMatch(reference, rootDir.relativize(file.toPath()));
                }
            }
        }
        if ((!foundInclude || included) && !excluded) {
            try (InputStream is = new FileInputStream(file)) {
                ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath + rootDir.relativize(Paths.get(file.getAbsolutePath())));
                zos.putArchiveEntry(archiveEntry);
                IOUtils.copy(is, zos);
                zos.closeArchiveEntry();
            } catch (Exception e) {
                CsarExporter.LOGGER.error("Could not copy file to ZIP outputstream", e);
            }
        }
    }
}
Also used : TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) JAXBException(javax.xml.bind.JAXBException) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Example 9 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project winery by eclipse.

the class CsarExporter method addArtifactTemplateToZipFile.

/**
 * Special handling for artifact template directories source and files
 *
 * @param zos         Output stream for the archive that should contain the file
 * @param ref         Reference to the file that should be added to the archive
 * @param archivePath Path to the file inside the archive
 * @throws IOException thrown when the temporary directory can not be created
 */
private void addArtifactTemplateToZipFile(ArchiveOutputStream zos, IGenericRepository repository, RepositoryFileReference ref, String archivePath) throws IOException {
    GitInfo gitInfo = BackendUtils.getGitInformation((DirectoryId) ref.getParent());
    if (gitInfo == null) {
        try (InputStream is = repository.newInputStream(ref)) {
            if (is != null) {
                ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath);
                zos.putArchiveEntry(archiveEntry);
                IOUtils.copy(is, zos);
                zos.closeArchiveEntry();
            }
        } catch (Exception e) {
            CsarExporter.LOGGER.error("Could not copy file to ZIP outputstream", e);
        }
        return;
    }
    // TODO: This is not quite correct. The files should reside checked out at "source/"
    Path tempDir = Files.createTempDirectory(WINERY_TEMP_DIR_PREFIX);
    try {
        Git git = Git.cloneRepository().setURI(gitInfo.URL).setDirectory(tempDir.toFile()).call();
        git.checkout().setName(gitInfo.BRANCH).call();
        String path = "artifacttemplates/" + Util.URLencode(((ArtifactTemplateId) ref.getParent().getParent()).getQName().getNamespaceURI()) + "/" + ((ArtifactTemplateId) ref.getParent().getParent()).getQName().getLocalPart() + "/files/";
        TArtifactTemplate template = BackendUtils.getTArtifactTemplate((DirectoryId) ref.getParent());
        addWorkingTreeToArchive(zos, template, tempDir, path);
    } catch (GitAPIException e) {
        CsarExporter.LOGGER.error(String.format("Error while cloning repo: %s / %s", gitInfo.URL, gitInfo.BRANCH), e);
    } finally {
        deleteDirectory(tempDir);
    }
}
Also used : Path(java.nio.file.Path) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) GitInfo(org.eclipse.winery.repository.GitInfo) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) JAXBException(javax.xml.bind.JAXBException) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)

Example 10 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project cuba by cuba-platform.

the class EntityImportExport method exportEntitiesToZIP.

@Override
public byte[] exportEntitiesToZIP(Collection<? extends Entity> entities) {
    String json = entitySerialization.toJson(entities, null, EntitySerializationOption.COMPACT_REPEATED_ENTITIES);
    byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream);
    zipOutputStream.setMethod(ZipArchiveOutputStream.STORED);
    zipOutputStream.setEncoding(StandardCharsets.UTF_8.name());
    ArchiveEntry singleDesignEntry = newStoredEntry("entities.json", jsonBytes);
    try {
        zipOutputStream.putArchiveEntry(singleDesignEntry);
        zipOutputStream.write(jsonBytes);
        zipOutputStream.closeArchiveEntry();
    } catch (Exception e) {
        throw new RuntimeException("Error on creating zip archive during entities export", e);
    } finally {
        IOUtils.closeQuietly(zipOutputStream);
    }
    return byteArrayOutputStream.toByteArray();
}
Also used : ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EntityValidationException(com.haulmont.cuba.core.global.validation.EntityValidationException) IOException(java.io.IOException) CustomValidationException(com.haulmont.cuba.core.global.validation.CustomValidationException)

Aggregations

ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)62 File (java.io.File)24 FileInputStream (java.io.FileInputStream)24 IOException (java.io.IOException)22 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)20 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)19 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)17 InputStream (java.io.InputStream)16 FileOutputStream (java.io.FileOutputStream)11 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)11 BufferedInputStream (java.io.BufferedInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Path (java.nio.file.Path)9 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)9 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)6 OutputStream (java.io.OutputStream)5 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)4