Anura Docs

Anura SDK for Java

The Anura SDK for Java makes it easy for developers to access Anura Direct within their Java code and begin analyzing their traffic. You can get started in minutes by installing the SDK with Maven, Gradle, or building and installing a JAR file from our source code.

Getting Started

  1. Have an open active account with Anura - You can see more about Anura's offerings here.
  2. Minimum Requirements - To use the SDK, you will need Java >=8.
  3. Install the SDK
  4. View our Quick Examples to immediately begin using the SDK!

Installing the SDK

With Maven

The recommended way to use the SDK in your project is by consuming it from Maven Central. You can add it to your project by adding the following:

pom.xml
<dependency>
  <groupId>io.anura</groupId>
  <artifactId>anura-sdk-java</artifactId>
  <version>2.0.0</version>
</dependency>

Building From Source

Once you clone the repository, navigate to /anura-sdk-java and build using the appropriate command according to your operating system:

Linux/Mac:

Terminal
./mvnw clean install

Windows:

Terminal
./mvnw.cmd clean install

Once installed, you are now able to use it within any of your Java projects by adding the installed JAR to your project.

Quick Examples

Create the Anura Direct client

// Import all SDK classes, exceptions, and the HashMap class
import io.anura.sdk.*;
import io.anura.sdk.exceptions.*;
import java.util.HashMap;
import java.io.IOException;
import java.util.Arrays;

// AnuraDirect constructor takes on parameter (a boolean) - whether to use HTTPS or not for API calls to Anura Direct.
// If false is given to the constructor, the API client will use HTTP instead of HTTPS.
AnuraDirect direct = new AnuraDirect("your-instance-id", true);

Create a DirectRequest object for AnuraDirect client

DirectRequestBuilder builder = new DirectRequestBuilder();
HashMap<Integer,String> additionalData = new HashMap<Integer,String>();
DirectRequest request = builder
    .setIpAddress("visitors-ip-address")                  // required
    .setSource("your-source-value")                       // optional
    .setCampaign("your-campaign-value")                   // optional
    .setUserAgent("visitors-user-agent")                  // optional
    .setApp("visitors-app-id")                            // optional
    .setDevice("visitors-device-id")                      // optional
    .setAdditionalData(additionalData)                    // optional
    .build();

Altering Additional Data

Any time you need to add/update your additional data, simply use the AdditionalData you passed as additional data:

// Adding an element
int indexToAdd = 1;
additionalData.put(indexToAdd, "your-data-value");

// Updating an element
int indexToUpdate = 1;
additionalData.put(indexToUpdate, "your-new-data-value");

Remove all Additional Data

To remove all additional data that you have added, call the clear() method on your additional data:

additionalData.clear();

Get a result from Anura Direct

DirectResult result;
try {
    result = direct.getResult(request);
} catch (IOException e) {
    // Handle IOException
} catch (InterruptedException e) {
    // Handle InterruptedException
} catch (AnuraClientException e) {
    // Handle any 4XX responses
} catch (AnuraServerException e) {
    // Handle any 5XX responses
} catch (AnuraException e) {
   /**
    * Handle any other exceptions that may have occurred.
    * Since AnuraClientException & AnuraServerException are children of AnuraException, 
    * feel free to remove those handling blocks if your handling logic is the same for both.
    */
}

if (result.isSuspect()) {
    // Perform some logic only for suspect visitors
}

if (result.isNonSuspect()) {
    // Perform some logic only for non-suspect visitors
}

if (result.isMobile()) {
    // Perform some logic only for visitors from mobile devices
}

String[] ruleSets = result.getRuleSets();
boolean isWebCrawler = (ruleSets != null && Arrays.asList(ruleSets).contains("WC"));
if (isWebCrawler) {
    // Perform some logic only for web crawlers
}

String anuraResult = result.getResult();
String invalidTrafficType = result.getInvalidTrafficType();

System.out.println("Visitor Result: " + anuraResult);
System.out.println("Invalid Traffic Type: " + invalidTrafficType);

API Reference

AnuraDirect

Can get results from Anura Direct. These results are fetched using Direct's /direct.json API endpoint.

Methods

DirectResult getResult(DirectRequest request) String getInstance() void setInstance(String instance) boolean isUseHttps() void setUseHttps(boolean useHttps)

DirectResult

The result upon a successful call to getResult() from the AnuraDirect client. It contains not only the result from Anura Direct, but some other methods to help you use the result.

Methods

boolean isSuspect() boolean isNonSuspect() Boolean isMobile() String getResult() String getRuleSets() String getInvalidTrafficType()

DirectRequest

A plain old java object (POJO) that represents an API request to be sent to the Anura Direct API.

Methods

String getSource() String getCampaign() String getIpAddress() String getUserAgent() String getApp() String getDevice() HashMap<Integer, String> getAdditionalData() void setSource(String source) void setCampaign(String campaign) void setIpAddress(String ipAddress) void setUserAgent(String userAgent) void setApp(String app) void setDevice(String device) void setAdditionalData(HashMap<Integer, String> additionalData)

DirectRequestBuilder

A builder class for creating a DirectRequest.

DirectRequest build() DirectRequestBuilder setSource(String source) DirectRequestBuilder setCampaign(String campaign) DirectRequestBuilder setIpAddress(String ipAddress) DirectRequestBuilder setUserAgent(String userAgent) DirectRequestBuilder setApp(String app) DirectRequestBuilder setDevice(String device) DirectRequestBuilder setAdditionalData(HashMap<Integer, String> additionalData)