|
1 | 1 | package io.github.easy4j.opencli.remote; |
2 | 2 |
|
3 | | -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
4 | 3 | import tools.jackson.core.JacksonException; |
| 4 | +import tools.jackson.databind.DeserializationFeature; |
5 | 5 | import tools.jackson.databind.JsonNode; |
| 6 | +import tools.jackson.databind.ObjectReader; |
6 | 7 | import tools.jackson.databind.json.JsonMapper; |
7 | 8 | import io.github.easy4j.opencli.OpenCliProperties; |
8 | 9 | import io.github.easy4j.opencli.core.OpenCliOutputParser; |
9 | 10 | import io.github.easy4j.opencli.core.OpenCliResult; |
10 | 11 | import io.github.easy4j.opencli.exception.OpenCliExecutableFailureException; |
11 | 12 | import io.github.easy4j.opencli.exception.OpenCliNonZeroExitException; |
| 13 | +import io.github.easy4j.opencli.exception.OpenCliRemoteProtocolException; |
12 | 14 | import io.github.easy4j.opencli.parser.OpenCliParsedFields; |
13 | 15 | import io.github.easy4j.opencli.util.OpenCliStrings; |
14 | 16 | import java.util.Objects; |
|
42 | 44 | public final class OpenCliRemoteAgentHttpClient implements AutoCloseable { |
43 | 45 |
|
44 | 46 | private static final JsonMapper MAPPER = new JsonMapper(); |
| 47 | + private static final ObjectReader ENVELOPE_READER = MAPPER.reader().with( |
| 48 | + DeserializationFeature.FAIL_ON_TRAILING_TOKENS, |
| 49 | + DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY); |
45 | 50 |
|
46 | 51 | private final OpenCliProperties properties; |
47 | 52 |
|
@@ -158,28 +163,35 @@ private static OpenCliExecutableFailureException failure(String message, String |
158 | 163 |
|
159 | 164 | private OpenCliResult mapResponse(String respBody, String correlationId) { |
160 | 165 | String rawCapture = captureRawIfEnabled(respBody); |
161 | | - AgentCollectEnvelope env; |
| 166 | + JsonNode envelope; |
162 | 167 | try { |
163 | | - env = MAPPER.readValue(respBody, AgentCollectEnvelope.class); |
| 168 | + envelope = ENVELOPE_READER.readTree(respBody); |
164 | 169 | } catch (JacksonException e) { |
165 | 170 | log.warn("Agent response envelope parse failed correlationId={} responseChars={}", |
166 | 171 | correlationId, respBody.length()); |
167 | | - throw failure("Failed to parse agent response envelope", correlationId); |
| 172 | + throw new OpenCliRemoteProtocolException("Failed to parse agent response envelope", correlationId); |
168 | 173 | } |
169 | | - if (Objects.isNull(env)) { |
170 | | - log.warn("Agent response envelope missing correlationId={} responseChars={}", |
| 174 | + if (Objects.isNull(envelope) || !envelope.isObject() || !envelope.path("success").isBoolean()) { |
| 175 | + log.warn("Agent response envelope status invalid correlationId={} responseChars={}", |
171 | 176 | correlationId, respBody.length()); |
172 | | - throw failure("Missing agent response envelope", correlationId); |
| 177 | + throw new OpenCliRemoteProtocolException("Invalid agent response envelope status", correlationId); |
173 | 178 | } |
174 | | - boolean success = Objects.nonNull(env.success) && env.success; |
175 | | - String err = Objects.isNull(env.error) ? "" : env.error; |
| 179 | + JsonNode error = envelope.path("error"); |
| 180 | + if (!error.isMissingNode() && !error.isNull() && !error.isTextual()) { |
| 181 | + log.warn("Agent response envelope error field invalid correlationId={} responseChars={}", |
| 182 | + correlationId, respBody.length()); |
| 183 | + throw new OpenCliRemoteProtocolException("Invalid agent response envelope error field", correlationId); |
| 184 | + } |
| 185 | + boolean success = envelope.path("success").booleanValue(); |
| 186 | + String err = error.isTextual() ? error.asText() : ""; |
| 187 | + JsonNode items = envelope.path("items"); |
176 | 188 | String stdout; |
177 | | - if (Objects.isNull(env.items) || env.items.isNull()) { |
| 189 | + if (items.isMissingNode() || items.isNull()) { |
178 | 190 | stdout = "[]"; |
179 | | - } else if (env.items.isArray()) { |
180 | | - stdout = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(env.items); |
| 191 | + } else if (items.isArray()) { |
| 192 | + stdout = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(items); |
181 | 193 | } else { |
182 | | - stdout = env.items.toPrettyString(); |
| 194 | + stdout = items.toPrettyString(); |
183 | 195 | } |
184 | 196 | OpenCliParsedFields parsed = OpenCliOutputParser.parseBestEffort(stdout, err); |
185 | 197 | if (!success) { |
@@ -216,13 +228,4 @@ private String captureRawIfEnabled(String respBody) { |
216 | 228 | return respBody; |
217 | 229 | } |
218 | 230 |
|
219 | | - @JsonIgnoreProperties(ignoreUnknown = true) |
220 | | - private static final class AgentCollectEnvelope { |
221 | | - |
222 | | - public Boolean success; |
223 | | - |
224 | | - public JsonNode items; |
225 | | - |
226 | | - public String error; |
227 | | - } |
228 | 231 | } |
0 commit comments