Search in sources :

Example 21 with ArtifactDownload

use of org.sonatype.aether.spi.connector.ArtifactDownload in project sonatype-aether by sonatype.

the class GetTest method testDownloadArtifact.

@Test
public void testDownloadArtifact() throws Exception {
    addDelivery("gid/aid/version/aid-version-classifier.extension", "artifact");
    addDelivery("gid/aid/version/aid-version-classifier.extension.sha1", sha1("artifact"));
    addDelivery("gid/aid/version/aid-version-classifier.extension.md5", md5("artifact"));
    File f = TestFileUtils.createTempFile("");
    Artifact a = artifact("bla");
    ArtifactDownload down = new ArtifactDownload(a, null, f, RepositoryPolicy.CHECKSUM_POLICY_FAIL);
    Collection<? extends ArtifactDownload> downs = Arrays.asList(down);
    RepositoryConnector c = connector();
    c.get(downs, null);
    assertNull(String.valueOf(down.getException()), down.getException());
    TestFileUtils.assertContent("artifact", f);
}
Also used : ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) File(java.io.File) Artifact(org.sonatype.aether.artifact.Artifact) Test(org.junit.Test)

Example 22 with ArtifactDownload

use of org.sonatype.aether.spi.connector.ArtifactDownload in project sonatype-aether by sonatype.

the class DefaultArtifactResolver method resolve.

