-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c39417
commit e3c0089
Showing
6 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cli.messages; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import argo.jdom.JsonNode; | ||
import argo.jdom.JsonNodeFactories; | ||
import argo.jdom.JsonRootNode; | ||
import argo.jdom.JsonStringNode; | ||
import utilities.IJsonable; | ||
|
||
public class TaskListMessage implements IJsonable { | ||
private TaskGroupMessage group; | ||
|
||
private TaskListMessage() {} | ||
public static TaskListMessage of() { | ||
return new TaskListMessage(); | ||
} | ||
|
||
private TaskListMessage(TaskGroupMessage group) { | ||
this.group = group; | ||
} | ||
|
||
public static TaskListMessage parseJSON(JsonNode node) { | ||
TaskGroupMessage group = null; | ||
if (node.isObjectNode("group")) { | ||
group = TaskGroupMessage.parseJSON(node.getNode("group")); | ||
} | ||
|
||
return new TaskListMessage(group); | ||
} | ||
|
||
@Override | ||
public JsonRootNode jsonize() { | ||
Map<JsonStringNode, JsonNode> data = new HashMap<>(); | ||
if (group != null) { | ||
data.put(JsonNodeFactories.string("group"), group.jsonize()); | ||
} | ||
return JsonNodeFactories.object(data); | ||
} | ||
|
||
public TaskGroupMessage getGroup() { | ||
return group; | ||
} | ||
public TaskListMessage setGroup(TaskGroupMessage group) { | ||
this.group = group; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cli.server.handlers; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.http.nio.protocol.HttpAsyncExchange; | ||
|
||
import argo.jdom.JsonNode; | ||
import cli.messages.TaskGroupMessage; | ||
import cli.messages.TaskListMessage; | ||
import cli.server.CliRpcCodec; | ||
import cli.server.utils.EnumerationUtils; | ||
import core.userDefinedTask.TaskGroup; | ||
import core.userDefinedTask.UserDefinedAction; | ||
|
||
public class TaskListActionHandler extends TaskActionHandler { | ||
|
||
@Override | ||
protected Void handleTaskActionWithBackend(HttpAsyncExchange exchange, JsonNode request) throws IOException { | ||
TaskListMessage message = TaskListMessage.parseJSON(request); | ||
boolean noGroup = message.getGroup() == null || | ||
(message.getGroup().getIndex() == TaskGroupMessage.UNKNOWN_INDEX && | ||
message.getGroup().getName().isEmpty()); | ||
|
||
if (noGroup) { | ||
List<String> names = backEndHolder.getTaskGroups().stream().map(TaskGroup::getName).collect(Collectors.toList()); | ||
return CliRpcCodec.prepareResponse(exchange, 200, EnumerationUtils.enumerate(names)); | ||
} | ||
TaskGroup group = getGroup(message.getGroup()); | ||
if (group == null) { | ||
return CliRpcCodec.prepareResponse(exchange, 400, "Cannot find group with given arguments."); | ||
} | ||
List<String> taskNames = group.getTasks().stream().map(UserDefinedAction::getName).collect(Collectors.toList()); | ||
return CliRpcCodec.prepareResponse(exchange, 200, EnumerationUtils.enumerate(taskNames)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cli.server.utils; | ||
|
||
public class EnumerationUtils { | ||
private EnumerationUtils() {} | ||
|
||
/** | ||
* Converts an iterable of Strings into a readable list. | ||
* Index starts from 0. | ||
* | ||
* E.g. [aa,bb,cc,dd] will become | ||
* 0) aa | ||
* 1) bb | ||
* 2) cc | ||
* 3) dd | ||
*/ | ||
public static String enumerate(Iterable<String> data) { | ||
StringBuilder builder = new StringBuilder(); | ||
int i = 0; | ||
for (String s : data) { | ||
builder.append(String.format("%d) %s\n", i++, s)); | ||
} | ||
return builder.toString(); | ||
} | ||
} |