Search in sources :

Example 1 with MetadataTransferException

use of org.sonatype.aether.transfer.MetadataTransferException in project sonatype-aether by sonatype.

the class DefaultUpdateCheckManagerTest method testCheckMetadataNotFoundInRepoCachingDisabled.

@Test
public void testCheckMetadataNotFoundInRepoCachingDisabled() throws Exception {
    metadata.getFile().delete();
    session.setNotFoundCachingEnabled(false);
    UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
    check.setException(new MetadataNotFoundException(metadata, repository, ""));
    manager.touchMetadata(session, check);
    resetSessionData(session);
    // ! file.exists && ! updateRequired -> artifact not found in remote repo
    // ignore NotFoundCaching-setting, don't check if update policy does not say so for metadata
    check.setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
    manager.checkMetadata(session, check);
    assertEquals(false, check.isRequired());
    assertTrue(check.getException() instanceof MetadataNotFoundException);
}
Also used : MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) Metadata(org.sonatype.aether.metadata.Metadata) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) Test(org.junit.Test)

Example 2 with MetadataTransferException

use of org.sonatype.aether.transfer.MetadataTransferException in project sonatype-aether by sonatype.

the class DefaultUpdateCheckManagerTest method testCheckMetadataNotFoundInRepoCachingEnabled.

@Test
public void testCheckMetadataNotFoundInRepoCachingEnabled() throws Exception {
    metadata.getFile().delete();
    session.setNotFoundCachingEnabled(true);
    UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
    check.setException(new MetadataNotFoundException(metadata, repository, ""));
    manager.touchMetadata(session, check);
    resetSessionData(session);
    // ! file.exists && ! updateRequired -> artifact not found in remote repo
    check = newMetadataCheck().setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
    manager.checkMetadata(session, check);
    assertEquals(false, check.isRequired());
    assertNotNull(check.getException());
}
Also used : MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) Metadata(org.sonatype.aether.metadata.Metadata) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) Test(org.junit.Test)

Example 3 with MetadataTransferException

use of org.sonatype.aether.transfer.MetadataTransferException in project sonatype-aether by sonatype.

the class DefaultUpdateCheckManagerTest method testCheckMetadataUpdatePolicyRequired.

@Test
public void testCheckMetadataUpdatePolicyRequired() throws Exception {
    UpdateCheck<Metadata, MetadataTransferException> check = newMetadataCheck();
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, -1);
    check.setLocalLastUpdated(cal.getTimeInMillis());
    check.setPolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
    manager.checkMetadata(session, check);
    assertNull(check.getException());
    assertTrue(check.isRequired());
    check.setPolicy(RepositoryPolicy.UPDATE_POLICY_DAILY);
    manager.checkMetadata(session, check);
    assertNull(check.getException());
    assertTrue(check.isRequired());
    check.setPolicy(RepositoryPolicy.UPDATE_POLICY_INTERVAL + ":60");
    manager.checkMetadata(session, check);
    assertNull(check.getException());
    assertTrue(check.isRequired());
}
Also used : Calendar(java.util.Calendar) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) Metadata(org.sonatype.aether.metadata.Metadata) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) Test(org.junit.Test)

Example 4 with MetadataTransferException

use of org.sonatype.aether.transfer.MetadataTransferException in project sonatype-aether by sonatype.

the class FileRepositoryWorker method run.

/**
 * Do transfer according to {@link RepositoryConnector} specifications.
 *
 * @see FileRepositoryConnector
 */
public void run() {
    File target = null;
    long totalTransferred = -1;
    try {
        transfer.setState(State.ACTIVE);
        resource = newResource(transfer, repository);
        DefaultTransferEvent event = newEvent(transfer);
        catapult.fireInitiated(event);
        File baseDir = new File(PathUtils.basedir(repository.getUrl()));
        File localFile = transfer.getFile();
        File repoFile = new File(baseDir, transfer.getRelativePath());
        File src = null;
        switch(direction) {
            case UPLOAD:
                src = localFile;
                target = repoFile;
                break;
            case DOWNLOAD:
                src = repoFile;
                target = localFile;
                break;
        }
        if (transfer.isExistenceCheck()) {
            if (!src.exists()) {
                throw new FileNotFoundException(src.getAbsolutePath());
            }
        } else {
            File tmp = tmpfile(target);
            totalTransferred = copy(src, tmp);
            fileProcessor.move(tmp, target);
            switch(direction) {
                case UPLOAD:
                    writeChecksum(src, target.getPath());
                    break;
                case DOWNLOAD:
                    verifyChecksum(src);
                    break;
            }
        }
    } catch (FileNotFoundException e) {
        switch(transfer.getType()) {
            case ARTIFACT:
                ArtifactTransferException artEx;
                if (Direction.DOWNLOAD.equals(direction)) {
                    artEx = new ArtifactNotFoundException(transfer.getArtifact(), repository);
                } else {
                    artEx = new ArtifactTransferException(transfer.getArtifact(), repository, e);
                }
                transfer.setException(artEx);
                break;
            case METADATA:
                MetadataTransferException mdEx;
                if (Direction.DOWNLOAD.equals(direction)) {
                    mdEx = new MetadataNotFoundException(transfer.getMetadata(), repository);
                } else {
                    mdEx = new MetadataTransferException(transfer.getMetadata(), repository, e);
                }
                transfer.setException(mdEx);
                break;
        }
    } catch (Throwable t) {
        logger.debug(t.getMessage(), t);
        switch(transfer.getType()) {
            case ARTIFACT:
                transfer.setException(new ArtifactTransferException(transfer.getArtifact(), repository, t));
                break;
            case METADATA:
                transfer.setException(new MetadataTransferException(transfer.getMetadata(), repository, t));
                break;
        }
    } finally {
        transfer.setState(State.DONE);
        if (transfer.getException() == null) {
            DefaultTransferEvent event = newEvent(transfer);
            event.setTransferredBytes((int) totalTransferred);
            catapult.fireSucceeded(event);
        } else {
            // cleanup
            if (direction.equals(Direction.UPLOAD)) {
                for (String ext : checksumAlgos.values()) {
                    new File(target.getPath() + ext).delete();
                }
            }
            if (target != null) {
                target.delete();
            }
            DefaultTransferEvent event = newEvent(transfer);
            catapult.fireFailed(event);
        }
    }
}
Also used : MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) FileNotFoundException(java.io.FileNotFoundException) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) DefaultTransferEvent(org.sonatype.aether.util.listener.DefaultTransferEvent) File(java.io.File) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException)

Example 5 with MetadataTransferException

use of org.sonatype.aether.transfer.MetadataTransferException in project sonatype-aether by sonatype.

the class DefaultDeployer method upload.

