Skip to content

Commit

Permalink
SHRINKWRAP-175 Add Filter support to Importers
Browse files Browse the repository at this point in the history
Add 2 new methods to interface StreamImporter

* importFrom(File, Filter)
* importFrom(InputStream, Filter)

Add ability create a new Archive based on content from given
Archive with Fitlers.

* Archive<T> shallowCopy(Filter)
* T filter(T)

Can be used to filter the export:

archive.filter(Filters.incldue("x")).as(ZipExporter.class).exportTo(..)
  • Loading branch information
aslakknutsen authored and ALRubinger committed Sep 13, 2015
1 parent 21e4367 commit ae0a43f
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 29 deletions.
19 changes: 19 additions & 0 deletions api/src/main/java/org/jboss/shrinkwrap/api/Archive.java
Expand Up @@ -384,6 +384,17 @@ <X extends Archive<X>> Collection<X> getAsType(Class<X> type, Filter<ArchivePath
*/
Map<ArchivePath, Node> getContent(Filter<ArchivePath> filter);

/**
* Obtains all assets matching given filter in this archive as a new Archive.<br/>
* <br/>
* This is an alias for shallowCopy(Filter).
*
* @see org.jboss.shrinkwrap.api.Archive#shallowCopy(Filter)
* @param filter
* @return
*/
T filter(Filter<ArchivePath> filter);

/**
* Add an archive under a specific context and maintain the archive name as context path.
*
Expand Down Expand Up @@ -580,4 +591,12 @@ T add(Archive<?> archive, ArchivePath path, Class<? extends StreamExporter> expo
*/
Archive<T> shallowCopy();

/**
* Creates a shallow copy of this {@link Archive} based on given filter.Assets from this archive are made available
* under the same paths. However, removing old assets or adding new assets on this archive affects does not affect
* the new archive.
*
* @return a new archive with a copy of the pointers to the assets
*/
Archive<T> shallowCopy(Filter<ArchivePath> filter);
}
Expand Up @@ -23,7 +23,9 @@
import java.util.zip.ZipInputStream;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.Assignable;
import org.jboss.shrinkwrap.api.Filter;

/**
* Generic importer capable of representing an {@link Assignable} as an entity capable of reading from an
Expand Down Expand Up @@ -51,16 +53,47 @@ public interface StreamImporter<I extends StreamImporter<I>> extends Assignable
*/
I importFrom(InputStream stream) throws ArchiveImportException;

/**
* Imports provided stream as a {@link Archive}. It remains the responsibility of the caller to close the stream.
*
* @param stream
* the stream to import; should be a raw type, not wrapped in any implementation-specific encoding (ie.
* {@link FileInputStream} is appropriate, but {@link ZipInputStream} or {@link GZIPInputStream} is not).
* @param filter
* Filter to match result
* @return Archive of the imported stream
* @throws ArchiveImportException
* If an error occurred during the import process
* @throws IllegalArgumentException
* If no stream is specified
*/
I importFrom(InputStream stream, Filter<ArchivePath> filter) throws ArchiveImportException;

/**
* Imports provided File as a {@link Archive}.
*
* @param file
* the file to import
* @return Archive of the imported Zip
* @return Archive of the imported file
* @throws ArchiveImportException
* If an error occurred during the import process
* @throws IllegalArgumentException
* If no file is specified or if the file is a directory
*/
I importFrom(File file) throws ArchiveImportException;

