Search in sources :

Example 1 with TransferDecorator

use of org.commonjava.maven.galley.spi.io.TransferDecorator in project galley by Commonjava.

the class PartyLineCacheProviderTest method setup.

@Before
public void setup() throws Exception {
    final PathGenerator pathgen = new HashedLocationPathGenerator();
    final FileEventManager events = new TestFileEventManager();
    final TransferDecorator decorator = new TestTransferDecorator();
    provider = new PartyLineCacheProvider(temp.newFolder(), pathgen, events, decorator);
}
Also used : TestTransferDecorator(org.commonjava.maven.galley.cache.testutil.TestTransferDecorator) TestFileEventManager(org.commonjava.maven.galley.cache.testutil.TestFileEventManager) FileEventManager(org.commonjava.maven.galley.spi.event.FileEventManager) HashedLocationPathGenerator(org.commonjava.maven.galley.io.HashedLocationPathGenerator) HashedLocationPathGenerator(org.commonjava.maven.galley.io.HashedLocationPathGenerator) PathGenerator(org.commonjava.maven.galley.spi.io.PathGenerator) TestFileEventManager(org.commonjava.maven.galley.cache.testutil.TestFileEventManager) TestTransferDecorator(org.commonjava.maven.galley.cache.testutil.TestTransferDecorator) TransferDecorator(org.commonjava.maven.galley.spi.io.TransferDecorator) Before(org.junit.Before)

Example 2 with TransferDecorator

use of org.commonjava.maven.galley.spi.io.TransferDecorator in project indy by Commonjava.

the class DefaultGalleyStorageProvider method setupTransferDecoratorPipeline.

/**
 * The order is important. We put the checksum decorator at the last because some decorators may change the content.
 */
private void setupTransferDecoratorPipeline() {
    List<TransferDecorator> decorators = new ArrayList<>();
    decorators.add(new IOLatencyDecorator(timerProviderFunction(), meterProvider(), cumulativeTimer()));
    decorators.add(new NoCacheTransferDecorator(specialPathManager));
    decorators.add(new UploadMetadataGenTransferDecorator(specialPathManager, timerProviderFunction()));
    for (TransferDecorator decorator : transferDecorators) {
        decorators.add(decorator);
    }
    decorators.add(getChecksummingTransferDecorator());
    if (featureConfig.getFileChangeTracking()) {
        decorators.add(new FileChangeTrackingDecorator(config));
    }
    transferDecorator = new TransferDecoratorManager(decorators);
}
Also used : NoCacheTransferDecorator(org.commonjava.maven.galley.io.NoCacheTransferDecorator) TransferDecoratorManager(org.commonjava.maven.galley.io.TransferDecoratorManager) ArrayList(java.util.ArrayList) UploadMetadataGenTransferDecorator(org.commonjava.maven.galley.transport.htcli.UploadMetadataGenTransferDecorator) ChecksummingTransferDecorator(org.commonjava.maven.galley.io.ChecksummingTransferDecorator) NoCacheTransferDecorator(org.commonjava.maven.galley.io.NoCacheTransferDecorator) UploadMetadataGenTransferDecorator(org.commonjava.maven.galley.transport.htcli.UploadMetadataGenTransferDecorator) TransferDecorator(org.commonjava.maven.galley.spi.io.TransferDecorator)

Example 3 with TransferDecorator

use of org.commonjava.maven.galley.spi.io.TransferDecorator in project galley by Commonjava.

the class TransferDecoratorPipeline method decorateRead.

@Override
public InputStream decorateRead(final InputStream stream, final Transfer transfer, final EventMetadata metadata) throws IOException {
    Logger logger = LoggerFactory.getLogger(getClass());
    InputStream result = stream;
    for (TransferDecorator decorator : decorators) {
        logger.debug("Decorating: {} using decorator: {}", result.getClass().getName(), decorator.getClass().getName());
        result = decorator.decorateRead(result, transfer, metadata);
    }
    logger.debug("Returning: {}", result.getClass().getName());
    return result;
}
Also used : InputStream(java.io.InputStream) Logger(org.slf4j.Logger) TransferDecorator(org.commonjava.maven.galley.spi.io.TransferDecorator)

