Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
Remove boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
siy committed Jun 2, 2021
1 parent 4cf4213 commit 7a79100
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* (C) Copyright 2021 Radix DLT Ltd
*
* Radix DLT Ltd licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
*/

package com.radixdlt.api;

import com.radixdlt.api.server.JsonRpcServer;

import io.undertow.server.RoutingHandler;

/**
* Common base class for JSON RPC endpoint controllers
*/
public abstract class AbstractJsonRpcController implements Controller {
private final JsonRpcServer jsonRpcServer;

protected AbstractJsonRpcController(JsonRpcServer jsonRpcServer) {
this.jsonRpcServer = jsonRpcServer;
}

public void configureRoutes(RoutingHandler handler) {
var path = sanitize(root());

handler.post(path, jsonRpcServer);
handler.post(path + "/", jsonRpcServer);
}

private static String sanitize(String baseUrl) {
return !baseUrl.endsWith("/")
? baseUrl
: baseUrl.substring(0, baseUrl.length() - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public static JSONObject parseError(String message) {
return protocolError(RpcError.PARSE_ERROR, message);
}

public static JSONObject methodNotFound(JSONObject request, String method) {
public static JSONObject methodNotFound(JSONObject request) {
var method = request.getString("method");
return extendedError(request, RpcError.METHOD_NOT_FOUND.code(), "Method " + method + " not found");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Deque;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import io.undertow.util.StatusCodes;

import static com.radixdlt.api.JsonRpcUtil.jsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,16 @@

package com.radixdlt.api.controller;

import com.radixdlt.api.Controller;
import com.radixdlt.api.AbstractJsonRpcController;
import com.radixdlt.api.server.JsonRpcServer;

import io.undertow.server.RoutingHandler;

public final class AccountController implements Controller {
private final JsonRpcServer jsonRpcServer;

public final class AccountController extends AbstractJsonRpcController {
public AccountController(JsonRpcServer jsonRpcServer) {
this.jsonRpcServer = jsonRpcServer;
super(jsonRpcServer);
}

@Override
public String root() {
return "/account";
}

@Override
public void configureRoutes(RoutingHandler handler) {
handler.post("/account", jsonRpcServer::handleHttpRequest);
handler.post("/account/", jsonRpcServer::handleHttpRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,19 @@

package com.radixdlt.api.controller;

import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import com.radixdlt.api.Controller;
import com.radixdlt.api.archive.qualifier.Archive;

import io.undertow.server.HttpServerExchange;
import io.undertow.server.RoutingHandler;

import static com.radixdlt.api.RestUtils.respond;
import static com.radixdlt.api.RestUtils.withBodyAsync;

public final class ArchiveController implements Controller {
private final JsonRpcServer jsonRpcServer;
import com.radixdlt.api.AbstractJsonRpcController;
import com.radixdlt.api.qualifier.Archive;
import com.radixdlt.api.server.JsonRpcServer;

public final class ArchiveController extends AbstractJsonRpcController {
@Inject
public ArchiveController(@Archive JsonRpcServer jsonRpcServer) {
this.jsonRpcServer = jsonRpcServer;
super(jsonRpcServer);
}

@Override
public String root() {
return "/archive";
}

@Override
public void configureRoutes(RoutingHandler handler) {
handler.post("/archive", this::handleRpc);
handler.post("/archive/", this::handleRpc);
}

@VisibleForTesting
void handleRpc(HttpServerExchange exchange) {
withBodyAsync(exchange, request -> respond(exchange, jsonRpcServer.handle(request)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static com.radixdlt.api.JsonRpcUtil.jsonObject;
import static com.radixdlt.api.RestUtils.respond;
import static com.radixdlt.api.RestUtils.withBody;
import static com.radixdlt.api.RestUtils.withBodyAsyncAndDefaultResponse;
import static com.radixdlt.api.RestUtils.withBodyAsync;

public final class ChaosController implements Controller {
private final EventDispatcher<MempoolFillerUpdate> mempoolDispatcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,16 @@

package com.radixdlt.api.controller;

import com.radixdlt.api.Controller;
import com.radixdlt.api.AbstractJsonRpcController;
import com.radixdlt.api.server.JsonRpcServer;

import io.undertow.server.RoutingHandler;

public final class ConstructionController implements Controller {
private final JsonRpcServer jsonRpcServer;

public final class ConstructionController extends AbstractJsonRpcController {
public ConstructionController(JsonRpcServer jsonRpcServer) {
this.jsonRpcServer = jsonRpcServer;
super(jsonRpcServer);
}

@Override
public String root() {
return "/construction";
}

@Override
public void configureRoutes(RoutingHandler handler) {
handler.post("/construction", jsonRpcServer::handleHttpRequest);
handler.post("/construction/", jsonRpcServer::handleHttpRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,16 @@

package com.radixdlt.api.controller;

import com.radixdlt.api.Controller;
import com.radixdlt.api.AbstractJsonRpcController;
import com.radixdlt.api.server.JsonRpcServer;

import io.undertow.server.RoutingHandler;

public final class FaucetController implements Controller {
private final JsonRpcServer jsonRpcServer;

public final class FaucetController extends AbstractJsonRpcController {
public FaucetController(JsonRpcServer jsonRpcServer) {
this.jsonRpcServer = jsonRpcServer;
super(jsonRpcServer);
}

@Override
public String root() {
return "/faucet";
}

@Override
public void configureRoutes(final RoutingHandler handler) {
handler.post("/faucet", jsonRpcServer::handleHttpRequest);
handler.post("/faucet/", jsonRpcServer::handleHttpRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,16 @@

package com.radixdlt.api.controller;

import com.radixdlt.api.Controller;
import com.radixdlt.api.AbstractJsonRpcController;
import com.radixdlt.api.server.JsonRpcServer;

import io.undertow.server.RoutingHandler;

public final class SystemController implements Controller {
private final JsonRpcServer jsonRpcServer;

public final class SystemController extends AbstractJsonRpcController {
public SystemController(JsonRpcServer jsonRpcServer) {
this.jsonRpcServer = jsonRpcServer;
super(jsonRpcServer);
}

@Override
public String root() {
return "/system";
}

@Override
public void configureRoutes(final RoutingHandler handler) {
handler.post("/system", jsonRpcServer::handleHttpRequest);
handler.post("/system/", jsonRpcServer::handleHttpRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,16 @@

package com.radixdlt.api.controller;

import com.radixdlt.api.Controller;
import com.radixdlt.api.AbstractJsonRpcController;
import com.radixdlt.api.server.JsonRpcServer;

import io.undertow.server.RoutingHandler;

public final class ValidationController implements Controller {
private final JsonRpcServer jsonRpcServer;

public final class ValidationController extends AbstractJsonRpcController {
public ValidationController(JsonRpcServer jsonRpcServer) {
this.jsonRpcServer = jsonRpcServer;
super(jsonRpcServer);
}

@Override
public String root() {
return "/validation";
}

@Override
public void configureRoutes(final RoutingHandler handler) {
handler.post("/validation", jsonRpcServer::handleHttpRequest);
handler.post("/validation/", jsonRpcServer::handleHttpRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.radixdlt.api.Controller;
import com.radixdlt.api.JsonRpcHandler;
import com.radixdlt.api.controller.ValidationController;
import com.radixdlt.api.handler.ArchiveValidationHandler;
import com.radixdlt.api.handler.ValidationHandler;
import com.radixdlt.api.qualifier.AtNode;
import com.radixdlt.api.qualifier.Validation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@
import java.util.HashMap;
import java.util.Map;

import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;

import static com.radixdlt.api.JsonRpcUtil.invalidParamsError;
import static com.radixdlt.api.JsonRpcUtil.methodNotFound;
import static com.radixdlt.api.RestUtils.respond;
import static com.radixdlt.api.RestUtils.withBodyAsync;

import static java.util.Optional.ofNullable;

/**
* Stateless Json Rpc 2.0 Server
*/
public final class JsonRpcServer {
public final class JsonRpcServer implements HttpHandler {
private static final Logger log = LogManager.getLogger();

private final Map<String, JsonRpcHandler> handlers = new HashMap<>();
Expand All @@ -46,6 +51,11 @@ public JsonRpcServer(Map<String, JsonRpcHandler> additionalHandlers) {
fillHandlers(additionalHandlers);
}

@Override
public void handleRequest(HttpServerExchange exchange) {
withBodyAsync(exchange, request -> respond(exchange, handle(request)));
}

public JSONObject handle(JSONObject request) {
log.trace("RPC: input {}", request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import com.google.inject.Inject;
import com.radixdlt.EndpointConfig;
import com.radixdlt.api.JsonRpcUtil;
import com.radixdlt.api.qualifier.AtArchive;
import com.radixdlt.api.qualifier.AtNode;
import com.radixdlt.consensus.bft.PacemakerTimeout;
Expand All @@ -36,7 +35,6 @@
import com.radixdlt.network.addressbook.AddressBook;
import com.radixdlt.network.addressbook.PeerWithSystem;
import com.radixdlt.network.transport.TransportInfo;
import com.radixdlt.properties.RuntimeProperties;
import com.radixdlt.statecomputer.MaxTxnsPerProposal;
import com.radixdlt.statecomputer.MaxValidators;
import com.radixdlt.statecomputer.MinValidators;
Expand Down

0 comments on commit 7a79100

Please sign in to comment.