org.mvnsearch.boot.xtermjs.XtermCommandHandler Maven / Gradle / Ivy
package org.mvnsearch.boot.xtermjs;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import org.jetbrains.annotations.NotNull;
import org.jline.reader.impl.DefaultParser;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStyle;
import org.mvnsearch.boot.xtermjs.commands.CustomizedCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.Shell;
import org.zeroturnaround.exec.ProcessExecutor;
import reactor.core.publisher.Mono;
import javax.annotation.PostConstruct;
import java.io.File;
import java.lang.reflect.Method;
import java.util.*;
/**
* Xterm command handler: execute the commands from xterm.js
*
* @author linux_china
*/
public class XtermCommandHandler {
@Autowired
private Shell shell;
@Autowired
public List customizedCommands;
private ObjectMapper objectMapper;
/**
* command line parser
*/
private DefaultParser lineParser = new DefaultParser();
private List stringOutputClasses = Arrays.asList("java.util.Date", "java.lang.Boolean", "java.lang.Void");
private Map customizedCommandMap = new HashMap<>();
@PostConstruct
public void init() {
this.objectMapper = new ObjectMapper();
this.objectMapper.registerModule(new Jdk8Module());
this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
for (CustomizedCommand customizedCommand : customizedCommands) {
String[] names = customizedCommand.getNames();
for (String name : names) {
customizedCommandMap.put(name, customizedCommand);
}
}
}
public Mono executeCommand(String commandLine) {
String command;
String arguments = null;
int spaceIndex = commandLine.indexOf(" ");
if (spaceIndex > 0) {
command = commandLine.substring(0, spaceIndex);
arguments = commandLine.substring(spaceIndex + 1).trim();
}
else {
command = commandLine;
}
Object result;
try {
if (customizedCommandMap.containsKey(command)) {
result = customizedCommandMap.get(command).execute(command, arguments);
}
else if (this.shell.listCommands().containsKey(command)) {
result = this.shell.evaluate(() -> commandLine);
}
else {
result = executeOsCommand(commandLine);
// result = new Exception("Command not found!");
}
}
catch (Exception e) {
result = e;
}
String textOutput;
if (result == null) {
textOutput = "";
}
else if (result instanceof Mono) {
return ((Mono