HSAdmin Backend Domains, E-Mail, Datenbanken
Peter Hormanns
2014-06-16 04be81163aa8df62e0830b498b34f1b0ab594665
refactorings, removed old cli-client
6 files renamed
2 files added
3 files deleted
5 files modified
1047 ■■■■ changed files
hsarjcli/.classpath 1 ●●●● patch | view | raw | blame | history
hsarjcli/build.xml 23 ●●●●● patch | view | raw | blame | history
hsarjcli/resource/js/functions.js 84 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/cli/Base64.java 244 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/cli/HSadmin.java 458 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/cli/Scripting.java 25 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/Main.java 19 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/RpcClient.java 26 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/ScriptClient.java 121 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/cas/CASTicketProvider.java 16 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/conf/CommandlineParser.java 4 ●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/conf/Config.java 2 ●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/console/ConsoleWrapper.java 11 ●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/console/PasswordReader.java 9 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/exception/JSCliException.java 2 ●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/json/JSONFormatter.java 2 ●●● patch | view | raw | blame | history
hsarjcli/.classpath
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="src" path="resource"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="lib" path="lib/commons-cli-1.2.jar"/>
    <classpathentry kind="lib" path="lib/xmlrpc-client-3.1.jar"/>
hsarjcli/build.xml
@@ -5,6 +5,7 @@
    <property name="lib.home" value="${basedir}/lib" />
    <property name="conf.home" value="${basedir}/conf" />
    <property name="src.home" value="${basedir}/src" />
    <property name="resource.home" value="${basedir}/resource" />
    <property name="test.home" value="${basedir}/test" />
    <property name="dist.home" value="${basedir}/dist" />
@@ -15,15 +16,25 @@
    <property name="compile.deprecation" value="true" />
    <property name="compile.optimize" value="true" />
    <path id="cp">
        <fileset dir="${lib.home}">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <target name="clean" description="aufräumen">
        <delete dir="${build.home}" />
        <delete dir="${dist.home}" />
    </target>
    <target name="jar" depends="compile">
        <copy todir="${build.home}/cls">
            <fileset dir="${resource.home}" />
        </copy>
        <jar destfile="build/hsadmin.jar" basedir="build/cls">
            <manifest>
                <attribute name="Main-Class" value="de.hsadmin.cli.HSadmin"/>
                <attribute name="Main-Class" value="de.hsadmin.jscli.Main"/>
                <attribute name="Class-Path" value="../lib/commons-cli-1.2.jar ../lib/jline-1.0.jar ../lib/ws-commons-util-1.0.1.jar ../lib/xmlrpc-client-3.1.jar ../lib/xmlrpc-common-3.1.jar"/>
            </manifest>
        </jar>
    </target>
@@ -31,7 +42,15 @@
    <target name="compile" description="compilieren">
        <mkdir dir="${build.home}" />
        <mkdir dir="${build.home}/cls" />
        <javac srcdir="${src.home}" destdir="${build.home}/cls" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}" encoding="UTF8" includeantruntime="false" />
        <javac
            srcdir="${src.home}"
            destdir="${build.home}/cls"
            classpathref="cp"
            debug="${compile.debug}"
            deprecation="${compile.deprecation}"
            optimize="${compile.optimize}"
            encoding="UTF8"
            includeantruntime="false" />
    </target>
