package com.amplify.honeydew_server.httpd; import android.util.Log; import com.amplify.honeydew_server.*; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import fi.iki.elonen.NanoHTTPD; import java.io.IOException; import java.lang.reflect.Type; import java.util.Map; public class RemoteCommandReceiver extends NanoHTTPD { private static final String PLAIN_TEXT = "plain/text"; private final ActionsExecutor actionsExecutor; private final Response statusOkResponse = new Response("honeydew-server is awaiting commands"); private final Response terminateOkResponse = new Response("honeydew-server is stopping"); private final Response badRequestResponse = new Response(Response.Status.BAD_REQUEST, PLAIN_TEXT, "Unsupported command"); private final Gson jsonParser = new Gson(); public RemoteCommandReceiver(ActionsExecutor actionsExecutor) throws IOException, InterruptedException { super(7120); this.actionsExecutor = actionsExecutor; } @Override public Response serve(String uri, Method method, Map headers, Map params, Map files) { if (method == Method.POST && uri.equalsIgnoreCase("/terminate")) { Log.d(getClass().getName(), "Got terminate request, finishing up..."); stop(); return terminateOkResponse; } else if (method == Method.POST && uri.equalsIgnoreCase("/command")) { return performCommand(params); } else if (method == Method.GET && uri.equalsIgnoreCase("/status")) { Log.d(getClass().getName(), "Got status request, responding OK"); return statusOkResponse; } return badRequestResponse; } private Response performCommand(Map params) { String action = params.get("action"); String argumentJson = params.get("arguments"); Type argumentCollectionType = new TypeToken>(){}.getType(); Map arguments = jsonParser.fromJson(argumentJson, argumentCollectionType); Log.i(getClass().getName(), String.format("Performing action %s: %s for %s", action, argumentJson, params.toString())); try { Result result = actionsExecutor.execute(new Command(action, arguments)); if (result.success) { return new Response(result.description); } else { return new Response(Response.Status.NO_CONTENT, null, (String) null); } } catch(Exception exception) { Log.e(getClass().getName(), String.format("Server error while processing command %s: %s", action, exception.toString())); return new Response(Response.Status.INTERNAL_ERROR, PLAIN_TEXT, exception.getMessage()); } } }