I eventually just settled on making it work with CURL embedded into Java:
public String curlConnect() throws Exception {
String r = null;
try {
List<String> theCommand = new ArrayList<String>();
theCommand.add("curl");
theCommand.add("-sk");
theCommand.add(this.surl + "/services/auth/login?output_mode=json");
logger.debug(this.surl+ "/services/auth/login?output_mode=json");
logger.debug("-d username=" + this.user + " password=XXXXXX");
theCommand.add("-d");
theCommand.add("username=" + this.user);
theCommand.add("-d");
theCommand.add("password=" + this.token);
ProcessBuilder pb = new ProcessBuilder(theCommand);
pb.directory(new File(System.getProperty("input.file.path")));
pb.redirectErrorStream(true);
Process p = pb.start();
InputStream is = p.getInputStream();
r = IOUtils.toString(is);
r = r.substring(r.indexOf("{\"sessionKey\":\""), r.length());
JSONObject jsonResponse = new JSONObject(r);
this.sessionKey = jsonResponse.getString("sessionKey");
r = this.sessionKey;
logger.debug(r);
} catch ( Exception e ) {
logger.error(this.surl);
logger.error(e.getMessage());
throw e;
}
return r;
}
public String query(String splunkQuery) throws Exception {
String r = null;
try {
List<String> theCommand = new ArrayList<String>();
theCommand.add("curl");
theCommand.add("-sku");
theCommand.add(this.user+":"+this.token);
//theCommand.add("-H");
//theCommand.add("\"Authorization: Splunk " + this.sessionKey + "\"");
theCommand.add(this.surl + "/services/search/jobs?output_mode=json");
logger.debug(this.surl+ "/services/search/jobs?output_mode=json");
theCommand.add("-d");
theCommand.add("search=" + URLEncoder.encode(splunkQuery, "UTF-8"));
logger.info("search=" + URLEncoder.encode(splunkQuery, "UTF-8"));
//theCommand.add("-d");
//theCommand.add("output_mode=json");
ProcessBuilder pb = new ProcessBuilder(theCommand);
pb.directory(new File(System.getProperty("input.file.path")));
pb.redirectErrorStream(true);
Process p = pb.start();
InputStream is = p.getInputStream();
String theResult = IOUtils.toString(is);
logger.info(theResult);
theResult = theResult.substring(theResult.indexOf("{\"sid\":\""), theResult.length());
JSONObject jsonResponse = new JSONObject(theResult);
r = jsonResponse.getString("sid");
while ( !resultsReady(r)) {
logger.info("waiting 30 seconds for async report to finish running sid " + r);
java.util.concurrent.TimeUnit.SECONDS.sleep(30);
}
} catch ( Exception e) {
logger.error(this.surl);
logger.error(e.getMessage());
throw e;
}
return r;
}
... View more