diff --git a/README.md b/README.md index 46dbd037..25c0be70 100644 --- a/README.md +++ b/README.md @@ -368,6 +368,25 @@ for (RepoPath searchItem : searchItems) { } ``` +##### Searching Files by GAVC and Virtual or Remote Repository + +```groovy +List results = artifactory.searches().artifactsByGavc() + .groupId("com.example") + .artifactId("com.example.test") + .repositories("maven-libs-release") + .specific() + .doSearch(); + +for (RepoPath searchItem : searchItems) { + String repoKey = searchItem.getRepoKey(); + String itemPath = searchItem.getItemPath(); +} +``` +* From Artifactory version 7.37.9, the following +&specific=true(default false) + attribute was added to support virtual and remote repositories. See [here](https://jfrog.com/help/r/jfrog-rest-apis/usage-for-remote-and-virtual-repositories). + ##### Searching Latest Version by GAVC and Repository ```groovy diff --git a/api/src/main/java/org/jfrog/artifactory/client/Searches.java b/api/src/main/java/org/jfrog/artifactory/client/Searches.java index f453bec6..28bc94b8 100644 --- a/api/src/main/java/org/jfrog/artifactory/client/Searches.java +++ b/api/src/main/java/org/jfrog/artifactory/client/Searches.java @@ -62,4 +62,7 @@ public interface Searches { PropertyFilters itemsByProperty(); List artifactsByFileSpec(FileSpec fileSpec); + + Searches specific(); + } diff --git a/api/src/main/java/org/jfrog/artifactory/client/model/SearchResultReport.java b/api/src/main/java/org/jfrog/artifactory/client/model/SearchResultReport.java index 3e9a7fef..bbfa92c5 100644 --- a/api/src/main/java/org/jfrog/artifactory/client/model/SearchResultReport.java +++ b/api/src/main/java/org/jfrog/artifactory/client/model/SearchResultReport.java @@ -9,12 +9,17 @@ public class SearchResultReport { private String uri; + private String downloadUri; private String created; public String getUri() { return uri; } + public String getDownloadUri() { + return downloadUri; + } + public String getCreated() { return created; } diff --git a/services/src/main/groovy/org/jfrog/artifactory/client/impl/SearchesImpl.groovy b/services/src/main/groovy/org/jfrog/artifactory/client/impl/SearchesImpl.groovy index 10fceacc..73658de3 100644 --- a/services/src/main/groovy/org/jfrog/artifactory/client/impl/SearchesImpl.groovy +++ b/services/src/main/groovy/org/jfrog/artifactory/client/impl/SearchesImpl.groovy @@ -96,6 +96,12 @@ class SearchesImpl implements Searches { this } + @Override + Searches specific() { + this.searchQuery << [specific: "true"] + this + } + List doSearch() { if (!searchUrl) { throw new IllegalArgumentException("Search url wasn't set. Please call one of the 'artifacts...' methods before calling 'search()'") @@ -122,9 +128,15 @@ class SearchesImpl implements Searches { String path = Util.getQueryPath("?", query) SearchResultImpl searchResults = artifactory.get("${getSearcherApi()}$url$path", SearchResultImpl, SearchResult) List pathList = new ArrayList<>(); + String fullPath; for (SearchResultReport searchResultReport : searchResults.getResults()) { String uri = searchResultReport.getUri(); - String fullPath = uri.split(baseApiPath + '/storage/')[1] + if (uri == null) { + uri = searchResultReport.getDownloadUri() + fullPath = uri.split('/artifactory/')[1] + } else { + fullPath = uri.split(baseApiPath + '/storage/')[1] + } String repo = fullPath.substring(0,fullPath.indexOf('/')) pathList.add(new RepoPathImpl(repo, fullPath - (repo + '/'))) } diff --git a/services/src/test/java/org/jfrog/artifactory/client/ArtifactoryTestsBase.java b/services/src/test/java/org/jfrog/artifactory/client/ArtifactoryTestsBase.java index 13b254a8..1401e0d7 100644 --- a/services/src/test/java/org/jfrog/artifactory/client/ArtifactoryTestsBase.java +++ b/services/src/test/java/org/jfrog/artifactory/client/ArtifactoryTestsBase.java @@ -10,7 +10,9 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.jfrog.artifactory.client.model.LocalRepository; +import org.jfrog.artifactory.client.model.RemoteRepository; import org.jfrog.artifactory.client.model.Repository; +import org.jfrog.artifactory.client.model.VirtualRepository; import org.jfrog.artifactory.client.model.repository.settings.impl.MavenRepositorySettingsImpl; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -20,7 +22,9 @@ import java.io.InputStreamReader; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Properties; import static org.apache.commons.codec.binary.Base64.encodeBase64; @@ -48,11 +52,15 @@ public abstract class ArtifactoryTestsBase { protected String fileMd5; protected String fileSha1; protected LocalRepository localRepository; + protected VirtualRepository virtualRepository; + protected RemoteRepository remoteRepository; protected String federationUrl; @BeforeClass public void init() throws IOException { String localRepositoryKey = "java-client-" + getClass().getSimpleName(); + String virtualRepositoryKey = "java-client-virtual-" + getClass().getSimpleName(); + String remoteRepositoryKey = "java-client-remote-" + getClass().getSimpleName(); Properties props = new Properties(); // This file is not in GitHub. Create your own in src/test/resources. InputStream inputStream = this.getClass().getResourceAsStream("/artifactory-client.properties"); @@ -76,6 +84,7 @@ public void init() throws IOException { .setPassword(password) .build(); deleteRepoIfExists(localRepositoryKey); + deleteRepoIfExists(virtualRepositoryKey); deleteRepoIfExists(getJCenterRepoName()); localRepository = artifactory.repositories().builders().localRepositoryBuilder() .key(localRepositoryKey) @@ -84,10 +93,34 @@ public void init() throws IOException { .propertySets(Arrays.asList("artifactory")) .build(); + Collection repositories = new ArrayList<>(); + repositories.add(localRepositoryKey); + virtualRepository = artifactory.repositories().builders().virtualRepositoryBuilder() + .key(virtualRepositoryKey) + .description("new virtual repository") + .repositorySettings(new MavenRepositorySettingsImpl()) + .repositories(repositories) + .build(); + if (!artifactory.repository(localRepository.getKey()).exists()) { artifactory.repositories().create(1, localRepository); } + if (!artifactory.repository(virtualRepository.getKey()).exists()) { + artifactory.repositories().create(1, virtualRepository); + } + + remoteRepository = artifactory.repositories().builders().remoteRepositoryBuilder() + .key(remoteRepositoryKey) + .url("https://repo1.maven.org/maven2/") + .description("new maven remote repository") + .repositorySettings(new MavenRepositorySettingsImpl()) + .build(); + + if (!artifactory.repository(remoteRepository.getKey()).exists()) { + artifactory.repositories().create(1, remoteRepository); + } + String jcenterRepoName = getJCenterRepoName(); if (!artifactory.repository(jcenterRepoName).exists()) { Repository jcenter = artifactory.repositories().builders().remoteRepositoryBuilder() diff --git a/services/src/test/java/org/jfrog/artifactory/client/SearchTests.java b/services/src/test/java/org/jfrog/artifactory/client/SearchTests.java index 195f642e..7c8f3c10 100644 --- a/services/src/test/java/org/jfrog/artifactory/client/SearchTests.java +++ b/services/src/test/java/org/jfrog/artifactory/client/SearchTests.java @@ -186,6 +186,34 @@ public void testSearchByGavcAndRepository() throws IOException { assertTrue(results.get(0).getItemPath().contains(artifactId + "-1.0.0-zip.jar")); } + @Test + public void testSearchByGavcAndVirtualRepository() throws IOException { + List results = artifactory.searches().artifactsByGavc() + .groupId("com.example") + .artifactId(artifactId) + .version("1.0.0") + .classifier("zip") + .repositories(virtualRepository.getKey()) + .specific() + .doSearch(); + assertEquals(results.size(), 1); + assertTrue(results.get(0).getItemPath().contains(artifactId + "-1.0.0-zip.jar")); + } + + @Test + public void testSearchByGavcAndRemoteRepository() throws IOException { + List results = artifactory.searches().artifactsByGavc() + .groupId("antlr") + .artifactId("antlr") + .version("2.7.1") + .classifier("jar") + .repositories(remoteRepository.getKey()) + .specific() + .doSearch(); + assertEquals(results.size(), 2); + assertTrue(results.get(0).getItemPath().contains("antlr-2.7.1.jar")); + } + @Test public void testArtifactsCreatedSinceSearch() throws IOException { long startTime = System.currentTimeMillis() - 86400000L;