PrivMX DOCS
Java

Getting Started

Initial Requirements

Before starting developing using PrivMX Endpoint Java follow our quick start guide.

To connect it to your environment, you need:

  • Bridge URL - URL address of the instance of your PrivMX Bridge
  • Solution ID - ID of the Solution provided by PrivMX Bridge during its initialization process
  • User Private Key - the private key from the user's public-private key pair

You also need to use your own application server to manage users (and their keys) and Contexts.

Installation

Our Java libraries contain all the necessary assets and helpers to get started with PrivMX Endpoint. Select which level is the best for you. Go to Java overview to see the descriptions of each library.

Java Dependencies

  1. Add mavenCentral() repository to your settings.gradle:
pluginManagement {
    repositories{
        mavenCentral()
    }
}

dependencyResolutionManagement{
    repositories{
        mavenCentral()
    }
}
  1. Add dependency to build.gradle:
dependencies {
    def privmxLibVersion = "2.2.0" // privmx-endpoint-version
    implementation("com.simplito.java:privmx-endpoint-extra:$privmxLibVersion")
    //implementation("com.simplito.java:privmx-endpoint:$privmxLibVersion")  #for base Java library
    //implementation("com.simplito.java:privmx-endpoint-android:$privmxLibVersion")  #for Android Java library
}
  1. Add PrivMX plugin to build.gradle:
plugins {
    id "com.simplito.privmx-endpoint-install-native" version "$pluginVersion"
}

Shared Libraries

The PrivMX plugin automatically adds the necessary native libraries to the runtime in your Java, Android, or Kotlin project. For Java applications, it includes libraries for all supported desktop platforms. For Android applications, it includes Android-specific binaries. The source code of the plugin is available on GitHub.

When you first use the API, the PrivMX wrapper unpacks and loads these native libraries. They are unpacked to your working directory if your java.library.path includes .. Otherwise, they are unpacked to the last path specified in the java.library.path.

Initial Assumptions

The initial assumptions for all the code examples below are as follows:

/*
    All the values below like BRIDGE_URL, SOLUTION_ID, CONTEXT_ID
    should be replaced by the ones corresponding to your Bridge Server instance.

    The private keys here are for demonstration purposes only.
    Normally, they should be kept separately by each user and stored in a safe place,
    or generated from a password (see the derivePrivateKey2() method in the Crypto API)
*/

String bridgeUrl = "YOUR_BRIDGE_URL";
String solutionId = "YOUR_SOLUTION_ID";
String contextId = "YOUR_CONTEXT_ID";

String user1Id = "USER_ID_1";
String user1PublicKey = "PUBLIC_KEY_1";
String user1PrivateKey = "PRIVATE_KEY_1";

String user2Id = "USER_ID_2";
String user2PublicKey = "PUBLIC_KEY_2";

Connecting to PrivMX Bridge

To use any of the library's elements, you must first connect to PrivMX Bridge. Use the API keys mentioned earlier:

String pathToCerts = "PATH_TO_CERTS"; // Path to .pem ssl certificate to connect with Privmx Bridge
Set<Modules> initModules = Set.of(
        Modules.THREAD, // initializes ThreadApi to working with Threads
        Modules.STORE, // initializes StoreApi to working with Stores
        Modules.INBOX // initializes InboxApi to working with Inboxes
        Modules.CUSTOM_EVENT // initializes EventApi to working with Custom Events
); // set of modules to activate in new connection

PrivmxEndpointContainer endpointContainer = new PrivmxEndpointContainer();
endpointContainer.setCertsPath(pathToCerts);

PrivmxEndpoint endpointSession = endpointContainer.connect(
        initModules,
        user1PrivateKey,
        solutionId,
        bridgeUrl
);

The active connection is kept by container and can be accessed using container.getEndpoint(Long) method.

endpointContainer.getEndpoint(endpointSession.connection.connectionId)

Disconnecting from PrivMX Bridge

endpointContainer.disconnect(endpointSession.connection.connectionId)

Verify Users

To verify whether users belong to the Context and the provided public key is valid, set a callback function that is triggered each time user interacts with a container. The verification process can be performed by an external service, such as your application server or PKI (Public Key Infrastructure) server.

Implement the UserVerifierInterface and provide your own custom verification logic within its verify method and pass it to setUserVerifier method.

val userVerifier: UserVerifierInterface = object : UserVerifierInterface {
    override fun verify(requests: List<VerificationRequest>): List<Boolean> {
        return requests.map { request ->
            // Your verification code for the request
            true
        }
    }
}

endpointSession.connection.setUserVerifier(userVerifier)

If the verify method does not confirm data then:

  • listing/getting containers/items will return object with a non-zero statusCode
  • modifying containers or items will throw an exception

By default, if no verifier is configured, the system operates as if a verifier is present and always returns successful verification (returns true).

Make sure the verify method always returns a valid, non-null result and doesn't throw exceptions.

Closing PrivmxEndpointContainer

When you finish working with the container, close it to end all connections, close the event loop, and release the resources being used. You can also use a try-with-resources block in Java or use function in Kotlin.

container.close()

Next Steps

With everything ready to go, now it's time to start using all of the platform's capabilities.

Learn how to use:

  • Threads - for exchanging encrypted messages;
  • Stores - for saving and sharing encrypted files;
  • Inboxes - for one way communication with external users.
  • KVDBs - for key-value database.

We use cookies on our website. We use them to ensure the proper functioning of the site and, if you agree, for purposes we set, such as analytics or marketing.

On this page