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;
}
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);
}
};
}
};
}
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;
}
Aggregations