|
2 | 2 |
|
3 | 3 | import java.util.regex.Matcher; |
4 | 4 | import java.util.regex.Pattern; |
5 | | -import javax.json.Json; |
6 | | -import javax.json.JsonException; |
7 | | -import javax.json.JsonPointer; |
8 | | -import javax.json.JsonStructure; |
9 | 5 | import io.apimatic.core.types.CoreApiException; |
10 | 6 | import io.apimatic.core.utilities.CoreHelper; |
11 | 7 | import io.apimatic.coreinterfaces.http.Context; |
@@ -139,48 +135,41 @@ private String replaceHeadersFromTemplate(HttpHeaders headers, String reason) { |
139 | 135 |
|
140 | 136 | private String replaceBodyFromTemplate(String responseBody, String reason) { |
141 | 137 | StringBuilder formatter = new StringBuilder(reason); |
142 | | - JsonStructure jsonStructure = CoreHelper.createJsonStructure(responseBody); |
143 | 138 | Matcher matcher = Pattern.compile("\\{(.*?)\\}").matcher(reason); |
144 | 139 | while (matcher.find()) { |
145 | 140 | String key = matcher.group(1); |
146 | 141 | String pointerKey = key; |
147 | | - replaceBodyString(responseBody, formatter, jsonStructure, key, pointerKey); |
| 142 | + replaceBodyString(responseBody, formatter, key, pointerKey); |
148 | 143 | } |
149 | 144 | return formatter.toString().replace("\"", ""); |
150 | 145 | } |
151 | 146 |
|
152 | 147 | private void replaceBodyString(String responseBody, StringBuilder formatter, |
153 | | - JsonStructure jsonStructure, String key, String pointerKey) { |
| 148 | + String key, String pointerKey) { |
154 | 149 | if (pointerKey.startsWith("$response.body")) { |
155 | 150 | String formatKey = String.format("{%s}", key); |
156 | 151 | int index = formatter.indexOf(formatKey); |
157 | | - String toReplaceString = ""; |
158 | | - toReplaceString = extractReplacementString(responseBody, jsonStructure, pointerKey, |
159 | | - toReplaceString); |
160 | 152 | if (index != -1) { |
161 | | - try { |
162 | | - |
163 | | - formatter.replace(index, index + formatKey.length(), toReplaceString); |
164 | | - } catch (JsonException ex) { |
165 | | - formatter.replace(index, index + formatKey.length(), ""); |
166 | | - } |
| 153 | + String toReplaceString = extractReplacementString(responseBody, pointerKey); |
| 154 | + formatter.replace(index, index + formatKey.length(), toReplaceString); |
167 | 155 | } |
168 | 156 | } |
169 | 157 | } |
170 | 158 |
|
171 | | - private String extractReplacementString(String responseBody, JsonStructure jsonStructure, |
172 | | - String pointerKey, String toReplaceString) { |
| 159 | + private String extractReplacementString(String responseBody, String pointerKey) { |
173 | 160 | if (pointerKey.contains("#")) { |
174 | 161 | pointerKey = pointerKey.replace("$response.body#", ""); |
175 | | - JsonPointer jsonPointer = Json.createPointer(pointerKey); |
176 | | - if (jsonStructure != null && jsonPointer.containsValue(jsonStructure)) { |
177 | | - toReplaceString = jsonPointer.getValue(jsonStructure).toString(); |
178 | | - } |
179 | | - } else { |
180 | | - if (responseBody != null && !responseBody.isEmpty()) { |
181 | | - toReplaceString = responseBody; |
| 162 | + String pointerValue = CoreHelper.getValueFromJson(pointerKey, responseBody); |
| 163 | + if (pointerValue != null) { |
| 164 | + return pointerValue; |
182 | 165 | } |
| 166 | + return ""; |
183 | 167 | } |
184 | | - return toReplaceString; |
| 168 | + |
| 169 | + if (responseBody != null && !responseBody.isEmpty()) { |
| 170 | + return responseBody; |
| 171 | + } |
| 172 | + |
| 173 | + return ""; |
185 | 174 | } |
186 | 175 | } |
0 commit comments