cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [10/34] git commit: Now with push support for crx files
Date Tue, 06 May 2014 19:29:24 GMT
Now with push support for crx files


Project: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/commit/fd52e9c1
Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/fd52e9c1
Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/fd52e9c1

Branch: refs/heads/master
Commit: fd52e9c1c52225d27d5f48ecaf93a0042c122c91
Parents: 06bf5d3
Author: Braden Shepherdson <braden.shepherdson@gmail.com>
Authored: Mon Jan 27 11:13:51 2014 -0500
Committer: Braden Shepherdson <braden.shepherdson@gmail.com>
Committed: Mon Jan 27 11:13:51 2014 -0500

----------------------------------------------------------------------
 README.md             | 14 ++++++++---
 src/android/Push.java | 61 +++++++++++++++++++++++++++++++++++++---------
 2 files changed, 60 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/fd52e9c1/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 61ff8c9..8d9ff1b 100644
--- a/README.md
+++ b/README.md
@@ -10,11 +10,11 @@ This is a [Cordova](http://cordova.io) plugin that integrates with the
[App Harn
 
 **You must** press the "Start Listening" button on the main menu of the App Harness prior
to using any of the requests below.
 
-There are currently three requests you can make:
+There are currently four kinds of requests you can make:
 
 ### Push - `cordova serve`
 
-Make a `POST` request to
+Make a `POST` request on port 2424 to:
 
     /push?type=serve&name=com.example.YourApp&url=http://192.168.1.101:8000
 
@@ -22,11 +22,17 @@ and this will cause the App Harness to return to the main menu, and fetch
the ap
 
 ### Push - `.crx` files
 
-TODO
+Make a `POST` request on port 2424 to
+
+    /push?type=crx&name=appname
+
+with the file passed as an HTTP form submission, with the key `file`. The following curl
line will do it, for example:
+
+    curl -X POST "http://192.168.1.102:2424/push?type=crx&name=myapp" -F "file=@path/to/myapp.crx"
 
 ### Menu - Return to the App Harness menu
 
-Sometimes, especially on emulators, it's hard or impossible to do the three-point touch that
triggers the App Harness context menu. Sending a `POST` request to `/menu` will return to
the App Harness main menu.
+Sometimes, especially on emulators, it's hard or impossible to do the three-point touch that
triggers the App Harness context menu. Sending a `POST` request on port 2424 to `/menu` will
return to the App Harness main menu.
 
 ### Exec - Run arbitrary Javascript
 

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/fd52e9c1/src/android/Push.java
----------------------------------------------------------------------
diff --git a/src/android/Push.java b/src/android/Push.java
index 674d196..b216a14 100644
--- a/src/android/Push.java
+++ b/src/android/Push.java
@@ -4,33 +4,33 @@
 
 package org.apache.appharness;
 
-import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import fi.iki.elonen.NanoHTTPD;
-
+import org.apache.cordova.CallbackContext;
 import org.apache.cordova.Config;
 import org.apache.cordova.CordovaArgs;
-import org.apache.cordova.CallbackContext;
 import org.apache.cordova.CordovaPlugin;
-import org.apache.cordova.LOG;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.annotation.SuppressLint;
 import android.os.Build;
-import android.net.Uri;
 import android.util.Log;
+import fi.iki.elonen.NanoHTTPD;
 
 public class Push extends CordovaPlugin {
 
     private static final String LOG_TAG = "HarnessPush";
     private static final int PORT = 2424;
-
+    
     private PushServer server;
     private JSONObject latestPush;
     private boolean listening = false;
@@ -67,6 +67,7 @@ public class Push extends CordovaPlugin {
         try {
             server = new PushServer();
             server.start();
+            listening = true;
             callbackContext.success();
         } catch (IOException ioe) {
             Log.w(LOG_TAG, "Error launching web server", ioe);
@@ -131,8 +132,45 @@ public class Push extends CordovaPlugin {
                 if (type == null) return new Response(Response.Status.BAD_REQUEST, "text/plain",
"No push type specified");
 
                 if ("crx".equals(type)) {
-                    // Insert code here for saving the CRX file.
-                    return new Response(Response.Status.OK, "text/plain", "CRX file uploading
is currently unsupported.");
+                    // Receive the file that came with the request, and save it under /data/data/my.app.id/push.crx.
+                    try {
+                        Map<String, String> files = new HashMap<String, String>();
+                        session.parseBody(files);
+                        if (!files.containsKey("file")) {
+                            return new Response(Response.Status.BAD_REQUEST, "text/plain",
"You must send a file with the form key 'file'.");
+                        }
+                        
+                        // Copy the file out of the ephemeral cache/foo location and into
somewhere permanent.
+                        String source = files.get("file");
+                        String target = source.replaceFirst("/cache/", "/crx_cache/") + ".crx";
+                        String dir = target.replaceFirst("/[^/]*$", "");
+                        
+                        File cacheDir = new File(dir);
+                        if (! (cacheDir.mkdir() || cacheDir.isDirectory()))
+                        	return new Response(Response.Status.INTERNAL_ERROR, "text/plain",
"Could not create cache directory");
+                        
+                        InputStream in = new FileInputStream(new File(source));
+                        OutputStream out = new FileOutputStream(new File(target));
+                        byte[] buf = new byte[1024];
+                        int len;
+                        while((len = in.read(buf)) > 0) {
+                        	out.write(buf, 0, len);
+                        }
+                        in.close();
+                        out.close();
+                        
+                        // Now prepare the return value for the harness.
+                        String url = "file://" + target;
+                        JSONObject payload = new JSONObject();
+                        payload.put("name", params.get("name").get(0));
+                        payload.put("type", "crx");
+                        payload.put("url", url);
+                        Push.this.latestPush = payload;
+                        Push.this.restartAppHarness();
+                        return new Response(Response.Status.OK, "text/plain", "Push successful");
+                    } catch (Exception e) {
+                        Log.w(LOG_TAG, "Exception while receiving files", e);
+                    }
                 } else if ("serve".equals(type)) {
                     // Create the latestPush value from the parameters.
                     try {
@@ -157,7 +195,8 @@ public class Push extends CordovaPlugin {
                 }
                 return new Response(Response.Status.OK, "text/plain", "Executed successfully");
             } else if (session.getUri().equals("/menu")) {
-            	injectJS("window.location = 'app-harness:///cdvah/index.html'");
+            	//injectJS("window.location = 'app-harness:///cdvah/index.html'");
+            	Push.this.restartAppHarness();
             	return new Response(Response.Status.OK, "text/plain", "Returning to main menu");
             } else {
                 return new Response(Response.Status.NOT_FOUND, "text/plain", "URI " + String.valueOf(session.getUri())
+ " not found");


Mime
View raw message