forked from firebase/firebase-admin-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicManagementResponse.java
More file actions
136 lines (120 loc) · 3.86 KB
/
TopicManagementResponse.java
File metadata and controls
136 lines (120 loc) · 3.86 KB
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
/*
* Copyright 2018 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.messaging;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import com.google.api.client.json.GenericJson;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.firebase.internal.NonNull;
import java.util.List;
import java.util.Map;
/**
* The response produced by FCM topic management operations.
*/
public class TopicManagementResponse {
private static final String UNKNOWN_ERROR = "unknown-error";
// Server error codes as defined in https://developers.google.com/instance-id/reference/server
// TODO: Should we handle other error codes here (e.g. PERMISSION_DENIED)?
private static final Map<String, String> ERROR_CODES = ImmutableMap.<String, String>builder()
.put("NOT_FOUND", "registration-token-not-registered")
.put("INTERNAL", "internal-error")
.build();
private final int successCount;
private final List<Error> errors;
TopicManagementResponse(List<GenericJson> results) {
checkArgument(results != null && !results.isEmpty(),
"unexpected response from topic management service");
int successCount = 0;
ImmutableList.Builder<Error> errors = ImmutableList.builder();
for (int i = 0; i < results.size(); i++) {
Map result = results.get(i);
if (result.isEmpty()) {
successCount++;
} else {
errors.add(new Error(i, (String) result.get("error")));
}
}
this.successCount = successCount;
this.errors = errors.build();
}
/**
* Gets the number of registration tokens that were successfully subscribed or unsubscribed.
*
* @return The number of successes.
*/
public int getSuccessCount() {
return successCount;
}
/**
* Gets the number of registration tokens that could not be subscribed or unsubscribed, and
* resulted in an error.
*
* @return The number of failures.
*/
public int getFailureCount() {
return errors.size();
}
/**
* Gets a list of errors encountered while executing the topic management operation.
*
* @return A non-null list.
*/
@NonNull
public List<Error> getErrors() {
return errors;
}
/**
* A topic management error.
*/
public static class Error {
private final int index;
private final String reason;
private Error(int index, String reason) {
this.index = index;
if (reason == null || reason.trim().isEmpty()) {
this.reason = UNKNOWN_ERROR;
} else {
this.reason = ERROR_CODES.getOrDefault(reason, reason.toLowerCase().replace('_', '-'));
}
}
/**
* Index of the registration token to which this error is related to.
*
* @return An index into the original registration token list.
*/
public int getIndex() {
return index;
}
/**
* String describing the nature of the error.
*
* @return A non-null, non-empty error message.
*/
@NonNull
public String getReason() {
return reason;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("index", index)
.add("reason", reason)
.toString();
}
}
}