Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload project failed with AJAX API Wrappered by HttpClient #764

Open
zxsimple opened this issue Oct 7, 2016 · 2 comments
Open

Upload project failed with AJAX API Wrappered by HttpClient #764

zxsimple opened this issue Oct 7, 2016 · 2 comments

Comments

@zxsimple
Copy link

zxsimple commented Oct 7, 2016

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;

}

@zxsimple
Copy link
Author

zxsimple commented Oct 7, 2016

This issue has already been discussed on google group, but no resolution.

https://groups.google.com/forum/#!topic/azkaban-dev/2ZkL6FZg1KY

@dannypv05261
Copy link

dannypv05261 commented Oct 4, 2018

Hi, All,

After trial and error, I can upload the file to the project finally.

I don't know if the document gets something wrong or not, since I make it works by comparing my request with the raw request in azkaban web and reading the source code.

So, there is something which is different from / does not mention in the document in my solution.

  1. The URL is /manger instead of /manager?ajax=upload
  2. Add field "action" with value "upload" in the body
  3. Add boundary in the Context-Type in request header
  4. Add boundary to separate each multipart parameters

I also have a piece of code which uses Apache HttpClient library to make the request and I am using Groovy.

  InputStream inputStream = new FileInputStream("$project.buildDir/azkaban/$project.name" + ".zip")
  def entity = MultipartEntityBuilder
          .create()
          .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
          .addBinaryBody("file", inputStream, ContentType.create("application/x-zip-compressed"), "$project.name" + ".zip")
          .addTextBody("session.id", sessionId, ContentType.TEXT_PLAIN)
          .addTextBody("project", "$project.name", ContentType.TEXT_PLAIN)
          .addTextBody("action", "upload", ContentType.TEXT_PLAIN)
          .setBoundary("----WebKitFormBoundary76hDprXdyeWQOqP6")
          .build()

  def httpPost = new HttpPost(host + "/manager")
  httpPost.setEntity(entity)
  httpPost.setHeader("Content-Type", "multipart/mixed; boundary=----WebKitFormBoundary76hDprXdyeWQOqP6")
  httpPost.setHeader("Accept", "application/json")

  def response = client.execute(httpPost)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants