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