use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestId in project besu by hyperledger.
the class JsonRpcExecutor method execute.
public JsonRpcResponse execute(final Optional<User> optionalUser, final Tracer tracer, final Context spanContext, final Supplier<Boolean> alive, final JsonObject jsonRpcRequest, final Function<JsonObject, JsonRpcRequest> requestBodyProvider) {
try {
final JsonRpcRequest requestBody = requestBodyProvider.apply(jsonRpcRequest);
final JsonRpcRequestId id = new JsonRpcRequestId(requestBody.getId());
// Handle notifications
if (requestBody.isNotification()) {
// Notifications aren't handled so create empty result for now.
return new JsonRpcNoResponse();
}
final Span span;
if (tracer != null) {
span = tracer.spanBuilder(requestBody.getMethod()).setSpanKind(SpanKind.INTERNAL).setParent(spanContext).startSpan();
} else {
span = Span.getInvalid();
}
final Optional<JsonRpcError> unavailableMethod = validateMethodAvailability(requestBody);
if (unavailableMethod.isPresent()) {
span.setStatus(StatusCode.ERROR, "method unavailable");
return new JsonRpcErrorResponse(id, unavailableMethod.get());
}
final JsonRpcMethod method = rpcMethods.get(requestBody.getMethod());
return rpcProcessor.process(id, method, span, new JsonRpcRequestContext(requestBody, optionalUser, alive));
} catch (IllegalArgumentException e) {
try {
final Integer id = jsonRpcRequest.getInteger("id", null);
return new JsonRpcErrorResponse(id, INVALID_REQUEST);
} catch (ClassCastException idNotIntegerException) {
return new JsonRpcErrorResponse(null, INVALID_REQUEST);
}
}
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestId in project besu by hyperledger.
the class JsonRpcHttpService method process.
private JsonRpcResponse process(final RoutingContext ctx, final JsonObject requestJson, final Optional<User> user) {
final JsonRpcRequest requestBody;
Object id = null;
try {
id = new JsonRpcRequestId(requestJson.getValue("id")).getValue();
requestBody = requestJson.mapTo(JsonRpcRequest.class);
} catch (final IllegalArgumentException exception) {
return errorResponse(id, INVALID_REQUEST);
}
Span span = tracer.spanBuilder(requestBody.getMethod()).setSpanKind(SpanKind.INTERNAL).setParent(ctx.get(SPAN_CONTEXT)).startSpan();
try {
// Handle notifications
if (requestBody.isNotification()) {
// Notifications aren't handled so create empty result for now.
return NO_RESPONSE;
}
final Optional<JsonRpcError> unavailableMethod = validateMethodAvailability(requestBody);
if (unavailableMethod.isPresent()) {
span.setStatus(StatusCode.ERROR, "method unavailable");
return errorResponse(id, unavailableMethod.get());
}
final JsonRpcMethod method = rpcMethods.get(requestBody.getMethod());
if (!authenticationService.isPresent() || (authenticationService.isPresent() && authenticationService.get().isPermitted(user, method, config.getNoAuthRpcApis()))) {
// Generate response
try (final OperationTimer.TimingContext ignored = requestTimer.labels(requestBody.getMethod()).startTimer()) {
if (user.isPresent()) {
return method.response(new JsonRpcRequestContext(requestBody, user.get(), () -> !ctx.response().closed()));
}
return method.response(new JsonRpcRequestContext(requestBody, () -> !ctx.response().closed()));
} catch (final InvalidJsonRpcParameters e) {
LOG.debug("Invalid Params", e);
span.setStatus(StatusCode.ERROR, "Invalid Params");
return errorResponse(id, JsonRpcError.INVALID_PARAMS);
} catch (final MultiTenancyValidationException e) {
span.setStatus(StatusCode.ERROR, "Unauthorized");
return unauthorizedResponse(id, JsonRpcError.UNAUTHORIZED);
} catch (final RuntimeException e) {
LOG.error("Error processing JSON-RPC requestBody", e);
span.setStatus(StatusCode.ERROR, "Error processing JSON-RPC requestBody");
return errorResponse(id, JsonRpcError.INTERNAL_ERROR);
}
} else {
span.setStatus(StatusCode.ERROR, "Unauthorized");
return unauthorizedResponse(id, JsonRpcError.UNAUTHORIZED);
}
} finally {
span.end();
}
}
Aggregations