private void upload(Collection<MetadataUpload> metadataUploads, RepositorySystemSession session, Metadata metadata, RemoteRepository repository, RepositoryConnector connector, EventCatapult catapult) throws DeploymentException {
    LocalRepositoryManager lrm = session.getLocalRepositoryManager();
    File basedir = lrm.getRepository().getBasedir();
    File dstFile = new File(basedir, lrm.getPathForRemoteMetadata(metadata, repository, ""));
    if (metadata instanceof MergeableMetadata) {
        if (!((MergeableMetadata) metadata).isMerged()) {
            {
                DefaultRepositoryEvent event = new DefaultRepositoryEvent(EventType.METADATA_RESOLVING, session, catapult.getTrace());
                event.setMetadata(metadata);
                event.setRepository(repository);
                repositoryEventDispatcher.dispatch(event);
                event = new DefaultRepositoryEvent(EventType.METADATA_DOWNLOADING, session, catapult.getTrace());
                event.setMetadata(metadata);
                event.setRepository(repository);
                repositoryEventDispatcher.dispatch(event);
            }
            RepositoryPolicy policy = getPolicy(session, repository, metadata.getNature());
            MetadataDownload download = new MetadataDownload();
            download.setMetadata(metadata);
            download.setFile(dstFile);
            download.setChecksumPolicy(policy.getChecksumPolicy());
            connector.get(null, Arrays.asList(download));
            Exception error = download.getException();
            if (error instanceof MetadataNotFoundException) {
                dstFile.delete();
            }
            {
                DefaultRepositoryEvent event = new DefaultRepositoryEvent(EventType.METADATA_DOWNLOADED, session, catapult.getTrace());
                event.setMetadata(metadata);
                event.setRepository(repository);
                event.setException(error);
                event.setFile(dstFile);
                repositoryEventDispatcher.dispatch(event);
                event = new DefaultRepositoryEvent(EventType.METADATA_RESOLVED, session, catapult.getTrace());
                event.setMetadata(metadata);
                event.setRepository(repository);
                event.setException(error);
                event.setFile(dstFile);
                repositoryEventDispatcher.dispatch(event);
            }
            if (error != null && !(error instanceof MetadataNotFoundException)) {
                throw new DeploymentException("Failed to retrieve remote metadata " + metadata + ": " + error.getMessage(), error);
            }
        }
        try {
            ((MergeableMetadata) metadata).merge(dstFile, dstFile);
        } catch (RepositoryException e) {
            throw new DeploymentException("Failed to update metadata " + metadata + ": " + e.getMessage(), e);
        }
    } else {
        if (metadata.getFile() == null) {
            throw new DeploymentException("Failed to update metadata " + metadata + ": No file attached.");
        }
        try {
            fileProcessor.copy(metadata.getFile(), dstFile, null);
        } catch (IOException e) {
            throw new DeploymentException("Failed to update metadata " + metadata + ": " + e.getMessage(), e);
        }
    }
    UpdateCheck<Metadata, MetadataTransferException> check = new UpdateCheck<Metadata, MetadataTransferException>();
    check.setItem(metadata);
    check.setFile(dstFile);
    check.setRepository(repository);
    check.setAuthoritativeRepository(repository);
    updateCheckManager.touchMetadata(session, check);
    metadataUploads.add(new MetadataUploadEx(metadata, dstFile, catapult));
}
Also used : RepositoryPolicy(org.sonatype.aether.repository.RepositoryPolicy) MergeableMetadata(org.sonatype.aether.metadata.MergeableMetadata) MergeableMetadata(org.sonatype.aether.metadata.MergeableMetadata) Metadata(org.sonatype.aether.metadata.Metadata) RepositoryException(org.sonatype.aether.RepositoryException) IOException(java.io.IOException) MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) DeploymentException(org.sonatype.aether.deployment.DeploymentException) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) RepositoryException(org.sonatype.aether.RepositoryException) IOException(java.io.IOException) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) DefaultRepositoryEvent(org.sonatype.aether.util.listener.DefaultRepositoryEvent) MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) UpdateCheck(org.sonatype.aether.impl.UpdateCheck) LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) DeploymentException(org.sonatype.aether.deployment.DeploymentException) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) File(java.io.File)

Aggregations

MetadataTransferException (org.sonatype.aether.transfer.MetadataTransferException)9 Metadata (org.sonatype.aether.metadata.Metadata)7 Test (org.junit.Test)5 StubMetadata (org.sonatype.aether.test.util.impl.StubMetadata)5 MetadataNotFoundException (org.sonatype.aether.transfer.MetadataNotFoundException)5 File (java.io.File)3 ArtifactTransferException (org.sonatype.aether.transfer.ArtifactTransferException)3 UpdateCheck (org.sonatype.aether.impl.UpdateCheck)2 LocalRepositoryManager (org.sonatype.aether.repository.LocalRepositoryManager)2 RepositoryPolicy (org.sonatype.aether.repository.RepositoryPolicy)2 NoRepositoryConnectorException (org.sonatype.aether.transfer.NoRepositoryConnectorException)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 Executor (java.util.concurrent.Executor)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 RepositoryException (org.sonatype.aether.RepositoryException)1 RequestTrace (org.sonatype.aether.RequestTrace)1