issues in project zip file uploading

246 views
Skip to first unread message

haide...@gmail.com

unread,
May 28, 2015, 9:10:20 AM5/28/15
to azkab...@googlegroups.com
Hi Richard/Team


   I am trying to upload project zip from from my code but unable to upload it however I am able to login and create project.

I have written below code. Please let me know where I am doing wrong. You help will be highly appreciated.


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class PostAFile {
 private String cookies;
 private HttpClient client = HttpClientBuilder.create().build();
 private final String USER_AGENT = "Mozilla/5.0";
 static String sessionId=null;
public static void main(String[] args) throws Exception {
String url = "http://10.188.73.95:8085";

CookieHandler.setDefault(new CookieManager());
PostAFile http = new PostAFile();
String page = http.GetPageContent(url);
//List<NameValuePair> postParams =               http.getFormParams(page, "username","password");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("action", "login"));
urlParameters.add(new BasicNameValuePair("username", "azkaban"));
urlParameters.add(new BasicNameValuePair("password", "azkaban"));
http.sendPost(url, urlParameters);
urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("ajax", "upload"));
urlParameters.add(new BasicNameValuePair("project", "aztest1;type/plain"));
urlParameters.add(new BasicNameValuePair("file", "D:\\MyFile.zip;type=application/x-zip-compressed"));
urlParameters.add(new BasicNameValuePair("session.id", sessionId));
http.sendPost1(url, urlParameters);
//String result = http.GetPageContent(gmail);
//System.out.println(result);
}
private void sendPost(String url, List<NameValuePair> urlParameters) 
       throws Exception {
 
HttpPost post = new HttpPost(url);
 
// add header
//post.setHeader("Host", "accounts.google.com");
post.setHeader("User-Agent", USER_AGENT);
post.setHeader("Accept", 
            "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.setHeader("Accept-Language", "en-US,en;q=0.5");
post.setHeader("Cookie","azkaban.browser.session.id="+ getCookies());
post.setHeader("Connection", "keep-alive");
post.setHeader("X-Requested-With", "XMLHttpRequest");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
 
post.setEntity(new UrlEncodedFormEntity(urlParameters));
 
HttpResponse response = client.execute(post);
 
int responseCode = response.getStatusLine().getStatusCode();
// System.out.println(response.getParams().getParameter("session.id"));
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader rd = new BufferedReader(
               new InputStreamReader(response.getEntity().getContent()));
 
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
JSONObject jsonObj = new JSONObject(result.toString());
sessionId = jsonObj.getString("session.id");
if(sessionId != null){
setCookies(sessionId);
}
System.out.println("Results:"+result.toString());
}


private void sendPost1(String url, List<NameValuePair> urlParameters) 
       throws Exception {
 
HttpPost post = new HttpPost(url);
 
// add header
//post.setHeader("Host", "accounts.google.com");
post.setHeader("User-Agent", USER_AGENT);
post.setHeader("Accept", 
            "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.setHeader("Accept-Language", "en-US,en;q=0.5");
post.setHeader("Cookie","azkaban.browser.session.id="+ getCookies());
post.setHeader("Connection", "keep-alive");
post.setHeader("X-Requested-With", "XMLHttpRequest");
// post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setHeader("Content-Type","multipart/form-data");
 
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
 
int responseCode = response.getStatusLine().getStatusCode();
// System.out.println(response.getParams().getParameter("session.id"));
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader rd = new BufferedReader(
               new InputStreamReader(response.getEntity().getContent()));
 
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
System.out.println(line);
}
JSONObject jsonObj = new JSONObject(result.toString());
sessionId = jsonObj.getString("session.id");
if(sessionId != null){
setCookies(sessionId);
}
System.out.println("Results:"+result.toString());
}
private String GetPageContent(String url) throws Exception {
 
HttpGet request = new HttpGet(url);
 
request.setHeader("User-Agent", USER_AGENT);
request.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
request.setHeader("Accept-Language", "en-US,en;q=0.5");
 
HttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
 
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
 
BufferedReader rd = new BufferedReader(
               new InputStreamReader(response.getEntity().getContent()));
 
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
 
// set cookies
setCookies(response.getFirstHeader("Set-Cookie") == null ? "" : 
                    response.getFirstHeader("Set-Cookie").toString());
 
return result.toString();
 
 }
public List<NameValuePair> getFormParams(
             String html, String username, String password)
throws UnsupportedEncodingException {
 
System.out.println("Extracting form's data...");
 
Document doc = Jsoup.parse(html);
 
// Google form id
Element loginform = doc.getElementById("gaia_loginform");
Elements inputElements = loginform.getElementsByTag("input");
 
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
 
for (Element inputElement : inputElements) {
String key = inputElement.attr("name");
String value = inputElement.attr("value");
 
if (key.equals("Email"))
value = username;
else if (key.equals("Passwd"))
value = password;
 
paramList.add(new BasicNameValuePair(key, value));
 
}
 
return paramList;
  }
public String getCookies() {
return cookies;
 }
 
 public void setCookies(String cookies) {
this.cookies = cookies;
 }
}

vikra...@gmail.com

unread,
Sep 30, 2015, 6:56:45 PM9/30/15
to azkaban, haide...@gmail.com
Hi
Did you get the upload working from code /project?

xishen...@gmail.com

unread,
May 10, 2016, 8:28:22 AM5/10/16
to azkaban, haide...@gmail.com
Hi,

I meet exactly the same issue as you while wrapping AJAX API with Java http request. Hope this issue has already been resolved, would you please show me the solution to this problem:

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


在 2015年5月28日星期四 UTC+8下午9:10:20,haide...@gmail.com写道:

danny...@gmail.com

unread,
Oct 5, 2018, 3:46:44 AM10/5/18
to azkaban
HI, all, I can upload the job file finally. Here is my solution. https://github.com/azkaban/azkaban/issues/764#issuecomment-426954595

xishen...@gmail.com於 2016年5月10日星期二 UTC+8下午8時28分22秒寫道:
Reply all
Reply to author
Forward
0 new messages