Using the Data Collector client to authenticate and invoke the REST API
Before you can use the REST API, you need to authenticate communications with Log Analysis. You can use the Data Collector client which, is available out of the box, to help you to do this. You can also use it to invoke the REST API and leverage public APIs.
Before you begin
- Ensure that you have at least a basic understanding of Java .
- Ensure that you are using Java 8.0 or later.
- Ensure that the Data Collector client that you use is from the same version of Log Analysis that you use for authentication. For example a Data Collector from 1.2 may not work with Log Analysis 1.3.
About this task
If you run your REST client on the same server where you installed Log Analysis, you do not need to complete steps 3 and 4 because the client certificate is already imported into the Java client that is installed with Log Analysis.
Procedure
What to do next
For example, imagine that you use a Java program called
MyClient.java
with 'main'
method
that is under a package called com.org.tools
.
You saved the Data Collector JAR file in the same directory as the
program. Go to the directory and run the following command:java -cp datacollector-client.jar:.: com.org.tools.MyClient
The
program runs and invokes the REST APIs that are exposed in the datacollectorclient.jar
file.To
invoke the REST API, declare and define the class member variable
for the REST Client APIs:
RestClient restClient = new RestClient(userID, password);
You
can also use the following public APIs. You can use the endpoint addresses
that you define in the Log Analysis REST
API as the
restAddr
value:public RequestResult postToServer(String restAddr, JSONObject json)
throws UnityException
public RequestResult putOnServer(String restAddr, JSONObject json)
public RequestResult getFromServer(String restAddr)
public RequestResult deleteOnServer(String deleteAddr)
To
load a batch of data into the Log Analysis server
hosted at
exmp123.example.com
, use the following
code: RestClient restClient = new RestClient("unityadmin","unityadmin");
JSONObject batch = ......
String restAddr = "https://exmp123.example.com:9987/Unity/DataCollector";
try {
restClient.postToServer(restAddr, batch);
}catch(UnityException ue){
System.err.println(ue.getMessage());
}