Tom Muldoon
Tom Muldoon

Reputation: 49

Looking for Artifactory Query Language example in Java

I'm trying to use the Artifactory REST API (and in particular, the Artifactory Query Language) from a Java client but the examples on the site are written in Groovy (snippets) and it's not immediately clear how a Java client would work.

A Java example that uses the REST API (or the Artifactory Java Client API) to execute an AQL query would be very much appreciated.

For what it's worth, here's a link to the Artifactory Query Language site...

https://www.jfrog.com/confluence/display/RTF/Artifactory+Query+Language

Upvotes: 1

Views: 3479

Answers (2)

Tom Muldoon
Tom Muldoon

Reputation: 49

@fundeldman Thanks for suggesting the answer above. After a few minor tweaks, the code is now working. Here's the latest and greatest...

String aqlQuery = "items.find({\"name\": {\"$match\" : \"*rup-receipt*\"}}).include(\"repo\", \"path\", \"name\")";
Artifactory artifactory = ArtifactoryClient.create(artifactoryUrl, username, password);
ArtifactoryRequest aqlRequest = new ArtifactoryRequestImpl()
            .method(ArtifactoryRequest.Method.POST)
            .apiUrl("api/search/aql")
            .requestType(ArtifactoryRequest.ContentType.TEXT)
            .responseType(ArtifactoryRequest.ContentType.JSON)
            .requestBody(aqlQuery);
Map<String, ?> aqlResponse = artifactory.restCall(aqlRequest);

Upvotes: 1

danf
danf

Reputation: 2709

The Artifactory Java Client does not support AQL queries natively. You can however use the generic Rest call interface it provides to create an ArtifactoryRequest pointing to the AQL API endpoint.

The examples in the wiki you linked are not in Groovy they are in AQL syntax - you simply construct a String of whatever query you'd like and send it as the body of the request - roughly something like

String aqlQuery = "items.find({\"name\": {\"$match\" : \"*test.*\"}})";
Artifactory artifactory = Artifactory.create(url, userName, password);
ArtifactoryRequest aqlRequest = new ArtifactoryRequestImpl()
            .method(ArtifactoryRequest.Method.POST)
            .apiUrl("/api/search/aql")
            .requestBody(aqlQuery);
 //Parse this string as json
 String aqlResponse = artifactory.restCall(aqlRequest);

The response is a JSON you can parse and work on - check the example from the API link I provided.

Upvotes: 8

Related Questions