private List<ArtifactResult> resolve(RepositorySystemSession session, Collection<? extends ArtifactRequest> requests) throws ArtifactResolutionException {
    List<ArtifactResult> results = new ArrayList<ArtifactResult>(requests.size());
    boolean failures = false;
    LocalRepositoryManager lrm = session.getLocalRepositoryManager();
    WorkspaceReader workspace = session.getWorkspaceReader();
    List<ResolutionGroup> groups = new ArrayList<ResolutionGroup>();
    for (ArtifactRequest request : requests) {
        RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
        ArtifactResult result = new ArtifactResult(request);
        results.add(result);
        Artifact artifact = request.getArtifact();
        List<RemoteRepository> repos = request.getRepositories();
        artifactResolving(session, trace, artifact);
        String localPath = artifact.getProperty(ArtifactProperties.LOCAL_PATH, null);
        if (localPath != null) {
            // unhosted artifact, just validate file
            File file = new File(localPath);
            if (!file.isFile()) {
                failures = true;
                result.addException(new ArtifactNotFoundException(artifact, null));
            } else {
                artifact = artifact.setFile(file);
                result.setArtifact(artifact);
                artifactResolved(session, trace, artifact, null, result.getExceptions());
            }
            continue;
        }
        VersionResult versionResult;
        try {
            VersionRequest versionRequest = new VersionRequest(artifact, repos, request.getRequestContext());
            versionRequest.setTrace(trace);
            versionResult = versionResolver.resolveVersion(session, versionRequest);
        } catch (VersionResolutionException e) {
            result.addException(e);
            continue;
        }
        artifact = artifact.setVersion(versionResult.getVersion());
        if (versionResult.getRepository() != null) {
            if (versionResult.getRepository() instanceof RemoteRepository) {
                repos = Collections.singletonList((RemoteRepository) versionResult.getRepository());
            } else {
                repos = Collections.emptyList();
            }
        }
        if (workspace != null) {
            File file = workspace.findArtifact(artifact);
            if (file != null) {
                artifact = artifact.setFile(file);
                result.setArtifact(artifact);
                result.setRepository(workspace.getRepository());
                artifactResolved(session, trace, artifact, result.getRepository(), null);
                continue;
            }
        }
        LocalArtifactResult local = lrm.find(session, new LocalArtifactRequest(artifact, repos, request.getRequestContext()));
        if (isLocallyInstalled(local, versionResult)) {
            if (local.getRepository() != null) {
                result.setRepository(local.getRepository());
            } else {
                result.setRepository(lrm.getRepository());
            }
            try {
                artifact = artifact.setFile(getFile(session, artifact, local.getFile()));
                result.setArtifact(artifact);
                artifactResolved(session, trace, artifact, result.getRepository(), null);
            } catch (ArtifactTransferException e) {
                result.addException(e);
            }
            if (!local.isAvailable()) {
                /*
                     * NOTE: Interop with simple local repository: An artifact installed by a simple local repo manager
                     * will not show up in the repository tracking file of the enhanced local repository. If however the
                     * maven-metadata-local.xml tells us the artifact was installed locally, we sync the repository
                     * tracking file.
                     */
                lrm.add(session, new LocalArtifactRegistration(artifact));
            }
            continue;
        } else if (local.getFile() != null) {
            logger.debug("Verifying availability of " + local.getFile() + " from " + repos);
        }
        if (session.isOffline()) {
            Exception exception = new ArtifactNotFoundException(artifact, null, "The repository system is offline but the artifact " + artifact + " is not available in the local repository.");
            result.addException(exception);
            artifactResolved(session, trace, artifact, null, result.getExceptions());
            continue;
        }
        AtomicBoolean resolved = new AtomicBoolean(false);
        Iterator<ResolutionGroup> groupIt = groups.iterator();
        for (RemoteRepository repo : repos) {
            if (!repo.getPolicy(artifact.isSnapshot()).isEnabled()) {
                continue;
            }
            ResolutionGroup group = null;
            while (groupIt.hasNext()) {
                ResolutionGroup t = groupIt.next();
                if (t.matches(repo)) {
                    group = t;
                    break;
                }
            }
            if (group == null) {
                group = new ResolutionGroup(repo);
                groups.add(group);
                groupIt = Collections.<ResolutionGroup>emptyList().iterator();
            }
            group.items.add(new ResolutionItem(trace, artifact, resolved, result, local, repo));
        }
    }
    for (ResolutionGroup group : groups) {
        List<ArtifactDownload> downloads = new ArrayList<ArtifactDownload>();
        for (ResolutionItem item : group.items) {
            Artifact artifact = item.artifact;
            if (item.resolved.get()) {
                // resolved in previous resolution group
                continue;
            }
            ArtifactDownload download = new ArtifactDownload();
            download.setArtifact(artifact);
            download.setRequestContext(item.request.getRequestContext());
            download.setTrace(item.trace);
            if (item.local.getFile() != null) {
                download.setFile(item.local.getFile());
                download.setExistenceCheck(true);
            } else {
                String path = lrm.getPathForRemoteArtifact(artifact, group.repository, item.request.getRequestContext());
                download.setFile(new File(lrm.getRepository().getBasedir(), path));
            }
            boolean snapshot = artifact.isSnapshot();
            RepositoryPolicy policy = remoteRepositoryManager.getPolicy(session, group.repository, !snapshot, snapshot);
            if (session.isNotFoundCachingEnabled() || session.isTransferErrorCachingEnabled()) {
                UpdateCheck<Artifact, ArtifactTransferException> check = new UpdateCheck<Artifact, ArtifactTransferException>();
                check.setItem(artifact);
                check.setFile(download.getFile());
                check.setFileValid(!download.isExistenceCheck());
                check.setRepository(group.repository);
                check.setPolicy(policy.getUpdatePolicy());
                item.updateCheck = check;
                updateCheckManager.checkArtifact(session, check);
                if (!check.isRequired()) {
                    item.result.addException(check.getException());
                    continue;
                }
            }
            download.setChecksumPolicy(policy.getChecksumPolicy());
            download.setRepositories(item.repository.getMirroredRepositories());
            downloads.add(download);
            item.download = download;
        }
        if (downloads.isEmpty()) {
            continue;
        }
        for (ArtifactDownload download : downloads) {
            artifactDownloading(session, download.getTrace(), download.getArtifact(), group.repository);
        }
        try {
            RepositoryConnector connector = remoteRepositoryManager.getRepositoryConnector(session, group.repository);
            try {
                connector.get(downloads, null);
            } finally {
                connector.close();
            }
        } catch (NoRepositoryConnectorException e) {
            for (ArtifactDownload download : downloads) {
                download.setException(new ArtifactTransferException(download.getArtifact(), group.repository, e));
            }
        }
        for (ResolutionItem item : group.items) {
            ArtifactDownload download = item.download;
            if (download == null) {
                continue;
            }
            if (item.updateCheck != null) {
                item.updateCheck.setException(download.getException());
                updateCheckManager.touchArtifact(session, item.updateCheck);
            }
            if (download.getException() == null) {
                item.resolved.set(true);
                item.result.setRepository(group.repository);
                Artifact artifact = download.getArtifact();
                try {
                    artifact = artifact.setFile(getFile(session, artifact, download.getFile()));
                    item.result.setArtifact(artifact);
                } catch (ArtifactTransferException e) {
                    item.result.addException(e);
                    continue;
                }
                lrm.add(session, new LocalArtifactRegistration(artifact, group.repository, download.getSupportedContexts()));
                artifactDownloaded(session, download.getTrace(), artifact, group.repository, null);
                artifactResolved(session, download.getTrace(), artifact, group.repository, null);
            } else {
                item.result.addException(download.getException());
                artifactDownloaded(session, download.getTrace(), download.getArtifact(), group.repository, download.getException());
            }
        }
    }
    for (ArtifactResult result : results) {
        ArtifactRequest request = result.getRequest();
        Artifact artifact = result.getArtifact();
        if (artifact == null || artifact.getFile() == null) {
            failures = true;
            if (result.getExceptions().isEmpty()) {
                Exception exception = new ArtifactNotFoundException(request.getArtifact(), null);
                result.addException(exception);
            }
            RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
            artifactResolved(session, trace, request.getArtifact(), null, result.getExceptions());
        }
    }
    if (failures) {
        throw new ArtifactResolutionException(results);
    }
    return results;
}
Also used : LocalArtifactRegistration(org.sonatype.aether.repository.LocalArtifactRegistration) ArrayList(java.util.ArrayList) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) RequestTrace(org.sonatype.aether.RequestTrace) DefaultRequestTrace(org.sonatype.aether.util.DefaultRequestTrace) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) RepositoryPolicy(org.sonatype.aether.repository.RepositoryPolicy) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) VersionRequest(org.sonatype.aether.resolution.VersionRequest) WorkspaceReader(org.sonatype.aether.repository.WorkspaceReader) Artifact(org.sonatype.aether.artifact.Artifact) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) VersionResolutionException(org.sonatype.aether.resolution.VersionResolutionException) IOException(java.io.IOException) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UpdateCheck(org.sonatype.aether.impl.UpdateCheck) VersionResult(org.sonatype.aether.resolution.VersionResult) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) VersionResolutionException(org.sonatype.aether.resolution.VersionResolutionException) File(java.io.File) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest)

