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

com.google.firebase.FirebaseOptions Maven / Gradle / Ivy

Go to download

This is the official Firebase Admin Java SDK. Build extraordinary native JVM apps in minutes with Firebase. The Firebase platform can power your app’s backend, user authentication, static hosting, and more.

There is a newer version: 9.3.0
Show newest version
/*
 * Copyright 2017 Google Inc.
 *
 * 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.google.firebase;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.client.googleapis.util.Utils;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.base.Strings;
import com.google.firebase.auth.FirebaseCredential;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.auth.internal.BaseCredential;
import com.google.firebase.auth.internal.FirebaseCredentialsAdapter;
import com.google.firebase.internal.FirebaseThreadManagers;
import com.google.firebase.internal.NonNull;
import com.google.firebase.internal.Nullable;

import java.util.HashMap;
import java.util.Map;

/** Configurable Firebase options. */
public final class FirebaseOptions {

  // TODO: deprecate and remove it once we can fetch these from Remote Config.

  private final String databaseUrl;
  private final String storageBucket;
  private final GoogleCredentials credentials;
  private final Map databaseAuthVariableOverride;
  private final String projectId;
  private final HttpTransport httpTransport;
  private final JsonFactory jsonFactory;
  private final ThreadManager threadManager;

  private FirebaseOptions(@NonNull FirebaseOptions.Builder builder) {
    this.credentials = checkNotNull(builder.credentials,
        "FirebaseOptions must be initialized with setCredentials().")
        .createScoped(BaseCredential.FIREBASE_SCOPES);
    this.databaseUrl = builder.databaseUrl;
    this.databaseAuthVariableOverride = builder.databaseAuthVariableOverride;
    this.projectId = builder.projectId;
    this.storageBucket = builder.storageBucket;
    this.httpTransport = checkNotNull(builder.httpTransport,
        "FirebaseOptions must be initialized with a non-null HttpTransport.");
    this.jsonFactory = checkNotNull(builder.jsonFactory,
        "FirebaseOptions must be initialized with a non-null JsonFactory.");
    this.threadManager = checkNotNull(builder.threadManager,
        "FirebaseOptions must be initialized with a non-null ThreadManager");
  }

  /**
   * Returns the Realtime Database URL to use for data storage.
   *
   * @return The Realtime Database URL supplied via {@link Builder#setDatabaseUrl}.
   */
  public String getDatabaseUrl() {
    return databaseUrl;
  }

  /**
   * Returns the name of the Google Cloud Storage bucket used for storing application data.
   *
   * @return The cloud storage bucket name set via {@link Builder#setStorageBucket}
   */
  public String getStorageBucket() {
    return storageBucket;
  }

  GoogleCredentials getCredentials() {
    return credentials;
  }

  /**
   * Returns the auth variable to be used in Security Rules.
   *
   * @return The auth variable supplied via {@link
   *     Builder#setDatabaseAuthVariableOverride}.
   */
  public Map getDatabaseAuthVariableOverride() {
    return databaseAuthVariableOverride;
  }

  /**
   * Returns the Google Cloud project ID.
   *
   * @return The project ID set via {@link Builder#setProjectId(String)}
   */
  public String getProjectId() {
    return projectId;
  }

  /**
   * Returns the HttpTransport used to call remote HTTP endpoints. This transport is
   * used by all services of the SDK, except for FirebaseDatabase.
   *
   * @return A Google API client HttpTransport instance.
   */
  @NonNull
  public HttpTransport getHttpTransport() {
    return httpTransport;
  }

  /**
   * Returns the JsonFactory used to parse JSON when calling remote HTTP endpoints.
   *
   * @return A Google API client JsonFactory instance.
   */
  @NonNull
  public JsonFactory getJsonFactory() {
    return jsonFactory;
  }

  @NonNull
  ThreadManager getThreadManager() {
    return threadManager;
  }

  /**
   * Builder for constructing {@link FirebaseOptions}. 
   */
  public static final class Builder {

    private String databaseUrl;
    private String storageBucket;
    private GoogleCredentials credentials;
    private Map databaseAuthVariableOverride = new HashMap<>();
    private String projectId;
    private HttpTransport httpTransport = Utils.getDefaultTransport();
    private JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
    private ThreadManager threadManager = FirebaseThreadManagers.DEFAULT_THREAD_MANAGER;

    /** Constructs an empty builder. */
    public Builder() {}

