Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,25 @@ for (RepoPath searchItem : searchItems) {
}
```

##### Searching Files by GAVC and Virtual or Remote Repository

```groovy
List<RepoPath> 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
Expand Down
3 changes: 3 additions & 0 deletions api/src/main/java/org/jfrog/artifactory/client/Searches.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ public interface Searches {
PropertyFilters itemsByProperty();

List<AqlItem> artifactsByFileSpec(FileSpec fileSpec);

Searches specific();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ class SearchesImpl implements Searches {
this
}

@Override
Searches specific() {
this.searchQuery << [specific: "true"]
this
}

List<RepoPath> doSearch() {
if (!searchUrl) {
throw new IllegalArgumentException("Search url wasn't set. Please call one of the 'artifacts...' methods before calling 'search()'")
Expand All @@ -122,9 +128,15 @@ class SearchesImpl implements Searches {
String path = Util.getQueryPath("?", query)
SearchResultImpl searchResults = artifactory.get("${getSearcherApi()}$url$path", SearchResultImpl, SearchResult)
List<RepoPath> 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 + '/')))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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)
Expand All @@ -84,10 +93,34 @@ public void init() throws IOException {
.propertySets(Arrays.asList("artifactory"))
.build();

Collection<String> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RepoPath> 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<RepoPath> 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;
Expand Down