/**
* Imports provided File as a {@link Archive}.
*
* @param file
* the file to import
* @param filter
* Filter to match result
* @return Archive of the imported file
* @throws ArchiveImportException
* If an error occurred during the import process
* @throws IllegalArgumentException
* If no file is specified or if the file is a directory
*/
I importFrom(File file, Filter<ArchivePath> filter) throws ArchiveImportException;
}
Expand Up @@ -342,6 +342,16 @@ public <X extends Archive<X>> Collection<X> getAsType(Class<X> type, Filter<Arch
return archives;
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.Archive#filter(Filter)
*/
@Override
public T filter(Filter<ArchivePath> filter) {
return this.shallowCopy(filter).as(getActualClass());
}

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -524,6 +534,17 @@ public T merge(final Archive<?> source, final String path) throws IllegalArgumen
*/
@Override
public final Archive<T> shallowCopy() {
return this.shallowCopy(Filters.includeAll());
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.Archive#shallowCopy(Filter)
*/
@Override
public final Archive<T> shallowCopy(Filter<ArchivePath> filter) {
Validate.notNull(filter, "Filter must be specified");
// Use existing configuration
final Configuration configuration = this.getConfiguration();

Expand Down Expand Up @@ -557,6 +578,9 @@ public final Archive<T> shallowCopy() {
for (final ArchivePath path : from.getContent().keySet()) {
Asset asset = from.get(path).getAsset();
if (asset != null) {
if(!filter.include(path)) {
continue;
}
to.add(asset, path);
}
}
Expand Down
Expand Up @@ -479,6 +479,15 @@ public <X extends Archive<X>> Collection<X> getAsType(final Class<X> type, final
return this.getArchive().getAsType(type, filter, archiveCompression);
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.Archive#filter(Filter)
*/
@Override
public T filter(Filter<ArchivePath> filter) {
return this.shallowCopy(filter).as(getActualClass());
}
/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -526,6 +535,18 @@ public String getId() {
*/
@Override
public Archive<T> shallowCopy() {
return this.shallowCopy(Filters.includeAll());
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.Archive#shallowCopy(Filter)
*/
@Override
public Archive<T> shallowCopy(Filter<ArchivePath> filter) {
Validate.notNull(filter, "Filter must be specified");

// Get the actual class type and make a shallow copy of the the underlying archive,
// using the same underlying configuration
final Class<T> actualClass = this.getActualClass();
Expand All @@ -538,6 +559,9 @@ public Archive<T> shallowCopy() {
for (final ArchivePath path : contents.keySet()) {
Asset asset = contents.get(path).getAsset();
if (asset != null) {
if(!filter.include(path)) {
continue;
}
newArchive.add(asset, path);
}
}
Expand Down
Expand Up @@ -24,6 +24,10 @@
import java.util.logging.Logger;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.Filter;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
import org.jboss.shrinkwrap.api.importer.ArchiveImportException;
import org.jboss.shrinkwrap.api.importer.StreamImporter;
Expand Down Expand Up @@ -97,7 +101,18 @@ private I covarientReturn() {
*/
@Override
public I importFrom(final InputStream stream) throws ArchiveImportException {
return importFrom(stream, Filters.includeAll());
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.importer.StreamImporter#importFrom(java.io.InputStream, Filter)
*/
@Override
public I importFrom(final InputStream stream, Filter<ArchivePath> filter) throws ArchiveImportException {
Validate.notNull(stream, "Stream must be specified");
Validate.notNull(filter, "Filter must be specified");
final S tarStream;
try {
tarStream = this.getInputStreamForRawStream(stream);
Expand All @@ -106,21 +121,19 @@ public I importFrom(final InputStream stream) throws ArchiveImportException {
} catch (final IOException e) {
throw new ArchiveImportException("Could not wrap raw input with TAR stream", e);
}
return this.importFrom(tarStream);
return this.importFrom(tarStream, filter);
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.importer.StreamImporter#importFrom(java.io.InputStream)
*/
private I importFrom(final S stream) throws ArchiveImportException {
private I importFrom(final S stream, Filter<ArchivePath> filter) throws ArchiveImportException {
Validate.notNull(stream, "Stream must be specified");
try {
TarEntry entry;
while ((entry = stream.getNextEntry()) != null) {
// Get the name
String entryName = entry.getName();
if(!filter.include(ArchivePaths.create(entryName))) {
continue;
}

final Archive<?> archive = this.getArchive();

Expand Down Expand Up @@ -149,10 +162,20 @@ private I importFrom(final S stream) throws ArchiveImportException {
/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.importer.StreamImporter#importFrom(java.lang.Object)
* @see org.jboss.shrinkwrap.api.importer.StreamImporter#importFrom(java.io.File)
*/
@Override
public I importFrom(final File file) throws ArchiveImportException {
return importFrom(file, Filters.includeAll());
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.importer.StreamImporter#importFrom(java.io.File, Filter)
*/
@Override
public I importFrom(final File file, Filter<ArchivePath> filter) throws ArchiveImportException {
Validate.notNull(file, "File must be specified");
if (!file.exists()) {
throw new IllegalArgumentException("Specified file for import does not exist: " + file);
Expand All @@ -168,7 +191,7 @@ public I importFrom(final File file) throws ArchiveImportException {
throw new ArchiveImportException("Could not read archive file " + file, e);
}

return this.importFrom(archive);
return this.importFrom(archive, filter);

}

Expand Down
Expand Up @@ -27,6 +27,10 @@
import java.util.zip.ZipInputStream;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.Filter;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
import org.jboss.shrinkwrap.api.importer.ArchiveImportException;
import org.jboss.shrinkwrap.api.importer.ZipImporter;
Expand Down Expand Up @@ -96,7 +100,18 @@ public ZipImporter importZip(ZipFile file) {
*/
@Override
public ZipImporter importFrom(final InputStream stream) throws ArchiveImportException {
return importFrom(stream, Filters.includeAll());
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.importer.StreamImporter#importFrom(java.io.InputStream, Filter)
*/
@Override
public ZipImporter importFrom(InputStream stream, Filter<ArchivePath> filter) throws ArchiveImportException {
Validate.notNull(stream, "Stream must be specified");
Validate.notNull(filter, "Filter must be specified");

try {
// Wrap in ZipInputStream if we haven't been given one
Expand All @@ -107,6 +122,11 @@ public ZipImporter importFrom(final InputStream stream) throws ArchiveImportExce
// Get the name
final String entryName = entry.getName();

if(!filter.include(ArchivePaths.create(entryName))) {
zipStream.closeEntry();
continue;
}

// Get the archive
final Archive<?> archive = this.getArchive();

Expand All @@ -132,12 +152,24 @@ public ZipImporter importFrom(final InputStream stream) throws ArchiveImportExce
*
* @see org.jboss.shrinkwrap.api.importer.StreamImporter#importFrom(java.io.File)
*/
@Override
public ZipImporter importFrom(final File file) throws ArchiveImportException {
return importFrom(file, Filters.includeAll());
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.importer.StreamImporter#importFrom(java.io.File, Filter)
*/
@Override
public ZipImporter importFrom(File file, Filter<ArchivePath> filter) throws ArchiveImportException {
Validate.notNull(file, "File must be specified");
if (file.isDirectory()) {
throw new IllegalArgumentException("File to import as ZIP must not be a directory: "
+ file.getAbsolutePath());
}
Validate.notNull(filter, "Filter must be specified");

final ZipFile zipFile;
try {
Expand All @@ -147,7 +179,7 @@ public ZipImporter importFrom(final File file) throws ArchiveImportException {
}

// Delegate
return this.importFrom(zipFile);
return this.importFrom(zipFile, filter);
}

/**
Expand All @@ -157,6 +189,10 @@ public ZipImporter importFrom(final File file) throws ArchiveImportException {
*/
@Override
public ZipImporter importFrom(final ZipFile file) throws ArchiveImportException {
return importFrom(file, Filters.includeAll());
}

private ZipImporter importFrom(final ZipFile file, Filter<ArchivePath> filter) throws ArchiveImportException {
Validate.notNull(file, "File must be specified");

try {
Expand All @@ -166,7 +202,9 @@ public ZipImporter importFrom(final ZipFile file) throws ArchiveImportException

// Get the entry (path) name
final String entryName = entry.getName();

if(!filter.include(ArchivePaths.create(entryName))) {
continue;
}
// Get the archive
final Archive<?> archive = this.getArchive();

Expand Down

0 comments on commit ae0a43f

Please sign in to comment.