Example 4 with TransferDecorator

use of org.commonjava.maven.galley.spi.io.TransferDecorator in project galley by Commonjava.

the class GridFileSystemCacheProviderTest method setup.

@Before
public void setup() throws Exception {
    final PathGenerator pathgen = new HashedLocationPathGenerator();
    final FileEventManager events = new TestFileEventManager();
    final TransferDecorator decorator = new TestTransferDecorator();
    Cache<String, byte[]> data = CACHE_MANAGER.getCache(name.getMethodName() + "-data");
    Cache<String, GridFile.Metadata> metadata = CACHE_MANAGER.getCache(name.getMethodName() + "-metadata");
    final GridFilesystem fs = new GridFilesystem(data, metadata);
    provider = new GridFileSystemCacheProvider(pathgen, events, decorator, fs);
}
Also used : TestTransferDecorator(org.commonjava.maven.galley.cache.testutil.TestTransferDecorator) GridFileSystemCacheProvider(org.commonjava.maven.galley.cache.infinispan.GridFileSystemCacheProvider) TestFileEventManager(org.commonjava.maven.galley.cache.testutil.TestFileEventManager) FileEventManager(org.commonjava.maven.galley.spi.event.FileEventManager) GridFilesystem(org.infinispan.io.GridFilesystem) HashedLocationPathGenerator(org.commonjava.maven.galley.io.HashedLocationPathGenerator) HashedLocationPathGenerator(org.commonjava.maven.galley.io.HashedLocationPathGenerator) PathGenerator(org.commonjava.maven.galley.spi.io.PathGenerator) TestFileEventManager(org.commonjava.maven.galley.cache.testutil.TestFileEventManager) TestTransferDecorator(org.commonjava.maven.galley.cache.testutil.TestTransferDecorator) TransferDecorator(org.commonjava.maven.galley.spi.io.TransferDecorator) Before(org.junit.Before)

Example 5 with TransferDecorator

use of org.commonjava.maven.galley.spi.io.TransferDecorator in project galley by Commonjava.

the class TransferManagerImpl method doList.

