Skip to content

Commit

Permalink
Replace org.apache.compress with java.util.zip (winery#348)
Browse files Browse the repository at this point in the history
This change removes ArchiveException from the compress library from the public API of
CsarExporter. That way downstream consumers do not need to explicitly rely on that library.

This also allows us to drop the commons-compress dependency from
org.eclipse.winery.repository.

Signed-off-by: Clemens Lieb <[email protected]>
  • Loading branch information
Vogel612 authored and lharzenetter committed Jul 31, 2018
1 parent 20451b8 commit ee7b5ca
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 57 deletions.
12 changes: 0 additions & 12 deletions org.eclipse.winery.repository/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@
<version>${com.fasterxml.jackson.core.jackson-annotations}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.6</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- Version 1.3 is approved by Eclipse, Version 1.4 is not -->
<groupId>org.tukaani</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import org.eclipse.winery.repository.exceptions.RepositoryCorruptException;
import org.eclipse.winery.repository.export.CsarExporter;

import org.apache.commons.compress.archivers.ArchiveException;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -421,10 +420,6 @@ private void checkCsar(DefinitionsChildId id, Path tempCsar) {
try (OutputStream outputStream = Files.newOutputStream(tempCsar, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
try {
exporter.writeCsar(RepositoryFactory.getRepository(), id, outputStream, exportConfiguration);
} catch (ArchiveException e) {
LOGGER.debug("Error during checking ZIP", e);
printAndAddError(id, "Invalid zip file: " + e.getMessage());
return;
} catch (JAXBException e) {
LOGGER.debug("Error during checking ZIP", e);
printAndAddError(id, "Some XML could not be parsed: " + e.getMessage() + " " + e.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@
import org.eclipse.winery.repository.configuration.FileBasedRepositoryConfiguration;
import org.eclipse.winery.repository.exceptions.WineryRepositoryException;

import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
Expand Down Expand Up @@ -773,23 +769,21 @@ public void getZippedContents(final GenericId id, OutputStream out) throws Winer

SortedSet<RepositoryFileReference> containedFiles = this.getContainedFiles(id);

try (final ArchiveOutputStream zos = new ArchiveStreamFactory().createArchiveOutputStream("zip", out)) {
try (final ZipOutputStream zos = new ZipOutputStream(out)) {
for (RepositoryFileReference ref : containedFiles) {
ZipArchiveEntry zipArchiveEntry;
ZipEntry zipArchiveEntry;
final Optional<Path> subDirectory = ref.getSubDirectory();
if (subDirectory.isPresent()) {
zipArchiveEntry = new ZipArchiveEntry(subDirectory.get().resolve(ref.getFileName()).toString());
zipArchiveEntry = new ZipEntry(subDirectory.get().resolve(ref.getFileName()).toString());
} else {
zipArchiveEntry = new ZipArchiveEntry(ref.getFileName());
zipArchiveEntry = new ZipEntry(ref.getFileName());
}
zos.putArchiveEntry(zipArchiveEntry);
zos.putNextEntry(zipArchiveEntry);
try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
IOUtils.copy(is, zos);
}
zos.closeArchiveEntry();
zos.closeEntry();
}
} catch (ArchiveException e) {
throw new WineryRepositoryException("Internal error while generating archive", e);
} catch (IOException e) {
throw new WineryRepositoryException("I/O exception during export", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.Objects;
import java.util.SortedSet;
import java.util.concurrent.CompletableFuture;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.xml.bind.JAXBException;
import javax.xml.transform.Transformer;
Expand Down Expand Up @@ -73,11 +75,6 @@
import org.eclipse.winery.repository.datatypes.ids.elements.ServiceTemplateSelfServiceFilesDirectoryId;
import org.eclipse.winery.repository.exceptions.RepositoryCorruptException;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.io.IOUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
Expand Down Expand Up @@ -126,7 +123,7 @@ private static String getDefinitionsPathInsideCSAR(IGenericRepository repository
}

public CompletableFuture<String> writeCsarAndSaveManifestInProvenanceLayer(IRepository repository, DefinitionsChildId entryId, OutputStream out)
throws IOException, JAXBException, RepositoryCorruptException, ArchiveException, ProvenanceException {
throws IOException, JAXBException, RepositoryCorruptException, ProvenanceException {
Map<String, Object> exportConfiguration = new HashMap<>();
exportConfiguration.put(CsarExportConfiguration.INCLUDE_HASHES.name(), true);
exportConfiguration.put(CsarExportConfiguration.INCLUDE_PROVENANCE.name(), true);
Expand All @@ -145,13 +142,13 @@ public CompletableFuture<String> writeCsarAndSaveManifestInProvenanceLayer(IRepo
* @param out the output stream to write to
*/
public String writeCsar(IRepository repository, DefinitionsChildId entryId, OutputStream out, Map<String, Object> exportConfiguration)
throws ArchiveException, IOException, JAXBException, RepositoryCorruptException {
throws IOException, JAXBException, RepositoryCorruptException {
CsarExporter.LOGGER.trace("Starting CSAR export with {}", entryId.toString());

Map<RepositoryFileReference, CsarContentProperties> refMap = new HashMap<>();
Collection<CsarContentProperties> definitionNames = new ArrayList<>();

try (final ArchiveOutputStream zos = new ArchiveStreamFactory().createArchiveOutputStream("zip", out)) {
try (final ZipOutputStream zos = new ZipOutputStream(out)) {
ToscaExportUtil exporter = new ToscaExportUtil();

ExportedState exportedState = new ExportedState();
Expand All @@ -162,10 +159,10 @@ public String writeCsar(IRepository repository, DefinitionsChildId entryId, Outp
CsarContentProperties definitionsFileProperties = new CsarContentProperties(definitionsPathInsideCSAR);
definitionNames.add(definitionsFileProperties);

zos.putArchiveEntry(new ZipArchiveEntry(definitionsPathInsideCSAR));
zos.putNextEntry(new ZipEntry(definitionsPathInsideCSAR));
Collection<DefinitionsChildId> referencedIds;
referencedIds = exporter.exportTOSCA(repository, currentId, definitionsFileProperties, zos, refMap, exportConfiguration);
zos.closeArchiveEntry();
zos.closeEntry();

// for each entryId add license and readme files (if they exist) to the refMap
addLicenseAndReadmeFiles(repository, currentId, refMap);
Expand Down Expand Up @@ -226,7 +223,7 @@ public String writeCsar(IRepository repository, DefinitionsChildId entryId, Outp
* @param fileProperties Describing the 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,
private void addArtifactTemplateToZipFile(ZipOutputStream zos, IGenericRepository repository, RepositoryFileReference ref,
CsarContentProperties fileProperties, Map<String, Object> exportConfiguration) throws IOException {
GitInfo gitInfo = BackendUtils.getGitInformation((DirectoryId) ref.getParent());

Expand Down Expand Up @@ -266,7 +263,7 @@ private void addArtifactTemplateToZipFile(ArchiveOutputStream zos, IGenericRepos
* @param ref Reference to the file that should be added to the archive
* @param csarContentProperties Describing the path inside the archive to the file
*/
private void addFileToZipArchive(ArchiveOutputStream zos, IGenericRepository repository, RepositoryFileReference ref,
private void addFileToZipArchive(ZipOutputStream zos, IGenericRepository repository, RepositoryFileReference ref,
CsarContentProperties csarContentProperties, Map<String, Object> exportConfiguration) {
if (exportConfiguration.containsKey(CsarExportConfiguration.INCLUDE_HASHES.toString())) {
try (InputStream is = repository.newInputStream(ref)) {
Expand All @@ -277,10 +274,9 @@ private void addFileToZipArchive(ArchiveOutputStream zos, IGenericRepository rep
}
}
try (InputStream is = repository.newInputStream(ref)) {
ArchiveEntry archiveEntry = new ZipArchiveEntry(csarContentProperties.getPathInsideCsar());
zos.putArchiveEntry(archiveEntry);
zos.putNextEntry(new ZipEntry(csarContentProperties.getPathInsideCsar()));
IOUtils.copy(is, zos);
zos.closeArchiveEntry();
zos.closeEntry();
} catch (Exception e) {
CsarExporter.LOGGER.error("Could not copy file content to ZIP outputstream", e);
}
Expand All @@ -295,12 +291,12 @@ private void addFileToZipArchive(ArchiveOutputStream zos, IGenericRepository rep
* @param ref The dummy document that should be exported as an archive
* @param fileProperties The output path of the archive
*/
private void addDummyRepositoryFileReferenceForGeneratedXSD(ArchiveOutputStream zos, Transformer transformer,
private void addDummyRepositoryFileReferenceForGeneratedXSD(ZipOutputStream zos, Transformer transformer,
DummyRepositoryFileReferenceForGeneratedXSD ref,
CsarContentProperties fileProperties,
Map<String, Object> exportConfiguration) throws IOException {
ArchiveEntry archiveEntry = new ZipArchiveEntry(fileProperties.getPathInsideCsar());
zos.putArchiveEntry(archiveEntry);
ZipEntry archiveEntry = new ZipEntry(fileProperties.getPathInsideCsar());
zos.putNextEntry(archiveEntry);
CsarExporter.LOGGER.trace("Special treatment for generated XSDs");

Document document = ref.getDocument();
Expand All @@ -318,7 +314,7 @@ private void addDummyRepositoryFileReferenceForGeneratedXSD(ArchiveOutputStream
}

zos.write(bytes);
zos.closeArchiveEntry();
zos.closeEntry();
} catch (TransformerException e) {
CsarExporter.LOGGER.debug("Could not serialize generated xsd", e);
} catch (NoSuchAlgorithmException e) {
Expand Down Expand Up @@ -358,7 +354,7 @@ private void deleteDirectory(Path path) {
* @param rootDir The root of the working tree
* @param archivePath The path inside the archive to the working tree
*/
private void addWorkingTreeToArchive(ArchiveOutputStream zos, TArtifactTemplate template, Path rootDir, String archivePath) {
private void addWorkingTreeToArchive(ZipOutputStream zos, TArtifactTemplate template, Path rootDir, String archivePath) {
addWorkingTreeToArchive(rootDir.toFile(), zos, template, rootDir, archivePath);
}

Expand All @@ -371,7 +367,7 @@ private void addWorkingTreeToArchive(ArchiveOutputStream zos, TArtifactTemplate
* @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) {
private void addWorkingTreeToArchive(File file, ZipOutputStream zos, TArtifactTemplate template, Path rootDir, String archivePath) {
if (file.isDirectory()) {
if (file.getName().equals(".git")) {
return;
Expand Down Expand Up @@ -415,10 +411,10 @@ private void addWorkingTreeToArchive(File file, ArchiveOutputStream zos, TArtifa

if ((!foundInclude || included) && !excluded) {
try (InputStream is = new FileInputStream(file)) {
ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath + rootDir.relativize(Paths.get(file.getAbsolutePath())));
zos.putArchiveEntry(archiveEntry);
ZipEntry archiveEntry = new ZipEntry(archivePath + rootDir.relativize(Paths.get(file.getAbsolutePath())));
zos.putNextEntry(archiveEntry);
IOUtils.copy(is, zos);
zos.closeArchiveEntry();
zos.closeEntry();
} catch (Exception e) {
CsarExporter.LOGGER.error("Could not copy file to ZIP outputstream", e);
}
Expand Down Expand Up @@ -554,10 +550,10 @@ private void addSelfServiceFiles(IRepository repository, ServiceTemplateId servi
}

private String addManifest(IRepository repository, DefinitionsChildId id, Collection<CsarContentProperties> definitionNames,
Map<RepositoryFileReference, CsarContentProperties> refMap, ArchiveOutputStream out, Map<String, Object> exportConfiguration) throws IOException {
Map<RepositoryFileReference, CsarContentProperties> refMap, ZipOutputStream out, Map<String, Object> exportConfiguration) throws IOException {
String entryDefinitionsReference = CsarExporter.getDefinitionsPathInsideCSAR(repository, id);

out.putArchiveEntry(new ZipArchiveEntry("TOSCA-Metadata/TOSCA.meta"));
out.putNextEntry(new ZipEntry("TOSCA-Metadata/TOSCA.meta"));
StringBuilder stringBuilder = new StringBuilder();

// Setting Versions
Expand Down Expand Up @@ -615,7 +611,7 @@ private String addManifest(IRepository repository, DefinitionsChildId id, Collec
String manifestString = stringBuilder.toString();
out.write(manifestString.getBytes());

out.closeArchiveEntry();
out.closeEntry();

return manifestString;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.eclipse.winery.repository.TestWithGitBackedRepository;
import org.eclipse.winery.repository.backend.RepositoryFactory;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.IOUtils;
import org.eclipse.virgo.util.parser.manifest.ManifestContents;
import org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser;
import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit ee7b5ca

Please sign in to comment.