|
1 | 1 | package com.slack.api.app_backend.events; |
2 | 2 |
|
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import com.google.gson.JsonSyntaxException; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | + |
| 8 | +@Slf4j |
3 | 9 | public class EventTypeExtractorImpl implements EventTypeExtractor { |
4 | 10 |
|
5 | 11 | @Override |
6 | 12 | public String extractEventType(String json) { |
7 | | - StringBuilder sb = new StringBuilder(); |
8 | | - char[] chars = json.toCharArray(); |
9 | | - boolean isInsideEventData = false; |
10 | | - for (int idx = 0; idx < (chars.length - 7); idx++) { |
11 | | - if (!isInsideEventData && chars[idx] == '"' |
12 | | - && chars[idx + 1] == 'e' |
13 | | - && chars[idx + 2] == 'v' |
14 | | - && chars[idx + 3] == 'e' |
15 | | - && chars[idx + 4] == 'n' |
16 | | - && chars[idx + 5] == 't' |
17 | | - && chars[idx + 6] == '"' |
18 | | - && chars[idx + 7] == ':') { |
19 | | - idx = idx + 8; |
20 | | - isInsideEventData = true; |
21 | | - } |
22 | | - |
23 | | - if (isInsideEventData && chars[idx] == '"' |
24 | | - && chars[idx + 1] == 't' |
25 | | - && chars[idx + 2] == 'y' |
26 | | - && chars[idx + 3] == 'p' |
27 | | - && chars[idx + 4] == 'e' |
28 | | - && chars[idx + 5] == '"' |
29 | | - && chars[idx + 6] == ':') { |
30 | | - idx = idx + 7; |
31 | | - int doubleQuoteCount = 0; |
32 | | - boolean isPreviousCharEscape = false; |
33 | | - while (doubleQuoteCount < 2 && idx < chars.length) { |
34 | | - char c = chars[idx]; |
35 | | - if (c == '"' && !isPreviousCharEscape) { |
36 | | - doubleQuoteCount++; |
37 | | - } else { |
38 | | - if (doubleQuoteCount == 1) { |
39 | | - sb.append(c); |
40 | | - } |
41 | | - } |
42 | | - isPreviousCharEscape = c == '\\'; |
43 | | - idx++; |
44 | | - } |
45 | | - break; |
46 | | - } |
47 | | - } |
48 | | - return sb.toString(); |
| 13 | + return extractFieldUnderEvent(json, "type"); |
49 | 14 | } |
50 | 15 |
|
51 | 16 | @Override |
52 | 17 | public String extractEventSubtype(String json) { |
53 | | - StringBuilder sb = new StringBuilder(); |
54 | | - char[] chars = json.toCharArray(); |
55 | | - boolean isInsideEventData = false; |
56 | | - for (int idx = 0; idx < (chars.length - 7); idx++) { |
57 | | - if (!isInsideEventData && chars[idx] == '"' |
58 | | - && chars[idx + 1] == 'e' |
59 | | - && chars[idx + 2] == 'v' |
60 | | - && chars[idx + 3] == 'e' |
61 | | - && chars[idx + 4] == 'n' |
62 | | - && chars[idx + 5] == 't' |
63 | | - && chars[idx + 6] == '"' |
64 | | - && chars[idx + 7] == ':') { |
65 | | - idx = idx + 8; |
66 | | - isInsideEventData = true; |
67 | | - } |
| 18 | + return extractFieldUnderEvent(json, "subtype"); |
| 19 | + } |
68 | 20 |
|
69 | | - if (isInsideEventData && chars[idx] == '"' |
70 | | - && chars[idx + 1] == 's' |
71 | | - && chars[idx + 2] == 'u' |
72 | | - && chars[idx + 3] == 'b' |
73 | | - && chars[idx + 4] == 't' |
74 | | - && chars[idx + 5] == 'y' |
75 | | - && chars[idx + 6] == 'p' |
76 | | - && chars[idx + 7] == 'e' |
77 | | - && chars[idx + 8] == '"' |
78 | | - && chars[idx + 9] == ':') { |
79 | | - idx = idx + 10; |
80 | | - int doubleQuoteCount = 0; |
81 | | - boolean isPreviousCharEscape = false; |
82 | | - while (doubleQuoteCount < 2 && idx < chars.length) { |
83 | | - char c = chars[idx]; |
84 | | - if (c == '"' && !isPreviousCharEscape) { |
85 | | - doubleQuoteCount++; |
86 | | - } else { |
87 | | - if (doubleQuoteCount == 1) { |
88 | | - sb.append(c); |
89 | | - } |
| 21 | + private static String extractFieldUnderEvent(String json, String fieldName) { |
| 22 | + try { |
| 23 | + JsonElement root = JsonParser.parseString(json); |
| 24 | + if (root != null && root.isJsonObject() && root.getAsJsonObject().has("event")) { |
| 25 | + JsonElement event = root.getAsJsonObject().get("event"); |
| 26 | + if (event.isJsonObject() && event.getAsJsonObject().has(fieldName)) { |
| 27 | + JsonElement eventType = event.getAsJsonObject().get(fieldName); |
| 28 | + if (eventType.isJsonPrimitive()) { |
| 29 | + return eventType.getAsString(); |
90 | 30 | } |
91 | | - isPreviousCharEscape = c == '\\'; |
92 | | - idx++; |
93 | 31 | } |
94 | | - break; |
95 | 32 | } |
| 33 | + } catch (JsonSyntaxException e) { |
| 34 | + log.debug("Failed to parse {} as a JSON data", json, e); |
96 | 35 | } |
97 | | - return sb.toString(); |
| 36 | + return ""; |
98 | 37 | } |
99 | 38 |
|
100 | 39 | } |
0 commit comments