Search in sources :

Example 26 with TransferException

use of org.commonjava.maven.galley.TransferException in project galley by Commonjava.

the class TransferManagerImpl method doDelete.

private Boolean doDelete(final Transfer item, final EventMetadata eventMetadata) throws TransferException {
    if (!item.exists()) {
        return false;
    }
    Logger contentLogger = LoggerFactory.getLogger(DELETE_CONTENT_LOG);
    contentLogger.info("BEGIN: Delete {} ({})", item.getResource(), eventMetadata);
    SpecialPathInfo specialPathInfo = specialPathManager.getSpecialPathInfo(item, eventMetadata.getPackageType());
    if (specialPathInfo != null && !specialPathInfo.isDeletable()) {
        throw new TransferException("Deleting not allowed for: %s", item);
    }
    if (item.isDirectory()) {
        String[] listing;
        try {
            listing = item.list();
        } catch (final IOException e) {
            throw new TransferException("Delete failed: {}. Reason: cannot list directory due to: {}", e, item, e.getMessage());
        }
        for (final String sub : listing) {
            if (!doDelete(item.getChild(sub), eventMetadata)) {
                contentLogger.info("FAIL: Delete: {}", item.getResource());
                return false;
            }
        }
    } else {
        try {
            if (!item.delete(true, eventMetadata)) {
                throw new TransferException("Failed to delete: {}.", item);
            }
        } catch (final IOException e) {
            throw new TransferException("Failed to delete stored location: {}. Reason: {}", e, item, e.getMessage());
        }
    }
    contentLogger.info("FINISH: Delete: {}", item.getResource());
    return true;
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) IOException(java.io.IOException) Logger(org.slf4j.Logger)

Example 27 with TransferException

use of org.commonjava.maven.galley.TransferException in project galley by Commonjava.

the class TransferManagerImpl method listAll.

@Override
public List<ListingResult> listAll(final VirtualResource virt, final EventMetadata metadata) throws TransferException {
    final Map<ConcreteResource, Future<ListingResult>> futureList = new HashMap<>();
    for (final ConcreteResource res : virt) {
        final Future<ListingResult> listingFuture = executorService.submit(new Callable<ListingResult>() {

            @Override
            public ListingResult call() throws TransferException {
                return doList(res, true, metadata);
            }
        });
        futureList.put(res, listingFuture);
    }
    final List<ListingResult> results = new ArrayList<>();
    for (Map.Entry<ConcreteResource, Future<ListingResult>> entry : futureList.entrySet()) {
        Future<ListingResult> listingFuture = entry.getValue();
        ConcreteResource res = entry.getKey();
        ListingResult listing;
        try {
            listing = listingFuture.get();
        } catch (InterruptedException ex) {
            throw new TransferException("Listing of %s was interrupted", ex, res);
        } catch (ExecutionException ex) {
            throw new TransferException("Listing of %s threw an error: %s", ex, res, ex);
        }
        if (listing != null) {
            results.add(listing);
        }
    }
    return results;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ListingResult(org.commonjava.maven.galley.model.ListingResult) TransferException(org.commonjava.maven.galley.TransferException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 28 with TransferException

use of org.commonjava.maven.galley.TransferException in project galley by Commonjava.

the class TransferManagerImpl method store.

@Override
public Transfer store(final ConcreteResource resource, final InputStream stream, final EventMetadata eventMetadata) throws TransferException {
    SpecialPathInfo specialPathInfo = specialPathManager.getSpecialPathInfo(resource, eventMetadata.getPackageType());
    if (!resource.allowsStoring() || (specialPathInfo != null && !specialPathInfo.isStorable())) {
        throw new TransferException("Storing not allowed for: {}", resource);
    }
    final Transfer target = getCacheReference(resource);
    logger.info("STORE {}", target.getResource());
    OutputStream out = null;
    try {
        out = target.openOutputStream(TransferOperation.UPLOAD, true, eventMetadata);
        copy(stream, out);
    } catch (final IOException e) {
        throw new TransferException("Failed to store: {}. Reason: {}", e, resource, e.getMessage());
    } finally {
        closeQuietly(out);
    }
    return target;
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) OutputStream(java.io.OutputStream) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException)

Example 29 with TransferException

use of org.commonjava.maven.galley.TransferException 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<String>();
    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) {
                            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) {
                    final TransferDecorator decorator = cachedListing.getDecorator();
                    if (decorator != null) {
                        try {
                            Logger logger = LoggerFactory.getLogger(getClass());
                            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) {
                    if (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<String>(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 30 with TransferException

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

the class ArtifactStoreValidator method isValid.

public boolean isValid(ArtifactStore store) {
    if (!(store instanceof RemoteRepository)) {
        return true;
    }
    RemoteRepository remoteRepository = (RemoteRepository) store;
    URL url = null;
    try {
        url = new URL(remoteRepository.getUrl());
        ConcreteResource concreteResource = new ConcreteResource(LocationUtils.toLocation(remoteRepository), PathUtils.ROOT);
        return transferManager.exists(concreteResource);
    } catch (final MalformedURLException e) {
        logger.error("[RemoteValidation] Failed to parse repository URL: '{}'. Reason: {}", e, url, e.getMessage());
    } catch (final TransferException e) {
        logger.warn("[RemoteValidation] Cannot connect to target repository: '{}'. Reason: {}", this, e.getMessage());
        logger.debug("[RemoteValidation] exception from validation attempt for: {}", this, e);
    }
    return false;
}
Also used : MalformedURLException(java.net.MalformedURLException) TransferException(org.commonjava.maven.galley.TransferException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) URL(java.net.URL)

Aggregations

TransferException (org.commonjava.maven.galley.TransferException)49 IOException (java.io.IOException)17 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)15 Transfer (org.commonjava.maven.galley.model.Transfer)12 ArrayList (java.util.ArrayList)9 TransferLocationException (org.commonjava.maven.galley.TransferLocationException)9 TransferTimeoutException (org.commonjava.maven.galley.TransferTimeoutException)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 OutputStream (java.io.OutputStream)7 ListingResult (org.commonjava.maven.galley.model.ListingResult)7 InputStream (java.io.InputStream)6 Location (org.commonjava.maven.galley.model.Location)6 File (java.io.File)5 HashMap (java.util.HashMap)5 JarFile (java.util.jar.JarFile)5 ZipEntry (java.util.zip.ZipEntry)5 ZipFile (java.util.zip.ZipFile)5 SimpleHttpLocation (org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation)5 Test (org.junit.Test)5 ExecutionException (java.util.concurrent.ExecutionException)4