Skip to content

Instantly share code, notes, and snippets.

@sauthieg
Created October 6, 2015 07:39
Show Gist options
  • Save sauthieg/5135a0d9c976778f350a to your computer and use it in GitHub Desktop.
Save sauthieg/5135a0d9c976778f350a to your computer and use it in GitHub Desktop.
Demonstrate usage of ForgeRock CREST Http Client
import org.forgerock.http.handler.HttpClientHandler
import org.forgerock.services.context.RootContext
import static org.forgerock.json.JsonValue.json
import static org.forgerock.json.resource.Requests.*
import static org.forgerock.json.resource.http.CrestHttp.newConnectionFactory
import static org.forgerock.util.query.QueryFilter.alwaysTrue
def handler = new HttpClientHandler()
connection = newConnectionFactory(handler, new URI("http://localhost:8080/")).connection
def context = new RootContext()
// Clear all users just to make sure :)
connection.action(context, newActionRequest("users", "clear"))
// Create operation
def alice = [:]
alice.uid = "alice"
alice.email = "alice@example.com"
def bob = [:]
bob.uid = "bob"
bob.email = "bob@example.com"
[alice, bob].each {
connection.createAsync(context, newCreateRequest("users", (String) it.uid, json(it)))
.thenOnResult { println "Resource created $it.content" }.get()
}
// Asynchronous query operation
def query = newQueryRequest("users")
query.queryFilter = alwaysTrue()
query.additionalParameters._prettyPrint = 'true'
connection.queryAsync(context, query) {
println "Queried $it.content"
return true
}.thenFinally {
handler.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment