-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathLogicalResponse.java
More file actions
116 lines (100 loc) · 4.13 KB
/
LogicalResponse.java
File metadata and controls
116 lines (100 loc) · 4.13 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
package com.bettercloud.vault.response;
import com.bettercloud.vault.api.Logical;
import com.bettercloud.vault.json.Json;
import com.bettercloud.vault.json.JsonArray;
import com.bettercloud.vault.json.JsonObject;
import com.bettercloud.vault.json.JsonValue;
import com.bettercloud.vault.rest.RestResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
/**
* This class is a container for the information returned by Vault in logical API
* operations (e.g. read, write).
*/
public class LogicalResponse extends VaultResponse {
private static final Logger logger = Logger.getLogger(LogicalResponse.class.getCanonicalName());
private Map<String, String> data = new HashMap<>();
private List<String> listData = new ArrayList<>();
private JsonObject dataObject = null;
private String leaseId;
private Boolean renewable;
private Long leaseDuration;
/**
* @param restResponse The raw HTTP response from Vault.
* @param retries The number of retry attempts that occurred during the API call (can be zero).
* @param operation The operation requested.
*/
public LogicalResponse(final RestResponse restResponse, final int retries, final Logical.logicalOperations operation) {
super(restResponse, retries);
parseMetadataFields();
parseResponseData(operation);
}
public Map<String, String> getData() {
return data;
}
public List<String> getListData() {
return listData;
}
public JsonObject getDataObject() {
return dataObject;
}
public String getLeaseId() {
return leaseId;
}
public Boolean getRenewable() {
return renewable;
}
public Long getLeaseDuration() {
return leaseDuration;
}
private void parseMetadataFields() {
try {
final String jsonString = new String(getRestResponse().getBody(), StandardCharsets.UTF_8);
final JsonObject jsonObject = Json.parse(jsonString).asObject();
this.leaseId = jsonObject.get("lease_id").asString();
this.renewable = jsonObject.get("renewable").asBoolean();
this.leaseDuration = jsonObject.get("lease_duration").asLong();
} catch (Exception e) {
logger.warning("RestResponse parse exception:" + e.getMessage());
}
}
private void parseResponseData(final Logical.logicalOperations operation) {
try {
final String jsonString = new String(getRestResponse().getBody(), StandardCharsets.UTF_8);
JsonObject jsonObject = Json.parse(jsonString).asObject();
if (operation.equals(Logical.logicalOperations.readV2)) {
jsonObject = jsonObject.get("data").asObject();
}
data = new HashMap<>();
dataObject = jsonObject.get("data").asObject();
for (final JsonObject.Member member : dataObject) {
final JsonValue jsonValue = member.getValue();
if (jsonValue == null || jsonValue.isNull()) {
continue;
} else if (jsonValue.isString()) {
data.put(member.getName(), jsonValue.asString());
} else {
data.put(member.getName(), jsonValue.toString());
}
}
// For list operations convert the array of keys to a list of values
if (operation.equals(Logical.logicalOperations.listV1) || operation.equals(Logical.logicalOperations.listV2)) {
if (
getRestResponse().getStatus() != 404
&& data.get("keys") != null
) {
final JsonArray keys = Json.parse(data.get("keys")).asArray();
for (int index = 0; index < keys.size(); index++) {
listData.add(keys.get(index).asString());
}
}
}
} catch (Exception e) {
logger.warning("RestResponse parse exception:" + e.getMessage());
}
}
}