-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEventHubUtils.java
More file actions
305 lines (273 loc) · 11.6 KB
/
EventHubUtils.java
File metadata and controls
305 lines (273 loc) · 11.6 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Copyright (c) 2016 GE. All Rights Reserved.
* GE Confidential: Restricted Internal Distribution
*/
package com.ge.predix.eventhub;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ge.predix.eventhub.configuration.EventHubConfiguration;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import static com.ge.predix.eventhub.EventHubConstants.*;
/**
* H
*/
public class EventHubUtils {
private static final String protocolGRPC = "grpc";
// this map caches the instance details based on it's name so we don't have to search through VCAP_SERVICES again
private static ConcurrentHashMap<String, JsonNode> instanceFromConfiguration = new ConcurrentHashMap<String, JsonNode>();
public static EventHubConfiguration getEventHubDetailsFromVCAPS(String vcapServices, String serviceName) throws EventHubClientException.InvalidConfigurationException {
JsonNode eventHubInstance = findByServiceName(vcapServices, serviceName);
// Currently publish and subscribe details for Event Hub instance will be the same
JsonNode protocols = eventHubInstance.get("credentials").get("publish").get("protocol_details");
JsonNode grpcDetails = null;
// find grpc details
Iterator<JsonNode> protocolsItr = protocols.iterator();
while (protocolsItr.hasNext()) {
JsonNode protocol = protocolsItr.next();
if (protocol.get("protocol").asText().equals(protocolGRPC)) {
grpcDetails = protocol;
break;
}
}
if (grpcDetails == null) {
throw new EventHubClientException.InvalidConfigurationException(String
.format("Could not find grpc protocol details for Event Hub Service Instance with name: %s", serviceName));
}
String zoneID = eventHubInstance.get("credentials").get("publish").get("zone-http-header-value").asText();
String URI = grpcDetails.get("uri").asText();
if (URI.isEmpty()) {
throw new EventHubClientException.InvalidConfigurationException("Could not fine GRPC URI in VCAP_SERVICES");
}
if (zoneID.isEmpty()) {
throw new EventHubClientException.InvalidConfigurationException("Could not fine GRPC zoneID in VCAP_SERVICES");
}
EventHubConfiguration EHPartialConfig = new EventHubConfiguration.Builder()
.host(getHostFromURL(URI))
.port(getPortFromURL(URI))
.zoneID(zoneID)
.automaticTokenRenew(false) // required to build config without auth details
.build();
return EHPartialConfig;
}
public static String getUAADetailsFromVCAPS(String vcapServices, String serviceName) throws EventHubClientException.InvalidConfigurationException {
JsonNode uaaInstance = findByServiceName(vcapServices, serviceName);
return uaaInstance.get("credentials").get("issuerId").asText();
}
public static String getValueFromEnv(String envName) throws EventHubClientException.InvalidConfigurationException {
String value = System.getenv(envName);
String error = String.format("%s environment variable", envName);
return checkNotNull(value, error);
}
public static String checkNotNull(String value, String valueName) throws EventHubClientException.InvalidConfigurationException {
if (value == null || value.isEmpty()) {
String error = String.format("%s is not set", valueName);
throw new EventHubClientException.InvalidConfigurationException(error);
}
return value;
}
public static JsonNode findByServiceName(String vcapServices, String serviceName) throws EventHubClientException.InvalidConfigurationException {
if (instanceFromConfiguration.containsKey(serviceName)) {
return instanceFromConfiguration.get(serviceName);
}
ObjectMapper mapper = new ObjectMapper();
JsonNode json;
try {
json = mapper.readTree(vcapServices.getBytes());
} catch (Exception e) {
throw new EventHubClientException.InvalidConfigurationException("Could not parse VCAP_SERVICES. Perhaps it is not set?");
}
try {
// loop through services
Iterator<JsonNode> serviceItr = json.iterator();
while (serviceItr.hasNext()) {
JsonNode service = serviceItr.next();
// loop through service instances
Iterator<JsonNode> instanceItr = service.iterator();
while (instanceItr.hasNext()) {
JsonNode instance = instanceItr.next();
if (instance.get("name").asText().equals(serviceName)) {
instanceFromConfiguration.put(serviceName, instance);
return instance;
}
}
}
} catch (Exception e) {
throw new EventHubClientException.InvalidConfigurationException(String.format("Error finding instance with name: %s", serviceName));
}
throw new EventHubClientException.InvalidConfigurationException(String.format("Could not find instance with name: %s", serviceName));
}
private static String getHostFromURL(String url) {
String[] parts = url.split(":");
return parts[0];
}
private static int getPortFromURL(String url) {
String[] parts = url.split(":");
return Integer.parseInt(parts[1]);
}
public static boolean checkErrorAllowed(Throwable thrown, Throwable expected) {
return thrown.getCause() != null && thrown.getCause().equals(expected);
}
/**
* insert the key->value pairs into a json
* If the value is of certain types then use the make json functionality
* @param jsonObj the json object to be worked on
* @param key the (str) key of the value
* @param value the object of interest to be placed into the value
*/
private static void putIntoJson(JSONObject jsonObj, String key, Object value){
if(value instanceof JSONArray || value instanceof JSONObject){
jsonObj.put(key, value);
return;
}
if(value instanceof PublishResponse) {
jsonObj.put(key, makeJson((PublishResponse) value));
return;
}
if(value instanceof List && ((List) value).size()>0 && ((List) value).get(0) instanceof Message){
jsonObj.put(key, makeJson((List<Message>) value));
return;
}
if(value instanceof Message){
jsonObj.put(key, makeJson((Message) value));
return;
}
if(value instanceof Ack){
jsonObj.put(key, makeJson((Ack) value));
return;
}
if(value instanceof Throwable){
jsonObj.put(key, makeJson((Throwable) value));
return;
}
if(value instanceof String && ((String) value).length() > 0){
try {
if (((String) value).charAt(0) == '[') {
jsonObj.put(key, new JSONArray(new JSONTokener((String) value)));
} else if (((String) value).charAt(0) == '{') {
jsonObj.put(key, new JSONObject(new JSONTokener((String) value)));
} else {
jsonObj.put(key, value);
}
}catch (JSONException e){
jsonObj.put(key, value);
}
return;
}
if(value instanceof List){
JSONArray array = new JSONArray();
for(Object o : (List) value){
array.put(o);
}
jsonObj.put(key, new JSONArray(((List) value)));
return;
}
jsonObj.put(key, value);
}
/**
* turn a GRPC PublishResponse into a JSONArray of AcKs in the from of JSONObjecsts
* @param publishResponse the publish response to be converted
* @return JSONArray
*/
public static JSONArray makeJson(PublishResponse publishResponse){
JSONArray acks = new JSONArray();
for(Ack a: publishResponse.getAckList()){
acks.put(makeJson(a));
}
return acks;
}
/**
* Turn the GRPC Message into a JSONObject
* @param m message to be converted to JSON
* @return JSONObject
*/
public static JSONObject makeJson(Message m){
JSONObject message = new JSONObject();
message.put("id", m.getId());
message.put("body", m.getBody().toStringUtf8());
message.put("topic", m.getTopic());
message.put("partition", m.getPartition());
message.put("offset", m.getOffset());
message.put("zoneId", m.getZoneId());
return message;
}
/**
* Turn a list of GRPC messages into a JSONArray
* @param messages the list to be converted to a JSONArray
* @return JSONArray
*/
public static JSONArray makeJson(List<Message> messages){
JSONArray jsonMessages = new JSONArray();
for(Message m : messages){
jsonMessages.put(makeJson(m));
}
return jsonMessages;
}
public static JSONArray makeJson(SubscriptionMessage subscriptionMessage){
return makeJson(subscriptionMessage.getMessages().getMsgList());
}
/**
* Turn a GRPC Ack object into a JSONObject
* @param a Ack to be converted into a json object
* @return JSONObject
*/
public static JSONObject makeJson(Ack a){
JSONObject ackJson = new JSONObject();
ackJson.put("id", a.getId());
ackJson.put("partition", a.getPartition());
ackJson.put("offset", a.getOffset());
ackJson.put("topic", a.getTopic());
ackJson.put("zoneId", a.getZoneId());
ackJson.put("status", a.getStatusCode().toString());
return ackJson;
}
/**
* Turn a Throwable into a json
* @param t throwable to be converted into a JSONObject
* @return JSONObject
*/
public static JSONObject makeJson(Throwable t){
JSONObject throwableJson = new JSONObject();
throwableJson.put(EXCEPTION_NAME_KEY, t.getClass());
putIntoJson(throwableJson, EXCEPTION_MSG_KEY, t.getMessage());
if(t.getStackTrace().length >= 3)
throwableJson.put(EXCEPTION_STACK_TRACE_KEY, new JSONArray(Arrays.copyOfRange(t.getStackTrace(), 0, 2)));
else{
throwableJson.put(EXCEPTION_STACK_TRACE_KEY, new JSONArray(t.getStackTrace()));
}
return throwableJson;
}
/**
* Take in a list of objects and format them as a JSONObject
* if even number of values is supplied:
* {value0:value1, value2:value3}
*
* if odd number, it will use the first value as a key for a nesed obejct
* {value0: value1: value2, value3: value4}
* @param values values list to be converted into a JSONObject
* @return JSONObject
*/
public static JSONObject formatJson(Object ... values){
JSONObject json = new JSONObject();
int startIndex = values.length % 2;
if(startIndex == 1 ){
JSONObject innerJson = new JSONObject();
for(int i=1; i<values.length;i+=2){
putIntoJson(innerJson, values[i].toString(), values[i+1]);
}
json.put(String.valueOf(values[0]), innerJson);
}else{
for(int i=0;i<values.length;i+=2){
putIntoJson(json, values[i].toString(), values[i+1]);
}
}
return json;
}
}