Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #323 from erandacr/cli-output-formatting
Browse files Browse the repository at this point in the history
Add queue consumer admin operations
  • Loading branch information
erandacr authored Mar 6, 2018
2 parents 53c2306 + 9aa821c commit 3654414
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private static JCommander buildCommanderTree(String rootCommand) {
addChildCommand(jCommanderList, Constants.CMD_EXCHANGE, commandFactory.createListExchangeCommand());
addChildCommand(jCommanderList, Constants.CMD_QUEUE, commandFactory.createListQueueCommand());
addChildCommand(jCommanderList, Constants.CMD_BINDING, commandFactory.createListBindingCommand());
addChildCommand(jCommanderList, Constants.CMD_CONSUMER, commandFactory.createListConsumerCommand());

// add create sub-commands
addChildCommand(jCommanderCreate, Constants.CMD_EXCHANGE, commandFactory.createCreateExchangeCommand());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.ballerina.messaging.broker.client.cmd.impl.delete.DeleteQueueCmd;
import io.ballerina.messaging.broker.client.cmd.impl.list.ListBindingCmd;
import io.ballerina.messaging.broker.client.cmd.impl.list.ListCmd;
import io.ballerina.messaging.broker.client.cmd.impl.list.ListConsumerCmd;
import io.ballerina.messaging.broker.client.cmd.impl.list.ListExchangeCmd;
import io.ballerina.messaging.broker.client.cmd.impl.list.ListQueueCmd;

Expand Down Expand Up @@ -81,6 +82,7 @@ public ListExchangeCmd createListExchangeCommand() {
public CreateQueueCmd createCreateQueueCommand() {
return new CreateQueueCmd(rootCommand);
}

public DeleteQueueCmd createDeleteQueueCommand() {
return new DeleteQueueCmd(rootCommand);
}
Expand All @@ -96,4 +98,8 @@ public CreateBindingCmd createCreateBindingCommand() {
public ListBindingCmd createListBindingCommand() {
return new ListBindingCmd(rootCommand);
}

public ListConsumerCmd createListConsumerCommand() {
return new ListConsumerCmd(rootCommand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.HttpURLConnection;

import static io.ballerina.messaging.broker.client.utils.Constants.BROKER_ERROR_MSG;
import static io.ballerina.messaging.broker.client.utils.Constants.HTTP_POST;

/**
* Command representing the resource creation.
Expand Down Expand Up @@ -75,7 +76,7 @@ void performResourceCreationOverHttp(String urlSuffix, String payload) {

// do POST
HttpRequest httpRequest = new HttpRequest(urlSuffix, payload);
HttpResponse response = httpClient.sendHttpRequest(httpRequest, "POST");
HttpResponse response = httpClient.sendHttpRequest(httpRequest, HTTP_POST);

// handle response
if (response.getStatusCode() == HttpURLConnection.HTTP_CREATED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.net.HttpURLConnection;

import static io.ballerina.messaging.broker.client.utils.Constants.BROKER_ERROR_MSG;
import static io.ballerina.messaging.broker.client.utils.Constants.HTTP_DELETE;

/**
* Command representing MB exchange deletion.
Expand Down Expand Up @@ -67,7 +68,7 @@ public void execute() {
}

// do DELETE
HttpResponse response = httpClient.sendHttpRequest(httpRequest, "DELETE");
HttpResponse response = httpClient.sendHttpRequest(httpRequest, HTTP_DELETE);

// handle response
if (response.getStatusCode() == HttpURLConnection.HTTP_OK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.net.HttpURLConnection;

import static io.ballerina.messaging.broker.client.utils.Constants.BROKER_ERROR_MSG;
import static io.ballerina.messaging.broker.client.utils.Constants.HTTP_DELETE;

/**
* Command representing MB queue deletion.
Expand Down Expand Up @@ -69,7 +70,7 @@ public void execute() {
httpRequest.setQueryParameters("?ifUnused=" + String.valueOf(ifUnused) + "&ifEmpty=" + String.valueOf(ifEmpty));

// do DELETE
HttpResponse response = httpClient.sendHttpRequest(httpRequest, "DELETE");
HttpResponse response = httpClient.sendHttpRequest(httpRequest, HTTP_DELETE);

// handle response
if (response.getStatusCode() == HttpURLConnection.HTTP_OK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.List;

import static io.ballerina.messaging.broker.client.utils.Constants.BROKER_ERROR_MSG;
import static io.ballerina.messaging.broker.client.utils.Constants.HTTP_GET;

/**
* Command representing MB binding information retrieval.
Expand Down Expand Up @@ -84,7 +85,7 @@ public void execute() {

// do GET
HttpRequest httpRequest = new HttpRequest(urlParamType + urlParamName + Constants.BINDINGS_URL_PARAM);
HttpResponse response = httpClient.sendHttpRequest(httpRequest, "GET");
HttpResponse response = httpClient.sendHttpRequest(httpRequest, HTTP_GET);

// handle data processing
ResponseFormatter responseFormatter = new TableFormatter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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 io.ballerina.messaging.broker.client.cmd.impl.list;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.gson.Gson;
import io.ballerina.messaging.broker.client.http.HttpClient;
import io.ballerina.messaging.broker.client.http.HttpRequest;
import io.ballerina.messaging.broker.client.http.HttpResponse;
import io.ballerina.messaging.broker.client.output.ResponseFormatter;
import io.ballerina.messaging.broker.client.output.TableFormatter;
import io.ballerina.messaging.broker.client.resources.Configuration;
import io.ballerina.messaging.broker.client.resources.Consumer;
import io.ballerina.messaging.broker.client.utils.Constants;
import io.ballerina.messaging.broker.client.utils.Utils;

import java.net.HttpURLConnection;

import static io.ballerina.messaging.broker.client.utils.Constants.BROKER_ERROR_MSG;
import static io.ballerina.messaging.broker.client.utils.Constants.HTTP_GET;

/**
* Command representing MB consumer information retrieval.
*/
@Parameters(commandDescription = "List consumers in a Broker queue")
public class ListConsumerCmd extends ListCmd {

@Parameter(names = { "--all", "-a" },
description = "return info on all consumer of the queue")
private boolean all;

@Parameter(names = { "--consumer", "-c" },
description = "id of the consumer which info needs to be retrieved")
private String consumerId = "";

@Parameter(description = "name of the queue",
required = true)
private String queueName = "";

public ListConsumerCmd(String rootCommand) {
super(rootCommand);
}

@Override
public void execute() {
if (help) {
processHelpLogs();
return;
}

Configuration configuration = Utils.readConfigurationFile();
HttpClient httpClient = new HttpClient(configuration);

if (all) {
consumerId = "";
}

// do GET
HttpRequest httpRequest = new HttpRequest(
Constants.QUEUES_URL_PARAM + queueName + Constants.CONSUMERS_URL_PARAM + consumerId);
HttpResponse response = httpClient.sendHttpRequest(httpRequest, HTTP_GET);

// handle data processing
ResponseFormatter responseFormatter = new TableFormatter();

if (response.getStatusCode() == HttpURLConnection.HTTP_OK) {
Gson gson = new Gson();
Consumer[] consumers;
if (consumerId.isEmpty()) {
consumers = gson.fromJson(response.getPayload(), Consumer[].class);
} else {
consumers = new Consumer[] { gson.fromJson(response.getPayload(), Consumer.class) };
}
responseFormatter.printConsumers(consumers);
} else {
ResponseFormatter.handleErrorResponse(buildResponseMessage(response, BROKER_ERROR_MSG));
}

}

@Override
public void appendUsage(StringBuilder out) {
out.append("Usage:\n");
out.append(" " + rootCommand + " list consumer [queue-name] [flag]*\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.net.HttpURLConnection;

import static io.ballerina.messaging.broker.client.utils.Constants.BROKER_ERROR_MSG;
import static io.ballerina.messaging.broker.client.utils.Constants.HTTP_GET;

/**
* Command representing MB exchange information retrieval.
Expand Down Expand Up @@ -68,7 +69,7 @@ public void execute() {

// do GET
HttpRequest httpRequest = new HttpRequest(Constants.EXCHANGES_URL_PARAM + exchangeName);
HttpResponse response = httpClient.sendHttpRequest(httpRequest, "GET");
HttpResponse response = httpClient.sendHttpRequest(httpRequest, HTTP_GET);

// handle data processing
ResponseFormatter responseFormatter = new TableFormatter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.net.HttpURLConnection;

import static io.ballerina.messaging.broker.client.utils.Constants.BROKER_ERROR_MSG;
import static io.ballerina.messaging.broker.client.utils.Constants.HTTP_GET;

/**
* Command representing MB queue information retrieval.
Expand Down Expand Up @@ -68,7 +69,7 @@ public void execute() {

// do GET
HttpRequest httpRequest = new HttpRequest(Constants.QUEUES_URL_PARAM + queueName);
HttpResponse response = httpClient.sendHttpRequest(httpRequest, "GET");
HttpResponse response = httpClient.sendHttpRequest(httpRequest, HTTP_GET);

// handle data processing
ResponseFormatter responseFormatter = new TableFormatter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.ballerina.messaging.broker.client.output;

import io.ballerina.messaging.broker.client.resources.Binding;
import io.ballerina.messaging.broker.client.resources.Consumer;
import io.ballerina.messaging.broker.client.resources.Exchange;
import io.ballerina.messaging.broker.client.resources.Message;
import io.ballerina.messaging.broker.client.resources.Queue;
Expand Down Expand Up @@ -73,4 +74,11 @@ static void printMessage(Message message) {
* @param bindings array of bindings.
*/
void printBindingsExchange(Binding[] bindings);

/**
* Print an array of Queue consumers into a desired output format.
*
* @param consumers array of consumers.
*/
void printConsumers(Consumer[] consumers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.ballerina.messaging.broker.client.output;

import io.ballerina.messaging.broker.client.resources.Binding;
import io.ballerina.messaging.broker.client.resources.Consumer;
import io.ballerina.messaging.broker.client.resources.Exchange;
import io.ballerina.messaging.broker.client.resources.Queue;

Expand Down Expand Up @@ -93,4 +94,24 @@ public void printBindingsExchange(Binding[] bindings) {
OUT_STREAM.printf(printTemplate, binding.getQueueName(), binding.getBindingPattern());
}
}

@Override
public void printConsumers(Consumer[] consumers) {
if (consumers.length == 0) {
return;
}
int maxIdLength = Arrays.stream(consumers)
.mapToInt(consumer -> String.valueOf(consumer.getId()).length())
.max()
.getAsInt();

int maxColumnSize = Math.max(maxIdLength, Consumer.CONSUMER_ID.length());

String printTemplate = "%-" + String.valueOf(maxColumnSize + TABLE_PADDING) + "s%-12s%s\n";

OUT_STREAM.printf(printTemplate, Consumer.CONSUMER_ID, Consumer.IS_EXCLUSIVE, Consumer.FLOW_ENABLED);
for (Consumer consumer : consumers) {
OUT_STREAM.printf(printTemplate, consumer.getId(), consumer.isExclusive(), consumer.isFlowEnabled());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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 io.ballerina.messaging.broker.client.resources;

/**
* Representation of consumer in a broker queue.
*/
public class Consumer {

public static final String CONSUMER_ID = "id";
public static final String IS_EXCLUSIVE = "isExclusive";
public static final String FLOW_ENABLED = "flowEnabled";

private String queueName;

private int id;

private boolean isExclusive;

private boolean flowEnabled;

public Consumer(String queueName, int id, boolean isExclusive, boolean flowEnabled) {
this.queueName = queueName;
this.id = id;
this.isExclusive = isExclusive;
this.flowEnabled = flowEnabled;
}

public String getQueueName() {
return queueName;
}

public int getId() {
return id;
}

public boolean isExclusive() {
return isExclusive;
}

public boolean isFlowEnabled() {
return flowEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Constants {
public static final String BROKER_CONNECTION_URL_SUFFIX = "/broker/v1.0/";
public static final String QUEUES_URL_PARAM = "queues/";
public static final String BINDINGS_URL_PARAM = "/bindings/";
public static final String CONSUMERS_URL_PARAM = "/consumers/";
public static final String EXCHANGES_URL_PARAM = "exchanges/";
public static final String DEFAULT_CONFIG_FILE_PATH = "cli-config.yml";

Expand All @@ -43,6 +44,12 @@ public class Constants {
public static final String CMD_EXCHANGE = "exchange";
public static final String CMD_QUEUE = "queue";
public static final String CMD_BINDING = "binding";
public static final String CMD_CONSUMER = "consumer";

public static final String BROKER_ERROR_MSG = "Error while invoking Brokers admin services";

// http constants
public static final String HTTP_GET = "GET";
public static final String HTTP_POST = "POST";
public static final String HTTP_DELETE = "DELETE";
}
Loading

0 comments on commit 3654414

Please sign in to comment.