Example Java
Introduction and project setup
Java is a robust, object-oriented programming language known for its platform independence and strong type system, making it ideal for enterprise applications, web services, and large-scale systems. To manage project dependencies and build processes effectively, it’s best practice to use a build tool like Maven or Gradle. For this guide, we’ll use Maven. You can create a new Maven project by running mvn archetype:generate -DgroupId=com.example.omniac -DartifactId=omniac-client -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false in your terminal. This creates a standard project structure with a pom.xml file for dependency management. Navigate to your project directory with cd omniac-client. The Maven structure keeps your source code in src/main/java and tests in src/test/java, providing a clean and organized development environment that’s easily understood by IDEs and other developers.
Recommendation: IntelliJ IDEA for development
IntelliJ IDEA is an excellent choice for Java development, offering powerful features out of the box. After installing it, open your Maven project by selecting File > Open and choosing your project’s root directory (where the pom.xml file is located). IntelliJ will automatically detect the Maven project structure and download dependencies. The IDE provides excellent IntelliSense (code completion), built-in refactoring tools, and comprehensive debugging capabilities. You can run your application directly from the IDE using the green play button, or use the integrated terminal (Alt+F12) to execute Maven commands like mvn compile, mvn test, or mvn package. The built-in version control integration makes it easy to manage your Git repository. With features like automatic imports, code formatting, and intelligent error detection, IntelliJ IDEA provides a comprehensive development environment for building and testing your Java application efficiently.
Lets go
We highly recommend to use a unix based development environment. This is not only possible by using linux or macOS but also by installing Linux Subsystem for Windows. If you use a plain windows you might need to adapt in certain steps.
Setup Maven project
As shown above, we will use Maven for dependency management. If you haven’t created the project yet, execute the following commands:
mvn archetype:generate -DgroupId=com.example.omniac -DartifactId=omniac-client -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd omniac-client
Generate API code
As recommended in our documentation we use openapi-generator to generate the necessary code to interact with the API. This ensures safety and will break on critical errors as soon as possible.
First, add the OpenAPI Generator Maven plugin to your pom.xml:
<project>
<!-- ... existing configuration ... -->
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<!-- Additional dependencies required by generated code -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.6.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/openapi.yaml</inputSpec>
<generatorName>java</generatorName>
<output>${project.basedir}/target/generated-sources</output>
<apiPackage>com.example.omniac.api</apiPackage>
<modelPackage>com.example.omniac.model</modelPackage>
<generateSupportingFiles>true</generateSupportingFiles>
<invokerPackage>com.example.omniac.client</invokerPackage>
<configOptions>
<library>okhttp-gson</library>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Then copy your openapi.yaml file to the project root and generate the client:
mvn clean generate-sources
Now create a main class to configure the API client. Create src/main/java/com/example/omniac/OmniacClient.java:
package com.example.omniac;
import com.example.omniac.client.ApiClient;
import com.example.omniac.client.Configuration;
public class OmniacClient {
private static ApiClient apiClient;
public static void initializeClient(String apiKey) {
apiClient = Configuration.getDefaultApiClient();
apiClient.setBasePath("https://api.omniac.de");
apiClient.setApiKey(apiKey);
}
public static ApiClient getApiClient() {
return apiClient;
}
}
Get tenant info
Now you can initialize an API instance of the TenantAPI. Use the getTenant function of your API client to get information about your tenant.
package com.example.omniac;
import com.example.omniac.api.TenantApi;
import com.example.omniac.model.Tenant;
public class TenantExample {
public static void main(String[] args) {
// Initialize the client with your API key
OmniacClient.initializeClient("YOUR_API_KEY_GOES_HERE");
TenantApi tenantApi = new TenantApi(OmniacClient.getApiClient());
try {
Tenant tenant = tenantApi.getTenant();
System.out.println(tenant);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
Add profile to tenant
In order to start the monitoring of attributes and to receive alerts you need to configure a user for your tenant. After successfully creating the profile, you can start adding attributes.
package com.example.omniac;
import com.example.omniac.api.ProfileApi;
import com.example.omniac.model.Profile;
import com.example.omniac.model.ProfileCreateRequest;
import com.example.omniac.model.AttributeCreateRequest;
import com.example.omniac.model.AttributeType;
import java.util.Arrays;
import java.util.List;
public class ProfileExample {
public static void main(String[] args) {
// Initialize the client with your API key
OmniacClient.initializeClient("YOUR_API_KEY_GOES_HERE");
ProfileApi profileApi = new ProfileApi(OmniacClient.getApiClient());
// Create a new profile
ProfileCreateRequest newProfile = new ProfileCreateRequest()
.name("test")
.userLanguage("de");
try {
Profile profile = profileApi.createProfile(newProfile);
System.out.println("Created profile with ID: " + profile.getId());
// Add attributes to the profile
AttributeCreateRequest emailAttribute = new AttributeCreateRequest()
.type("email")
.value("test@example.com");
List<AttributeCreateRequest> attributes = Arrays.asList(emailAttribute);
Object apiResponse = profileApi.replaceAttributes(profile.getId(), attributes);
System.out.println("Attributes added: " + apiResponse);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
Create multiple profiles as a batch
In addition to the option of creating individual profiles, omniac Business also offers the ability to create up to 500 profiles with attributes at the same time.
package com.example.omniac;
import java.util.ArrayList;
import java.util.List;
import com.example.omniac.api.ProfileApi;
import com.example.omniac.model.AttributeCreateRequest;
import com.example.omniac.model.CreateProfiles201Response;
import com.example.omniac.model.ProfileCreateBatchRequest;
public static void main(String[] args) {
// Replace with a real API key or load from environment
OmniacClient.initializeClient("YOUR_API_KEY_GOES_HERE");
ProfileApi profileApi = new ProfileApi(OmniacClient.getApiClient());
List<ProfileCreateBatchRequest> batch = new ArrayList<>();
ProfileCreateBatchRequest req = new ProfileCreateBatchRequest()
.name("Test1")
.userLanguage("de");
// add attribute
req.addAttributesItem(new AttributeCreateRequest()
.type("email")
.value("test@example.com")
);
batch.add(req);
try {
CreateProfiles201Response resp = profileApi.createProfiles(batch);
System.out.println("CreateProfiles response: " + resp);
} catch (com.example.omniac.client.ApiException e) {
System.err.println("Error calling createProfiles: " + e.getMessage());
e.printStackTrace();
}
}
Get alerts
After waiting some minutes alerts will appear, if there are any leaks found. You can pull them by using the getProfile function with your profile ID. The two parameters after the profileID let you also return the users attributes (hashed and encrypted) as well as the alerts.
package com.example.omniac;
import com.example.omniac.api.ProfileApi;
import com.example.omniac.model.Profile;
public class AlertsExample {
public static void main(String[] args) {
// Initialize the client with your API key
OmniacClient.initializeClient("YOUR_API_KEY_GOES_HERE");
ProfileApi profileApi = new ProfileApi(OmniacClient.getApiClient());
try {
// Replace "profileID" with your actual profile ID
Profile profile = profileApi.getProfile("profileID", true, true);
System.out.println("Profile with alerts: " + profile);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
Provide a push alert endpoint
To get notified even faster, we provide a push API for new alerts. As soon as something is found, we will publish the alert into your system. This is done over an HTTP call. The delivery will be marked as done on our side as soon as your API returns a 2xx HTTP status code.
To use this feature, you need to contact us, so we can enable the feature for your tenant.