    /**
     * Initializes the builder's values from the options object. *
     *
     * 

The new builder is not backed by this objects values, that is changes made to the new * builder don't change the values of the origin object. */ public Builder(FirebaseOptions options) { databaseUrl = options.databaseUrl; storageBucket = options.storageBucket; credentials = options.credentials; databaseAuthVariableOverride = options.databaseAuthVariableOverride; projectId = options.projectId; httpTransport = options.httpTransport; jsonFactory = options.jsonFactory; threadManager = options.threadManager; } /** * Sets the Realtime Database URL to use for data storage. * *

See * Initialize the SDK for code samples and detailed documentation. * * @param databaseUrl The Realtime Database URL to use for data storage. * @return This Builder instance is returned so subsequent calls can be chained. */ public Builder setDatabaseUrl(@Nullable String databaseUrl) { this.databaseUrl = databaseUrl; return this; } /** * Sets the name of the Google Cloud Storage bucket for reading and writing application data. * The same credential used to initialize the SDK (see {@link Builder#setCredential}) will be * used to access the bucket. * *

See * Initialize the SDK for code samples and detailed documentation. * * @param storageBucket The name of an existing Google Cloud Storage bucket. * @return This Builder instance is returned so subsequent calls can be chained. */ public Builder setStorageBucket(String storageBucket) { checkArgument(!Strings.isNullOrEmpty(storageBucket), "Storage bucket must not be null or empty"); this.storageBucket = storageBucket; return this; } /** * Sets the GoogleCredentials to use to authenticate the SDK. * *

See * Initialize the SDK for code samples and detailed documentation. * * @param credentials A * {@code GoogleCredentials} * instance used to authenticate the SDK. * @return This Builder instance is returned so subsequent calls can be chained. */ public Builder setCredentials(GoogleCredentials credentials) { this.credentials = checkNotNull(credentials); return this; } /** * Sets the FirebaseCredential to use to authenticate the SDK. * * @param credential A FirebaseCredential used to authenticate the SDK. See {@link * FirebaseCredentials} for default implementations. * @return This Builder instance is returned so subsequent calls can be chained. * @deprecated Use {@link FirebaseOptions.Builder#setCredentials(GoogleCredentials)}. */ public Builder setCredential(@NonNull FirebaseCredential credential) { checkNotNull(credential); if (credential instanceof BaseCredential) { this.credentials = ((BaseCredential) credential).getGoogleCredentials(); } else { this.credentials = new FirebaseCredentialsAdapter(credential); } return this; } /** * Sets the auth variable to be used by the Realtime Database rules. * *

When set, security rules for Realtime Database actions are evaluated using the provided * auth object. During evaluation the object is available on the auth variable. Use * this option to enforce schema validation and additional security for this app instance. * *

If this option is not provided, security rules are bypassed entirely for this app * instance. If this option is set to null, security rules are evaluated against an * unauthenticated user. That is, the auth variable is null. * *

See * Authenticate with limited privileges for code samples and detailed documentation. * * @param databaseAuthVariableOverride The value to use for the auth variable in * the security rules for Realtime Database actions. * @return This Builder instance is returned so subsequent calls can be chained. */ public Builder setDatabaseAuthVariableOverride( @Nullable Map databaseAuthVariableOverride) { this.databaseAuthVariableOverride = databaseAuthVariableOverride; return this; } /** * Sets the Google Cloud project ID that should be associated with an app. * * @param projectId A non-null, non-empty project ID string. * @return This Builder instance is returned so subsequent calls can be chained. */ public Builder setProjectId(@NonNull String projectId) { checkArgument(!Strings.isNullOrEmpty(projectId), "Project ID must not be null or empty"); this.projectId = projectId; return this; } /** * Sets the HttpTransport used to make remote HTTP calls. A reasonable default * is used if not explicitly set. The transport specified by calling this method is * used by all services of the SDK, except for FirebaseDatabase. * * @param httpTransport An HttpTransport instance * @return This Builder instance is returned so subsequent calls can be chained. */ public Builder setHttpTransport(HttpTransport httpTransport) { this.httpTransport = httpTransport; return this; } /** * Sets the JsonFactory used to parse JSON when making remote HTTP calls. A * reasonable default is used if not explicitly set. * * @param jsonFactory A JsonFactory instance. * @return This Builder instance is returned so subsequent calls can be chained. */ public Builder setJsonFactory(JsonFactory jsonFactory) { this.jsonFactory = jsonFactory; return this; } /** * Sets the ThreadManager used to initialize thread pools and thread factories * for Firebase apps. * * @param threadManager A ThreadManager instance. * @return This Builder instance is returned so subsequent calls can be chained. */ public Builder setThreadManager(ThreadManager threadManager) { this.threadManager = threadManager; return this; } /** * Builds the {@link FirebaseOptions} instance from the previously set options. * * @return A {@link FirebaseOptions} instance created from the previously set options. */ public FirebaseOptions build() { return new FirebaseOptions(this); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy