Search in sources :

Example 16 with ListingResult

use of org.commonjava.maven.galley.model.ListingResult in project indy by Commonjava.

the class DefaultDownloadManager method list.

@Override
public List<StoreResource> list(final ArtifactStore store, final String path) throws IndyWorkflowException {
    final List<StoreResource> result = new ArrayList<>();
    if (store.getKey().getType() == StoreType.group) {
        try {
            final List<ListingResult> results = transfers.listAll(locationExpander.expand(new VirtualResource(LocationUtils.toLocations(store), path)));
            for (final ListingResult lr : results) {
                if (lr != null && lr.getListing() != null) {
                    for (final String file : lr.getListing()) {
                        result.add(new StoreResource((KeyedLocation) lr.getLocation(), path, file));
                    }
                }
            }
        } catch (final BadGatewayException e) {
            Location location = e.getLocation();
            KeyedLocation kl = (KeyedLocation) location;
            fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
            logger.warn("Bad gateway: " + e.getMessage(), e);
        } catch (final TransferTimeoutException e) {
            Location location = e.getLocation();
            KeyedLocation kl = (KeyedLocation) location;
            fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
            logger.warn("Timeout: " + e.getMessage(), e);
        } catch (final TransferLocationException e) {
            Location location = e.getLocation();
            KeyedLocation kl = (KeyedLocation) location;
            fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
            logger.warn("Location Error: " + e.getMessage(), e);
        } catch (final TransferException e) {
            logger.error(e.getMessage(), e);
            throw new IndyWorkflowException("Failed to list ALL paths: {} from: {}. Reason: {}", e, path, store.getKey(), e.getMessage());
        }
    } else {
        final KeyedLocation loc = LocationUtils.toLocation(store);
        final StoreResource res = new StoreResource(loc, path);
        if (store instanceof RemoteRepository) {
            try {
                final ListingResult lr = transfers.list(res);
                if (lr != null && lr.getListing() != null) {
                    for (final String file : lr.getListing()) {
                        result.add(new StoreResource(loc, path, file));
                    }
                }
            } catch (final BadGatewayException e) {
                Location location = e.getLocation();
                KeyedLocation kl = (KeyedLocation) location;
                fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
                logger.warn("Bad gateway: " + e.getMessage(), e);
            } catch (final TransferTimeoutException e) {
                Location location = e.getLocation();
                KeyedLocation kl = (KeyedLocation) location;
                fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
                logger.warn("Timeout: " + e.getMessage(), e);
            } catch (final TransferLocationException e) {
                Location location = e.getLocation();
                KeyedLocation kl = (KeyedLocation) location;
                fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
                logger.warn("Location Error: " + e.getMessage(), e);
            } catch (final TransferException e) {
                logger.error(e.getMessage(), e);
                throw new IndyWorkflowException("Failed to list path: {} from: {}. Reason: {}", e, path, store.getKey(), e.getMessage());
            }
        } else {
            try {
                final ListingResult listing = transfers.list(res);
                if (listing != null && listing.getListing() != null) {
                    for (final String child : listing.getListing()) {
                        result.add(new StoreResource(loc, path, child));
                    }
                }
            } catch (final TransferLocationException e) {
                Location location = res.getLocation();
                KeyedLocation kl = (KeyedLocation) location;
                logger.warn("Timeout  / bad gateway: " + e.getMessage(), e);
                fileEventManager.fire(new IndyStoreErrorEvent(kl.getKey(), e));
            } catch (final TransferException e) {
                logger.error(e.getMessage(), e);
                throw new IndyWorkflowException("Failed to list path: {} from: {}. Reason: {}", e, path, store.getKey(), e.getMessage());
            }
        }
    }
    return dedupeListing(result);
}
Also used : TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) ArrayList(java.util.ArrayList) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) ListingResult(org.commonjava.maven.galley.model.ListingResult) StoreResource(org.commonjava.indy.content.StoreResource) TransferException(org.commonjava.maven.galley.TransferException) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) BadGatewayException(org.commonjava.maven.galley.BadGatewayException) TransferLocationException(org.commonjava.maven.galley.TransferLocationException) IndyStoreErrorEvent(org.commonjava.indy.change.event.IndyStoreErrorEvent) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) Location(org.commonjava.maven.galley.model.Location)

Example 17 with ListingResult

use of org.commonjava.maven.galley.model.ListingResult in project indy by Commonjava.

the class DefaultDownloadManager method list.

@Override
@Measure
public List<StoreResource> list(final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    final List<StoreResource> result = new ArrayList<>();
    if (store.getKey().getType() == StoreType.group) {
        try {
            final List<ListingResult> results = transfers.listAll(locationExpander.expand(new VirtualResource(LocationUtils.toLocations(store), path)), eventMetadata);
            for (final ListingResult lr : results) {
                if (lr != null && lr.getListing() != null) {
                    for (final String file : lr.getListing()) {
                        result.add(new StoreResource((KeyedLocation) lr.getLocation(), path, file));
                    }
                }
            }
        } catch (final TransferLocationException e) {
            fireIndyStoreErrorEvent(e);
            logger.warn("Location Error: " + e.getMessage(), e);
            throw new IndyWorkflowException("Failed to list ALL paths: {} from: {}. Reason: {}", e, path, store.getKey(), e.getMessage());
        } catch (final TransferException e) {
            logger.error(e.getMessage(), e);
            throw new IndyWorkflowException("Failed to list ALL paths: {} from: {}. Reason: {}", e, path, store.getKey(), e.getMessage());
        }
    } else {
        if (!PathMaskChecker.checkListingMask(store, path)) {
            // if list not permitted for the path, return empty list
            return result;
        }
        final KeyedLocation loc = LocationUtils.toLocation(store);
        final StoreResource res = new StoreResource(loc, path);
        if (store instanceof RemoteRepository) {
            try {
                final ListingResult lr = transfers.list(res, eventMetadata);
                if (lr != null && lr.getListing() != null) {
                    for (final String file : lr.getListing()) {
                        result.add(new StoreResource(loc, path, file));
                    }
                }
            } catch (final TransferLocationException e) {
                fireIndyStoreErrorEvent(e);
                logger.warn("Location Error: " + e.getMessage(), e);
                throw new IndyWorkflowException("Failed to list path: {} from: {}. Reason: {}", e, path, store.getKey(), e.getMessage());
            } catch (final TransferException e) {
                logger.error(e.getMessage(), e);
                throw new IndyWorkflowException("Failed to list path: {} from: {}. Reason: {}", e, path, store.getKey(), e.getMessage());
            }
        } else {
            try {
                final ListingResult listing = transfers.list(res, eventMetadata);
                if (listing != null && listing.getListing() != null) {
                    for (final String child : listing.getListing()) {
                        result.add(new StoreResource(loc, path, child));
                    }
                }
            } catch (final TransferLocationException e) {
                fireIndyStoreErrorEvent(e);
                logger.warn("Timeout  / bad gateway: " + e.getMessage(), e);
                throw new IndyWorkflowException("Failed to list path: {} from: {}. Reason: {}", e, path, store.getKey(), e.getMessage());
            } catch (final TransferException e) {
                logger.error(e.getMessage(), e);
                throw new IndyWorkflowException("Failed to list path: {} from: {}. Reason: {}", e, path, store.getKey(), e.getMessage());
            }
        }
    }
    return dedupeListing(result);
}
Also used : StoreResource(org.commonjava.indy.content.StoreResource) TransferException(org.commonjava.maven.galley.TransferException) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArrayList(java.util.ArrayList) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) ListingResult(org.commonjava.maven.galley.model.ListingResult) TransferLocationException(org.commonjava.maven.galley.TransferLocationException) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 18 with ListingResult

use of org.commonjava.maven.galley.model.ListingResult in project indy by Commonjava.

the class MavenMetadataGeneratorTest method setupSnapshotDirWith2Snapshots.

