store tickets and passwords in config files

format output
This commit is contained in:
Peter Hormanns 2012-07-25 16:50:49 +00:00
parent 7b0315df1f
commit 6ce4645026
4 changed files with 209 additions and 18 deletions

View File

@ -3,12 +3,17 @@ package de.hsadmin.jscli;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.Console; 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.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Properties;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -21,12 +26,15 @@ public class CASTicket {
private String backendURL; private String backendURL;
private String grantingTicket; private String grantingTicket;
private String runAs; private String runAs;
private String user;
public CASTicket(String user, String runAs) throws JSCliException { public CASTicket(String user, String runAs) throws JSCliException {
this.user = user;
this.runAs = runAs; this.runAs = runAs;
loginURL = Config.getInstance().getProperty("loginURL", LOGIN_URL); Config config = Config.getInstance();
backendURL = Config.getInstance().getProperty("backendURL", BACKEND_URL); loginURL = config.getProperty("loginURL", LOGIN_URL);
grantingTicket = getGrantingTicket(user); backendURL = config.getProperty("backendURL", BACKEND_URL);
grantingTicket = readFiledGrantingTicket();
} }
public String getTicket() throws JSCliException { public String getTicket() throws JSCliException {
@ -43,28 +51,35 @@ public class CASTicket {
return runAs; return runAs;
} }
private String getGrantingTicket(String user) throws JSCliException { private String getGrantingTicket() throws JSCliException {
grantingTicket = null; 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; char[] password = null;
Console console = System.console(); Console console = System.console();
if (console == null) { if (console == null) {
throw new JSCliException("fatal error: console not found"); throw new JSCliException("fatal error: console not found");
} }
password = console.readPassword("password:"); password = console.readPassword("password:");
// password = "test123".toCharArray();
if (password == null || password.length <= 0) { if (password == null || password.length <= 0) {
throw new JSCliException("no password given"); throw new JSCliException("no password given");
} }
try { return new String(password);
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;
} }
private String doHttpPost(String urlString, String encodedParams) throws JSCliException { private String doHttpPost(String urlString, String encodedParams) throws JSCliException {
@ -91,12 +106,48 @@ public class CASTicket {
if (ticket != null && ticket.startsWith("ST-")) { if (ticket != null && ticket.startsWith("ST-")) {
result = ticket; result = ticket;
} }
} catch (FileNotFoundException e) {
grantingTicket = getGrantingTicket();
writeFiledGrantingTicket(grantingTicket);
return getTicket();
} catch (IOException e) { } catch (IOException e) {
throw new JSCliException(e); throw new JSCliException(e);
} }
return result; 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 @Override
public String toString() { public String toString() {
return grantingTicket; return grantingTicket;

View 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);
}
}

View File

@ -1,14 +1,35 @@
package de.hsadmin.jscli; package de.hsadmin.jscli;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
JSONFormatter formatter = new JSONFormatter();
CommandlineParser cmdParser = new CommandlineParser(args); CommandlineParser cmdParser = new CommandlineParser(args);
ScriptClient scriptClient = new ScriptClient(cmdParser.getUser(), cmdParser.getRunAs()); ScriptClient scriptClient = new ScriptClient(cmdParser.getUser(), cmdParser.getRunAs());
Object o = scriptClient.execute(cmdParser.getExpression()); String expr = cmdParser.getExpression();
System.out.println(o); 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) { } catch (JSCliException e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
System.exit(-1); System.exit(-1);

View File

@ -1,5 +1,6 @@
package de.hsadmin.jscli; package de.hsadmin.jscli;
import java.io.Reader;
import java.util.List; import java.util.List;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
@ -20,11 +21,23 @@ public class ScriptClient {
engine.put("xmlrpcclient", rpcClient); engine.put("xmlrpcclient", rpcClient);
engine.eval("importClass(java.util.ArrayList);"); engine.eval("importClass(java.util.ArrayList);");
engine.eval("importClass(java.util.HashMap);"); 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) { " + engine.eval("function hsaParseParamObject(o) { " +
"var hsh = new HashMap(); " + "var hsh = new HashMap(); " +
"for (var key in o) { " + "for (var key in o) { " +
" var val = o[key]; " + " 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); " + " hsh.put(key, val); " +
"}; " + "}; " +
"return hsh; " + "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);
}
}
} }