Search in sources :

Example 1 with YamlObject

use of com.bluenimble.platform.cli.impls.YamlObject in project serverless by bluenimble.

the class RemoteUtils method processRequest.

public static CommandResult processRequest(Tool tool, JsonObject source, final Map<String, String> options) throws CommandExecutionException {
    if (Lang.isDebugMode()) {
        tool.printer().content("Remote Command", source.toString());
    }
    JsonObject oKeys = null;
    Keys keys = BlueNimble.keys();
    if (keys != null) {
        oKeys = keys.json();
    } else {
        oKeys = new JsonObject();
    }
    @SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    Object oTrustAll = vars.get(DefaultVars.SslTrust);
    if (oTrustAll == null) {
        Http.setTrustAll(false);
    } else {
        Http.setTrustAll(Lang.TrueValues.contains(String.valueOf(oTrustAll)));
    }
    boolean isOutFile = AbstractTool.CMD_OUT_FILE.equals(vars.get(AbstractTool.CMD_OUT));
    List<Object> streams = new ArrayList<Object>();
    HttpResponse response = null;
    try {
        HttpRequest request = request(oKeys, Json.getObject(source, Spec.request.class.getSimpleName()), tool, BlueNimble.Config, options, streams);
        response = Http.send(request);
        String contentType = response.getContentType();
        if (contentType == null) {
            contentType = ApiContentTypes.Text;
        }
        int indexOfSemi = contentType.indexOf(Lang.SEMICOLON);
        if (indexOfSemi > 0) {
            contentType = contentType.substring(0, indexOfSemi).trim();
        }
        OutputStream out = System.out;
        if (Printable.contains(contentType) && !isOutFile) {
            out = new ByteArrayOutputStream();
            response.getBody().dump(out, Encodings.UTF8, null);
        }
        List<HttpHeader> rHeaders = response.getHeaders();
        if (rHeaders != null && !rHeaders.isEmpty()) {
            JsonObject oHeaders = new JsonObject();
            for (HttpHeader h : rHeaders) {
                oHeaders.set(h.getName(), Lang.join(h.getValues(), Lang.COMMA));
            }
            vars.put(RemoteResponseHeaders, oHeaders);
        }
        if (contentType.startsWith(ApiContentTypes.Json)) {
            JsonObject result = new JsonObject(new String(((ByteArrayOutputStream) out).toByteArray()));
            String trace = null;
            if (response.getStatus() >= 400) {
                trace = result.getString("trace");
                result.remove("trace");
            }
            if (trace != null && Lang.isDebugMode()) {
                vars.put(RemoteResponseError, trace);
            }
            if (response.getStatus() < 400) {
                return new DefaultCommandResult(CommandResult.OK, result);
            } else {
                return new DefaultCommandResult(CommandResult.KO, result);
            }
        } else if (contentType.startsWith(ApiContentTypes.Yaml)) {
            Yaml yaml = new Yaml();
            String ys = new String(((ByteArrayOutputStream) out).toByteArray());
            ys = Lang.replace(ys, Lang.TAB, "  ");
            @SuppressWarnings("unchecked") Map<String, Object> map = yaml.loadAs(ys, Map.class);
            Object trace = null;
            if (response.getStatus() >= 400) {
                trace = map.get("trace");
                map.remove("trace");
            }
            if (trace != null && Lang.isDebugMode()) {
                vars.put(RemoteResponseError, trace);
            }
            if (response.getStatus() < 400) {
                return new DefaultCommandResult(CommandResult.OK, new YamlObject(map));
            } else {
                return new DefaultCommandResult(CommandResult.KO, new YamlObject(map));
            }
        } else if (contentType.startsWith(ApiContentTypes.Text) || contentType.startsWith(ApiContentTypes.Html)) {
            String content = new String(((ByteArrayOutputStream) out).toByteArray());
            if (response.getStatus() < 400) {
                return new DefaultCommandResult(CommandResult.OK, content);
            } else {
                return new DefaultCommandResult(CommandResult.KO, content);
            }
        } else {
            if (response.getStatus() < 400) {
                if (isOutFile) {
                    return new DefaultCommandResult(CommandResult.OK, response.getBody().get(0).toInputStream());
                } else {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    response.getBody().dump(baos, Encodings.UTF8, null);
                    return new DefaultCommandResult(CommandResult.OK, new String(((ByteArrayOutputStream) baos).toByteArray()));
                }
            } else {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                response.getBody().dump(baos, Encodings.UTF8, null);
                return new DefaultCommandResult(CommandResult.KO, new String(((ByteArrayOutputStream) baos).toByteArray()));
            }
        }
    } catch (Exception e) {
        throw new CommandExecutionException(e.getMessage(), e);
    } finally {
        if (streams != null) {
            for (Object s : streams) {
                if (s instanceof InputStream) {
                    IOUtils.closeQuietly((InputStream) s);
                } else if (s instanceof StreamPointer) {
                    StreamPointer sp = (StreamPointer) s;
                    IOUtils.closeQuietly(sp.getStream());
                    if (sp.shouldDelete()) {
                        try {
                            FileUtils.delete(sp.getPointer());
                        } catch (IOException e) {
                        // IGNORE
                        }
                    }
                }
            }
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) JsonObject(com.bluenimble.platform.json.JsonObject) StreamPointer(com.bluenimble.platform.cli.command.parser.converters.StreamPointer) DefaultCommandResult(com.bluenimble.platform.cli.command.impls.DefaultCommandResult) HttpHeader(com.bluenimble.platform.http.HttpHeader) HttpRequest(com.bluenimble.platform.http.request.HttpRequest) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpResponse(com.bluenimble.platform.http.response.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HttpEndpoint(com.bluenimble.platform.http.HttpEndpoint) Yaml(org.yaml.snakeyaml.Yaml) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) IOException(java.io.IOException) Keys(com.bluenimble.platform.icli.mgm.Keys) YamlObject(com.bluenimble.platform.cli.impls.YamlObject) JsonObject(com.bluenimble.platform.json.JsonObject) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) YamlObject(com.bluenimble.platform.cli.impls.YamlObject) Spec(com.bluenimble.platform.icli.mgm.commands.mgm.RemoteCommand.Spec) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with YamlObject

use of com.bluenimble.platform.cli.impls.YamlObject in project serverless by bluenimble.

the class SetCommand method execute.

@SuppressWarnings("unchecked")
@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    String cmd = (String) tool.currentContext().get(ToolContext.CommandLine);
    if (Lang.isNullOrEmpty(cmd)) {
        throw new CommandExecutionException("command '" + name + "' missing arguments").showUsage();
    }
    String[] nameValue = Lang.split(cmd, Lang.SPACE, true);
    String varName = nameValue[0];
    Object value = Lang.BLANK;
    if (nameValue.length > 1) {
        value = nameValue[1];
    }
    if (value == null) {
        value = Lang.BLANK;
    }
    final Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    try {
        if (varName.equals(Tool.ParaPhraseVar)) {
            tool.setParaphrase((String) value, true);
            tool.printer().content("Security", "Paraphase Updated");
        }
        if (value instanceof YamlObject) {
            value = ((YamlObject) value).toJson();
        }
        value = varName.equals(Tool.ParaPhraseVar) ? tool.getParaphrase(false) : value;
        vars.put(varName, value);
        tool.saveVariable(varName, value);
    } catch (Exception e) {
        throw new CommandExecutionException(e.getMessage(), e);
    }
    tool.printer().content(varName, String.valueOf(vars.get(varName)));
    return null;
}
Also used : CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException) YamlObject(com.bluenimble.platform.cli.impls.YamlObject) YamlObject(com.bluenimble.platform.cli.impls.YamlObject) Map(java.util.Map) CommandExecutionException(com.bluenimble.platform.cli.command.CommandExecutionException)

