-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealthRenderer.java
More file actions
59 lines (52 loc) · 1.76 KB
/
Copy pathHealthRenderer.java
File metadata and controls
59 lines (52 loc) · 1.76 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
package com.retailsvc.http.internal;
import com.retailsvc.http.Dependency;
import java.util.List;
/**
* Built-in JSON writer for the health endpoint wire shape. Keeps {@code Handlers} free of
* serialisation logic and removes the need for a JSON library on the classpath.
*/
public final class HealthRenderer {
/** Initial StringBuilder capacity sized for an outcome with one dependency (the common case). */
private static final int INITIAL_CAPACITY = 64;
private HealthRenderer() {}
public static String renderJson(boolean up, List<Dependency> dependencies) {
StringBuilder sb = new StringBuilder(INITIAL_CAPACITY);
sb.append("{\"outcome\":\"").append(label(up)).append("\",\"dependencies\":[");
for (int i = 0; i < dependencies.size(); i++) {
if (i > 0) {
sb.append(',');
}
Dependency d = dependencies.get(i);
sb.append("{\"id\":");
appendJsonString(sb, d.id());
sb.append(",\"status\":\"").append(label(d.up())).append("\"}");
}
return sb.append("]}").toString();
}
private static void appendJsonString(StringBuilder sb, String s) {
sb.append('"');
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '"' -> sb.append("\\\"");
case '\\' -> sb.append("\\\\");
case '\b' -> sb.append("\\b");
case '\f' -> sb.append("\\f");
case '\n' -> sb.append("\\n");
case '\r' -> sb.append("\\r");
case '\t' -> sb.append("\\t");
default -> {
if (c < 0x20) {
sb.append(String.format("\\u%04x", (int) c));
} else {
sb.append(c);
}
}
}
}
sb.append('"');
}
private static String label(boolean up) {
return up ? "Up" : "Down";
}
}