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

com.google.cloud.storage.NotificationInfo Maven / Gradle / Ivy

There is a newer version: 2.45.0
Show newest version
/*
 * Copyright 2020 Google LLC
 *
 * 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.cloud.storage;

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

import com.google.api.core.InternalApi;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/** The class representing Pub/Sub Notification metadata for the Storage. */
public class NotificationInfo implements Serializable {

  private static final long serialVersionUID = -996243512290027661L;
  private static final PathTemplate PATH_TEMPLATE =
      PathTemplate.createWithoutUrlEncoding("projects/{project}/topics/{topic}");

  // TODO: Change to StringEnum in next major version
  public enum PayloadFormat {
    JSON_API_V1,
    NONE
  }

  // TODO: Change to StringEnum in next major version
  public enum EventType {
    OBJECT_FINALIZE,
    OBJECT_METADATA_UPDATE,
    OBJECT_DELETE,
    OBJECT_ARCHIVE
  }

  private final String notificationId;
  private final String topic;
  private final List eventTypes;
  private final Map customAttributes;
  private final PayloadFormat payloadFormat;
  private final String objectNamePrefix;
  private final String etag;
  private final String selfLink;
  private final String bucket;

  /** Builder for {@code NotificationInfo}. */
  public abstract static class Builder {
    Builder() {}

    abstract Builder setNotificationId(String notificationId);

    public abstract Builder setSelfLink(String selfLink);

    public abstract Builder setTopic(String topic);

    public abstract Builder setPayloadFormat(PayloadFormat payloadFormat);

    public abstract Builder setObjectNamePrefix(String objectNamePrefix);

    public abstract Builder setEventTypes(EventType... eventTypes);

    public abstract Builder setEtag(String etag);

    public abstract Builder setCustomAttributes(Map customAttributes);

    abstract Builder setBucket(String bucket);

    /** Creates a {@code NotificationInfo} object. */
    public abstract NotificationInfo build();
  }

  /** Builder for {@code NotificationInfo}. */
  public static class BuilderImpl extends Builder {

    private String notificationId;
    private String topic;
    private List eventTypes;
    private Map customAttributes;
    private PayloadFormat payloadFormat;
    private String objectNamePrefix;
    private String etag;
    private String selfLink;
    private String bucket;

    BuilderImpl(String topic) {
      this.topic = topic;
    }

    BuilderImpl(NotificationInfo notificationInfo) {
      notificationId = notificationInfo.notificationId;
      etag = notificationInfo.etag;
      selfLink = notificationInfo.selfLink;
      topic = notificationInfo.topic;
      eventTypes = notificationInfo.eventTypes;
      customAttributes = notificationInfo.customAttributes;
      payloadFormat = notificationInfo.payloadFormat;
      objectNamePrefix = notificationInfo.objectNamePrefix;
      bucket = notificationInfo.bucket;
    }

    @Override
    Builder setNotificationId(String notificationId) {
      this.notificationId = notificationId;
      return this;
    }

    @Override
    public Builder setSelfLink(String selfLink) {
      this.selfLink = selfLink;
      return this;
    }

    /** Sets a topic in the format of "projects/{project}/topics/{topic}". */
    @Override
    public Builder setTopic(String topic) {
      this.topic = topic;
      return this;
    }

    @Override
    public Builder setPayloadFormat(PayloadFormat payloadFormat) {
      this.payloadFormat = payloadFormat;
      return this;
    }

    @Override
    public Builder setObjectNamePrefix(String objectNamePrefix) {
      this.objectNamePrefix = objectNamePrefix;
      return this;
    }

    @Override
    public Builder setEventTypes(EventType... eventTypes) {
      this.eventTypes = eventTypes != null ? Arrays.asList(eventTypes) : null;
      return this;
    }

    @Override
    public Builder setEtag(String etag) {
      this.etag = etag;
      return this;
    }

