check for request id when sending files

avoids sending the file data in response to every single request issued after the one for the file.
This commit is contained in:
Keith Wait
2018-09-09 17:00:54 -05:00
parent 128933f721
commit 752148e0c8
4 changed files with 32 additions and 68 deletions
+32 -36
View File
@@ -207,7 +207,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
pluginResult.setKeepCallback(true);
this.webserver.onRequestCallbackContext.sendPluginResult(pluginResult);
while (!this.webserver.responses.containsKey(requestUUID) && !this.webserver.responses.containsKey("file")) {
while (!this.webserver.responses.containsKey(requestUUID)) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
@@ -215,45 +215,41 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
}
}
JSONObject responseObject;
JSONObject responseObject = (JSONObject) this.webserver.responses.get(requestUUID);
Response response = null;
if (this.webserver.responses.containsKey("file")) {
// TODO should specify a more correct mime-type
try {
responseObject = (JSONObject) this.webserver.responses.get("file");
Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString());
return serveFile(session.getHeaders(), new File(responseObject.getString("path")), responseObject.getString("type"));
}
catch (JSONException e) {
e.printStackTrace();
}
return response;
}
responseObject = (JSONObject) this.webserver.responses.get(requestUUID);
Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString());
try {
response = newFixedLengthResponse(
Response.Status.lookup(responseObject.getInt("status")),
getContentType(responseObject),
responseObject.getString("body")
);
Iterator<?> keys = responseObject.getJSONObject("headers").keys();
while (keys.hasNext()) {
String key = (String) keys.next();
response.addHeader(
key,
responseObject.getJSONObject("headers").getString(key)
);
if (responseObject.containsKey("path")) {
// TODO should specify a more correct mime-type
try {
return serveFile(session.getHeaders(), new File(responseObject.getString("path")), responseObject.getString("type"));
}
} catch (JSONException e) {
e.printStackTrace();
catch (JSONException e) {
e.printStackTrace();
}
return response;
}
else {
try {
response = newFixedLengthResponse(
Response.Status.lookup(responseObject.getInt("status")),
getContentType(responseObject),
responseObject.getString("body")
);
Iterator<?> keys = responseObject.getJSONObject("headers").keys();
while (keys.hasNext()) {
String key = (String) keys.next();
response.addHeader(
key,
responseObject.getJSONObject("headers").getString(key)
);
}
} catch (JSONException e) {
e.printStackTrace();
}
return response;
}
return response;
}
}
-10
View File
@@ -44,10 +44,6 @@ public class Webserver extends CordovaPlugin {
this.sendResponse(args, callbackContext);
return true;
}
else if ("sendFileResponse".equals(action)) {
this.sendFileResponse(args, callbackContext);
return true;
}
return false; // Returning false results in a "MethodNotFound" error.
}
@@ -100,12 +96,6 @@ public class Webserver extends CordovaPlugin {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
}
private void sendFileResponse(JSONArray args, CallbackContext callbackContext) throws JSONException {
Log.d(this.getClass().getName(), "Got sendResponse: " + args.toString());
this.responses.put("file", args.get(0));
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
}
/**
* Just register the onRequest and send no result. This is needed to save the callbackContext to
* invoke it later