use of org.apache.archiva.maven.model.Artifact in project archiva by apache.
the class AbstractRestService method buildArtifacts.
protected List<Artifact> buildArtifacts(Collection<ArtifactMetadata> artifactMetadatas, String repositoryId) throws ArchivaRestServiceException {
try {
if (artifactMetadatas != null && !artifactMetadatas.isEmpty()) {
List<Artifact> artifacts = new ArrayList<>(artifactMetadatas.size());
for (ArtifactMetadata artifact : artifactMetadatas) {
String repoId = repositoryId != null ? repositoryId : artifact.getRepositoryId();
if (repoId == null) {
throw new IllegalStateException("Repository Id is null");
}
ManagedRepository repo = repositoryRegistry.getManagedRepository(repoId);
if (repo == null) {
throw new RepositoryException("Repository not found " + repoId);
}
ManagedRepositoryContent content = repo.getContent();
ArtifactBuilder builder = new ArtifactBuilder().forArtifactMetadata(artifact).withManagedRepositoryContent(content);
Artifact art = builder.build();
art.setUrl(getArtifactUrl(art, repositoryId));
artifacts.add(art);
}
return artifacts;
}
return Collections.emptyList();
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
}
use of org.apache.archiva.maven.model.Artifact in project archiva by apache.
the class DefaultSearchService method getArtifactByChecksum.
public List<Artifact> getArtifactByChecksum(ChecksumSearch checksumSearch) throws ArchivaRestServiceException {
// if no repos set we use ones available for the user
if (checksumSearch.getRepositories() == null || checksumSearch.getRepositories().isEmpty()) {
checksumSearch.setRepositories(getObservableRepos());
}
RepositorySession repositorySession = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
MetadataRepository metadataRepository = repositorySession.getRepository();
Set<Artifact> artifactSet = new HashSet<>();
try {
for (String repoId : checksumSearch.getRepositories()) {
Collection<ArtifactMetadata> artifactMetadatas = metadataRepository.getArtifactsByChecksum(repositorySession, repoId, checksumSearch.getChecksum());
artifactSet.addAll(buildArtifacts(artifactMetadatas, repoId));
}
return new ArrayList<>(artifactSet);
} catch (MetadataRepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
} finally {
repositorySession.closeQuietly();
}
}
use of org.apache.archiva.maven.model.Artifact in project archiva by apache.
the class DefaultSearchService method redirectToArtifactFile.
@Override
public Response redirectToArtifactFile(String repositoryId, String groupId, String artifactId, String version, String packaging, String classifier) throws ArchivaRestServiceException {
try {
if (StringUtils.isEmpty(groupId)) {
return Response.status(new Response.StatusType() {
@Override
public int getStatusCode() {
return Response.Status.BAD_REQUEST.getStatusCode();
}
@Override
public Response.Status.Family getFamily() {
return Response.Status.BAD_REQUEST.getFamily();
}
@Override
public String getReasonPhrase() {
return "groupId mandatory";
}
}).build();
}
if (StringUtils.isEmpty(version)) {
return Response.status(new Response.StatusType() {
@Override
public int getStatusCode() {
return Response.Status.BAD_REQUEST.getStatusCode();
}
@Override
public Response.Status.Family getFamily() {
return Response.Status.BAD_REQUEST.getFamily();
}
@Override
public String getReasonPhrase() {
return "version mandatory";
}
}).build();
}
if (StringUtils.isEmpty(artifactId)) {
return Response.status(new Response.StatusType() {
@Override
public int getStatusCode() {
return Response.Status.BAD_REQUEST.getStatusCode();
}
@Override
public Response.Status.Family getFamily() {
return Response.Status.BAD_REQUEST.getFamily();
}
@Override
public String getReasonPhrase() {
return "artifactId mandatory";
}
}).build();
}
SearchFields searchField = new SearchFields();
searchField.setGroupId(groupId);
searchField.setArtifactId(artifactId);
searchField.setPackaging(StringUtils.isBlank(packaging) ? "jar" : packaging);
if (!StringUtils.equals(version, LATEST_KEYWORD)) {
searchField.setVersion(version);
}
searchField.setClassifier(classifier);
List<String> userRepos = getObservablesRepoIds().getStrings();
searchField.setRepositories(StringUtils.isEmpty(repositoryId) ? userRepos : Arrays.asList(repositoryId));
searchField.setExactSearch(true);
SearchResults searchResults = repositorySearch.search(getPrincipal(), searchField, null);
List<Artifact> artifacts = getArtifacts(searchResults);
if (artifacts.isEmpty()) {
return Response.status(new Response.StatusType() {
@Override
public int getStatusCode() {
return Response.Status.NO_CONTENT.getStatusCode();
}
@Override
public Response.Status.Family getFamily() {
return Response.Status.NO_CONTENT.getFamily();
}
@Override
public String getReasonPhrase() {
return "your query doesn't return any artifact";
}
}).build();
}
// so simple loop and retain only artifact with null classifier
if (classifier == null) {
List<Artifact> filteredArtifacts = new ArrayList<>(artifacts.size());
for (Artifact artifact : artifacts) {
if (artifact.getClassifier() == null) {
filteredArtifacts.add(artifact);
}
}
artifacts = filteredArtifacts;
}
// TODO return json result of the query ?
if (artifacts.size() > 1 && !StringUtils.equals(version, LATEST_KEYWORD)) {
return Response.status(new Response.StatusType() {
@Override
public int getStatusCode() {
return Response.Status.BAD_REQUEST.getStatusCode();
}
@Override
public Response.Status.Family getFamily() {
return Response.Status.BAD_REQUEST.getFamily();
}
@Override
public String getReasonPhrase() {
return "your query return more than one artifact";
}
}).build();
}
// version is LATEST so we have to find the latest one from the result
if (artifacts.size() > 1 && StringUtils.equals(version, LATEST_KEYWORD)) {
TreeMap<String, Artifact> artifactPerVersion = new TreeMap<>(VersionComparator.getInstance());
for (Artifact artifact : artifacts) {
artifactPerVersion.put(artifact.getVersion(), artifact);
}
return Response.temporaryRedirect(new URI(artifactPerVersion.lastEntry().getValue().getUrl())).build();
}
Artifact artifact = artifacts.get(0);
return Response.temporaryRedirect(new URI(artifact.getUrl())).build();
} catch (Exception e) {
throw new ArchivaRestServiceException(e.getMessage(), e);
}
}
use of org.apache.archiva.maven.model.Artifact in project archiva by apache.
the class DefaultSearchService method getArtifacts.
// -------------------------------------
// internal
// -------------------------------------
protected List<Artifact> getArtifacts(SearchResults searchResults) throws ArchivaRestServiceException {
if (searchResults == null || searchResults.isEmpty()) {
return Collections.emptyList();
}
List<Artifact> artifacts = new ArrayList<>(searchResults.getReturnedHitsCount());
for (SearchResultHit hit : searchResults.getHits()) {
// duplicate Artifact one per available version
if (hit.getVersions().size() > 0) {
for (String version : hit.getVersions()) {
Artifact versionned = getModelMapper().map(hit, Artifact.class);
if (StringUtils.isNotBlank(version)) {
versionned.setVersion(version);
versionned.setUrl(getArtifactUrl(versionned));
artifacts.add(versionned);
}
}
}
}
return artifacts;
}
use of org.apache.archiva.maven.model.Artifact in project archiva by apache.
the class ArtifactBuilder method build.
public Artifact build() {
String type = null, classifier = null;
MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
if (facet != null) {
type = facet.getType();
classifier = facet.getClassifier();
}
ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactMetadata.getNamespace()).withProjectId(artifactMetadata.getProject()).withVersion(artifactMetadata.getProjectVersion()).withArtifactId(artifactMetadata.getProject()).withArtifactVersion(artifactMetadata.getVersion());
if (StringUtils.isNotEmpty(type)) {
selectorBuilder.withType(type);
}
if (StringUtils.isNotEmpty(classifier)) {
selectorBuilder.withClassifier(classifier);
}
BaseRepositoryContentLayout layout;
try {
layout = managedRepositoryContent.getLayout(BaseRepositoryContentLayout.class);
} catch (LayoutException e) {
throw new RuntimeException("Could not convert to layout " + e.getMessage());
}
org.apache.archiva.repository.content.Artifact repoArtifact = layout.getArtifact(selectorBuilder.build());
String extension = repoArtifact.getExtension();
Artifact artifact = new Artifact(repoArtifact.getVersion().getProject().getNamespace().getId(), repoArtifact.getId(), repoArtifact.getArtifactVersion());
artifact.setRepositoryId(artifactMetadata.getRepositoryId());
artifact.setClassifier(classifier);
artifact.setPackaging(type);
artifact.setType(type);
artifact.setFileExtension(extension);
artifact.setPath(managedRepositoryContent.toPath(repoArtifact));
// TODO: find a reusable formatter for this
double s = this.artifactMetadata.getSize();
String symbol = "b";
if (s > 1024) {
symbol = "K";
s /= 1024;
if (s > 1024) {
symbol = "M";
s /= 1024;
if (s > 1024) {
symbol = "G";
s /= 1024;
}
}
}
artifact.setContext(managedRepositoryContent.getId());
DecimalFormat df = new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.US));
artifact.setSize(df.format(s) + " " + symbol);
artifact.setId(repoArtifact.getId() + "-" + repoArtifact.getArtifactVersion() + "." + repoArtifact.getType());
return artifact;
}
Aggregations