From a81b90e50da28a575f7a5ac126822c36ceb8e06d Mon Sep 17 00:00:00 2001 From: aogilvie Date: Wed, 21 Aug 2013 13:35:11 +0900 Subject: [PATCH] added download capability for basic authentication urls --- .../plugin/WizAssets/WizAssetsPlugin.java | 303 +++++++++--------- .../plugin/WizAssets/WizAssetsPlugin.java | 303 +++++++++--------- 2 files changed, 292 insertions(+), 314 deletions(-) diff --git a/android/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java b/android/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java index 941fe0b..7894c50 100755 --- a/android/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java +++ b/android/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java @@ -18,10 +18,13 @@ import java.net.URL; import java.util.zip.GZIPInputStream; +import android.net.Uri; import android.os.AsyncTask; import android.util.Base64; import org.apache.cordova.api.PluginResult; import org.apache.http.Header; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -40,138 +43,138 @@ public class WizAssetsPlugin extends CordovaPlugin { - private String TAG = "WizAssetsPlugin"; - private WizAssetManager wizAssetMan = null; - - @Override - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { - - if (wizAssetMan == null) { - wizAssetMan = new WizAssetManager(cordova.getActivity().getApplicationContext()); - } - - if ("downloadFile".equals(action)) { - Log.d(TAG, "[downloadFile] *********** "+args.toString() ); - try { - // Split by "/" - String[] splitURL = args.getString(1).split("/"); - - // Last element is name - String fileName = splitURL[splitURL.length-1]; - - // Build directory - Resources appR = cordova.getActivity().getApplicationContext().getResources(); - // Dir name includes game/application name as folder root.. ie. "cordovaExample" - CharSequence txt = appR.getText(appR.getIdentifier("app_name", "string", cordova.getActivity().getApplicationContext().getPackageName()))+"/"; - - String dirName = ""+txt; + private String TAG = "WizAssetsPlugin"; + private WizAssetManager wizAssetMan = null; + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + + if (wizAssetMan == null) { + wizAssetMan = new WizAssetManager(cordova.getActivity().getApplicationContext()); + } + + if ("downloadFile".equals(action)) { + Log.d(TAG, "[downloadFile] *********** "+args.toString() ); + try { + // Split by "/" + String[] splitURL = args.getString(1).split("/"); + + // Last element is name + String fileName = splitURL[splitURL.length-1]; + + // Build directory + Resources appR = cordova.getActivity().getApplicationContext().getResources(); + // Dir name includes game/application name as folder root.. ie. "cordovaExample" + CharSequence txt = appR.getText(appR.getIdentifier("app_name", "string", cordova.getActivity().getApplicationContext().getPackageName()))+"/"; + + String dirName = ""+txt; dirName = ""; - for (int i=0; i>>>>>> "); - JSONObject assetObject = wizAssetMan.getAllAssets(); - Log.d(TAG, "[getFileURIs] RETURN *********** >>>>>>> " + assetObject.toString()); - callbackContext.success(assetObject); - return true; - - } else if ("deleteFiles".equals(action)) { - - // Delete all files from array given - Log.d(TAG, "[deleteFiles] *********** "); - try { - for (int i = 0; i>>>>>> "); + JSONObject assetObject = wizAssetMan.getAllAssets(); + Log.d(TAG, "[getFileURIs] RETURN *********** >>>>>>> " + assetObject.toString()); + callbackContext.success(assetObject); + return true; + + } else if ("deleteFiles".equals(action)) { + + // Delete all files from array given + Log.d(TAG, "[deleteFiles] *********** "); + try { + for (int i = 0; i { @@ -218,15 +221,28 @@ protected String doInBackground(File... params) { httpRequest = new HttpGet(url.toURI()); HttpClient httpclient = new DefaultHttpClient(); + + // Credential check + String credentials = url.getUserInfo(); + if (credentials != null) { + // Add Basic Authentication header + httpRequest.setHeader("Authorization", "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP)); + } + HttpResponse response = httpclient.execute(httpRequest); HttpEntity entity = response.getEntity(); InputStream is; Header contentHeader = entity.getContentEncoding(); - if (contentHeader.getValue().contains("gzip")) { - Log.d(TAG, "GGGGGGGGGZIIIIIPPPPPED!"); - is = new GZIPInputStream(entity.getContent()); + if (contentHeader != null) { + if (contentHeader.getValue().contains("gzip")) { + Log.d(TAG, "GGGGGGGGGZIIIIIPPPPPED!"); + is = new GZIPInputStream(entity.getContent()); + } else { + BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); + is = bufHttpEntity.getContent(); + } } else { BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); is = bufHttpEntity.getContent(); @@ -263,32 +279,5 @@ protected String doInBackground(File... params) { return null; } } -/* - private String decompress(String zipText) throws IOException { - byte[] compressed = Base64.decode(zipText); - if (compressed.length > 4) - { - GZIPInputStream gzipInputStream = new GZIPInputStream( - new ByteArrayInputStream(compressed, 4, - compressed.length - 4)); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - for (int value = 0; value != -1;) { - value = gzipInputStream.read(); - if (value != -1) { - baos.write(value); - } - } - gzipInputStream.close(); - baos.close(); - String sReturn = new String(baos.toByteArray(), "UTF-8"); - return sReturn; - } - else - { - return ""; - } - } - */ } diff --git a/example/android/src/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java b/example/android/src/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java index 941fe0b..7894c50 100755 --- a/example/android/src/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java +++ b/example/android/src/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java @@ -18,10 +18,13 @@ import java.net.URL; import java.util.zip.GZIPInputStream; +import android.net.Uri; import android.os.AsyncTask; import android.util.Base64; import org.apache.cordova.api.PluginResult; import org.apache.http.Header; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -40,138 +43,138 @@ public class WizAssetsPlugin extends CordovaPlugin { - private String TAG = "WizAssetsPlugin"; - private WizAssetManager wizAssetMan = null; - - @Override - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { - - if (wizAssetMan == null) { - wizAssetMan = new WizAssetManager(cordova.getActivity().getApplicationContext()); - } - - if ("downloadFile".equals(action)) { - Log.d(TAG, "[downloadFile] *********** "+args.toString() ); - try { - // Split by "/" - String[] splitURL = args.getString(1).split("/"); - - // Last element is name - String fileName = splitURL[splitURL.length-1]; - - // Build directory - Resources appR = cordova.getActivity().getApplicationContext().getResources(); - // Dir name includes game/application name as folder root.. ie. "cordovaExample" - CharSequence txt = appR.getText(appR.getIdentifier("app_name", "string", cordova.getActivity().getApplicationContext().getPackageName()))+"/"; - - String dirName = ""+txt; + private String TAG = "WizAssetsPlugin"; + private WizAssetManager wizAssetMan = null; + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + + if (wizAssetMan == null) { + wizAssetMan = new WizAssetManager(cordova.getActivity().getApplicationContext()); + } + + if ("downloadFile".equals(action)) { + Log.d(TAG, "[downloadFile] *********** "+args.toString() ); + try { + // Split by "/" + String[] splitURL = args.getString(1).split("/"); + + // Last element is name + String fileName = splitURL[splitURL.length-1]; + + // Build directory + Resources appR = cordova.getActivity().getApplicationContext().getResources(); + // Dir name includes game/application name as folder root.. ie. "cordovaExample" + CharSequence txt = appR.getText(appR.getIdentifier("app_name", "string", cordova.getActivity().getApplicationContext().getPackageName()))+"/"; + + String dirName = ""+txt; dirName = ""; - for (int i=0; i>>>>>> "); - JSONObject assetObject = wizAssetMan.getAllAssets(); - Log.d(TAG, "[getFileURIs] RETURN *********** >>>>>>> " + assetObject.toString()); - callbackContext.success(assetObject); - return true; - - } else if ("deleteFiles".equals(action)) { - - // Delete all files from array given - Log.d(TAG, "[deleteFiles] *********** "); - try { - for (int i = 0; i>>>>>> "); + JSONObject assetObject = wizAssetMan.getAllAssets(); + Log.d(TAG, "[getFileURIs] RETURN *********** >>>>>>> " + assetObject.toString()); + callbackContext.success(assetObject); + return true; + + } else if ("deleteFiles".equals(action)) { + + // Delete all files from array given + Log.d(TAG, "[deleteFiles] *********** "); + try { + for (int i = 0; i { @@ -218,15 +221,28 @@ protected String doInBackground(File... params) { httpRequest = new HttpGet(url.toURI()); HttpClient httpclient = new DefaultHttpClient(); + + // Credential check + String credentials = url.getUserInfo(); + if (credentials != null) { + // Add Basic Authentication header + httpRequest.setHeader("Authorization", "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP)); + } + HttpResponse response = httpclient.execute(httpRequest); HttpEntity entity = response.getEntity(); InputStream is; Header contentHeader = entity.getContentEncoding(); - if (contentHeader.getValue().contains("gzip")) { - Log.d(TAG, "GGGGGGGGGZIIIIIPPPPPED!"); - is = new GZIPInputStream(entity.getContent()); + if (contentHeader != null) { + if (contentHeader.getValue().contains("gzip")) { + Log.d(TAG, "GGGGGGGGGZIIIIIPPPPPED!"); + is = new GZIPInputStream(entity.getContent()); + } else { + BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); + is = bufHttpEntity.getContent(); + } } else { BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); is = bufHttpEntity.getContent(); @@ -263,32 +279,5 @@ protected String doInBackground(File... params) { return null; } } -/* - private String decompress(String zipText) throws IOException { - byte[] compressed = Base64.decode(zipText); - if (compressed.length > 4) - { - GZIPInputStream gzipInputStream = new GZIPInputStream( - new ByteArrayInputStream(compressed, 4, - compressed.length - 4)); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - for (int value = 0; value != -1;) { - value = gzipInputStream.read(); - if (value != -1) { - baos.write(value); - } - } - gzipInputStream.close(); - baos.close(); - String sReturn = new String(baos.toByteArray(), "UTF-8"); - return sReturn; - } - else - { - return ""; - } - } - */ }