How to Use Java SDK

For using the SDK, your application need to call the method first. You need to initialize the SDK class and declare the callback variable to be able to call the method.

Basic Syntax

import com.safous.waap.ApiProtection;
import com.safous.waap.ApiProtectionCallBack;

import java.io.IOException;
import java.security.GeneralSecurityException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

// Initiate ApiProtection Class
ApiProtection apiProtection = new ApiProtection(CONFIG_FILE_PATH);

// Initiate ApiProtection Callback Class
ApiProtectionCallBack callBack = new ApiProtectionCallBack() {
@Override
public void onCompleted(OkHttpClient okHttpClient)
throws IOException, GeneralSecurityException
{
// Your logic
}
};

// Call ApiProtection Method
apiProtection.METHOD(callBack);

Initiate ApiProtection Class

Initialize ApiProtection class. The argument is the path to the config file. If omitted, safouswaap.ini under the current directory of the jar file is targeted.

For example,

ApiProtection apiProtection = new ApiProtection("/app/safouswaap.ini");

Initiate ApiProtection Callback Class

Initialize ApiProtection Callback class. Some ApiProtection methods have Callback as an argument. That method calls the onCompleted method if the process is successful. If your app requires additional processing, you can write the logic in this onCompleted method.

For example,

ApiProtectionCallBack callBack = new ApiProtectionCallBack() {
@Override
public void onCompleted(OkHttpClient okHttpClient)
throws IOException, GeneralSecurityException
{
// Your logic
}
};

The onComplete method takes OkHttpClient as its argument. Your API Endpoint is accessed using this OkHttpClient.

For example,

ApiProtectionCallBack callBack = new ApiProtectionCallBack() {
@Override
public void onCompleted(OkHttpClient okHttpClient)
throws IOException, GeneralSecurityException
{
Request request = new Request.Builder()
.url("https://simple-text.example.waap.safous.com")
       .get()
       .build();

Response response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println(response.body().string());
} else {
throw new IOException("HTTP Error: " + response.code());
}
}
};

Call ApiProtection Method

The Java SDK provides the following methods.

Category Method Argument
Registration registerDevice ApiProtectionCallBack
verifyDevice -
Transaction transaction ApiProtectionCallBack
terminate -

For example,

apiProtection.transaction(callBack);
apiProtection.terminate();

Device Registration and Verification

A device can be used as a client of Safous WAAP only after it is registered with Safous WAAP.

registerDevice

The method for device registration. This method alone does not complete the registration; it must be followed by a call to verifyDevice.

The onComplete method takes OkHttpClient and the device's ID as its argument. If your application needs to register user and so on, API Endpoint is accessed using this OkHttpClient.

For example,

ApiProtectionCallBack callBack = new ApiProtectionCallBack() {
@Override
public void onCompleted(OkHttpClient okHttpClient, String deviceId)
throws IOException, GeneralSecurityException, ReflectiveOperationException
{
// your logic
apiProtection.verifyDevice();
}
};

apiProtection.registerDevice(callBack);

verifyDevice

The method for device registration. This method must be called after registerDevice and can be called inside or outside of onComplete.

For example of inside onComplete,

ApiProtectionCallBack callBack = new ApiProtectionCallBack() {
@Override
public void onCompleted(OkHttpClient okHttpClient, String deviceId)
throws IOException, GeneralSecurityException, ReflectiveOperationException
{
// your logic
apiProtection.verifyDevice();
}
};

apiProtection.registerDevice(callBack);

For example of outside onComplete,

apiProtection.registerDevice(callBack);
apiProtection.verifyDevice();

The verifyDevice verifies Verify Token API

Transaction to API endpoints

Upon successful registration, clients will be able to access back-end API endpoints through the Safous WAAP SDK.

transaction

API endpoints are accessed using the transaction method.

The onComplete method takes OkHttpClient as its argument. API Endpoint is accessed using this OkHttpClient.

For example,

ApiProtectionCallBack callBack = new ApiProtectionCallBack() {
@Override
public void onCompleted(OkHttpClient okHttpClient)
throws IOException, GeneralSecurityException
{
Request request = new Request.Builder()
.url("https://simple-text.example.waap.safous.com")
.get()
.build();

Response response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println(response.body().string());
} else {
throw new IOException("HTTP Error: " + response.code());
}
}
};

apiProtection.transaction(callBack);

terminate

The terminate method terminates the session. It's used for logout, etc.

For example,

apiProtection.terminate();

Error handling

Each method of the SDK throws an error as is if an error occurs.

Therefore, the usual "try ... catch" syntax to handle errors.

For example,

ApiProtection apiProtection = new ApiProtection();

ApiProtectionCallBack callBack = new ApiProtectionCallBack() {
@Override
public void onCompleted(OkHttpClient okHttpClient)
throws IOException, GeneralSecurityException
{
// Your logic
}
};

try {
apiProtection.transaction(callBack);
} catch (Exception e) {
e.printStackTrace();
}