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

io.scalecube.services.auth.ConnectionSetup Maven / Gradle / Ivy

package io.scalecube.services.auth;

import io.scalecube.utils.MaskUtil;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Collectors;

public final class ConnectionSetup implements Externalizable {

  private static final long serialVersionUID = 1L;

  private Map credentials = Collections.emptyMap();

  /**
   * Constructor for de/serialization purpose.
   *
   * @deprecated exposed only for de/serialization purpose.
   */
  @Deprecated
  public ConnectionSetup() {}

  /**
   * Constructor.
   *
   * @param credentials credentials (not null)
   */
  public ConnectionSetup(Map credentials) {
    this.credentials =
        Collections.unmodifiableMap(
            new HashMap<>(Objects.requireNonNull(credentials, "ConnectionSetup.credentials")));
  }

  public Map credentials() {
    return credentials;
  }

  public boolean hasCredentials() {
    return !credentials.isEmpty();
  }

  @Override
  public String toString() {
    return new StringJoiner(", ", ConnectionSetup.class.getSimpleName() + "[", "]")
        .add("credentials=" + mask(credentials))
        .toString();
  }

  private static Map mask(Map creds) {
    return creds.entrySet().stream()
        .collect(Collectors.toMap(Entry::getKey, entry -> MaskUtil.mask(entry.getValue())));
  }

  @Override
  public void writeExternal(ObjectOutput out) throws IOException {
    // credentials
    out.writeInt(credentials.size());
    for (Entry entry : credentials.entrySet()) {
      out.writeUTF(entry.getKey());
      out.writeObject(entry.getValue()); // value is nullable
    }
  }

  @Override
  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    // credentials
    int credsSize = in.readInt();
    Map creds = new HashMap<>(credsSize);
    for (int i = 0; i < credsSize; i++) {
      String key = in.readUTF();
      String value = (String) in.readObject(); // value is nullable
      creds.put(key, value);
    }
    this.credentials = Collections.unmodifiableMap(creds);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy