Search in sources :

Example 1 with Metadata

use of org.eclipse.aether.metadata.Metadata in project fabric8 by jboss-fuse.

the class MavenProxyServletSupport method download.

@Override
public File download(String path) throws InvalidMavenArtifactRequest {
    if (path == null) {
        throw new InvalidMavenArtifactRequest();
    }
    Matcher artifactMatcher = ARTIFACT_REQUEST_URL_REGEX.matcher(path);
    Matcher metdataGaMatcher = ARTIFACT_GA_METADATA_URL_REGEX.matcher(path);
    if (metdataGaMatcher.matches()) {
        LOGGER.info("Received request for maven metadata : {}", path);
        Metadata metadata = null;
        try {
            metadata = convertPathToMetadata(path);
            // Only handle xxx/maven-metadata.xml requests
            if (!"maven-metadata.xml".equals(metadata.getType()) || metdataGaMatcher.group(7) != null) {
                return null;
            }
            if (isHostedRepository()) {
                // do not resolve metadata via Aether - simply look it up in hosted storage
                // This is important, because `mvn deploy` or `mvn fabric8:deploy` goals, even if they PUT
                // artifacts and metadata also GET metadata (not artifacts)
                File hostedMetadata = new File(uploadRepository, path);
                LOGGER.debug("Getting metadata from hosted storage : {}", hostedMetadata);
                if (hostedMetadata.isFile()) {
                    File tmpFile = Files.createTempFile(runtimeProperties.getDataPath());
                    Files.copy(hostedMetadata, tmpFile);
                    return tmpFile;
                }
                // never look up in io.fabric8.maven.defaultRepositories or io.fabric8.maven.repositories
                return null;
            }
            // Try with default repositories - first metadata found is returned
            VersionConstraint vc = new GenericVersionScheme().parseVersionConstraint(metadata.getVersion());
            if (vc.getVersion() != null) {
                for (LocalRepository repo : resolver.getDefaultRepositories()) {
                    if (vc.getVersion().toString().endsWith("SNAPSHOT") && !resolver.handlesSnapshot(repo)) {
                        continue;
                    }
                    // clone session to swap local repository manager
                    DefaultRepositorySystemSession localSession = new DefaultRepositorySystemSession(session);
                    localSession.setLocalRepositoryManager(system.newLocalRepositoryManager(localSession, repo));
                    LOGGER.debug("Getting metadata from default repository : {}", repo.getBasedir());
                    List<MetadataResult> results = system.resolveMetadata(localSession, Collections.singletonList(new MetadataRequest(metadata, null, null)));
                    File file = processMetadataResults(metadata, results);
                    if (file != null) {
                        return file;
                    }
                }
            }
            // fallback to remote repositories - this time results are merged
            List<MetadataRequest> requests = new ArrayList<>();
            for (RemoteRepository repository : repositories) {
                MetadataRequest request = new MetadataRequest(metadata, repository, null);
                request.setFavorLocalRepository(false);
                requests.add(request);
            }
            MetadataRequest request = new MetadataRequest(metadata, null, null);
            request.setFavorLocalRepository(true);
            requests.add(request);
            List<MetadataResult> results = system.resolveMetadata(session, requests);
            File result = processMetadataResults(metadata, results);
            if (result != null) {
                return result;
            }
        } catch (Exception e) {
            LOGGER.warn(String.format("Could not find metadata : %s due to %s", metadata, e.getMessage()), e);
            return null;
        }
        // If no matching metadata found return nothing
        return null;
    } else if (artifactMatcher.matches()) {
        LOGGER.info("Received request for maven artifact : {}", path);
        Artifact artifact = convertPathToArtifact(path);
        try {
            if (artifact.getExtension() != null && (artifact.getExtension().endsWith(".sha1") || artifact.getExtension().endsWith(".md5"))) {
                return null;
            }
            File download = resolver.resolveFile(artifact);
            File tmpFile = Files.createTempFile(runtimeProperties.getDataPath());
            Files.copy(download, tmpFile);
            return tmpFile;
        } catch (Exception e) {
            LOGGER.warn(String.format("Could not find artifact : %s due to %s", artifact, e.getMessage()), e);
            return null;
        }
    }
    return null;
}
Also used : VersionConstraint(org.eclipse.aether.version.VersionConstraint) Matcher(java.util.regex.Matcher) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) Metadata(org.eclipse.aether.metadata.Metadata) LocalRepository(org.eclipse.aether.repository.LocalRepository) ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) MetadataResult(org.eclipse.aether.resolution.MetadataResult) MetadataRequest(org.eclipse.aether.resolution.MetadataRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with Metadata

use of org.eclipse.aether.metadata.Metadata in project revapi by revapi.

the class ArtifactResolver method makeRemoteOnly.

private RepositorySystemSession makeRemoteOnly(RepositorySystemSession session) {
    return new AbstractForwardingRepositorySystemSession() {

        @Override
        protected RepositorySystemSession getSession() {
            return session;
        }

        @Override
        public WorkspaceReader getWorkspaceReader() {
            return null;
        }

        @Override
        public LocalRepositoryManager getLocalRepositoryManager() {
            LocalRepositoryManager wrapped = session.getLocalRepositoryManager();
            return new LocalRepositoryManager() {

                @Override
                public LocalRepository getRepository() {
                    return wrapped.getRepository();
                }

                @Override
                public String getPathForLocalArtifact(Artifact artifact) {
                    return wrapped.getPathForLocalArtifact(artifact);
                }

                @Override
                public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
                    return wrapped.getPathForRemoteArtifact(artifact, repository, context);
                }

                @Override
                public String getPathForLocalMetadata(Metadata metadata) {
                    return wrapped.getPathForLocalMetadata(metadata);
                }

                @Override
                public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
                    return wrapped.getPathForRemoteMetadata(metadata, repository, context);
                }

                @Override
                public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) {
                    return wrapped.find(session, request);
                }

                @Override
                public void add(RepositorySystemSession session, LocalArtifactRegistration request) {
                    wrapped.add(session, request);
                }

                @Override
                public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
                    if (request.getRepository() == null) {
                        // local metadata request... the returned file must not be null but may not exist
                        // we exploit that to not include the locally built results
                        LocalMetadataResult ret = new LocalMetadataResult(request);
                        ret.setFile(new File("<faked-to-force-remote-only-resolution-of-artifacts>"));
                        return ret;
                    } else {
                        return wrapped.find(session, request);
                    }
                }

                @Override
                public void add(RepositorySystemSession session, LocalMetadataRegistration request) {
                    wrapped.add(session, request);
                }
            };
        }
    };
}
Also used : AbstractForwardingRepositorySystemSession(org.eclipse.aether.AbstractForwardingRepositorySystemSession) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) LocalArtifactRegistration(org.eclipse.aether.repository.LocalArtifactRegistration) LocalMetadataRequest(org.eclipse.aether.repository.LocalMetadataRequest) LocalMetadataRegistration(org.eclipse.aether.repository.LocalMetadataRegistration) Metadata(org.eclipse.aether.metadata.Metadata) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) LocalRepositoryManager(org.eclipse.aether.repository.LocalRepositoryManager) LocalMetadataResult(org.eclipse.aether.repository.LocalMetadataResult) AbstractForwardingRepositorySystemSession(org.eclipse.aether.AbstractForwardingRepositorySystemSession) File(java.io.File) LocalArtifactRequest(org.eclipse.aether.repository.LocalArtifactRequest)

