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

com.shapestone.authentication.client.AuthenticationClient Maven / Gradle / Ivy

The newest version!
package com.shapestone.authentication.client;

import com.shapestone.rest.jersey.hystrix.rx.HystrixRxCommand;
import com.shapestone.rest.jersey.hystrix.rx.HystrixRxMethod;
import com.shapestone.authentication.Credential;
import org.glassfish.jersey.client.rx.rxjava.RxObservable;
import rx.Observable;

import javax.ws.rs.client.Client;
import javax.ws.rs.core.GenericType;

import java.util.List;

import static java.util.UUID.randomUUID;
import static javax.ws.rs.client.Entity.entity;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

/**
 * Name: Michael Williams
 * Date: 9/11/16.
 */
public class AuthenticationClient {

  private AuthenticationClientData authenticationClientData;
  private Client client;

  public AuthenticationClient(AuthenticationClientData authenticationClientData, Client client) {
    this.authenticationClientData = authenticationClientData;
    this.client = client;
  }

  public Observable addCredential(Credential credential) {
    return this.addCredential(credential, null);
  }

  public Observable addCredential(Credential credential, String correlationId) {
    final HystrixRxMethod> hystrixMethod = () ->
        RxObservable.from(client.target(authenticationClientData.getUrl()))
            .path("/authentication/credentials")
            .request()
            .header("correlationId", determineCorrelationId(correlationId))
            .rx()
            .post(entity(credential, APPLICATION_JSON), Credential.class);
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, "addCredential").observe();
  }

  public Observable> getCredentialsByIds(List credentialIds) {
    return getCredentialsByIds(credentialIds, null);
  }

  public Observable> getCredentialsByIds(List credentialIds, String correlationId) {
    final HystrixRxMethod>> hystrixMethod = () ->
      RxObservable.from(client.target(authenticationClientData.getUrl()))
          .path("/authentication/credentials")
          .queryParam("credentialIds", credentialIds.toArray())
          .request()
          .header("correlationId", determineCorrelationId(correlationId))
          .rx()
          .get(new GenericType>() {});
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, "getCredential").observe();
  }

  public Observable getCredentialByPartyId(String partyId, String companyPartyId) {
    return getCredentialByPartyId(partyId, companyPartyId, null);
  }

  public Observable getCredentialByPartyId(String partyId, String companyPartyId, String correlationId) {
    final HystrixRxMethod> hystrixMethod = () ->
        RxObservable.from(client.target(authenticationClientData.getUrl()))
            .path("/authentication/credentials/parties/{partyId}")
            .resolveTemplate("partyId", partyId)
            .request()
            .header("companyPartyId", companyPartyId)
            .header("correlationId", determineCorrelationId(correlationId))
            .rx()
            .get(Credential.class);
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, "getCredential").observe();
  }

  public Observable> getCredentialByOwnerPartyId(String ownerPartyId, String correlationId) {
    final HystrixRxMethod>> hystrixMethod = () ->
        RxObservable.from(client.target(authenticationClientData.getUrl()))
            .path("/authentication/credentials/parties/{ownerPartyId}")
            .resolveTemplate("ownerPartyId", ownerPartyId)
            .request()
            .header("ownerPartyId", ownerPartyId)
            .header("correlationId", determineCorrelationId(correlationId))
            .rx()
            .get(new GenericType>(){});
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, "getCredentialByOwnerPartyId").observe();
  }

  public Observable getCredentialById(String credentialId) {
    return this.getCredentialById(credentialId, null);
  }

  public Observable getCredentialById(String credentialId, String correlationId) {
    final HystrixRxMethod> hystrixMethod = () ->
        RxObservable.from(client.target(authenticationClientData.getUrl()))
            .path("/authentication/credentials/{credentialId}")
            .resolveTemplate("credentialId", credentialId)
            .request()
            .header("correlationId", determineCorrelationId(correlationId))
            .rx()
            .get(Credential.class);
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, "getCredential").observe();
  }

  public Observable validate(Credential credential) {
    return this.validate(credential, null);
  }

  public Observable validate(Credential credential, String correlationId) {
    final HystrixRxMethod> hystrixMethod = () ->
      RxObservable.from(client.target(authenticationClientData.getUrl()))
        .path("/authentication/credentials/validate")
        .request()
        .header("correlationId", determineCorrelationId(correlationId))
        .rx()
        .post(entity(credential, APPLICATION_JSON), Boolean.class);
    final String commandKey = "validate";
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, commandKey).observe();
  }

  public Observable credentialExists(Credential credential) {
    return this.credentialExists(credential, null);
  }

  public Observable credentialExists(Credential credential, String correlationId) {
    final HystrixRxMethod> hystrixMethod = () ->
      RxObservable.from(client.target(authenticationClientData.getUrl()))
        .path("/authentication/credentials/exists")
        .request()
        .header("correlationId", determineCorrelationId(correlationId))
        .rx()
        .post(entity(credential, APPLICATION_JSON), Boolean.class);
    final String commandKey = "credentialExists";
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, commandKey).observe();
  }

  public Observable notExistsAndValid(Credential credential, String ownerPartyId) {
    return this.notExistsAndValid(credential, ownerPartyId,null);
  }

  public Observable notExistsAndValid(Credential credential, String ownerPartyId, String correlationId) {
    final HystrixRxMethod> hystrixMethod = () ->
      RxObservable.from(client.target(authenticationClientData.getUrl()))
        .path("/authentication/credentials/not-exists-and-valid")
        .request()
        .header("ownerPartyId", ownerPartyId)
        .header("correlationId", determineCorrelationId(correlationId))
        .rx()
        .post(entity(credential, APPLICATION_JSON), Boolean.class);
    final String commandKey = "notExistsAndValid";
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, commandKey).observe();
  }

  public Observable usernameExists(String username) {
    return usernameExists(username, null);
  }

  public Observable usernameExists(String username, String correlationId) {
    final Credential credential = new Credential(username);
    return usernameExists(credential);
  }

  public Observable usernameExists(Credential credential) {
    return usernameExists(credential, null);
  }

  public Observable usernameExists(Credential credential, String correlationId) {
    final HystrixRxMethod> hystrixMethod = () ->
      RxObservable.from(client.target(authenticationClientData.getUrl()))
        .path("/authentication/credentials/username-exists")
        .request()
        .header("correlationId", determineCorrelationId(correlationId))
        .rx()
        .post(entity(credential, APPLICATION_JSON), Boolean.class);
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, "validate").observe();
  }

  public Observable authenticate(Credential credential) {
    return this.authenticate(credential, null);
  }

  public Observable authenticate(Credential credential, String correlationId) {
    final HystrixRxMethod> hystrixMethod = () ->
      RxObservable.from(client.target(authenticationClientData.getUrl()))
        .path("/authentication/credentials/authenticate")
        .request()
        .header("correlationId", determineCorrelationId(correlationId))
        .rx()
        .post(entity(credential, APPLICATION_JSON), Credential.class);
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, "authenticate").observe();
  }

  public Observable deleteCredentialByPartyId(String partyId, String customerPartyId, String correlationId) {
    final HystrixRxMethod> hystrixMethod = () ->
        RxObservable.from(client.target(authenticationClientData.getUrl()))
            .path("/authentication/credentials/parties/{partyId}")
            .resolveTemplate("partyId", partyId)
            .request()
            .header("customerPartyId", customerPartyId)
            .header("correlationId", determineCorrelationId(correlationId))
            .rx()
            .delete(Boolean.class);
    return new HystrixRxCommand<>(authenticationClientData, hystrixMethod, "deleteCredentialByPartyId").observe();
  }

  private String determineCorrelationId(String correlationId) {
    return correlationId != null ? correlationId : randomUUID().toString();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy