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

com.contentful.vault.SyncConfig Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018 Contentful GmbH
 *
 * 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 com.contentful.vault;

import com.contentful.java.cda.CDAClient;

import static com.contentful.vault.build.GeneratedBuildParameters.PROJECT_VERSION;
import static java.text.MessageFormat.format;

public final class SyncConfig {
  private final CDAClient client;

  private final boolean invalidate;

  SyncConfig(Builder builder) {
    this.invalidate = builder.invalidate;

    if (builder.client == null) {
      if (builder.accessToken == null) {
        throw new IllegalStateException("Cannot create a CDA client with no access token. " +
            "Please set it.");
      }

      if (builder.spaceId == null) {
        throw new IllegalStateException("Cannot create a CDA client with no space id. " +
            "Please set it.");
      }

      this.client = CDAClient
          .builder()
          .setToken(builder.accessToken)
          .setSpace(builder.spaceId)
          .setEnvironment(builder.environment)
          .setIntegration("Vault", PROJECT_VERSION)
          .build();
    } else {
      this.client = builder.client;
    }
  }

  public CDAClient client() {
    return client;
  }

  public boolean shouldInvalidate() {
    return invalidate;
  }

  public static Builder builder() {
    return new Builder();
  }

  public static class Builder {

    private static final String FIELD_ALREADY_EXISTS = "Do not set {0}, when {1} is already set. " +
            "Use either {0} or a previously added {1}.";

    CDAClient client;
    boolean invalidate;
    String accessToken;
    String spaceId;
    String environment;

    public Builder setAccessToken(String accessToken) {
      if (client != null) {
        throw new IllegalStateException(format(FIELD_ALREADY_EXISTS, "access token", "client"));
      }
      this.accessToken = accessToken;
      return this;
    }

    public Builder setSpaceId(String spaceId) {
      if (client != null) {
        throw new IllegalStateException(format(FIELD_ALREADY_EXISTS, "space id", "client"));
      }
      this.spaceId = spaceId;
      return this;
    }

    public Builder setEnvironment(String environment) {
      if (client != null) {
        throw new IllegalStateException(format(FIELD_ALREADY_EXISTS, "environment", "client"));
      }
      this.environment = environment;
      return this;
    }

    public Builder setClient(CDAClient client) {
      if (accessToken != null) {
        throw new IllegalStateException(format(FIELD_ALREADY_EXISTS, "client", "access token"));
      }
      if (spaceId != null) {
        throw new IllegalStateException(format(FIELD_ALREADY_EXISTS, "client", "space id"));
      }

      if (environment != null) {
        throw new IllegalStateException(format(FIELD_ALREADY_EXISTS, "client", "environment"));
      }

      this.client = client;
      return this;
    }

    public Builder setInvalidate(boolean invalidate) {
      this.invalidate = invalidate;
      return this;
    }

    public SyncConfig build() {
      return new SyncConfig(this);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy