[CB-861] Header support for FileTransfer download

Added support for an optional options object as the final arg. Currently only handles the options.headers object (as per the issue).

`FileTransfer.download(source, target, successCallback, errorCallback, trustAllHosts, options)`

This is needed for using FileTransfer.download with Basic Authentication, etc. Sadly since Android 2.x doesn't support XHR2, this is needed in FileTransfer.

I have only added support to Android and iOS (see other PR's).
This commit is contained in:
Tommy-Carlos Williams 2013-02-24 14:52:52 +11:00 committed by Andrew Grieve
parent 5d79d6e134
commit aa4820c3b7

View File

@ -158,6 +158,25 @@ public class FileTransfer extends CordovaPlugin {
return false; return false;
} }
private static void addHeadersToRequest(URLConnection connection, JSONObject headers) {
try {
for (Iterator<?> iter = headers.keys(); iter.hasNext(); ) {
String headerKey = iter.next().toString();
JSONArray headerValues = headers.optJSONArray(headerKey);
if (headerValues == null) {
headerValues = new JSONArray();
headerValues.put(headers.getString(headerKey));
}
connection.setRequestProperty(headerKey, headerValues.getString(0));
for (int i = 1; i < headerValues.length(); ++i) {
connection.addRequestProperty(headerKey, headerValues.getString(i));
}
}
} catch (JSONException e1) {
// No headers to be manipulated!
}
}
/** /**
* Uploads the specified file to the server URL provided using an HTTP multipart request. * Uploads the specified file to the server URL provided using an HTTP multipart request.
* @param source Full path of the file on the file system * @param source Full path of the file on the file system
@ -272,22 +291,7 @@ public class FileTransfer extends CordovaPlugin {
// Handle the other headers // Handle the other headers
if (headers != null) { if (headers != null) {
try { addHeadersToRequest(conn, headers);
for (Iterator<?> iter = headers.keys(); iter.hasNext(); ) {
String headerKey = iter.next().toString();
JSONArray headerValues = headers.optJSONArray(headerKey);
if (headerValues == null) {
headerValues = new JSONArray();
headerValues.put(headers.getString(headerKey));
}
conn.setRequestProperty(headerKey, headerValues.getString(0));
for (int i = 1; i < headerValues.length(); ++i) {
conn.addRequestProperty(headerKey, headerValues.getString(i));
}
}
} catch (JSONException e1) {
// No headers to be manipulated!
}
} }
/* /*
@ -616,6 +620,7 @@ public class FileTransfer extends CordovaPlugin {
final boolean trustEveryone = args.optBoolean(2); final boolean trustEveryone = args.optBoolean(2);
final String objectId = args.getString(3); final String objectId = args.getString(3);
final JSONObject headers = args.optJSONObject(4);
final URL url; final URL url;
try { try {
@ -693,6 +698,11 @@ public class FileTransfer extends CordovaPlugin {
{ {
connection.setRequestProperty("cookie", cookie); connection.setRequestProperty("cookie", cookie);
} }
// Handle the other headers
if (headers != null) {
addHeadersToRequest(connection, headers);
}
connection.connect(); connection.connect();