Search in sources :

Example 11 with TransferException

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

the class TestDownload method call.

@Override
public DownloadJob call() throws Exception {
    if (data == null) {
        return null;
    }
    OutputStream stream = null;
    try {
        logger.info("Writing '{}' to: {}.", new String(data), transfer);
        stream = transfer.openOutputStream(TransferOperation.DOWNLOAD, true, eventMetadata);
        IOUtils.write(data, stream);
    } catch (final IOException e) {
        e.printStackTrace();
        error = new TransferException("Failed to write: %s", e, e.getMessage());
    } finally {
        IOUtils.closeQuietly(stream);
    }
    logger.debug("After write, does transfer exist? {}", transfer.exists());
    return this;
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 12 with TransferException

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

the class FileTransport method createPublishJob.

@Override
public PublishJob createPublishJob(final ConcreteResource resource, final InputStream stream, final long length, final String contentType, final int timeoutSeconds) throws TransferException {
    final File pubDir = config.getPubDir();
    if (pubDir == null) {
        throw new TransferException("This transport is read-only!");
    }
    final File dest = new File(pubDir, config.getGenerator().getFilePath(resource));
    final File dir = dest.getParentFile();
    if (dir != null && !dir.exists() && !dir.mkdirs()) {
        throw new TransferException("Cannot create directory: %s", dir);
    }
    return new FilePublish(dest, stream);
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) FilePublish(org.commonjava.maven.galley.filearc.internal.FilePublish) File(java.io.File)

Example 13 with TransferException

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

the class HttpExistenceTest method simpleRetrieveOfAvailableUrl.

@Test
public void simpleRetrieveOfAvailableUrl() throws Exception {
    final String fname = "simple-retrieval.html";
    final String url = fixture.formatUrl(fname);
    fixture.getServer().expect(url, 200, fname);
    final String baseUri = fixture.getBaseUri();
    final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
    final HttpExistence dl = new HttpExistence(url, location, fixture.getTransfer(new ConcreteResource(location, fname)), fixture.getHttp(), new ObjectMapper());
    final Boolean result = dl.call();
    final TransferException error = dl.getError();
    assertThat(error, nullValue());
    assertThat(result, notNullValue());
    assertThat(result, equalTo(true));
    final String path = fixture.getUrlPath(url);
    assertThat(fixture.getAccessesFor("HEAD", path), equalTo(1));
}
Also used : SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) TransferException(org.commonjava.maven.galley.TransferException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 14 with TransferException

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

the class TransferManagerImpl method doBatch.

private <T extends TransferBatch> T doBatch(final Set<Resource> resources, final T batch, final boolean suppressFailures, final EventMetadata eventMetadata) throws TransferException {
    logger.info("Attempting to batch-retrieve {} resources:\n  {}", resources.size(), new JoinString("\n  ", resources));
    final Set<BatchRetriever> retrievers = new HashSet<BatchRetriever>(resources.size());
    for (final Resource resource : resources) {
        retrievers.add(new BatchRetriever(this, resource, suppressFailures, eventMetadata));
    }
    final Map<ConcreteResource, TransferException> errors = new HashMap<ConcreteResource, TransferException>();
    final Map<ConcreteResource, Transfer> transfers = new HashMap<ConcreteResource, Transfer>();
    do {
        for (final BatchRetriever retriever : retrievers) {
            batchExecutor.submit(retriever);
        }
        int count = retrievers.size();
        for (int i = 0; i < count; i++) {
            try {
                Future<BatchRetriever> pending = batchExecutor.take();
                BatchRetriever retriever = pending.get();
                final ConcreteResource resource = retriever.getLastTry();
                final TransferException error = retriever.getError();
                if (error != null) {
                    logger.warn("ERROR: {}...{}", error, resource, error.getMessage());
                    retrievers.remove(retriever);
                    if (!(error instanceof TransferLocationException)) {
                        errors.put(resource, error);
                    }
                    continue;
                }
                final Transfer transfer = retriever.getTransfer();
                if (transfer != null && transfer.exists()) {
                    transfers.put(resource, transfer);
                    retrievers.remove(retriever);
                    logger.debug("Completed: {}", resource);
                    continue;
                }
                if (!retriever.hasMoreTries()) {
                    logger.debug("Not completed, but out of tries: {}", resource);
                    retrievers.remove(retriever);
                }
            } catch (final InterruptedException e) {
                logger.error(String.format("Failed to wait for batch retrieval attempts to complete: %s", e.getMessage()), e);
                break;
            } catch (ExecutionException e) {
                logger.error(String.format("Failed to retrieve next completed retrieval: %s", e.getMessage()), e);
            }
        }
    } while (!retrievers.isEmpty());
    batch.setErrors(errors);
    batch.setTransfers(transfers);
    return batch;
}
Also used : HashMap(java.util.HashMap) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Resource(org.commonjava.maven.galley.model.Resource) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) BatchRetriever(org.commonjava.maven.galley.internal.xfer.BatchRetriever) TransferException(org.commonjava.maven.galley.TransferException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) TransferLocationException(org.commonjava.maven.galley.TransferLocationException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 15 with TransferException

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

the class TransferManagerImpl method retrieveFirst.

@Override
public Transfer retrieveFirst(final VirtualResource virt, final EventMetadata eventMetadata) throws TransferException {
    Transfer target = null;
    TransferException lastError = null;
    int tries = 0;
    for (final ConcreteResource res : virt) {
        tries++;
        if (res == null) {
            continue;
        }
        try {
            target = retrieve(res, true, eventMetadata);
            lastError = null;
            if (target != null && target.exists()) {
                return target;
            }
        } catch (final TransferException e) {
            logger.warn("Failed to retrieve: {}. {} more tries. (Reason: {})", res, (virt.toConcreteResources().size() - tries), e.getMessage());
            lastError = e;
        }
    }
    if (lastError != null) {
        throw lastError;
    }
    fileEventManager.fire(new FileNotFoundEvent(virt, eventMetadata));
    return null;
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) FileNotFoundEvent(org.commonjava.maven.galley.event.FileNotFoundEvent) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource)

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