Example 3 with Metadata

use of org.eclipse.aether.metadata.Metadata in project fabric8 by jboss-fuse.

the class MavenProxyServletSupport method processMetadataResults.

/**
 * Given a list of {@link MetadataResult} performs a merge of metadata
 *
 * @param metadata
 * @param results
 * @return <code>If no metadata is found</code>
 * @throws IOException
 * @throws XmlPullParserException
 */
private File processMetadataResults(Metadata metadata, List<MetadataResult> results) throws IOException, XmlPullParserException {
    org.apache.maven.artifact.repository.metadata.Metadata mr = new org.apache.maven.artifact.repository.metadata.Metadata();
    mr.setModelVersion("1.1.0");
    mr.setGroupId(metadata.getGroupId());
    mr.setArtifactId(metadata.getArtifactId());
    if (Strings.isNotBlank(metadata.getVersion())) {
        mr.setVersion(metadata.getVersion());
    }
    mr.setVersioning(new Versioning());
    boolean merged = false;
    for (MetadataResult result : results) {
        if (result.getMetadata() != null && result.getMetadata().getFile() != null) {
            FileInputStream fis = new FileInputStream(result.getMetadata().getFile());
            org.apache.maven.artifact.repository.metadata.Metadata m = new MetadataXpp3Reader().read(fis, false);
            fis.close();
            if (m.getVersioning() != null) {
                if (mr.getVersion() != null && mr.getVersion().endsWith("-SNAPSHOT")) {
                    handleSnapshot(mr.getVersioning(), m.getVersioning());
                }
                mr.getVersioning().setLastUpdated(latestTimestamp(mr.getVersioning().getLastUpdated(), m.getVersioning().getLastUpdated()));
                mr.getVersioning().setLatest(latestVersion(mr.getVersioning().getLatest(), m.getVersioning().getLatest()));
                mr.getVersioning().setRelease(latestVersion(mr.getVersioning().getRelease(), m.getVersioning().getRelease()));
                for (String v : m.getVersioning().getVersions()) {
                    if (!mr.getVersioning().getVersions().contains(v)) {
                        mr.getVersioning().getVersions().add(v);
                    }
                }
                mr.getVersioning().getSnapshotVersions().addAll(m.getVersioning().getSnapshotVersions());
            }
            merged = true;
        }
    }
    if (merged) {
        Collections.sort(mr.getVersioning().getVersions(), VERSION_COMPARATOR);
        Collections.sort(mr.getVersioning().getSnapshotVersions(), SNAPSHOT_VERSION_COMPARATOR);
        File tmpFile = Files.createTempFile(runtimeProperties.getDataPath());
        FileOutputStream fos = new FileOutputStream(tmpFile);
        new MetadataXpp3Writer().write(fos, mr);
        fos.close();
        return tmpFile;
    }
    return null;
}
Also used : MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) Metadata(org.eclipse.aether.metadata.Metadata) FileInputStream(java.io.FileInputStream) MetadataResult(org.eclipse.aether.resolution.MetadataResult) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

File (java.io.File)3 Metadata (org.eclipse.aether.metadata.Metadata)3 JarFile (java.util.jar.JarFile)2 Artifact (org.eclipse.aether.artifact.Artifact)2 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)2 DefaultMetadata (org.eclipse.aether.metadata.DefaultMetadata)2 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)2 MetadataResult (org.eclipse.aether.resolution.MetadataResult)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)1 MetadataXpp3Reader (org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader)1 MetadataXpp3Writer (org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer)1 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)1 AbstractForwardingRepositorySystemSession (org.eclipse.aether.AbstractForwardingRepositorySystemSession)1 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)1