I am using azkaban 3 in my project to manage jobs, I am using ajax api to operate all steps for flows/jobs, I have achieved all steps including creating project, deleting project, schedule flow and executing flows etc with ajax api using java, but now I have a problem when uploading the zip to azkaban, below are the issue, sincerely hope you can help have a look into this, Thanks very much.
Issue desc: when invoke the uploadZip method it promoted me below error, but when I create project or execute flow with the same way it is fine:
Login error222. Need username and password
net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of Login error222. Need username and password
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:499)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:972)
at net.sf.json.JSONObject._fromString(JSONObject.java:1201)
at net.sf.json.JSONObject.fromObject(JSONObject.java:165)
at net.sf.json.JSONObject.fromObject(JSONObject.java:134)
at com.td.rd.dmsys.jobs.scheduler.AjaxHelper.newPost(AjaxHelper.java:166)
at com.td.rd.dmsys.jobs.scheduler.AzkabanAPI.uploadZip(AzkabanAPI.java:97)
at com.td.rd.dmsys.jobs.controller.AzkabanMain.main(AzkabanMain.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Exception in thread "main" java.lang.NullPointerException
at com.td.rd.dmsys.jobs.scheduler.AzkabanAPI.uploadZip(AzkabanAPI.java:104)
at com.td.rd.dmsys.jobs.controller.AzkabanMain.main(AzkabanMain.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
uploadZip method as below:
public long uploadZip(String zipPath, String proName){
if(sessionId != null){
String uploadZipStr = "session.id=" + sessionId
+ "&ajax=upload&file=@" + zipPath
+ ";type=application/zip&project=" + proName
+ ";type=plain";
JSONObject uploadJson = null;
try {
uploadJson = AjaxHelper.newPost(url + "/manager", uploadZipStr, null, "multipart/mixed");
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error("Upload zip submitted failed, pls check...");
e.printStackTrace();
}
return Long.parseLong(uploadJson.getString("projectId"));
}
return -1L;
}
newPost method as below:
public static JSONObject newPost(String url, String xmlStr, String cookie, String contentType) {
try {
initHttpsURLConnection();
} catch (Exception e) {
e.printStackTrace();
}
JSONObject jsonObj = null;
HttpsURLConnection urlCon = null;
try {
urlCon = (HttpsURLConnection) (new URL(url)).openConnection();
urlCon.setDoInput(true);
urlCon.setDoOutput(true);
urlCon.setRequestMethod("POST");
if(contentType == null || contentType.equals("")) {
urlCon.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
} else {
urlCon.setRequestProperty("Content-Type", contentType + "; boundary=" + "--aifudao7816510d1hq");
}
urlCon.setRequestProperty("X-Requested-With", "XMLHttpRequest");
urlCon.setUseCaches(true);
//if cookie setted
if(cookie != null && !"".equals(cookie)) {
urlCon.setRequestProperty("Cookie", cookie);
}
urlCon.getOutputStream().write(xmlStr.getBytes("gbk"));
urlCon.getOutputStream().flush();
urlCon.getOutputStream().close();
BufferedReader in = new BufferedReader(new InputStreamReader(
urlCon.getInputStream()));
String line="";
String temp;
while ((temp = in.readLine()) != null) {
line = line + temp;
}
System.out.println("xxxxxxxxxxxxxxxxxx " + line);
jsonObj = JSONObject.fromObject(line);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return jsonObj;
}