Example 23 with ArtifactDownload

use of org.sonatype.aether.spi.connector.ArtifactDownload in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testRepositoryEventsUnsuccessfulRemote.

@Test
public void testRepositoryEventsUnsuccessfulRemote() throws IOException, ArtifactResolutionException {
    RecordingRepositoryConnector connector = new RecordingRepositoryConnector() {

        @Override
        public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
            super.get(artifactDownloads, metadataDownloads);
            ArtifactDownload download = artifactDownloads.iterator().next();
            ArtifactTransferException exception = new ArtifactNotFoundException(download.getArtifact(), null, "not found");
            download.setException(exception);
        }
    };
    remoteRepositoryManager.setConnector(connector);
    RecordingRepositoryListener listener = new RecordingRepositoryListener();
    session.setRepositoryListener(listener);
    ArtifactRequest request = new ArtifactRequest(artifact, null, "");
    request.addRepository(new RemoteRepository("id", "default", "file:///"));
    try {
        resolver.resolveArtifact(session, request);
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
    }
    List<EventWrapper> events = listener.getEvents();
    assertEquals(2, events.size());
    EventWrapper event = events.get(0);
    assertEquals(artifact, event.getEvent().getArtifact());
    assertEquals(Type.ARTIFACT_RESOLVING, event.getType());
    event = events.get(1);
    assertEquals(artifact, event.getEvent().getArtifact());
    assertEquals(Type.ARTIFACT_RESOLVED, event.getType());
    assertNotNull(event.getEvent().getException());
    assertEquals(1, event.getEvent().getExceptions().size());
}
Also used : ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) Collection(java.util.Collection) RecordingRepositoryListener(org.sonatype.aether.test.impl.RecordingRepositoryListener) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) EventWrapper(org.sonatype.aether.test.impl.RecordingRepositoryListener.EventWrapper) Test(org.junit.Test)

Example 24 with ArtifactDownload

use of org.sonatype.aether.spi.connector.ArtifactDownload in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testArtifactNotFoundCache.