Example 3 with YamlObject

use of com.bluenimble.platform.cli.impls.YamlObject in project serverless by bluenimble.

the class VarsCommand method execute.

@SuppressWarnings("unchecked")
@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
    String varName = (String) tool.currentContext().get(ToolContext.CommandLine);
    Object result = "";
    if (!Lang.isNullOrEmpty(varName)) {
        Object value = vars.get(varName);
        if (value != null) {
            if (value instanceof JsonObject) {
                result = value;
            } else {
                result = varName + Lang.EQUALS + value;
            }
        } else {
            result = "variable " + varName + " not found";
        }
    } else {
        if (vars != null && !vars.isEmpty()) {
            result = new YamlObject(vars);
        }
    }
    return new DefaultCommandResult(CommandResult.OK, result);
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) YamlObject(com.bluenimble.platform.cli.impls.YamlObject) JsonObject(com.bluenimble.platform.json.JsonObject) YamlObject(com.bluenimble.platform.cli.impls.YamlObject) Map(java.util.Map)

Aggregations

YamlObject (com.bluenimble.platform.cli.impls.YamlObject)3 Map (java.util.Map)3 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)2 JsonObject (com.bluenimble.platform.json.JsonObject)2 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)1 StreamPointer (com.bluenimble.platform.cli.command.parser.converters.StreamPointer)1 HttpEndpoint (com.bluenimble.platform.http.HttpEndpoint)1 HttpHeader (com.bluenimble.platform.http.HttpHeader)1 HttpRequest (com.bluenimble.platform.http.request.HttpRequest)1 HttpResponse (com.bluenimble.platform.http.response.HttpResponse)1 Keys (com.bluenimble.platform.icli.mgm.Keys)1 Spec (com.bluenimble.platform.icli.mgm.commands.mgm.RemoteCommand.Spec)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Yaml (org.yaml.snakeyaml.Yaml)1