CB-5059 Add a CookieManager abstraction for pluggable webviews (close #60)

This allows FileTransfer to correctly use webview cookies regardless of
which webview engine is in use.
This commit is contained in:
Darryl Pogue
2015-01-27 21:14:45 -08:00
committed by Andrew Grieve
parent adf0816544
commit c12f1eb9be
+32 -2
View File
@@ -213,6 +213,34 @@ public class FileTransfer extends CordovaPlugin {
}
}
private String getCookies(final String target) {
boolean gotCookie = false;
String cookie = null;
Class webViewClass = webView.getClass();
try {
Method gcmMethod = webViewClass.getMethod("getCookieManager");
Class iccmClass = gcmMethod.getReturnType();
Method gcMethod = iccmClass.getMethod("getCookie");
cookie = (String)gcMethod.invoke(
iccmClass.cast(
gcmMethod.invoke(webView)
), target);
gotCookie = true;
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (ClassCastException e) {
}
if (!gotCookie) {
cookie = CookieManager.getInstance().getCookie(target);
}
return cookie;
}
/**
* 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
@@ -316,7 +344,8 @@ public class FileTransfer extends CordovaPlugin {
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
// Set the cookies on the response
String cookie = CookieManager.getInstance().getCookie(target);
String cookie = getCookies(target);
if (cookie != null) {
conn.setRequestProperty("Cookie", cookie);
}
@@ -781,7 +810,8 @@ public class FileTransfer extends CordovaPlugin {
connection.setRequestMethod("GET");
// TODO: Make OkHttp use this CookieManager by default.
String cookie = CookieManager.getInstance().getCookie(sourceUri.toString());
String cookie = getCookies(sourceUri.toString());
if(cookie != null)
{
connection.setRequestProperty("cookie", cookie);