/
JsonHelper.java
115 lines (97 loc) · 3.61 KB
/
JsonHelper.java
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
class JsonHelper {
static ObjectMapper mapper;
static {
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
private JsonHelper() {
}
static <T> T convertJsonToObject(final String json, final Class<T> tClass) {
try {
return mapper.readValue(json, tClass);
} catch (IOException e) {
throw new MsalClientException(e);
}
}
/**
* Throws exception if given String does not follow JSON syntax
*/
static void validateJsonFormat(String jsonString) {
try {
mapper.readTree(jsonString);
} catch (IOException e) {
throw new MsalClientException(e.getMessage(), AuthenticationErrorCode.INVALID_JSON);
}
}
/**
* Take a set of Strings and return a String representing a JSON object of the format:
* {
* "access_token": {
* "xms_cc": {
* "values": [ clientCapabilities ]
* }
* }
* }
*/
public static String formCapabilitiesJson(Set<String> clientCapabilities) {
if (clientCapabilities != null && !clientCapabilities.isEmpty()) {
ClaimsRequest cr = new ClaimsRequest();
RequestedClaimAdditionalInfo capabilitiesValues = new RequestedClaimAdditionalInfo(false, null, new ArrayList<>(clientCapabilities));
cr.requestClaimInAccessToken("xms_cc", capabilitiesValues);
return cr.formatAsJSONString();
} else {
return null;
}
}
/**
* Merges given JSON strings into one Jackson JSONNode object, which is returned as a String
*/
static String mergeJSONString(String mainJsonString, String addJsonString) {
JsonNode mainJson;
JsonNode addJson;
try {
mainJson = mapper.readTree(mainJsonString);
addJson = mapper.readTree(addJsonString);
} catch (IOException e) {
throw new MsalClientException(e.getMessage(), AuthenticationErrorCode.INVALID_JSON);
}
mergeJSONNode(mainJson, addJson);
return mainJson.toString();
}
/**
* Merges given Jackson JsonNode object into another JsonNode
*/
static void mergeJSONNode(JsonNode mainNode, JsonNode addNode) {
if (addNode == null) {
return;
}
Iterator<String> fieldNames = addNode.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode jsonNode = mainNode.get(fieldName);
if (jsonNode != null && jsonNode.isObject()) {
mergeJSONNode(jsonNode, addNode.get(fieldName));
} else {
if (mainNode instanceof ObjectNode) {
JsonNode value = addNode.get(fieldName);
((ObjectNode) mainNode).put(fieldName, value);
}
}
}
}
}