|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.storage; |
| 17 | + |
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | + |
| 20 | +import com.google.api.pathtemplate.PathTemplate; |
| 21 | +import com.google.api.services.storage.model.Notification; |
| 22 | +import com.google.common.base.Function; |
| 23 | +import com.google.common.base.MoreObjects; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.common.collect.ImmutableMap; |
| 26 | +import java.io.Serializable; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +/** |
| 32 | + * Google Storage Notification metadata; |
| 33 | + * |
| 34 | + * @see <a href="https://cloud.google.com/storage/docs/concepts-techniques#concepts">Concepts and |
| 35 | + * Terminology</a> |
| 36 | + */ |
| 37 | +public class NotificationInfo implements Serializable { |
| 38 | + |
| 39 | + private static final long serialVersionUID = 5725883368559753810L; |
| 40 | + private static final PathTemplate PATH_TEMPLATE = |
| 41 | + PathTemplate.createWithoutUrlEncoding("projects/{project}/topics/{topic}"); |
| 42 | + |
| 43 | + public enum PayloadFormat { |
| 44 | + JSON_API_V1, |
| 45 | + NONE |
| 46 | + } |
| 47 | + |
| 48 | + static final Function<Notification, NotificationInfo> FROM_PB_FUNCTION = |
| 49 | + new Function<Notification, NotificationInfo>() { |
| 50 | + @Override |
| 51 | + public NotificationInfo apply(Notification pb) { |
| 52 | + return NotificationInfo.fromPb(pb); |
| 53 | + } |
| 54 | + }; |
| 55 | + static final Function<NotificationInfo, Notification> TO_PB_FUNCTION = |
| 56 | + new Function<NotificationInfo, Notification>() { |
| 57 | + @Override |
| 58 | + public Notification apply(NotificationInfo NotificationInfo) { |
| 59 | + return NotificationInfo.toPb(); |
| 60 | + } |
| 61 | + }; |
| 62 | + private final String generatedId; |
| 63 | + private final String topic; |
| 64 | + private final List<String> eventTypes; |
| 65 | + private final Map<String, String> customAttributes; |
| 66 | + private final PayloadFormat payloadFormat; |
| 67 | + private final String objectNamePrefix; |
| 68 | + private final String etag; |
| 69 | + private final String selfLink; |
| 70 | + |
| 71 | + public static final class Builder { |
| 72 | + |
| 73 | + private String generatedId; |
| 74 | + private String topic; |
| 75 | + private List<String> eventTypes; |
| 76 | + private Map<String, String> customAttributes; |
| 77 | + private PayloadFormat payloadFormat; |
| 78 | + private String objectNamePrefix; |
| 79 | + private String etag; |
| 80 | + private String selfLink; |
| 81 | + |
| 82 | + Builder(String topic) { |
| 83 | + this.topic = topic; |
| 84 | + } |
| 85 | + |
| 86 | + Builder(NotificationInfo NotificationInfo) { |
| 87 | + generatedId = NotificationInfo.generatedId; |
| 88 | + etag = NotificationInfo.etag; |
| 89 | + selfLink = NotificationInfo.selfLink; |
| 90 | + topic = NotificationInfo.topic; |
| 91 | + eventTypes = NotificationInfo.eventTypes; |
| 92 | + customAttributes = NotificationInfo.customAttributes; |
| 93 | + payloadFormat = NotificationInfo.payloadFormat; |
| 94 | + objectNamePrefix = NotificationInfo.objectNamePrefix; |
| 95 | + } |
| 96 | + |
| 97 | + Builder setGeneratedId(String generatedId) { |
| 98 | + this.generatedId = generatedId; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + Builder setSelfLink(String selfLink) { |
| 103 | + this.selfLink = selfLink; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** The name of the topic. It must have the format "projects/{project}/topics/{topic}". */ |
| 108 | + public Builder setTopic(String topic) { |
| 109 | + this.topic = topic; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public Builder setPayloadFormat(PayloadFormat payloadFormat) { |
| 114 | + this.payloadFormat = payloadFormat; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + public Builder setObjectNamePrefix(String objectNamePrefix) { |
| 119 | + this.objectNamePrefix = objectNamePrefix; |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + public Builder setEventTypes(Iterable<String> eventTypes) { |
| 124 | + this.eventTypes = eventTypes != null ? ImmutableList.copyOf(eventTypes) : null; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + Builder setEtag(String etag) { |
| 129 | + this.etag = etag; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + public Builder setCustomAttributes(Map<String, String> customAttributes) { |
| 134 | + this.customAttributes = |
| 135 | + customAttributes != null ? ImmutableMap.copyOf(customAttributes) : null; |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + public NotificationInfo build() { |
| 140 | + checkNotNull(topic); |
| 141 | + return new NotificationInfo(this); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + NotificationInfo(Builder builder) { |
| 146 | + generatedId = builder.generatedId; |
| 147 | + etag = builder.etag; |
| 148 | + selfLink = builder.selfLink; |
| 149 | + topic = builder.topic; |
| 150 | + eventTypes = builder.eventTypes; |
| 151 | + customAttributes = builder.customAttributes; |
| 152 | + payloadFormat = builder.payloadFormat; |
| 153 | + objectNamePrefix = builder.objectNamePrefix; |
| 154 | + } |
| 155 | + |
| 156 | + /** Returns the service-generated id for the notification. */ |
| 157 | + public String getGeneratedId() { |
| 158 | + return generatedId; |
| 159 | + } |
| 160 | + |
| 161 | + /** Returns the topic to which this subscription publishes. */ |
| 162 | + public String getTopic() { |
| 163 | + return topic; |
| 164 | + } |
| 165 | + |
| 166 | + /** Returns the canonical URI of this topic as a string. */ |
| 167 | + public String getSelfLink() { |
| 168 | + return selfLink; |
| 169 | + } |
| 170 | + |
| 171 | + /** Returns the desired content of the Payload. */ |
| 172 | + public PayloadFormat getPayloadFormat() { |
| 173 | + return payloadFormat; |
| 174 | + } |
| 175 | + |
| 176 | + /** Returns the object name prefix for which this notification configuration applies. */ |
| 177 | + public String getObjectNamePrefix() { |
| 178 | + return objectNamePrefix; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Returns HTTP 1.1 Entity tag for the notification. |
| 183 | + * |
| 184 | + * @see <a href="http://tools.ietf.org/html/rfc2616#section-3.11">Entity Tags</a> |
| 185 | + */ |
| 186 | + public String getEtag() { |
| 187 | + return etag; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Returns the list of event types that this notification will apply to. If empty, notifications |
| 192 | + * will be sent on all event types. |
| 193 | + * |
| 194 | + * @see <a href="https://cloud.google.com/storage/docs/cross-origin">Cross-Origin Resource Sharing |
| 195 | + * (CORS)</a> |
| 196 | + */ |
| 197 | + public List<String> getEventTypes() { |
| 198 | + return eventTypes; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Returns the list of additional attributes to attach to each Cloud PubSub message published for |
| 203 | + * this notification subscription. |
| 204 | + * |
| 205 | + * @see <a href="https://cloud.google.com/storage/docs/access-control#About-Access-Control-Lists"> |
| 206 | + * About Access Control Lists</a> |
| 207 | + */ |
| 208 | + public Map<String, String> getCustomAttributes() { |
| 209 | + return customAttributes; |
| 210 | + } |
| 211 | + |
| 212 | + /** Returns a builder for the current notification. */ |
| 213 | + public Builder toBuilder() { |
| 214 | + return new Builder(this); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public int hashCode() { |
| 219 | + return Objects.hash(getTopic()); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public boolean equals(Object obj) { |
| 224 | + return obj == this |
| 225 | + || obj != null |
| 226 | + && obj.getClass().equals(NotificationInfo.class) |
| 227 | + && Objects.equals(toPb(), ((NotificationInfo) obj).toPb()); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public String toString() { |
| 232 | + return MoreObjects.toStringHelper(this).add("topic", getTopic()).toString(); |
| 233 | + } |
| 234 | + |
| 235 | + Notification toPb() { |
| 236 | + Notification notificationPb = new Notification(); |
| 237 | + notificationPb.setId(generatedId); |
| 238 | + notificationPb.setEtag(etag); |
| 239 | + if (customAttributes != null) { |
| 240 | + notificationPb.setCustomAttributes(customAttributes); |
| 241 | + } |
| 242 | + if (eventTypes != null) { |
| 243 | + notificationPb.setEventTypes(eventTypes); |
| 244 | + } |
| 245 | + if (objectNamePrefix != null) { |
| 246 | + notificationPb.setObjectNamePrefix(objectNamePrefix); |
| 247 | + } |
| 248 | + if (payloadFormat != null) { |
| 249 | + notificationPb.setPayloadFormat(payloadFormat.toString()); |
| 250 | + } else { |
| 251 | + notificationPb.setPayloadFormat(PayloadFormat.NONE.toString()); |
| 252 | + } |
| 253 | + notificationPb.setSelfLink(selfLink); |
| 254 | + notificationPb.setTopic(topic); |
| 255 | + |
| 256 | + return notificationPb; |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Creates a {@code NotificationInfo} object for the provided topic name. |
| 261 | + * |
| 262 | + * @param topic The name of the topic. It must have the format |
| 263 | + * "projects/{project}/topics/{topic}". |
| 264 | + */ |
| 265 | + public static NotificationInfo of(String topic) { |
| 266 | + PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format"); |
| 267 | + return newBuilder(topic).build(); |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Returns a {@code NotificationInfo} builder where the topic's name is set to the provided name. |
| 272 | + * |
| 273 | + * @param topic The name of the topic. It must have the format |
| 274 | + * "projects/{project}/topics/{topic}". |
| 275 | + */ |
| 276 | + public static Builder newBuilder(String topic) { |
| 277 | + PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format"); |
| 278 | + return new Builder(topic); |
| 279 | + } |
| 280 | + |
| 281 | + static NotificationInfo fromPb(Notification notificationPb) { |
| 282 | + Builder builder = newBuilder(notificationPb.getTopic()); |
| 283 | + if (notificationPb.getId() != null) { |
| 284 | + builder.setGeneratedId(notificationPb.getId()); |
| 285 | + } |
| 286 | + if (notificationPb.getEtag() != null) { |
| 287 | + builder.setEtag(notificationPb.getEtag()); |
| 288 | + } |
| 289 | + if (notificationPb.getCustomAttributes() != null) { |
| 290 | + builder.setCustomAttributes(notificationPb.getCustomAttributes()); |
| 291 | + } |
| 292 | + if (notificationPb.getSelfLink() != null) { |
| 293 | + builder.setSelfLink(notificationPb.getSelfLink()); |
| 294 | + } |
| 295 | + if (notificationPb.getObjectNamePrefix() != null) { |
| 296 | + builder.setObjectNamePrefix(notificationPb.getObjectNamePrefix()); |
| 297 | + } |
| 298 | + if (notificationPb.getTopic() != null) { |
| 299 | + builder.setTopic(notificationPb.getTopic()); |
| 300 | + } |
| 301 | + if (notificationPb.getEventTypes() != null) { |
| 302 | + builder.setEventTypes(notificationPb.getEventTypes()); |
| 303 | + } |
| 304 | + if (notificationPb.getPayloadFormat() != null) { |
| 305 | + builder.setPayloadFormat(PayloadFormat.valueOf(notificationPb.getPayloadFormat())); |
| 306 | + } |
| 307 | + return builder.build(); |
| 308 | + } |
| 309 | +} |
0 commit comments