use of org.apache.archiva.indexer.search.SearchFields in project archiva by apache.
the class MavenRepositorySearchTest method testAdvancedSearchClassNameInWar.
@Test
public void testAdvancedSearchClassNameInWar() throws Exception {
createIndexContainingMoreArtifacts(true);
List<String> selectedRepos = Arrays.asList(TEST_REPO_1);
SearchFields searchFields = new SearchFields();
searchFields.setClassName("SomeClass");
searchFields.setRepositories(selectedRepos);
EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
archivaConfigControl.replay();
SearchResults results = search.search("user", searchFields, null);
archivaConfigControl.verify();
assertNotNull(results);
assertEquals(1, results.getHits().size());
assertEquals("test-webapp", results.getHits().get(0).getArtifactId());
}
use of org.apache.archiva.indexer.search.SearchFields in project archiva by apache.
the class MavenRepositorySearchTest method testAdvancedSearch.
// tests for advanced search
@Test
public void testAdvancedSearch() throws Exception {
List<Path> files = new ArrayList<>();
files.add(Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar"));
files.add(Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar"));
createIndex(TEST_REPO_2, files, false);
List<String> selectedRepos = new ArrayList<>();
selectedRepos.add(TEST_REPO_2);
SearchFields searchFields = new SearchFields();
searchFields.setGroupId("org.apache.archiva");
searchFields.setVersion("1.0");
searchFields.setRepositories(selectedRepos);
EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
archivaConfigControl.replay();
SearchResults results = search.search("user", searchFields, null);
archivaConfigControl.verify();
assertNotNull(results);
assertEquals(1, results.getTotalHits());
SearchResultHit hit = results.getHits().get(0);
assertEquals("org.apache.archiva", hit.getGroupId());
assertEquals("archiva-search", hit.getArtifactId());
assertEquals("1.0", hit.getVersions().get(0));
}
use of org.apache.archiva.indexer.search.SearchFields 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.SearchFields 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.SearchFields 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