@Test
public void testArtifactNotFoundCache() throws Exception {
    RecordingRepositoryConnector connector = new RecordingRepositoryConnector() {

        @Override
        public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
            super.get(artifactDownloads, metadataDownloads);
            for (ArtifactDownload download : artifactDownloads) {
                download.getFile().delete();
                ArtifactTransferException exception = new ArtifactNotFoundException(download.getArtifact(), null, "not found");
                download.setException(exception);
            }
        }
    };
    remoteRepositoryManager.setConnector(connector);
    resolver.setUpdateCheckManager(new DefaultUpdateCheckManager());
    session.setNotFoundCachingEnabled(true);
    session.setUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
    RemoteRepository remoteRepo = new RemoteRepository("id", "default", "file:///");
    Artifact artifact1 = artifact;
    Artifact artifact2 = artifact.setVersion("ver2");
    ArtifactRequest request1 = new ArtifactRequest(artifact1, Arrays.asList(remoteRepo), "");
    ArtifactRequest request2 = new ArtifactRequest(artifact2, Arrays.asList(remoteRepo), "");
    connector.setExpectGet(new Artifact[] { artifact1, artifact2 });
    try {
        resolver.resolveArtifacts(session, Arrays.asList(request1, request2));
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
    }
    TestFileUtils.write("artifact", new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifact2)));
    lrm.setArtifactAvailability(artifact2, false);
    DefaultUpdateCheckManagerTest.resetSessionData(session);
    connector.resetActual();
    connector.setExpectGet(new Artifact[0]);
    try {
        resolver.resolveArtifacts(session, Arrays.asList(request1, request2));
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
        for (ArtifactResult result : e.getResults()) {
            Throwable t = result.getExceptions().get(0);
            assertEquals(t.toString(), true, t instanceof ArtifactNotFoundException);
            assertEquals(t.toString(), true, t.getMessage().contains("cached"));
        }
    }
}
Also used : RemoteRepository(org.sonatype.aether.repository.RemoteRepository) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) Collection(java.util.Collection) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) File(java.io.File) Test(org.junit.Test)

Example 25 with ArtifactDownload

use of org.sonatype.aether.spi.connector.ArtifactDownload in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testResolveRemoteArtifactUnsuccessful.

@Test
public void testResolveRemoteArtifactUnsuccessful() throws IOException, ArtifactResolutionException {
    RecordingRepositoryConnector connector = new RecordingRepositoryConnector() {

        @Override
        public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
            super.get(artifactDownloads, metadataDownloads);
            ArtifactDownload download = artifactDownloads.iterator().next();
            ArtifactTransferException exception = new ArtifactNotFoundException(download.getArtifact(), null, "not found");
            download.setException(exception);
        }
    };
    connector.setExpectGet(artifact);
    remoteRepositoryManager.setConnector(connector);
    ArtifactRequest request = new ArtifactRequest(artifact, null, "");
    request.addRepository(new RemoteRepository("id", "default", "file:///"));
    try {
        resolver.resolveArtifact(session, request);
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
        assertNotNull(e.getResults());
        assertEquals(1, e.getResults().size());
        ArtifactResult result = e.getResults().get(0);
        assertSame(request, result.getRequest());
        assertFalse(result.getExceptions().isEmpty());
        assertTrue(result.getExceptions().get(0) instanceof ArtifactNotFoundException);
        Artifact resolved = result.getArtifact();
        assertNull(resolved);
    }
}
Also used : ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) Collection(java.util.Collection) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) Test(org.junit.Test)

Aggregations

ArtifactDownload (org.sonatype.aether.spi.connector.ArtifactDownload)28 File (java.io.File)21 Test (org.junit.Test)19 Artifact (org.sonatype.aether.artifact.Artifact)16 MetadataDownload (org.sonatype.aether.spi.connector.MetadataDownload)15 RepositoryConnector (org.sonatype.aether.spi.connector.RepositoryConnector)10 ArtifactUpload (org.sonatype.aether.spi.connector.ArtifactUpload)9 MetadataUpload (org.sonatype.aether.spi.connector.MetadataUpload)8 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)8 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)6 StubMetadata (org.sonatype.aether.test.util.impl.StubMetadata)5 ArtifactNotFoundException (org.sonatype.aether.transfer.ArtifactNotFoundException)5 ArrayList (java.util.ArrayList)4 Metadata (org.sonatype.aether.metadata.Metadata)4 LocalArtifactRequest (org.sonatype.aether.repository.LocalArtifactRequest)4 ArtifactRequest (org.sonatype.aether.resolution.ArtifactRequest)4 ArtifactResolutionException (org.sonatype.aether.resolution.ArtifactResolutionException)4 ArtifactTransferException (org.sonatype.aether.transfer.ArtifactTransferException)4 IOException (java.io.IOException)3 Collection (java.util.Collection)3