April 28, 2016

Artifactory Java Client API Examples

For its Artifactory product, JFrog offers a Java client API as a convenience to using its REST API but as far as I can tell, all of the REST API examples online are written in Groovy…

def T get(String path, Map query, ContentType
 responseContentType = ANY, 
          def responseClass = null, Map headers = null) {
    rest(GET, path, query, responseContentType, responseClass, ANY, null, headers)
  }

So, here’s a couple of quick examples written in Java (using version 0.17 of the Java client API) which leverage the Artifact and GAVC searches, respectively.

Example 1: Artifact Search (Quick Search)

First, I instantiate an instance of the ArtifactoryImpl class. The cast exists because, for some reason, the get, post, put, and delete methods are not declared on the Artifactory interface (which the class implements). Odd? I think so.

ArtifactoryImpl artifactoryImpl=(ArtifactoryImpl)
ArtifactoryClient.create(http://artifacts.company.com”);

I then set the query path…

String path = "/api/search/artifact";

And the query parameters…

Map queryMap = new HashMap();
 queryMap.put(name, pArtifactName);
 queryMap.put("repos", pArtifactoryRepo);

And the responseContentType…

 String responseContentType = groovyx.net.http.ContentType.JSON;

And the headers…

String usernamePassword = pUsername + ":" + pPassword;
String authorizationHeader = "Basic " +  Base64.getEncoder().encodeToString(usernamePassword.getBytes("iso-8859-1"));

Map headersMap = new HashMap();
headersMap.put("Authorization", authorizationHeader);

And finally, I invoke the ArtifactoryImpl.get method…

Object results = artifactoryImpl.get(path, queryMap, 
                    responseContentType, null, headersMap);

Example 2: GAVC Search

The Artifactory API also allows you to search by GroupId, ArtifactId, Version, and Classifier (GAVC) as well as using Artifact Search.

First, I instantiate an instance of the ArtifactoryImpl class. As in Example 1, the cast exists because the get, post, put, and delete methods are not declared on the Artifactory interface (which the class implements).

ArtifactoryImpl artifactoryImpl = (ArtifactoryImpl) 
 ArtifactoryClient.create(http://artifacts.company.com”);

I then set the query path…

String path = "/api/search/gavc";

And the query parameters…

Map queryMap = new HashMap();
queryMap.put(g, pArtifactGroup);
queryMap.put(a, pArtifactName);
queryMap.put(v, pArtifactVersion);
queryMap.put(c, pClassifier);
queryMap.put("repos", pArtifactoryRepo);

And the responseContentType…

String responseContentType = groovyx.net.http.ContentType.JSON;

And the headers…

String usernamePassword = pUsername + ":" + pPassword;
String authorizationHeader = "Basic " + Base64.getEncoder().
 encodeToString(usernamePassword.getBytes("iso-8859-1"));

Map headersMap = new HashMap();
headersMap.put("Authorization", authorizationHeader);

And finally, I invoke the ArtifactoryImpl.get method…

Object results = artifactoryImpl.get(path, queryMap, 
 responseContentType, null, headersMap);

References

For information regarding the Artifactory REST API and, in particular, the Artifact Search and GAVC Search:

Update 2016-05-02 JFrog has now updated the artifactory-client-java documentation with examples in Java. See the README for more information.

Tom Muldoon

Software Architect