Search in sources :

Example 1 with IsAliveHandler

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));
            }
        }
    }
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) ServerWebSocket(io.vertx.core.http.ServerWebSocket) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) DecodeException(io.vertx.core.json.DecodeException) LoggerFactory(org.slf4j.LoggerFactory) IsAliveHandler(org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler) JsonRpcResponseType(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType) ArrayList(java.util.ArrayList) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) JsonObject(io.vertx.core.json.JsonObject) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) Logger(org.slf4j.Logger) INVALID_REQUEST(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INVALID_REQUEST) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Feature(com.fasterxml.jackson.core.JsonGenerator.Feature) User(io.vertx.ext.auth.User) Buffer(io.vertx.core.buffer.Buffer) Optional(java.util.Optional) JsonRpcExecutor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor) JsonObject(io.vertx.core.json.JsonObject) WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) DecodeException(io.vertx.core.json.DecodeException) JsonArray(io.vertx.core.json.JsonArray) IsAliveHandler(org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler) ArrayList(java.util.ArrayList) List(java.util.List) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 2 with IsAliveHandler

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);
    }
}
Also used : GraphQLErrorResponse(org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLErrorResponse) ExecutionResult(graphql.ExecutionResult) IsAliveHandler(org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler) GraphQLSuccessResponse(org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLSuccessResponse) GraphQLError(graphql.GraphQLError) ExecutionInput(graphql.ExecutionInput) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with IsAliveHandler

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);
    }
}
Also used : JsonRpcUnauthorizedResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcUnauthorizedResponse) IsAliveHandler(org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) RpcMethodTimeoutException(org.hyperledger.besu.ethereum.api.handlers.RpcMethodTimeoutException) JsonRpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod) InvalidJsonRpcParameters(org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters) DecodeException(io.vertx.core.json.DecodeException) RpcMethodTimeoutException(org.hyperledger.besu.ethereum.api.handlers.RpcMethodTimeoutException) IOException(java.io.IOException) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Aggregations

IsAliveHandler (org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler)3 DecodeException (io.vertx.core.json.DecodeException)2 IOException (java.io.IOException)2 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)2 Feature (com.fasterxml.jackson.core.JsonGenerator.Feature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 ExecutionInput (graphql.ExecutionInput)1 ExecutionResult (graphql.ExecutionResult)1 GraphQLError (graphql.GraphQLError)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 ServerWebSocket (io.vertx.core.http.ServerWebSocket)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 User (io.vertx.ext.auth.User)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1