-
-
Save larrytech7/957cc61a3bdd82e3e030 to your computer and use it in GitHub Desktop.
Trying to upload a workflow to the player
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class WorkflowProcessTask extends AsyncTask<String, Void, String>{ | |
private Context context; | |
private WorkflowProcessTask(Context context) { | |
this.context = context; | |
} | |
@Override | |
protected void onPreExecute() { | |
progressDialog.setMessage("Uploading Workflow ... "); | |
progressDialog.show(); | |
} | |
@Override | |
protected String doInBackground(String... params) { | |
StringBuffer sb = new StringBuffer(); | |
try { | |
//prepare connection requests | |
URL workflowurl = new URL(params[0]); //the resource xml file representing the workflow to be uploaded to the player | |
String playerurl = new TavernaPlayerAPI(this.context).PLAYER_BASE_URL+"workflows.json"; | |
URL posturl = new URL(playerurl); | |
HttpURLConnection connection = (HttpURLConnection) posturl.openConnection(); | |
HttpURLConnection wconn = (HttpURLConnection) workflowurl.openConnection(); | |
wconn.setRequestMethod("GET"); | |
wconn.setDoOutput(true); | |
wconn.setRequestProperty("Accept", "application/xml"); | |
wconn.connect(); | |
String user = "user" + ":" + "passs"; | |
String basicAuth = "Basic " + Base64.encodeToString(user.getBytes(), Base64.DEFAULT); | |
//read the file from remote resource and encode the stream with a base64 algorithm | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(wconn.getInputStream())); | |
String str = ""; | |
while ((str = bufferedReader.readLine()) != null) | |
sb.append(str); | |
bufferedReader.close(); | |
String data = "{\"document\":\"data:application/octet-stream;base64," + | |
Base64.encodeToString(sb.toString().getBytes("UTF-8"), Base64.URL_SAFE|Base64.NO_WRAP).replace('-','+')+"\"}"; | |
String post = "{\"workflow\":"+data+"}"; | |
//clear sb so that we can use it again to fetch results from this post request | |
sb.delete(0,sb.length()-1); | |
System.out.println("BODY=>"+post); | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Authorization", basicAuth); | |
connection.setRequestProperty("Accept", "*/*"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
connection.setRequestProperty("Content-Encoding", "UTF-8"); | |
connection.setUseCaches (false); | |
connection.setDoOutput(true); | |
connection.connect(); //send request | |
DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); | |
dos.writeBytes(post);//write post data which is a formatted json data representing body of workflow | |
dos.flush(); | |
dos.close(); | |
System.out.println("Post Response Code: "+connection.getResponseCode()); | |
System.out.println("Post response message: "+connection.getResponseMessage()); | |
connection.disconnect(); | |
}catch (IOException e){ | |
e.printStackTrace(); | |
sb.append("Error reading remote workflow. Please try again later"); | |
} | |
return sb.toString(); | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
System.out.println(s); | |
progressDialog.dismiss(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment