use of org.apache.archiva.rest.api.services.ArchivaRestServiceException 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.rest.api.services.ArchivaRestServiceException in project archiva by apache.
the class DefaultSearchService method getAllGroupIds.
@Override
public GroupIdList getAllGroupIds(List<String> selectedRepos) throws ArchivaRestServiceException {
List<String> observableRepos = getObservableRepos();
List<String> repos = ListUtils.intersection(observableRepos, selectedRepos);
if (repos == null || repos.isEmpty()) {
return new GroupIdList(Collections.<String>emptyList());
}
try {
return new GroupIdList(new ArrayList<>(repositorySearch.getAllGroupIds(getPrincipal(), repos)));
} catch (RepositorySearchException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
}
}
use of org.apache.archiva.rest.api.services.ArchivaRestServiceException 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 = repositorySessionFactory.createSession();
MetadataRepository metadataRepository = repositorySession.getRepository();
Set<Artifact> artifactSet = new HashSet<>();
try {
for (String repoId : checksumSearch.getRepositories()) {
Collection<ArtifactMetadata> artifactMetadatas = metadataRepository.getArtifactsByChecksum(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.rest.api.services.ArchivaRestServiceException in project archiva by apache.
the class DefaultBrowseService method browseGroupId.
@Override
public BrowseResult browseGroupId(String groupId, String repositoryId) throws ArchivaRestServiceException {
List<String> selectedRepos = getSelectedRepos(repositoryId);
Set<String> projects = new LinkedHashSet<>();
RepositorySession repositorySession = repositorySessionFactory.createSession();
Set<String> namespaces;
try {
MetadataResolver metadataResolver = repositorySession.getResolver();
Set<String> namespacesToCollapse = new LinkedHashSet<>();
for (String repoId : selectedRepos) {
namespacesToCollapse.addAll(metadataResolver.resolveNamespaces(repositorySession, repoId, groupId));
projects.addAll(metadataResolver.resolveProjects(repositorySession, repoId, groupId));
}
// TODO: this logic should be optional, particularly remembering we want to keep this code simple
// it is located here to avoid the content repository implementation needing to do too much for what
// is essentially presentation code
namespaces = new LinkedHashSet<>();
for (String n : namespacesToCollapse) {
// TODO: check performance of this
namespaces.add(collapseNamespaces(repositorySession, metadataResolver, selectedRepos, groupId + "." + n));
}
} catch (MetadataResolutionException e) {
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} finally {
repositorySession.close();
}
List<BrowseResultEntry> browseGroupResultEntries = new ArrayList<>(namespaces.size() + projects.size());
for (String namespace : namespaces) {
browseGroupResultEntries.add(new BrowseResultEntry(namespace, false).groupId(namespace));
}
for (String project : projects) {
browseGroupResultEntries.add(new BrowseResultEntry(groupId + '.' + project, true).groupId(groupId).artifactId(project));
}
Collections.sort(browseGroupResultEntries);
return new BrowseResult(browseGroupResultEntries);
}
use of org.apache.archiva.rest.api.services.ArchivaRestServiceException in project archiva by apache.
the class DefaultBrowseService method addMetadata.
@Override
public Boolean addMetadata(String groupId, String artifactId, String version, String key, String value, String repositoryId) throws ArchivaRestServiceException {
ProjectVersionMetadata projectVersionMetadata = getProjectMetadata(groupId, artifactId, version, repositoryId);
if (projectVersionMetadata == null) {
return Boolean.FALSE;
}
Map<String, String> properties = new HashMap<>();
MetadataFacet metadataFacet = projectVersionMetadata.getFacet(GenericMetadataFacet.FACET_ID);
if (metadataFacet != null && metadataFacet.toProperties() != null) {
properties.putAll(metadataFacet.toProperties());
} else {
metadataFacet = new GenericMetadataFacet();
}
properties.put(key, value);
metadataFacet.fromProperties(properties);
projectVersionMetadata.addFacet(metadataFacet);
RepositorySession repositorySession = repositorySessionFactory.createSession();
try {
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.updateProjectVersion(repositoryId, groupId, artifactId, projectVersionMetadata);
repositorySession.save();
} catch (MetadataRepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} finally {
repositorySession.close();
}
return Boolean.TRUE;
}
Aggregations