From 6ce4645026ed642c2c958b4ac1093c8e7ed95e7b Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Wed, 25 Jul 2012 16:50:49 +0000 Subject: [PATCH] store tickets and passwords in config files format output --- hsarjcli/src/de/hsadmin/jscli/CASTicket.java | 81 ++++++++++++--- .../src/de/hsadmin/jscli/JSONFormatter.java | 98 +++++++++++++++++++ hsarjcli/src/de/hsadmin/jscli/Main.java | 25 ++++- .../src/de/hsadmin/jscli/ScriptClient.java | 23 ++++- 4 files changed, 209 insertions(+), 18 deletions(-) create mode 100644 hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java diff --git a/hsarjcli/src/de/hsadmin/jscli/CASTicket.java b/hsarjcli/src/de/hsadmin/jscli/CASTicket.java index 714feae..e216c3f 100644 --- a/hsarjcli/src/de/hsadmin/jscli/CASTicket.java +++ b/hsarjcli/src/de/hsadmin/jscli/CASTicket.java @@ -3,12 +3,17 @@ package de.hsadmin.jscli; 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 @@ public class CASTicket { 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 @@ public class CASTicket { 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 @@ public class CASTicket { 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; diff --git a/hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java b/hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java new file mode 100644 index 0000000..3ccd9d7 --- /dev/null +++ b/hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java @@ -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); + } + +} diff --git a/hsarjcli/src/de/hsadmin/jscli/Main.java b/hsarjcli/src/de/hsadmin/jscli/Main.java index 864b1b5..3016551 100644 --- a/hsarjcli/src/de/hsadmin/jscli/Main.java +++ b/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); diff --git a/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java b/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java index 958c129..17a2b1f 100644 --- a/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java +++ b/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 @@ public class ScriptClient { 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 class ScriptClient { } } + public Object execute(Reader rd) throws JSCliException { + try { + return engine.eval(rd); + } catch (ScriptException e) { + throw new JSCliException(e); + } + } + }