private ListingResult doList(final ConcreteResource resource, final boolean suppressFailures, EventMetadata metadata) throws TransferException {
    final Transfer cachedListing = getCacheReference(resource.getChild(".listing.txt"));
    Set<String> filenames = new HashSet<>();
    if (cachedListing.exists()) {
        InputStream stream = null;
        try {
            stream = cachedListing.openInputStream();
            filenames.addAll(IOUtils.readLines(stream, "UTF-8"));
            Logger logger = LoggerFactory.getLogger(getClass());
            logger.debug("Got cached listing:\n\n{}\n\n", filenames);
        } catch (final IOException e) {
            throw new TransferException("Failed to read listing from cached file: %s. Reason: %s", e, cachedListing, e.getMessage());
        } finally {
            closeQuietly(stream);
        }
    } else {
        final Transfer cached = getCacheReference(resource);
        if (cached.exists()) {
            if (cached.isFile()) {
                throw new TransferException("Cannot list: {}. It does not appear to be a directory.", resource);
            } else {
                try {
                    // This is fairly stupid, but we need to append '/' to the end of directories in the listing so content processors can figure
                    // out what to do with them.
                    String[] fnames = cached.list();
                    if (fnames != null && fnames.length > 0) {
                        for (String fname : fnames) {
                            // this will avoid some package.json redirect path empty errors, especially for the group.
                            if (metadata.get(STORAGE_PATH) != null) {
                                filenames.add(fname);
                                continue;
                            }
                            final ConcreteResource child = resource.getChild(fname);
                            final Transfer childRef = getCacheReference(child);
                            if (childRef.isFile()) {
                                filenames.add(fname);
                            } else {
                                filenames.add(fname + "/");
                            }
                        }
                    }
                } catch (final IOException e) {
                    throw new TransferException("Listing failed: {}. Reason: {}", e, resource, e.getMessage());
                }
            }
        }
        if (resource.getLocation().allowsDownloading()) {
            final int timeoutSeconds = getTimeoutSeconds(resource);
            Transport transport = getTransport(resource);
            final ListingResult remoteResult = lister.list(resource, cachedListing, timeoutSeconds, transport, suppressFailures);
            if (remoteResult != null) {
                String[] remoteListing = remoteResult.getListing();
                if (remoteListing != null && remoteListing.length > 0) {
                    final TransferDecorator decorator = cachedListing.getDecorator();
                    if (decorator != null) {
                        try {
                            Logger logger = LoggerFactory.getLogger(getClass());
                            // noinspection ConfusingArgumentToVarargsMethod
                            logger.debug("Un-decorated listing:\n\n{}\n\n", remoteListing);
                            remoteListing = decorator.decorateListing(cachedListing.getParent(), remoteListing, metadata);
                        } catch (final IOException e) {
                            logger.error("Failed to decorate directory listing for: {}. Reason: {}", e, resource, e.getMessage());
                            remoteListing = null;
                        }
                    }
                }
                if (remoteListing != null && remoteListing.length > 0) {
                    if (transport != null && transport.allowsCaching()) {
                        OutputStream stream = null;
                        try {
                            Logger logger = LoggerFactory.getLogger(getClass());
                            logger.debug("Writing listing:\n\n{}\n\nto: {}", remoteListing, cachedListing);
                            stream = cachedListing.openOutputStream(TransferOperation.DOWNLOAD);
                            stream.write(join(remoteListing, "\n").getBytes("UTF-8"));
                        } catch (final IOException e) {
                            logger.debug("Failed to store directory listing for: {}. Reason: {}", e, resource, e.getMessage());
                        } finally {
                            closeQuietly(stream);
                        }
                    }
                    filenames.addAll(Arrays.asList(remoteListing));
                }
            }
        }
    }
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Listing before non-listable file removal:\n\n{}\n\n", filenames);
    List<String> resultingNames = new ArrayList<>(filenames.size());
    for (String fname : filenames) {
        ConcreteResource child = resource.getChild(fname);
        SpecialPathInfo specialPathInfo = specialPathManager.getSpecialPathInfo(child, metadata.getPackageType());
        if (specialPathInfo != null && !specialPathInfo.isListable()) {
            continue;
        }
        resultingNames.add(fname);
    }
    logger.debug("Final listing result:\n\n{}\n\n", resultingNames);
    return new ListingResult(resource, resultingNames.toArray(new String[resultingNames.size()]));
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) IOException(java.io.IOException) Logger(org.slf4j.Logger) ListingResult(org.commonjava.maven.galley.model.ListingResult) TransferDecorator(org.commonjava.maven.galley.spi.io.TransferDecorator) TransferException(org.commonjava.maven.galley.TransferException) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transport(org.commonjava.maven.galley.spi.transport.Transport) HashSet(java.util.HashSet)

Aggregations

TransferDecorator (org.commonjava.maven.galley.spi.io.TransferDecorator)5 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 TestFileEventManager (org.commonjava.maven.galley.cache.testutil.TestFileEventManager)2 TestTransferDecorator (org.commonjava.maven.galley.cache.testutil.TestTransferDecorator)2 HashedLocationPathGenerator (org.commonjava.maven.galley.io.HashedLocationPathGenerator)2 FileEventManager (org.commonjava.maven.galley.spi.event.FileEventManager)2 PathGenerator (org.commonjava.maven.galley.spi.io.PathGenerator)2 Before (org.junit.Before)2 Logger (org.slf4j.Logger)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HashSet (java.util.HashSet)1 JoinString (org.commonjava.maven.atlas.ident.util.JoinString)1 TransferException (org.commonjava.maven.galley.TransferException)1 GridFileSystemCacheProvider (org.commonjava.maven.galley.cache.infinispan.GridFileSystemCacheProvider)1 ChecksummingTransferDecorator (org.commonjava.maven.galley.io.ChecksummingTransferDecorator)1 NoCacheTransferDecorator (org.commonjava.maven.galley.io.NoCacheTransferDecorator)1 TransferDecoratorManager (org.commonjava.maven.galley.io.TransferDecoratorManager)1 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)1