Java - RapturePlatform/Rapture GitHub Wiki

Building Rapture Clients using Java

If we have a Rapture server running somewhere which presents and endpoint for the Rapture API - and RaptureAPIServer is one such application - we can use the Rapture Java Client API to connect to it and interact with it. This page describes how to do that.

Before you start

In these examples we use gradle (http://www.gradle.org as a build system. You can use your own approach to building Java applications, gradle is convenient as it provides a simple way of defining the dependencies and getting the version of the Rapture API available to your application.

Project set up

If we follow the gradle approach we should layout our project as follows:

  • There needs to be a file build.gradle at the top level of our project
  • Java source files go in src/main/java
  • Java resources go in src/main/resources

This page assumes we are following that approach.

build.gradle

The build.gradle of our sample application is reproduced below:

apply plugin: 'java'
apply plugin: 'application'
project.version = '3.0'

respositories {
   mavenLocal()
   mavenCentral()
   jcenter()
}

dependencies {
   compile "com.rapture:RaptureAPI:$project.platformVersion"
}

mainClassName = "com.demo.RaptureDemo"

This build file specifies a dependency on the Rapture API (at a given version) at defines the entry point for your application as the class 'com.demo.RaptureDemo'

RaptureDemo.java

RaptureDemo.java, which should be placed in src/main/java/com/demo/RaptureDemo.java needs to connect (login) to the environment and call methods in the API. In a real application the target location, user name and password will more than likely be passed as command line arguments or asked for in some UI. In this simple example we have them hardcoded.

package com.demo;
import rapture.common.client.HttpLoginApi;
import rapture.common.client.ScriptClient;
import rapture.common.client.SimpleCredentialsProvider;

public class RaptureDemo {
    public static void main(String[] args) {
         HttpLoginApi loginApi = new HttpLoginApi("http://testenv:8665", new SimpleCredentialsProvider('test','test'));
         loginApi.login();
         ScriptClient sc = new ScriptClient(loginApi);
         // Retrieve a document from Rapture
         String content = sc.getDoc().getContent("//testRepo/document1");
         System.out.println(content);
    }
} 

This artificial example shows the act of logging into a Rapture server on the machine "testenv" and logging in with the username "test" and password "test". Once logged in it attempts to read the document on Rapture referenced by the URI "//testRepo/document1".

The API calls throw unchecked exceptions (RaptureException) if calls fail which we are not catching here.