-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathHealthResponse.java
More file actions
118 lines (101 loc) · 4.77 KB
/
HealthResponse.java
File metadata and controls
118 lines (101 loc) · 4.77 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
package com.bettercloud.vault.response;
import com.bettercloud.vault.VaultException;
import com.bettercloud.vault.json.Json;
import com.bettercloud.vault.json.JsonObject;
import com.bettercloud.vault.json.JsonValue;
import com.bettercloud.vault.rest.RestResponse;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
/**
* This class is a container for the information returned by Vault in <code>v1/sys/health</code>
* operations.
*/
public class HealthResponse implements Serializable {
private RestResponse restResponse;
private int retries;
private Boolean initialized;
private Boolean sealed;
private Boolean standby;
private Long serverTimeUTC;
private Boolean performanceStandby;
private String replicationPerformanceMode;
private String replicationDrMode;
/**
* <p>Constructs a <code>HealthResponse</code> object from the data received in a health
* check operation.</p>
*
* <p>Note that if the REST response is valid, but has an empty payload, this constructor
* will silently return an instance with <code>initialized</code>, <code>sealed</code>,
* <code>standby</code>, and <code>serverTimeUTC</code> set to <code>null</code>. This
* typically happens when you use optional parameters in the health call, to designate
* non-standard HTTP status codes. See docs for
* {@link com.bettercloud.vault.api.Debug#health(Boolean, Integer, Integer, Integer)}.</p>
*
* @param restResponse The raw HTTP response from Vault
* @param retries The number of retry attempts that occurred during the API call (can be zero)
* @throws VaultException If any error occurs or unexpected response is received from Vault
*/
public HealthResponse(final RestResponse restResponse, final int retries) throws VaultException {
this.restResponse = restResponse;
this.retries = retries;
if (restResponse == null) {
throw new VaultException("Response is null");
}
if (restResponse.getBody() == null) {
throw new VaultException("Response contains a bad payload", restResponse.getStatus());
}
if (restResponse.getBody().length > 0) {
final String mimeType = restResponse.getMimeType() == null ? "null" : restResponse.getMimeType();
if (!mimeType.equals("application/json")) {
throw new VaultException("Vault responded with MIME type: " + mimeType, restResponse.getStatus());
}
try {
final String jsonString = new String(restResponse.getBody(), StandardCharsets.UTF_8);//NOPMD
final JsonObject jsonObject = Json.parse(jsonString).asObject();
this.initialized = jsonObject.get("initialized") == null ? null : jsonObject.get("initialized").asBoolean();
this.sealed = jsonObject.get("sealed") == null ? null : jsonObject.get("sealed").asBoolean();
this.standby = jsonObject.get("standby") == null ? null : jsonObject.get("standby").asBoolean();
this.serverTimeUTC = jsonObject.get("server_time_utc") == null ? null : jsonObject.get("server_time_utc").asLong();
this.performanceStandby = jsonObject.get("performance_standby") == null ? null :
jsonObject.get("performance_standby").asBoolean();
this.replicationPerformanceMode = jsonObject.get("replication_performance_mode") == null ? null :
jsonObject.get("replication_performance_mode").asString();
this.replicationDrMode = jsonObject.get("replication_dr_mode") == null ? null :
jsonObject.get("replication_dr_mode").asString();
} catch (final Exception e) {
throw new VaultException("Unable to parse JSON payload: " + e, restResponse.getStatus());
}
}
}
public RestResponse getRestResponse() {
return restResponse;
}
public int getRetries() {
return retries;
}
public Boolean getInitialized() {
return initialized;
}
public Boolean getSealed() {
return sealed;
}
public Boolean getStandby() {
return standby;
}
/**
* @return A value representing the number of milliseconds since the epoch. With all of the changes in date API's between Java 8 and
* pre-Java 8, it seemed best for the library not to convert this value into any particular one.
*/
public Long getServerTimeUTC() {
return serverTimeUTC;
}
public Boolean getPerformanceStandby() {
return performanceStandby;
}
public String getReplicationPerformanceMode() {
return replicationPerformanceMode;
}
public String getReplicationDrMode() {
return replicationDrMode;
}
}