OffloadingBrowserServlet.java
13.2 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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.ServletException
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.discovery.ClusterView
* org.apache.sling.discovery.DiscoveryService
* org.apache.sling.discovery.InstanceDescription
* org.apache.sling.discovery.InstanceFilter
* org.apache.sling.discovery.TopologyView
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.offloading.impl.servlets;
import com.adobe.granite.offloading.api.InstanceNetworkInfoService;
import com.adobe.granite.offloading.api.OffloadingTopicManager;
import com.adobe.granite.offloading.api.TopicInstancesHolder;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.discovery.ClusterView;
import org.apache.sling.discovery.DiscoveryService;
import org.apache.sling.discovery.InstanceDescription;
import org.apache.sling.discovery.InstanceFilter;
import org.apache.sling.discovery.TopologyView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(resourceTypes={"granite/offloading/components/view"}, methods={"GET"}, extensions={"json"}, selectors={"list"})
public class OffloadingBrowserServlet
extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(OffloadingBrowserServlet.class);
public static final String JSON_KEY_SLING_ID = "slingID";
public static final String JSON_KEY_INSTANCE_IP = "ip";
public static final String JSON_KEY_INSTANCE_PORT = "port";
public static final String JSON_KEY_INSTANCES = "instances";
public static final String JSON_KEY_DISABLED_DO_TO_WHITELISTING = "disabledDueToWhitelisting";
public static final String JSON_KEY_TOPICS = "topics";
public static final String JSON_KEY_TOPIC = "topic";
public static final String JSON_KEY_CLUSTER = "cluster";
public static final String JSON_KEY_INSTANCE_IS_LEADER = "isLeader";
public static final String JSON_KEY_INSTANCE_IS_LOCAL = "isLocal";
public static final String JSON_KEY_TOPIC_ENABLED = "topicEnabled";
public static final String JSON_KEY_TOPIC_EXCLUSIVE = "topicExclusive";
@Reference
private DiscoveryService ds;
@Reference
private OffloadingTopicManager otm;
@Reference
private InstanceNetworkInfoService inis;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String clusterID;
response.setContentType("application/json");
RequestPathInfo rpi = request.getRequestPathInfo();
String[] selectors = rpi.getSelectors();
JSONObject jsonResponse = new JSONObject();
if (selectors.length == 1) {
try {
this.prepareAllInstancesJSONResponse(jsonResponse);
}
catch (JSONException e) {
LOG.error("Unable to create all instances JSON response.", (Throwable)e);
}
} else if (selectors.length == 2) {
clusterID = selectors[1];
try {
this.prepareClusterDetailsJSONResponse(jsonResponse, clusterID);
}
catch (JSONException e) {
LOG.error("Unable to create cluster details JSON response for cluster with ID " + clusterID, (Throwable)e);
}
} else if (selectors.length == 3) {
clusterID = selectors[1];
String slingID = selectors[2];
try {
this.prepareInstanceDetailsJSONResponse(jsonResponse, clusterID, slingID);
}
catch (JSONException e) {
LOG.error("Unable to create instance details JSON response for instance with ID " + slingID, (Throwable)e);
}
}
PrintWriter out = response.getWriter();
out.write(jsonResponse.toString());
}
private void prepareAllInstancesJSONResponse(JSONObject jsonResponse) throws JSONException {
Map<String, TopicInstancesHolder> topicToInstancesMap = this.otm.getInstances();
if (!topicToInstancesMap.isEmpty()) {
TreeSet<String> topics = new TreeSet<String>(topicToInstancesMap.keySet());
JSONArray topicToInstancesArray = new JSONArray();
for (String topic : topics) {
int instancePort;
Set<String> ipSet;
String slingID;
String instanceIP;
JSONObject topicToInstances = new JSONObject();
topicToInstances.put("topic", (Object)topic);
TopicInstancesHolder holder = topicToInstancesMap.get(topic);
Set<InstanceDescription> enabledInstances = holder.getEnabledInstances();
Set<InstanceDescription> disabledInstances = holder.getDisabledInstances();
JSONArray instancesJSONArray = new JSONArray();
for (InstanceDescription instance2 : enabledInstances) {
slingID = instance2.getSlingId();
instanceIP = "";
instancePort = this.inis.getListeningPort(instance2);
ipSet = this.inis.getListeningAddresses(instance2);
if (!ipSet.isEmpty()) {
instanceIP = ipSet.iterator().next();
}
JSONObject instanceJSON = new JSONObject();
instanceJSON.put("slingID", (Object)slingID);
instanceJSON.put("ip", (Object)instanceIP);
instanceJSON.put("port", instancePort);
instanceJSON.put("cluster", (Object)instance2.getClusterView().getId());
instanceJSON.put("topicEnabled", true);
instanceJSON.put("disabledDueToWhitelisting", false);
instancesJSONArray.put((Object)instanceJSON);
}
for (InstanceDescription instance2 : disabledInstances) {
slingID = instance2.getSlingId();
instanceIP = "";
instancePort = this.inis.getListeningPort(instance2);
ipSet = this.inis.getListeningAddresses(instance2);
if (!ipSet.isEmpty()) {
instanceIP = ipSet.iterator().next();
}
Set<String> blacklist = this.otm.getBlacklistedTopics(instance2);
boolean disabledDueToWhitelisting = true;
for (String t : blacklist) {
if (!t.equals(topic)) continue;
disabledDueToWhitelisting = false;
break;
}
JSONObject instanceJSON = new JSONObject();
instanceJSON.put("slingID", (Object)slingID);
instanceJSON.put("ip", (Object)instanceIP);
instanceJSON.put("port", instancePort);
instanceJSON.put("cluster", (Object)instance2.getClusterView().getId());
instanceJSON.put("topicEnabled", false);
instanceJSON.put("disabledDueToWhitelisting", disabledDueToWhitelisting);
instancesJSONArray.put((Object)instanceJSON);
}
topicToInstances.put("instances", (Object)instancesJSONArray);
topicToInstancesArray.put((Object)topicToInstances);
}
jsonResponse.put("topics", (Object)topicToInstancesArray);
}
}
private void prepareClusterDetailsJSONResponse(JSONObject jsonResponse, String clusterID) throws JSONException {
TopologyView topologyView = this.ds.getTopology();
Set clusters = topologyView.getClusterViews();
ClusterView cluster = null;
for (ClusterView c : clusters) {
if (!clusterID.equals(c.getId())) continue;
cluster = c;
break;
}
if (cluster != null) {
JSONArray instancesArray = new JSONArray();
List instances = cluster.getInstances();
for (InstanceDescription instance : instances) {
JSONObject instanceJSON = new JSONObject();
instanceJSON.put("slingID", (Object)instance.getSlingId());
instanceJSON.put("isLeader", instance.isLeader());
instanceJSON.put("isLocal", instance.isLocal());
instancesArray.put((Object)instanceJSON);
}
jsonResponse.put("cluster", (Object)clusterID);
jsonResponse.put("instances", (Object)instancesArray);
}
}
private void prepareInstanceDetailsJSONResponse(JSONObject jsonResponse, String clusterID, final String instanceID) throws JSONException {
InstanceDescription instance;
InstanceFilter filter = new InstanceFilter(){
public boolean accept(InstanceDescription instanceDescription) {
return instanceID.equals(instanceDescription.getSlingId());
}
};
TopologyView topologyView = this.ds.getTopology();
Set filteredInstances = topologyView.findInstances(filter);
if (filteredInstances != null && filteredInstances.size() == 1 && (instance = (InstanceDescription)filteredInstances.toArray()[0]) != null) {
String slingID = instance.getSlingId();
String instanceIP = "";
int instancePort = this.inis.getListeningPort(instance);
Set<String> ipSet = this.inis.getListeningAddresses(instance);
if (!ipSet.isEmpty()) {
instanceIP = ipSet.iterator().next();
}
jsonResponse.put("slingID", (Object)slingID);
jsonResponse.put("ip", (Object)instanceIP);
jsonResponse.put("port", instancePort);
jsonResponse.put("cluster", (Object)clusterID);
String registeredTopicsString = instance.getProperty("com.adobe.granite.offloading.job.registeredtopics");
String whitelistTopicsString = instance.getProperty("job.consumermanager.whitelist");
String activeTopics = instance.getProperty("org.apache.sling.event.jobs.consumer.topics");
if (StringUtils.isNotEmpty((String)registeredTopicsString)) {
String[] registeredTopics = registeredTopicsString.split(",");
JSONArray topicsJSONArray = new JSONArray();
for (String topic : registeredTopics) {
boolean topicIsExclusive = this.isTopicIn(topic, whitelistTopicsString);
boolean topicIsEnabled = this.isTopicIn(topic, activeTopics);
JSONObject topicJSON = new JSONObject();
topicJSON.put("topic", (Object)topic);
topicJSON.put("topicEnabled", topicIsEnabled);
topicJSON.put("topicExclusive", topicIsExclusive);
topicsJSONArray.put((Object)topicJSON);
}
jsonResponse.put("topics", (Object)topicsJSONArray);
}
}
}
private boolean isTopicIn(String topic, String topicsString) {
if (StringUtils.isNotEmpty((String)topicsString)) {
String[] topics;
for (String t : topics = topicsString.split(",")) {
if (!topic.equals(t)) continue;
return true;
}
}
return false;
}
protected void bindDs(DiscoveryService discoveryService) {
this.ds = discoveryService;
}
protected void unbindDs(DiscoveryService discoveryService) {
if (this.ds == discoveryService) {
this.ds = null;
}
}
protected void bindOtm(OffloadingTopicManager offloadingTopicManager) {
this.otm = offloadingTopicManager;
}
protected void unbindOtm(OffloadingTopicManager offloadingTopicManager) {
if (this.otm == offloadingTopicManager) {
this.otm = null;
}
}
protected void bindInis(InstanceNetworkInfoService instanceNetworkInfoService) {
this.inis = instanceNetworkInfoService;
}
protected void unbindInis(InstanceNetworkInfoService instanceNetworkInfoService) {
if (this.inis == instanceNetworkInfoService) {
this.inis = null;
}
}
}