in extension-base/src/main/java/com/azure/autorest/extension/base/jsonrpc/Connection.java [217:294]
public void process(String content, boolean isObject) {
// The only times this method is called is when the beginning portion of the JSON text is '{' or '['.
// So, instead of the previous design when using Jackson where a fully processed JsonNode was passed, use a
// simpler parameter 'isObject' to check if we are in a valid processing state.
if (!isObject) {
System.err.println("Unhandled: Batch Request");
return;
}
executorService.submit(() -> {
try (JsonReader jsonReader = JsonProviders.createReader(content)) {
Map<String, String> jobject = jsonReader.readMap(reader -> {
if (reader.isStartArrayOrObject()) {
return reader.readChildren();
} else {
return reader.getString();
}
});
String method = jobject.get("method");
if (method != null) {
int id = processIdField(jobject.get("id"));
// this is a method call.
// pass it to the service that is listening...
if (dispatch.containsKey(method)) {
Function<String, String> fn = dispatch.get(method);
String parameters = jobject.get("params");
String result = fn.apply(parameters);
if (id != -1) {
// if this is a request, send the response.
respond(id, result);
}
}
return;
}
if (jobject.containsKey("result")) {
String result = jobject.get("result");
int id = processIdField(jobject.get("id"));
if (id != -1) {
CompletableFuture<String> f = tasks.remove(id);
try {
f.complete(result);
} catch (Exception e) {
f.completeExceptionally(e);
}
}
return;
}
String error = jobject.get("error");
if (error != null) {
int id = processIdField(jobject.get("id"));
if (id != -1) {
CompletableFuture<String> f = tasks.remove(id);
try (JsonReader errorReader = JsonProviders.createReader(error)) {
Map<String, Object> errorObject = errorReader.readMap(JsonReader::readUntyped);
String message = String.valueOf(errorObject.get("message"));
Object dataField = errorObject.get("data");
if (dataField != null) {
message += " (" + dataField + ")";
}
f.completeExceptionally(new RuntimeException(message));
} catch (Exception e) {
f.completeExceptionally(e);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
}