    @Override
    public Builder setCustomAttributes(Map customAttributes) {
      this.customAttributes =
          customAttributes != null ? ImmutableMap.copyOf(customAttributes) : null;
      return this;
    }

    @Override
    Builder setBucket(String bucket) {
      this.bucket = bucket;
      return this;
    }

    public NotificationInfo build() {
      checkNotNull(topic);
      checkTopicFormat(topic);
      return new NotificationInfo(this);
    }
  }

  NotificationInfo(BuilderImpl builder) {
    notificationId = builder.notificationId;
    etag = builder.etag;
    selfLink = builder.selfLink;
    topic = builder.topic;
    eventTypes = builder.eventTypes;
    customAttributes = builder.customAttributes;
    payloadFormat = builder.payloadFormat;
    objectNamePrefix = builder.objectNamePrefix;
    bucket = builder.bucket;
  }

  /** Returns the service-generated id for the notification. */
  public String getNotificationId() {
    return notificationId;
  }

  /** Returns the topic in Pub/Sub that receives notifications. */
  public String getTopic() {
    return topic;
  }

  /** Returns the canonical URI of this topic as a string. */
  public String getSelfLink() {
    return selfLink;
  }

  /** Returns the desired content of the Payload. */
  public NotificationInfo.PayloadFormat getPayloadFormat() {
    return payloadFormat;
  }

  /** Returns the object name prefix for which this notification configuration applies. */
  public String getObjectNamePrefix() {
    return objectNamePrefix;
  }

  /**
   * Returns HTTP 1.1 Entity tag for the notification. See Entity Tags
   */
  public String getEtag() {
    return etag;
  }

  /**
   * Returns the events that trigger a notification to be sent. If empty, notifications are
   * triggered by any event. See Event types to get
   * list of available events.
   */
  public List getEventTypes() {
    return eventTypes;
  }

  /**
   * Returns the list of additional attributes to attach to each Cloud PubSub message published for
   * this notification subscription.
   */
  public Map getCustomAttributes() {
    return customAttributes;
  }

  /**
   * gRPC has the bucket name encoded in the notification name, use this internal property to track
   * it.
   */
  @InternalApi
  String getBucket() {
    return bucket;
  }

  @Override
  public int hashCode() {
    return Objects.hash(
        notificationId,
        topic,
        eventTypes,
        customAttributes,
        payloadFormat,
        objectNamePrefix,
        etag,
        selfLink);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof NotificationInfo)) {
      return false;
    }
    NotificationInfo that = (NotificationInfo) o;
    return Objects.equals(notificationId, that.notificationId)
        && Objects.equals(topic, that.topic)
        && Objects.equals(eventTypes, that.eventTypes)
        && Objects.equals(customAttributes, that.customAttributes)
        && payloadFormat == that.payloadFormat
        && Objects.equals(objectNamePrefix, that.objectNamePrefix)
        && Objects.equals(etag, that.etag)
        && Objects.equals(selfLink, that.selfLink);
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).add("topic", topic).toString();
  }

  /**
   * Creates a {@code NotificationInfo} object for the provided topic.
   *
   * 

Example of creating the NotificationInfo object: * *

{@code
   * String topic = "projects/myProject/topics/myTopic"
   * NotificationInfo notificationInfo = NotificationInfo.of(topic)
   * }
* * @param topic a string in the format "projects/{project}/topics/{topic}" */ public static NotificationInfo of(String topic) { checkTopicFormat(topic); return newBuilder(topic).build(); } /** * Creates a {@code NotificationInfo} object for the provided topic. * * @param topic a string in the format "projects/{project}/topics/{topic}" */ public static Builder newBuilder(String topic) { checkTopicFormat(topic); return new BuilderImpl(topic); } /** Returns a builder for the current notification. */ public Builder toBuilder() { return new BuilderImpl(this); } Notification asNotification(Storage storage) { return new Notification(storage, new BuilderImpl(this)); } private static void checkTopicFormat(String topic) { // todo: why does this exist? PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy