use of org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler in project besu by hyperledger.
the class WebSocketMessageHandler method handle.
public void handle(final ServerWebSocket websocket, final Buffer buffer, final Optional<User> user) {
if (buffer.length() == 0) {
replyToClient(websocket, errorResponse(null, JsonRpcError.INVALID_REQUEST));
} else {
try {
final JsonObject jsonRpcRequest = buffer.toJsonObject();
vertx.<JsonRpcResponse>executeBlocking(promise -> {
try {
final JsonRpcResponse jsonRpcResponse = jsonRpcExecutor.execute(user, null, null, new IsAliveHandler(ethScheduler, timeoutSec), jsonRpcRequest, req -> {
final WebSocketRpcRequest websocketRequest = req.mapTo(WebSocketRpcRequest.class);
websocketRequest.setConnectionId(websocket.textHandlerID());
return websocketRequest;
});
promise.complete(jsonRpcResponse);
} catch (RuntimeException e) {
promise.fail(e);
}
}).onSuccess(jsonRpcResponse -> replyToClient(websocket, jsonRpcResponse)).onFailure(throwable -> {
try {
final Integer id = jsonRpcRequest.getInteger("id", null);
replyToClient(websocket, errorResponse(id, JsonRpcError.INTERNAL_ERROR));
} catch (ClassCastException idNotIntegerException) {
replyToClient(websocket, errorResponse(null, JsonRpcError.INTERNAL_ERROR));
}
});
} catch (DecodeException jsonObjectDecodeException) {
try {
final JsonArray batchJsonRpcRequest = buffer.toJsonArray();
vertx.<List<JsonRpcResponse>>executeBlocking(promise -> {
List<JsonRpcResponse> responses = new ArrayList<>();
for (int i = 0; i < batchJsonRpcRequest.size(); i++) {
final JsonObject jsonRequest;
try {
jsonRequest = batchJsonRpcRequest.getJsonObject(i);
} catch (ClassCastException e) {
responses.add(new JsonRpcErrorResponse(null, INVALID_REQUEST));
continue;
}
responses.add(jsonRpcExecutor.execute(user, null, null, new IsAliveHandler(ethScheduler, timeoutSec), jsonRequest, req -> {
final WebSocketRpcRequest websocketRequest = req.mapTo(WebSocketRpcRequest.class);
websocketRequest.setConnectionId(websocket.textHandlerID());
return websocketRequest;
}));
}
promise.complete(responses);
}).onSuccess(jsonRpcBatchResponse -> {
final JsonRpcResponse[] completed = jsonRpcBatchResponse.stream().filter(jsonRpcResponse -> jsonRpcResponse.getType() != JsonRpcResponseType.NONE).toArray(JsonRpcResponse[]::new);
replyToClient(websocket, completed);
}).onFailure(throwable -> replyToClient(websocket, errorResponse(null, JsonRpcError.INTERNAL_ERROR)));
} catch (RuntimeException jsonArrayDecodeException) {
replyToClient(websocket, errorResponse(null, JsonRpcError.INTERNAL_ERROR));
}
}
}
}
use of org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler in project besu by hyperledger.
the class GraphQLHttpService method process.
private GraphQLResponse process(final String requestJson, final String operationName, final Map<String, Object> variables) {
Map<GraphQLContextType, Object> contextMap = new ConcurrentHashMap<>();
contextMap.putAll(graphQlContextMap);
contextMap.put(GraphQLContextType.IS_ALIVE_HANDLER, new IsAliveHandler(scheduler, config.getHttpTimeoutSec()));
final ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(requestJson).operationName(operationName).variables(variables).graphQLContext(contextMap).build();
final ExecutionResult result = graphQL.execute(executionInput);
final Map<String, Object> toSpecificationResult = result.toSpecification();
final List<GraphQLError> errors = result.getErrors();
if (errors.size() == 0) {
return new GraphQLSuccessResponse(toSpecificationResult);
} else {
return new GraphQLErrorResponse(toSpecificationResult);
}
}
use of org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler in project besu by hyperledger.
the class WebSocketRequestHandler method process.
private JsonRpcResponse process(final Optional<AuthenticationService> authenticationService, final ServerWebSocket websocket, final Optional<User> user, final WebSocketRpcRequest requestBody, final Collection<String> noAuthApiMethods) {
if (!methods.containsKey(requestBody.getMethod())) {
LOG.debug("Can't find method {}", requestBody.getMethod());
return new JsonRpcErrorResponse(requestBody.getId(), JsonRpcError.METHOD_NOT_FOUND);
}
final JsonRpcMethod method = methods.get(requestBody.getMethod());
try {
LOG.debug("WS-RPC request -> {}", requestBody.getMethod());
requestBody.setConnectionId(websocket.textHandlerID());
if (authenticationService.isEmpty() || (authenticationService.isPresent() && authenticationService.get().isPermitted(user, method, noAuthApiMethods))) {
final JsonRpcRequestContext requestContext = new JsonRpcRequestContext(requestBody, user, new IsAliveHandler(ethScheduler, timeoutSec));
return method.response(requestContext);
} else {
return new JsonRpcUnauthorizedResponse(requestBody.getId(), JsonRpcError.UNAUTHORIZED);
}
} catch (final InvalidJsonRpcParameters e) {
LOG.debug("Invalid Params", e);
return new JsonRpcErrorResponse(requestBody.getId(), JsonRpcError.INVALID_PARAMS);
} catch (final RpcMethodTimeoutException e) {
LOG.error(JsonRpcError.TIMEOUT_ERROR.getMessage(), e);
return new JsonRpcErrorResponse(requestBody.getId(), JsonRpcError.TIMEOUT_ERROR);
} catch (final Exception e) {
LOG.error(JsonRpcError.INTERNAL_ERROR.getMessage(), e);
return new JsonRpcErrorResponse(requestBody.getId(), JsonRpcError.INTERNAL_ERROR);
}
}
Aggregations