private StoreResource setupSnapshotDirWith2Snapshots() throws Exception {
    final RemoteRepository store = new RemoteRepository(MAVEN_PKG_KEY, "testrepo", "http://foo.bar");
    stores.storeArtifactStore(store, summary, false, true, new EventMetadata());
    final String path = "org/group/artifact/1.0-SNAPSHOT";
    final KeyedLocation location = LocationUtils.toLocation(store);
    final StoreResource resource = new StoreResource(location, path);
    final TestListing listing = new TestListing(new ListingResult(resource, new String[] { "artifact-1.0-20140828.221400-1.pom", "artifact-1.0-20140828.221400-1.jar", "artifact-1.0-20140828.225800-1.pom", "artifact-1.0-20140828.225800-1.jar" }));
    fixture.getTransport().registerListing(resource, listing);
    return resource;
}
Also used : StoreResource(org.commonjava.indy.content.StoreResource) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) TestListing(org.commonjava.maven.galley.testing.core.transport.job.TestListing) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) ListingResult(org.commonjava.maven.galley.model.ListingResult) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 19 with ListingResult

use of org.commonjava.maven.galley.model.ListingResult 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)

Example 20 with ListingResult

use of org.commonjava.maven.galley.model.ListingResult in project galley by Commonjava.

the class ListingHandler method list.

public ListingResult list(final ConcreteResource resource, final Transfer target, final int timeoutSeconds, final Transport transport, final boolean suppressFailures) throws TransferException {
    if (nfc.isMissing(resource)) {
        logger.debug("NFC: Already marked as missing: {}", resource);
        return null;
    }
    if (transport == null) {
        logger.warn("No transports available to handle: {} with location type: {}", resource, resource.getLocation().getClass().getSimpleName());
        return null;
    }
    logger.debug("LIST {}", resource);
    final ListingJob job = transport.createListingJob(resource, target, timeoutSeconds);
    // TODO: execute this stuff in a thread just like downloads/publishes. Requires cache storage...
    try {
        final ListingResult result = job.call();
        if (job.getError() != null) {
            logger.debug("NFC: Download error. Marking as missing: {}", resource);
            nfc.addMissing(resource);
            if (!suppressFailures) {
                throw job.getError();
            }
        } else if (result == null) {
            logger.debug("NFC: Download did not complete. Marking as missing: {}", resource);
            nfc.addMissing(resource);
        }
        return result;
    } catch (final TimeoutException e) {
        if (!suppressFailures) {
            throw new TransferTimeoutException(target, "Timed-out download: {}. Reason: {}", e, resource, e.getMessage());
        }
    } catch (final TransferException e) {
        if (!suppressFailures) {
            throw e;
        }
    } catch (final Exception e) {
        if (!suppressFailures) {
            throw new TransferException("Failed listing: {}. Reason: {}", e, resource, e.getMessage());
        }
    }
    return null;
}
Also used : TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException) TransferException(org.commonjava.maven.galley.TransferException) ListingResult(org.commonjava.maven.galley.model.ListingResult) ListingJob(org.commonjava.maven.galley.spi.transport.ListingJob) TimeoutException(java.util.concurrent.TimeoutException) TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException) TransferException(org.commonjava.maven.galley.TransferException) TimeoutException(java.util.concurrent.TimeoutException) TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException)

Aggregations

ListingResult (org.commonjava.maven.galley.model.ListingResult)21 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)12 TransferException (org.commonjava.maven.galley.TransferException)11 ArrayList (java.util.ArrayList)9 Transfer (org.commonjava.maven.galley.model.Transfer)8 StoreResource (org.commonjava.indy.content.StoreResource)7 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)7 SimpleHttpLocation (org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation)7 Test (org.junit.Test)7 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)5 VirtualResource (org.commonjava.maven.galley.model.VirtualResource)5 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)4 TransferLocationException (org.commonjava.maven.galley.TransferLocationException)4 IOException (java.io.IOException)3 TransferTimeoutException (org.commonjava.maven.galley.TransferTimeoutException)3 Logger (org.slf4j.Logger)3 InputStream (java.io.InputStream)2 IndyStoreErrorEvent (org.commonjava.indy.change.event.IndyStoreErrorEvent)2 BadGatewayException (org.commonjava.maven.galley.BadGatewayException)2 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)2