hsarjcli/src/de/hsadmin/jscli/CASTicket.java | ●●●●● patch | view | raw | blame | history | |
hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java | ●●●●● patch | view | raw | blame | history | |
hsarjcli/src/de/hsadmin/jscli/Main.java | ●●●●● patch | view | raw | blame | history | |
hsarjcli/src/de/hsadmin/jscli/ScriptClient.java | ●●●●● patch | view | raw | blame | history |
hsarjcli/src/de/hsadmin/jscli/CASTicket.java
@@ -3,12 +3,17 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Console; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; import java.util.Properties; import javax.net.ssl.HttpsURLConnection; @@ -21,12 +26,15 @@ private String backendURL; private String grantingTicket; private String runAs; private String user; public CASTicket(String user, String runAs) throws JSCliException { this.user = user; this.runAs = runAs; loginURL = Config.getInstance().getProperty("loginURL", LOGIN_URL); backendURL = Config.getInstance().getProperty("backendURL", BACKEND_URL); grantingTicket = getGrantingTicket(user); Config config = Config.getInstance(); loginURL = config.getProperty("loginURL", LOGIN_URL); backendURL = config.getProperty("backendURL", BACKEND_URL); grantingTicket = readFiledGrantingTicket(); } public String getTicket() throws JSCliException { @@ -43,28 +51,35 @@ return runAs; } private String getGrantingTicket(String user) throws JSCliException { private String getGrantingTicket() throws JSCliException { grantingTicket = null; String password = Config.getInstance().getProperty(user + ".passWord"); if (password == null || password.length() <= 0) { password = readPasswordFromConsole(); } try { String encodedParams = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8") + "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); grantingTicket = doHttpPost(loginURL, encodedParams); } catch (UnsupportedEncodingException e) { throw new JSCliException(e); } return grantingTicket; } private String readPasswordFromConsole() throws JSCliException { char[] password = null; Console console = System.console(); if (console == null) { throw new JSCliException("fatal error: console not found"); } password = console.readPassword("password:"); // password = "test123".toCharArray(); if (password == null || password.length <= 0) { throw new JSCliException("no password given"); } try { String encodedParams = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8") + "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(new String(password), "UTF-8"); grantingTicket = doHttpPost(loginURL, encodedParams); } catch (UnsupportedEncodingException e) { throw new JSCliException(e); } return grantingTicket; return new String(password); } private String doHttpPost(String urlString, String encodedParams) throws JSCliException { @@ -91,12 +106,48 @@ if (ticket != null && ticket.startsWith("ST-")) { result = ticket; } } catch (FileNotFoundException e) { grantingTicket = getGrantingTicket(); writeFiledGrantingTicket(grantingTicket); return getTicket(); } catch (IOException e) { throw new JSCliException(e); } return result; } private String readFiledGrantingTicket() throws JSCliException { String userHome = System.getProperty("user.home"); String ticketFileName = userHome + "/.hsadmin.tgt"; File file = new File(ticketFileName); if (file.isFile() && file.canRead()) { Properties properties = new Properties(); try { properties.load(new FileReader(file)); return properties.getProperty(user); } catch (IOException e) { throw new JSCliException(e); } } return null; } private void writeFiledGrantingTicket(String ticket) throws JSCliException { String userHome = System.getProperty("user.home"); String ticketFileName = userHome + "/.hsadmin.tgt"; File file = new File(ticketFileName); if (file.isFile() && file.canWrite()) { Properties properties = new Properties(); try { properties.load(new FileReader(file)); properties.put(user, ticket); properties.store(new FileWriter(file), "hsscript"); } catch (IOException e) { throw new JSCliException(e); } } } @Override public String toString() { return grantingTicket; hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java
New file @@ -0,0 +1,98 @@ package de.hsadmin.jscli; import java.util.List; import java.util.Map; import java.util.Set; public class JSONFormatter { private int indent = 1; public String formatMap(Map<?, ?> map) { StringBuffer result = new StringBuffer(); result.append('{'); incr(); result.append(newline()); StringBuffer formattedMap = new StringBuffer(); Set<?> keySet = map.keySet(); for (Object key : keySet) { if (formattedMap.length() > 0) { formattedMap.append(','); formattedMap.append(newline()); } formattedMap.append(key.toString()); formattedMap.append(':'); formattedMap.append(format(map.get(key))); } result.append(formattedMap.toString()); decr(); result.append(newline()); result.append('}'); return result.toString(); } public String formatString(String str) { return "'" + str + "'"; } public String formatList(List<?> list) { return formatArr(list.toArray()); } public String formatArr(Object[] arr) { StringBuffer result = new StringBuffer(); result.append('['); incr(); result.append(newline()); StringBuffer formattedList = new StringBuffer(); for (int idx = 0; idx < arr.length; idx ++) { if (formattedList.length() > 0) { formattedList.append(','); formattedList.append(newline()); } formattedList.append(format(arr[idx])); } result.append(formattedList.toString()); decr(); if (formattedList.length() > 0) { result.append(newline()); } result.append(']'); return result.toString(); } public String format(Object object) { if (object instanceof List<?>) { return formatList((List<?>) object); } if (object instanceof Map<?, ?>) { return formatMap((Map<?, ?>) object); } if (object instanceof String) { return formatString((String) object); } if (object instanceof Object[]) { return formatArr((Object[]) object); } return "an instance of " + object.getClass().getCanonicalName(); } private void incr() { indent += 3; } private void decr() { if (indent > 3) { indent -= 3; } } private String newline() { int ind = indent; if (ind > 52) { ind = 52; } return "\n ".substring(0, indent); } } hsarjcli/src/de/hsadmin/jscli/Main.java
@@ -1,14 +1,35 @@ package de.hsadmin.jscli; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { try { JSONFormatter formatter = new JSONFormatter(); CommandlineParser cmdParser = new CommandlineParser(args); ScriptClient scriptClient = new ScriptClient(cmdParser.getUser(), cmdParser.getRunAs()); Object o = scriptClient.execute(cmdParser.getExpression()); System.out.println(o); String expr = cmdParser.getExpression(); if (expr != null && expr.length() > 0) { System.out.println(formatter.format(scriptClient.execute(expr))); } String file = cmdParser.getFile(); if (file != null && file.length() > 0) { if ("-".equals(file)) { System.out.println(formatter.format(scriptClient.execute(new InputStreamReader(System.in)))); } else { File fileHandle = new File(file); try { System.out.println(formatter.format(scriptClient.execute(new FileReader(fileHandle)))); } catch (FileNotFoundException e) { System.err.println("file not found: " + file); } } } } catch (JSCliException e) { System.err.println(e.getMessage()); System.exit(-1); hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
@@ -1,5 +1,6 @@ package de.hsadmin.jscli; import java.io.Reader; import java.util.List; import javax.script.ScriptEngine; @@ -20,11 +21,23 @@ engine.put("xmlrpcclient", rpcClient); engine.eval("importClass(java.util.ArrayList);"); engine.eval("importClass(java.util.HashMap);"); 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 = hsaParseParamObject(val); }; " + " if (typeof val === 'object' && val.constructor === Array) { val = hsaParseParamArray(val); } " + " else if (typeof val === 'object') { val = hsaParseParamObject(val); }; " + " hsh.put(key, val); " + "}; " + "return hsh; " + @@ -67,4 +80,12 @@ } } public Object execute(Reader rd) throws JSCliException { try { return engine.eval(rd); } catch (ScriptException e) { throw new JSCliException(e); } } }