All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.privacyone.sdk.keychain.client.BaseKeychainClient Maven / Gradle / Ivy

The newest version!
/*************************************************************************
 *
 * Privacy1 AB CONFIDENTIAL
 * ________________________
 *
 *  [2017] - [2020] Privacy1 AB
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains the property
 * of Privacy1 AB.  The intellectual and technical concepts contained herein
 * are proprietary to Privacy1 AB and may be covered by European, U.S. and Foreign
 * Patents, patents in process, and are protected by trade secret or copyright law.
 *
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden.
 */
package co.privacyone.sdk.keychain.client;

import co.privacyone.sdk.restapi.util.HttpBuilder;

import java.util.Map;
import java.util.Optional;

import com.google.common.collect.ImmutableMap;
import okhttp3.HttpUrl;

/**
 * BaseKeychainClient.
 * this client implements base http request operations.
 */
public abstract class BaseKeychainClient {

  private static final String PRIVACY_KEYCHAIN_PROXY = "proxy-keychain/";

  private static final String PRIVACY_CONSENT_KEY_ENTRY = "api/v1/keys/";
  private static final String PRIVACY_USER_ENTRY = "api/v1/users/";
  private static final String PRIVACY_CRYPTO_ENTRY = "api/v1/crypto/";
  private static final String PRIVACY_CONSENT_CATEGORY_ENTRY = "api/v1/consentcategories/";

  private static final String CONSENTKEY_USERKEY_ENDPOINT = "key";
  private static final String USER_LICENSE_ENDPOINT = "license";
  private static final String USER_SUSPENSION_ENDPOINT = "suspensions";
  private static final String USER_CREATE_ENDPOINT = "user";
  private static final String USER_BATCH_CREATE_ENDPOINT = "users";
  private static final String USER_EXISTENCE_CHECK_ENDPOINT = "user";
  private static final String USER_SOFT_DELETE_ENDPOINT = "user/soft";
  private static final String USER_HARD_DELETE_ENDPOINT = "user/hard";
  private static final String USER_DATA_CONTROL_ENDPOINT = "datacontrols";
  private static final String CRYPTO_ENCRYPT_ENDPOINT = "encrypt";
  private static final String CRYPTO_DECRYPT_ENDPOINT = "decrypt";

  private final String schema;
  private final String keysEntry;
  private final String usersEntry;
  private final String cryptoEntry;
  private final String consentCategoryEntry;

  private Optional consentCategoryUrl;
  private Optional consentKeyUrl;
  private Optional userLicenseUrl;
  private Optional userCreateUrl;
  private Optional userBatchCreateUrl;
  private Optional userExistenceCheckUrl;
  private Optional userSoftDeleteUrl;
  private Optional userHardDeleteUrl;
  private Optional cryptoEncryptUrl;
  private Optional cryptoDecryptUrl;

  /**
   * Constructor
   */
  public BaseKeychainClient(final String schema, final String version) {
    this.schema = schema;
    this.keysEntry = PRIVACY_CONSENT_KEY_ENTRY.replaceFirst("v1", version);;
    this.usersEntry = PRIVACY_USER_ENTRY.replaceFirst("v1", version);
    this.cryptoEntry = PRIVACY_CRYPTO_ENTRY.replaceFirst("v1", version);
    this.consentCategoryEntry = PRIVACY_CONSENT_CATEGORY_ENTRY.replaceFirst("v1", version);

    consentCategoryUrl = Optional.empty();
    consentKeyUrl = Optional.empty();
    userLicenseUrl = Optional.empty();
    userCreateUrl = Optional.empty();
    userBatchCreateUrl = Optional.empty();
    userExistenceCheckUrl = Optional.empty();
    userSoftDeleteUrl = Optional.empty();
    userHardDeleteUrl = Optional.empty();
    cryptoEncryptUrl = Optional.empty();
    cryptoDecryptUrl = Optional.empty();
  }


  /**
   * Get the url of user deletion.
   * @return url of user deletion request.
   */
  protected HttpUrl getUserLicenseUrl() {
    if (!userLicenseUrl.isPresent()) {
      synchronized (userLicenseUrl) {
        userLicenseUrl = Optional
            .of(buildEndpointUrl(this.usersEntry + USER_LICENSE_ENDPOINT));
      }
    }
    return userLicenseUrl.get();
  }

  /**
   * Get the url of getting consent category
   * @return entry point url
   */
  protected HttpUrl getConsentCategoryUrl() {
    if (!consentCategoryUrl.isPresent()) {
      synchronized (consentCategoryUrl) {
        consentCategoryUrl = Optional
            .of(buildEndpointUrl(consentCategoryEntry));
      }
    }
    return consentCategoryUrl.get();
  }
  /**
   * getConsentKeyUrl
   * @return
   */
  protected HttpUrl getConsentKeyUrl() {
    if (!consentKeyUrl.isPresent()) {
      synchronized (consentKeyUrl) {
        consentKeyUrl = Optional
            .of(buildEndpointUrl(keysEntry + CONSENTKEY_USERKEY_ENDPOINT));
      }
    }
    return consentKeyUrl.get();
  }

  /**
   * getUserCreateUrl
   * @return
   */
  protected HttpUrl getUserExistenceCheckUrl() {
    if (!userExistenceCheckUrl.isPresent()) {
      synchronized (userExistenceCheckUrl) {
        userExistenceCheckUrl = Optional.of(
            buildEndpointUrl(usersEntry + USER_EXISTENCE_CHECK_ENDPOINT)
        );
      }
    }
    return userExistenceCheckUrl.get();
  }

  /**
   * getUserCreateUrl
   * @return
   */
  protected HttpUrl getUserCreateUrl() {
    if (!userCreateUrl.isPresent()) {
      synchronized (userCreateUrl) {
        userCreateUrl = Optional.of(
            buildEndpointUrl(usersEntry + USER_CREATE_ENDPOINT)
        );
      }
    }
    return userCreateUrl.get();
  }

  /**
   * getUserCreateUrl
   * @return
   */
  protected HttpUrl getUserBatchCreateUrl() {
    if (!userBatchCreateUrl.isPresent()) {
      synchronized (userBatchCreateUrl) {
        userBatchCreateUrl = Optional.of(
            buildEndpointUrl(usersEntry + USER_BATCH_CREATE_ENDPOINT)
        );
      }
    }
    return userBatchCreateUrl.get();
  }

  /**
   * getUserSoftDeleteUrl
   * @return
   */
  protected HttpUrl getUserSoftDeleteUrl() {
    if (!userSoftDeleteUrl.isPresent()) {
      synchronized (userSoftDeleteUrl) {
        userSoftDeleteUrl = Optional.of(
            buildEndpointUrl(usersEntry + USER_SOFT_DELETE_ENDPOINT)
        );
      }
    }
    return userSoftDeleteUrl.get();
  }

  /**
   * getUserHardDeleteUrl
   * @return
   */
  protected HttpUrl getUserHardDeleteUrl() {
    if (!userHardDeleteUrl.isPresent()) {
      synchronized (userHardDeleteUrl) {
        userHardDeleteUrl = Optional.of(
            buildEndpointUrl(usersEntry + USER_HARD_DELETE_ENDPOINT)
        );
      }
    }
    return userHardDeleteUrl.get();
  }

  protected HttpUrl getCryptoEncryptUrl() {
    if (!cryptoEncryptUrl.isPresent()) {
      synchronized (cryptoEncryptUrl) {
        cryptoEncryptUrl = Optional.of(
            buildEndpointUrl(cryptoEntry + CRYPTO_ENCRYPT_ENDPOINT)
        );
      }
    }
    return cryptoEncryptUrl.get();
  }

  protected HttpUrl getCryptoDecryptUrl() {
    if (!cryptoDecryptUrl.isPresent()) {
      synchronized (cryptoDecryptUrl) {
        cryptoDecryptUrl = Optional.of(
            buildEndpointUrl(cryptoEntry + CRYPTO_DECRYPT_ENDPOINT)
        );
      }
    }
    return cryptoDecryptUrl.get();
  }

  protected HttpUrl buildGetDataControlsUrl(final String userId) {
    return buildEndpointUrl(usersEntry + userId + "/" + USER_DATA_CONTROL_ENDPOINT);
  }

  protected HttpUrl buildSetSuspensionUrl(final String userId, final Integer categoryId, boolean suspended) {
    return buildEndpointUrl(usersEntry + userId + "/" + USER_SUSPENSION_ENDPOINT,
            ImmutableMap.of("suspended", String.valueOf(suspended),
                    "categoryId", String.valueOf(categoryId)));
}

  protected HttpUrl buildGetCategoryLocalizationUrl(final int categoryId) {
    return buildEndpointUrl(consentCategoryEntry + categoryId);
  }

  private HttpUrl buildEndpointUrl(final String pathSegments) {
    if (this.schema.equals("https")) {
      return HttpBuilder.buildHttpsUrl(
              getKeychainHost(),
              getKeychainPort(),
              PRIVACY_KEYCHAIN_PROXY + pathSegments);
    }
    else {
      return HttpBuilder.buildHttpUrl(
              getKeychainHost(),
              getKeychainPort(),
              pathSegments
      );
    }
  }

  private HttpUrl buildEndpointUrl(final String pathSegments, Map parameters) {
    if (this.schema.equals("https")) {
      return HttpBuilder.buildHttpsUrl(
              getKeychainHost(),
              getKeychainPort(),
              PRIVACY_KEYCHAIN_PROXY + pathSegments,
              parameters);
    }
    else {
      return HttpBuilder.buildHttpUrl(
              getKeychainHost(),
              getKeychainPort(),
              pathSegments,
              parameters);
    }
  }
  public abstract String getKeychainHost();

  public abstract int getKeychainPort();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy