use of org.apache.archiva.indexer.search.SearchResults in project archiva by apache.
the class MavenRepositorySearchPaginateTest method nonPaginatedResult.
@Test
public void nonPaginatedResult() throws Exception {
MavenRepositorySearch search = new MavenRepositorySearch();
SearchResults searchResults = build(10, new SearchResultLimits(0));
searchResults = search.paginate(searchResults);
assertEquals(10, searchResults.getReturnedHitsCount());
}
use of org.apache.archiva.indexer.search.SearchResults in project archiva by apache.
the class DefaultSearchService method searchArtifacts.
@Override
public List<Artifact> searchArtifacts(SearchRequest searchRequest) throws ArchivaRestServiceException {
if (searchRequest == null) {
return Collections.emptyList();
}
SearchFields searchField = getModelMapper().map(searchRequest, SearchFields.class);
SearchResultLimits limits = new SearchResultLimits(0);
limits.setPageSize(searchRequest.getPageSize());
// if no repos set we use ones available for the user
if (searchField.getRepositories() == null || searchField.getRepositories().isEmpty()) {
searchField.setRepositories(getObservableRepos());
}
try {
SearchResults searchResults = repositorySearch.search(getPrincipal(), searchField, limits);
return getArtifacts(searchResults);
} catch (RepositorySearchException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
}
}
use of org.apache.archiva.indexer.search.SearchResults 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.indexer.search.SearchResults in project archiva by apache.
the class DefaultSearchService method quickSearchWithRepositories.
@Override
public List<Artifact> quickSearchWithRepositories(SearchRequest searchRequest) throws ArchivaRestServiceException {
String queryString = searchRequest.getQueryTerms();
if (StringUtils.isBlank(queryString)) {
return Collections.emptyList();
}
List<String> repositories = searchRequest.getRepositories();
if (repositories == null || repositories.isEmpty()) {
repositories = getObservableRepos();
}
SearchResultLimits limits = new SearchResultLimits(searchRequest.getPageSize(), searchRequest.getSelectedPage());
try {
SearchResults searchResults = repositorySearch.search(getPrincipal(), repositories, queryString, limits, Collections.<String>emptyList());
return getArtifacts(searchResults);
} catch (RepositorySearchException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
}
}
use of org.apache.archiva.indexer.search.SearchResults in project archiva by apache.
the class DefaultSearchService method getArtifactVersions.
@Override
public List<Artifact> getArtifactVersions(String groupId, String artifactId, String packaging) throws ArchivaRestServiceException {
if (StringUtils.isBlank(groupId) || StringUtils.isBlank(artifactId)) {
return Collections.emptyList();
}
SearchFields searchField = new SearchFields();
searchField.setGroupId(groupId);
searchField.setArtifactId(artifactId);
searchField.setPackaging(StringUtils.isBlank(packaging) ? "jar" : packaging);
searchField.setRepositories(getObservableRepos());
try {
SearchResults searchResults = repositorySearch.search(getPrincipal(), searchField, null);
return getArtifacts(searchResults);
} catch (RepositorySearchException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
}
}
Aggregations