Skip to content

Instantly share code, notes, and snippets.

@bamedro
Created May 23, 2013 08:11
Show Gist options
  • Save bamedro/5633426 to your computer and use it in GitHub Desktop.
Save bamedro/5633426 to your computer and use it in GitHub Desktop.
private String submitToScheduler(File jobFile) throws AuthenticationException, IOException {
// Submit XML Job to the Scheduler
String sessid = connectToScheduler(schedulerUrl, schedulerUsername, schedulerPassword);
String endpoint = schedulerUrl + "/scheduler/submit";
HttpPost post = new HttpPost(endpoint);
post.addHeader("sessionid", sessid);
MultipartEntity me = new MultipartEntity();
me.addPart("descriptor", new FileBody(jobFile, "application/xml"));
post.setEntity(me);
HttpResponse response = httpClient.execute(post);
String result = EntityUtils.toString(response.getEntity());
logger.info("Job submitted: " + result);
return result;
}
private String connectToScheduler(String restapi, String username, String password) throws AuthenticationException {
// Authenticate to the Scheduler
try {
String endpoint = restapi + "/scheduler/login";
HttpPost post = new HttpPost(endpoint);
post.addHeader("Content-type", "application/x-www-form-urlencoded");
post.setEntity(new StringEntity("username=" + username + "&password=" + password, "UTF-8"));
HttpResponse response = httpClient.execute(post);
String sessid = EntityUtils.toString(response.getEntity());
logger.debug("Scheduler Session ID: " + sessid);
return sessid;
} catch (Throwable e) {
throw new AuthenticationException("Failed authenticating to the Scheduler", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment