Skip to content

Commit

Permalink
removed group-name checks from rest calls
Browse files Browse the repository at this point in the history
  • Loading branch information
hasancelik committed Sep 18, 2018
1 parent 1f6b04f commit abe4f6f
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 362 deletions.
Expand Up @@ -23,12 +23,18 @@
import com.hazelcast.internal.cluster.ClusterService;
import com.hazelcast.internal.cluster.impl.ClusterServiceImpl;
import com.hazelcast.internal.partition.InternalPartitionService;
import com.hazelcast.internal.util.rest.RestUtil;
import com.hazelcast.logging.ILogger;
import com.hazelcast.nio.ConnectionManager;
import com.hazelcast.util.StringUtil;

import static com.hazelcast.internal.ascii.TextCommandConstants.MIME_TEXT_PLAIN;
import static com.hazelcast.internal.ascii.rest.HttpCommand.CONTENT_TYPE_BINARY;
import static com.hazelcast.internal.ascii.rest.HttpCommand.CONTENT_TYPE_PLAIN_TEXT;
import static com.hazelcast.internal.util.rest.RestUtil.exceptionResponse;
import static com.hazelcast.internal.util.rest.RestUtil.response;
import static com.hazelcast.internal.util.rest.RestUtil.sendResponse;
import static com.hazelcast.util.StringUtil.lowerCaseInternal;
import static com.hazelcast.util.StringUtil.stringToBytes;

public class HttpGetCommandProcessor extends HttpCommandProcessor<HttpGetCommand> {
Expand All @@ -41,11 +47,15 @@ public class HttpGetCommandProcessor extends HttpCommandProcessor<HttpGetCommand
private static final String HEALTH_PATH_PARAM_MIGRATION_QUEUE_SIZE = "/migration-queue-size";
private static final String HEALTH_PATH_PARAM_CLUSTER_SIZE = "/cluster-size";

private final ILogger logger;

public HttpGetCommandProcessor(TextCommandService textCommandService) {
super(textCommandService);
this.logger = textCommandService.getNode().getLogger(HttpGetCommandProcessor.class);
}

@Override
@SuppressWarnings({"checkstyle:cyclomaticcomplexity"})
public void handle(HttpGetCommand command) {
String uri = command.getURI();
if (uri.startsWith(URI_MAPS)) {
Expand All @@ -58,6 +68,23 @@ public void handle(HttpGetCommand command) {
handleHealthcheck(command, uri);
} else if (uri.startsWith(URI_CLUSTER_VERSION_URL)) {
handleGetClusterVersion(command);
} else if (uri.startsWith(URI_CLUSTER_STATE_URL)) {
handleGetClusterState(command);
} else if (uri.startsWith(URI_FORCESTART_CLUSTER_URL)) {
handleForceStart(command);
} else if (uri.startsWith(URI_PARTIALSTART_CLUSTER_URL)) {
handlePartialStart(command);
} else if (uri.startsWith(URI_HOT_RESTART_BACKUP_CLUSTER_URL)) {
handleHotRestartBackup(command);
} else if (uri.startsWith(URI_HOT_RESTART_BACKUP_INTERRUPT_CLUSTER_URL)) {
handleHotRestartBackupInterrupt(command);
} else if (uri.startsWith(URI_SHUTDOWN_NODE_CLUSTER_URL)) {
handleShutdownNode(command);
} else if (uri.startsWith(URI_SHUTDOWN_CLUSTER_URL)) {
handleClusterShutdown(command);
return;
} else if (uri.startsWith(URI_CLUSTER_NODES_URL)) {
handleListNodes(command);
} else {
command.send400();
}
Expand Down Expand Up @@ -114,11 +141,30 @@ private static String booleanToString(boolean b) {
}

private void handleGetClusterVersion(HttpGetCommand command) {
String res = "{\"status\":\"${STATUS}\",\"version\":\"${VERSION}\"}";
Node node = textCommandService.getNode();
ClusterService clusterService = node.getClusterService();
res = res.replace("${STATUS}", "success");
res = res.replace("${VERSION}", clusterService.getClusterVersion().toString());
String res;
try {
Node node = textCommandService.getNode();
ClusterService clusterService = node.getClusterService();
String clusterVersion = clusterService.getClusterVersion().toString();
res = response(RestUtil.ResponseType.SUCCESS, "version", clusterVersion);
} catch (Throwable throwable) {
logger.warning("Error occurred while getting cluster version", throwable);
res = exceptionResponse(throwable);
}
command.setResponse(HttpCommand.CONTENT_TYPE_JSON, stringToBytes(res));
}

private void handleGetClusterState(HttpGetCommand command) {
String res;
try {
Node node = textCommandService.getNode();
ClusterService clusterService = node.getClusterService();
ClusterState clusterState = clusterService.getClusterState();
res = response(RestUtil.ResponseType.SUCCESS, "state", lowerCaseInternal(clusterState.toString()));
} catch (Throwable throwable) {
logger.warning("Error occurred while getting cluster state", throwable);
res = exceptionResponse(throwable);
}
command.setResponse(HttpCommand.CONTENT_TYPE_JSON, stringToBytes(res));
}

Expand All @@ -143,6 +189,56 @@ private void handleCluster(HttpGetCommand command) {
command.setResponse(null, stringToBytes(res.toString()));
}

private void handleListNodes(HttpGetCommand command) {
String res;
try {
Node node = textCommandService.getNode();
ClusterService clusterService = node.getClusterService();
final String responseTxt = clusterService.getMembers().toString() + "\n"
+ node.getBuildInfo().getVersion() + "\n"
+ System.getProperty("java.version");
res = response(RestUtil.ResponseType.SUCCESS, "response", responseTxt);
sendResponse(textCommandService, command, res);
return;
} catch (Throwable throwable) {
logger.warning("Error occurred while listing nodes", throwable);
res = exceptionResponse(throwable);
}
sendResponse(textCommandService, command, res);
}


private void handleClusterShutdown(HttpGetCommand command) {
String res;
try {
Node node = textCommandService.getNode();
ClusterService clusterService = node.getClusterService();
res = response(RestUtil.ResponseType.SUCCESS);
sendResponse(textCommandService, command, res);
clusterService.shutdown();
return;
} catch (Throwable throwable) {
logger.warning("Error occurred while shutting down cluster", throwable);
res = exceptionResponse(throwable);
}
sendResponse(textCommandService, command, res);
}

private void handleShutdownNode(HttpGetCommand command) {
String res;
try {
Node node = textCommandService.getNode();
res = response(RestUtil.ResponseType.SUCCESS);
sendResponse(textCommandService, command, res);
node.hazelcastInstance.shutdown();
return;
} catch (Throwable throwable) {
logger.warning("Error occurred while shutting down", throwable);
res = exceptionResponse(throwable);
}
sendResponse(textCommandService, command, res);
}

private void handleQueue(HttpGetCommand command, String uri) {
int indexEnd = uri.indexOf('/', URI_QUEUES.length());
String queueName = uri.substring(URI_QUEUES.length(), indexEnd);
Expand All @@ -166,6 +262,56 @@ private void handleMap(HttpGetCommand command, String uri) {
prepareResponse(command, value);
}

private void handleForceStart(HttpGetCommand command) {
String res;
try {
Node node = textCommandService.getNode();
boolean success = node.getNodeExtension().getInternalHotRestartService().triggerForceStart();
res = response(success ? RestUtil.ResponseType.SUCCESS : RestUtil.ResponseType.FAIL);
} catch (Throwable throwable) {
logger.warning("Error occurred while handling force start", throwable);
res = exceptionResponse(throwable);
}
sendResponse(textCommandService, command, res);
}

private void handlePartialStart(HttpGetCommand command) {
String res;
try {
Node node = textCommandService.getNode();
boolean success = node.getNodeExtension().getInternalHotRestartService().triggerPartialStart();
res = response(success ? RestUtil.ResponseType.SUCCESS : RestUtil.ResponseType.FAIL);
} catch (Throwable throwable) {
logger.warning("Error occurred while handling partial start", throwable);
res = exceptionResponse(throwable);
}
sendResponse(textCommandService, command, res);
}

private void handleHotRestartBackup(HttpGetCommand command) {
String res;
try {
textCommandService.getNode().getNodeExtension().getHotRestartService().backup();
res = response(RestUtil.ResponseType.SUCCESS);
} catch (Throwable throwable) {
logger.warning("Error occurred while invoking hot backup", throwable);
res = exceptionResponse(throwable);
}
sendResponse(textCommandService, command, res);
}

private void handleHotRestartBackupInterrupt(HttpGetCommand command) {
String res;
try {
textCommandService.getNode().getNodeExtension().getHotRestartService().interruptBackupTask();
res = response(RestUtil.ResponseType.SUCCESS);
} catch (Throwable throwable) {
logger.warning("Error occurred while interrupting hot backup", throwable);
res = exceptionResponse(throwable);
}
sendResponse(textCommandService, command, res);
}

@Override
public void handleRejection(HttpGetCommand command) {
handle(command);
Expand Down

0 comments on commit abe4f6f

Please sign in to comment.