</project>
hsarjcli/resource/js/functions.js
New file
@@ -0,0 +1,84 @@
importClass(java.util.ArrayList);
importClass(java.util.HashMap);
function hsaParseParam(val) {
    if (val instanceof java.util.List) {
        return val;
    }
    if (val instanceof java.util.Map) {
        return val;
    }
    if (typeof val === 'object' && val.constructor === Array) {
        return hsaParseParamArray(val);
    }
    if (typeof val === 'object') {
        return hsaParseParamObject(val);
    }
}
function hsaParseParamArray(o) {
    var lst = new ArrayList();
    var val = '';
    for (var idx=0; idx < o.length; idx++) {
        val = o[idx];
        if (typeof val === 'object' && val.constructor === Array) {
            val = hsaParseParamArray(val);
        }
        else if (typeof val === 'object') {
            val = hsaParseParamObject(val);
        };
        lst.add(val);
    };
    return lst;
}
function hsaParseParamObject(o) {
    var hsh = new HashMap();
    for (var key in o) {
        var val = o[key];
        if (typeof val === 'object' && val.constructor === Array) {
            val = hsaParseParamArray(val);
        }
        else if (typeof val === 'object') {
            val = hsaParseParamObject(val);
        };
        hsh.put(key, val);
    };
    return hsh;
}
function hsaToNativeJSObject(val) {
    if (val instanceof java.util.List) {
        var res = [];
        for (i = 0; i < val.size(); i++) {
            res[i] = hsaToNativeJSObject(val.get(i));
        }
        return res;
    }
    if (val instanceof java.util.Map) {
        var res = {};
        var iter = val.keySet().iterator();
        while (iter.hasNext()) {
            var key = iter.next();
            res[key] = hsaToNativeJSObject(val.get(key));
        }
        return res;
    }
}
function hsaModuleCall(mod, fct, json) {
    var params = new ArrayList();
    params.add(casgrantingticket.getRunAs());
    params.add(casgrantingticket.getTicket());
    if (typeof json === "undefined") {
        json = {where:{}, set:{}};
    }
    if (fct == "update" || fct == "add") {
        params.add(hsaParseParamObject(json["set"]));
    }
    if (fct == "update" || fct == "delete" || fct == "search") {
        params.add(hsaParseParamObject(json["where"]));
    }
    xmlrpcLastResult = xmlrpcclient.execute(mod + "." + fct, params);
    return hsaToNativeJSObject(xmlrpcLastResult);
}
hsarjcli/src/de/hsadmin/cli/Base64.java
File was deleted
hsarjcli/src/de/hsadmin/cli/HSadmin.java
File was deleted
hsarjcli/src/de/hsadmin/cli/Scripting.java
File was deleted
hsarjcli/src/de/hsadmin/jscli/Main.java
@@ -5,19 +5,22 @@
import java.io.FileReader;
import java.io.InputStreamReader;
import de.hsadmin.jscli.conf.CommandlineParser;
import de.hsadmin.jscli.console.ConsoleWrapper;
import de.hsadmin.jscli.json.JSONFormatter;
public class Main {
    public static void main(String[] args) {
        ConsoleWrapper console = null;
        final ConsoleWrapper console = new ConsoleWrapper();
        final JSONFormatter formatter = new JSONFormatter();
        try {
            CommandlineParser cmdParser = new CommandlineParser(args);
            String runAs = cmdParser.getRunAs();
            console = new ConsoleWrapper();
            final CommandlineParser cmdParser = new CommandlineParser(args);
            final String runAs = cmdParser.getRunAs();
            console.open(runAs + "@hsadmin> ");
            JSONFormatter formatter = new JSONFormatter();
            ScriptClient scriptClient = new ScriptClient(console, cmdParser.getUser(), runAs);
            String file = cmdParser.getFile();
            final ScriptClient scriptClient = new ScriptClient(console, cmdParser.getUser(), runAs);
            final String file = cmdParser.getFile();
            if (file != null && file.length() > 0) {
                if ("-".equals(file)) {
                    scriptClient.execute(new InputStreamReader(System.in));
@@ -32,7 +35,7 @@
                    }
                }
            }
            String expr = cmdParser.getExpression();
            final String expr = cmdParser.getExpression();
            if (expr != null && expr.length() > 0) {
                scriptClient.execute(expr);
                console.println(formatter.format(scriptClient.getLastRpcResult()));
hsarjcli/src/de/hsadmin/jscli/RpcClient.java
@@ -9,14 +9,18 @@
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import de.hsadmin.jscli.cas.CASTicketProvider;
import de.hsadmin.jscli.conf.Config;
import de.hsadmin.jscli.exception.JSCliException;
public class RpcClient {
    private static final String XMLRPC_URL = "https://config.hostsharing.net:443/hsar/xmlrpc/hsadmin";
    
    private String xmlrpcURL;
    private XmlRpcClient client;
    private final String xmlrpcURL;
    private final XmlRpcClient client;
    public RpcClient(CASTicket tgt) throws JSCliException {
    public RpcClient(final CASTicketProvider tgt) throws JSCliException {
        try {
            xmlrpcURL = Config.getInstance().getProperty("xmlrpcURL", XMLRPC_URL);
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
@@ -30,24 +34,24 @@
    }
    
    public List<String> listMethods() throws JSCliException {
        List<String> methodList = new ArrayList<String>();
        List<Object> execute = execute("system.listMethods");
        for (Object obj : execute) {
        final List<String> methodList = new ArrayList<String>();
        final List<Object> execute = execute("system.listMethods");
        for (final Object obj : execute) {
            methodList.add(obj.toString());
        }
        return methodList;
    }
    private List<Object> execute(String method) throws JSCliException {
    private List<Object> execute(final String method) throws JSCliException {
        return execute(method, new ArrayList<Object>());
    }
    public List<Object> execute(String method, List<?> params) throws JSCliException {
    public List<Object> execute(final String method, final List<?> params) throws JSCliException {
        try {
            Object execute = client.execute(method, params);
            ArrayList<Object> list = new ArrayList<Object>();
            final Object execute = client.execute(method, params);
            final ArrayList<Object> list = new ArrayList<Object>();
            if (execute instanceof Object[]) {
                Object[] resArray = (Object[]) execute;
                final Object[] resArray = (Object[]) execute;
                for (int idx=0; idx < resArray.length; idx++) {
                    list.add(resArray[idx]);                
                }
hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
@@ -1,5 +1,7 @@
package de.hsadmin.jscli;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashSet;
import java.util.List;
@@ -9,99 +11,52 @@
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import de.hsadmin.jscli.cas.CASTicketProvider;
import de.hsadmin.jscli.console.ConsoleWrapper;
import de.hsadmin.jscli.exception.JSCliException;
public class ScriptClient {
    final private ScriptEngine engine;
    final private Set<String> completionStrings;
    public ScriptClient(ConsoleWrapper console, String user, String runAs) throws JSCliException {
    public ScriptClient(final ConsoleWrapper console, final String user, final String runAs) throws JSCliException {
        final CASTicketProvider ticketProvider = new CASTicketProvider(console, user, runAs);
        final RpcClient rpcClient = new RpcClient(ticketProvider);
        final ScriptEngineManager engineManager = new ScriptEngineManager();
        completionStrings = new HashSet<String>();
        CASTicket grantingTicket = new CASTicket(console, user, runAs);
        RpcClient rpcClient = new RpcClient(grantingTicket);
        ScriptEngineManager engineManager = new ScriptEngineManager();
        engine = engineManager.getEngineByName("js");
        engine.put("casgrantingticket", ticketProvider);
        engine.put("xmlrpcclient", rpcClient);
        engine.put("xmlrpcLastResult", null);
        try {
            engine.put("casgrantingticket", grantingTicket);
            engine.put("xmlrpcclient", rpcClient);
            engine.put("xmlrpcLastResult", null);
            engine.eval("importClass(java.util.ArrayList);");
            engine.eval("importClass(java.util.HashMap);");
            engine.eval("function hsaParseParam(val) { " +
                    "if (val instanceof java.util.List) return val;" +
                    "if (val instanceof java.util.Map) return val;" +
                    "if (typeof val === 'object' && val.constructor === Array) { res = hsaParseParamArray(val); } " +
                    "else if (typeof val === 'object') { res = hsaParseParamObject(val); }; " +
                    "return res; " +
                    "}");
            engine.eval("function hsaParseParamArray(o) { " +
                    "var lst = new ArrayList(); " +
                    "var val = ''; " +
                    "for (var idx=0; idx < o.length; idx++) { " +
                    "  val = o[idx]; " +
                    "  if (typeof val === 'object' && val.constructor === Array) { val = hsaParseParamArray(val); } " +
                    "  else if (typeof val === 'object') { val = hsaParseParamObject(val); }; " +
                    "  lst.add(val); " +
                    "}; " +
                    "return lst; " +
                    "}");
            engine.eval("function hsaParseParamObject(o) { " +
                    "var hsh = new HashMap(); " +
                    "for (var key in o) { " +
                    "  var val = o[key]; " +
                    "  if (typeof val === 'object' && val.constructor === Array) { val = hsaParseParamArray(val); } " +
                    "  else if (typeof val === 'object') { val = hsaParseParamObject(val); }; " +
                    "  hsh.put(key, val); " +
                    "}; " +
                    "return hsh; " +
                    "}");
            engine.eval("function hsaToNativeJSObject(val) { " +
                    "if (val instanceof java.util.List) {" +
                    "    var res = [];" +
                    "    for (i = 0; i < val.size(); i++) {" +
                    "        res[i] = hsaToNativeJSObject(val.get(i));" +
                    "    }" +
                    "    return res;" +
                    "}" +
                    "if (val instanceof java.util.Map) {" +
                    "    var res = {};" +
                    "    var iter = val.keySet().iterator();" +
                    "    while (iter.hasNext()) {" +
                    "        var key = iter.next();" +
                    "        res[key] = hsaToNativeJSObject(val.get(key));" +
                    "    }" +
                    "    return res;" +
                    "}" +
                    "return val;" +
                    "};");
            final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js");
            engine.eval(new InputStreamReader(inputResource));
        } catch (ScriptException e) {
            throw new JSCliException(e);
        }
        List<String> methods = rpcClient.listMethods();
        for (String method : methods) {
            String[] parts = method.split("\\.");
        final List<String> methods = rpcClient.listMethods();
        for (final String method : methods) {
            final String[] parts = method.split("\\.");
            if (parts.length == 2) {
                String module = parts[0];
                String function = parts[1];
                if ("delete".equals(function)) {
                    function = "remove";
                }
                final String module = parts[0];
                completionStrings.add(module);
                completionStrings.add(module + "." + function);
                final String function = parts[1];
                final String jsFunctionIdent;
                if ("delete".equals(function)) {
                    jsFunctionIdent = module + "['remove']";
                    completionStrings.add(module + ".remove");
                } else {
                    jsFunctionIdent = module + "['" + function + "']";
                    completionStrings.add(module + "." + function);
                }
                try {
                    engine.eval("if (typeof " + module + " === 'undefined') " +
                                "{ var " + module + " = { }; };\n" +
                                module + "['" + function + "'] = function(json) { " +
                                "var mod = '" + module + "'; " +
                                ("remove".equals(function) ? "var fct = 'delete'; " : "var fct = '" + function + "'; ") +
                                "var params = new ArrayList(); " +
                                "params.add(casgrantingticket.getRunAs()); " +
                                "params.add(casgrantingticket.getTicket()); " +
                                "if (typeof json === 'undefined') { json = { where:{}, set:{} } };" +
                                "if (fct == 'update' || fct == 'add') { params.add(hsaParseParamObject(json['set'])); }; " +
                                "if (fct == 'update' || fct == 'delete' || fct == 'search') { params.add(hsaParseParamObject(json['where'])); }; " +
                                "xmlrpcLastResult = xmlrpcclient.execute(mod + '.' + fct, params);" +
                                "return hsaToNativeJSObject(xmlrpcLastResult); " +
                                "};");
                    engine.eval(
                        "if (typeof " + module + " === 'undefined')" +
                        "    { var " + module + " = { }; };\n" +
                        jsFunctionIdent +
                        "   = function(json) { hsaModuleCall('" + module + "', '" + function + "', json); }"
                    );
                } catch (ScriptException e) {
                    e.printStackTrace();
                }
@@ -111,16 +66,16 @@
    }
    
    public String[] getCodeCompletionStrings() {
        String[] codeCompletionStrings = new String[completionStrings.size()];
        final String[] codeCompletionStrings = new String[completionStrings.size()];
        int idx = 0;
        for (String s : completionStrings) {
        for (final String s : completionStrings) {
            codeCompletionStrings[idx] = s;
            idx++;
        }
        return codeCompletionStrings;
    }
    public Object execute(String snippet) throws JSCliException {
    public Object execute(final String snippet) throws JSCliException {
        try {
            engine.put("xmlrpcLastResult", null);
            return engine.eval(snippet);
@@ -129,7 +84,7 @@
        }
    }
    
    public Object execute(Reader rd) throws JSCliException {
    public Object execute(final Reader rd) throws JSCliException {
        try {
            engine.put("xmlrpcLastResult", null);
            return engine.eval(rd);
hsarjcli/src/de/hsadmin/jscli/cas/CASTicketProvider.java
File was renamed from hsarjcli/src/de/hsadmin/jscli/CASTicket.java
@@ -1,4 +1,4 @@
package de.hsadmin.jscli;
package de.hsadmin.jscli.cas;
import java.io.BufferedReader;
import java.io.BufferedWriter;
@@ -15,7 +15,11 @@
import javax.net.ssl.HttpsURLConnection;
public class CASTicket {
import de.hsadmin.jscli.conf.Config;
import de.hsadmin.jscli.console.PasswordReader;
import de.hsadmin.jscli.exception.JSCliException;
public class CASTicketProvider {
    private static final String LOGIN_URL = "https://login.hostsharing.net:443/cas/v1/tickets";
    private static final String BACKEND_URL = "https://admin.hostsharing.net:443/hsar/backend";
@@ -24,12 +28,12 @@
    final private String backendURL;
    final private String runAs;
    final private String user;
    final private ConsoleWrapper cons;
    final private PasswordReader passwordReader;
    private String grantingTicket;
    public CASTicket(ConsoleWrapper console, String user, String runAs) throws JSCliException {
        this.cons = console;
    public CASTicketProvider(final PasswordReader console, final String user, final String runAs) throws JSCliException {
        this.passwordReader = console;
        this.user = user;
        this.runAs = runAs;
        Config config = Config.getInstance();
@@ -78,7 +82,7 @@
    }
    private String readPasswordFromConsole() throws JSCliException {
        return cons.readPassword();
        return passwordReader.readPassword();
    }
    private String doHttpPost(String urlString, String encodedParams) throws JSCliException {
hsarjcli/src/de/hsadmin/jscli/conf/CommandlineParser.java
File was renamed from hsarjcli/src/de/hsadmin/jscli/CommandlineParser.java
@@ -1,4 +1,4 @@
package de.hsadmin.jscli;
package de.hsadmin.jscli.conf;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
@@ -6,6 +6,8 @@
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import de.hsadmin.jscli.exception.JSCliException;
public class CommandlineParser {
hsarjcli/src/de/hsadmin/jscli/conf/Config.java
File was renamed from hsarjcli/src/de/hsadmin/jscli/Config.java
@@ -1,4 +1,4 @@
package de.hsadmin.jscli;
package de.hsadmin.jscli.conf;
import java.io.File;
import java.io.FileReader;
hsarjcli/src/de/hsadmin/jscli/console/ConsoleWrapper.java
File was renamed from hsarjcli/src/de/hsadmin/jscli/ConsoleWrapper.java
@@ -1,11 +1,12 @@
package de.hsadmin.jscli;
package de.hsadmin.jscli.console;
import java.io.IOException;
import de.hsadmin.jscli.exception.JSCliException;
import jline.ConsoleReader;
import jline.SimpleCompletor;
public class ConsoleWrapper {
public class ConsoleWrapper implements PasswordReader {
    private ConsoleReader cons;
    private String prompt;
@@ -32,7 +33,7 @@
        }
    }
    
    public void println(String text) throws JSCliException {
    public void println(final String text) throws JSCliException {
        try {
            if (cons != null) {
                cons.printString(text);
@@ -47,7 +48,7 @@
    public String readPassword() throws JSCliException {
        try {
            String pw = cons.readLine("Password: ", new Character('*'));
            final String pw = cons.readLine("Password: ", new Character('*'));
            cons.setDefaultPrompt(prompt);
            return pw;
        } catch (IOException e) {
@@ -55,7 +56,7 @@
        }
    }
    public void codeCompletion(String[] candidateStrings) {
    public void codeCompletion(final String[] candidateStrings) {
        cons.addCompletor(new SimpleCompletor(candidateStrings));
    }
hsarjcli/src/de/hsadmin/jscli/console/PasswordReader.java
New file
@@ -0,0 +1,9 @@
package de.hsadmin.jscli.console;
import de.hsadmin.jscli.exception.JSCliException;
public interface PasswordReader {
    String readPassword() throws JSCliException;
}
hsarjcli/src/de/hsadmin/jscli/exception/JSCliException.java
File was renamed from hsarjcli/src/de/hsadmin/jscli/JSCliException.java
@@ -1,4 +1,4 @@
package de.hsadmin.jscli;
package de.hsadmin.jscli.exception;
public class JSCliException extends Exception {
hsarjcli/src/de/hsadmin/jscli/json/JSONFormatter.java
File was renamed from hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java
@@ -1,4 +1,4 @@
package de.hsadmin.jscli;
package de.hsadmin.jscli.json;
import java.util.List;
import java.util.Map;