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

org.infrastructurebuilder.util.ServerProxy Maven / Gradle / Ivy

Go to download

Lightly encumberd Standard interfaces (and exceptions) for JVM-based applications within IB codebases

There is a newer version: 0.21.1
Show newest version
/**
 * Copyright © 2019 admin ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.infrastructurebuilder.util;

import static java.util.Objects.requireNonNull;
import static org.infrastructurebuilder.IBException.cet;
import static org.infrastructurebuilder.util.IBUtils.readFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

import org.infrastructurebuilder.util.artifacts.Identified;
import org.infrastructurebuilder.util.artifacts.JSONBuilder;
import org.infrastructurebuilder.util.artifacts.JSONOutputEnabled;
import org.json.JSONObject;

public class ServerProxy implements Identified, JSONOutputEnabled {
  public static final String ID_STRING = "id";
  private final String id;
  private final Optional keyPath;
  private final Optional passphrase;
  private final Optional principal;
  private final Optional secret;
  private final Optional filePermissions;
  private final Optional directoryPermissions;
  private final Optional configurationAsWrittenXMLString;

  public ServerProxy() {
    this("default", Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(),
        Optional.empty(), Optional.empty());
  }

  public ServerProxy(final String id, final Optional principal, final Optional secret,
      final Optional passphrase, final Optional keyPath, final Optional filePerms,
      final Optional directoryPerms, final Optional configAsXML) {
    super();
    this.id = requireNonNull(id);
    this.principal = requireNonNull(principal);
    this.secret = requireNonNull(secret);
    this.passphrase = requireNonNull(passphrase);
    this.keyPath = requireNonNull(keyPath);
    this.filePermissions = requireNonNull(filePerms);
    this.directoryPermissions = requireNonNull(directoryPerms);
    this.configurationAsWrittenXMLString = requireNonNull(configAsXML);
  }

  public BasicCredentials getBasicCredentials() {
    return new DefaultBasicCredentials(principal.orElse(null), secret);
  }

  public String getId() {
    return id;
  }

  public Optional getKeyPath() {
    return keyPath.filter(Files::isReadable);
  }

  public Optional getPassphrase() {
    return passphrase;
  }

  public Optional getPrincipal() {
    return principal;
  }

  public Optional getSecret() {
    return secret;
  }

  public Optional readKey() {
    return getKeyPath().map(f -> cet.withReturningTranslation(() -> readFile(f)));
  }

  public Optional getConfiguration() {
    return configurationAsWrittenXMLString;
  }

  public Optional getDirectoryPermissions() {
    return directoryPermissions;
  }

  public Optional getFilePermissions() {
    return filePermissions;
  }

  @Override
  public JSONObject asJSON() {
    return JSONBuilder.newInstance()
        // id
        .addString(ID_STRING, getId())
        //
        .addString("key", readKey())
        //
        .addString("principal", getPrincipal())
        //
        .addString("secret", getSecret())
        //
        .addString("configuration", getConfiguration())
        //
        .addString("directoryPermissions", getDirectoryPermissions())
        //
        .addString("filePermissions", getFilePermissions())
        // fin
        .asJSON();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy