Search in sources :

Example 1 with AbstractCommand

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commands.AbstractCommand in project GNS by MobilityFirst.

the class GNSHttpServer method processQuery.

/*
   * Process queries for the http service. Converts the URI of e the HTTP query into
   * the JSON Object format that is used by the CommandModeule class, then finds
   * executes the matching command.
   *
   * @throws InternalRequestException
   */
private CommandResponse processQuery(String host, String commandName, String queryString, boolean secureServer) throws InternalRequestException {
    // the signature, and the message signed.
    try {
        // Note that the commandName is not part of the queryString string here so
        // it doesn't end up in the jsonCommand. Also see below where we put the
        // command integer into the jsonCommand.
        JSONObject jsonCommand = Util.parseURIQueryStringIntoJSONObject(queryString);
        // If the signature exists it is Base64 encoded so decode it now.
        if (jsonCommand.has(GNSProtocol.SIGNATURE.toString())) {
            jsonCommand.put(GNSProtocol.SIGNATURE.toString(), new String(Base64.decode(jsonCommand.getString(GNSProtocol.SIGNATURE.toString())), GNSProtocol.CHARSET.toString()));
        }
        // getCommandForHttp allows for "dump" as well as "Dump"
        CommandType commandType = CommandType.getCommandForHttp(commandName);
        if (commandType == null) {
            return new CommandResponse(ResponseCode.OPERATION_NOT_SUPPORTED, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString() + " Sorry, don't understand " + commandName + QUERYPREFIX + queryString);
        }
        //Only allow mutual auth commands if we're on a secure (HTTPS) server
        if (commandType.isMutualAuth() && !secureServer) {
            return new CommandResponse(ResponseCode.OPERATION_NOT_SUPPORTED, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString() + " Not authorized to execute " + commandName + QUERYPREFIX + queryString);
        }
        // The client currently just uses the command name (which is not part of the
        // query string above) so we need to stuff
        // in the Command integer for the signature check and execution.
        jsonCommand.put(GNSProtocol.COMMAND_INT.toString(), commandType.getInt());
        // Optionally does some sanity checking on the message if that was enabled at the client.
        // This makes necessary changes to the jsonCommand so don't remove this call
        // unless you know what you're doing and also change the code in the HTTP client.
        sanityCheckMessage(jsonCommand);
        // is true (or there was a problem).
        if (client == null || commandType.isLocallyHandled()) {
            // EXECUTE IT LOCALLY
            AbstractCommand command;
            try {
                command = commandModule.lookupCommand(commandType);
                // Do some work to get the signature and message into the command for
                // signature checking that happens later on.
                // This only happens for local execution because remote handling (in the
                // other side of the if) already does this.
                processSignature(jsonCommand);
                if (command != null) {
                    return CommandHandler.executeCommand(command, new CommandPacket((long) (Math.random() * Long.MAX_VALUE), jsonCommand, false), requestHandler);
                }
                LOGGER.log(Level.FINE, "lookupCommand returned null for {0}", commandName);
            } catch (IllegalArgumentException e) {
                LOGGER.log(Level.FINE, "lookupCommand failed for {0}", commandName);
            }
            return new CommandResponse(ResponseCode.OPERATION_NOT_SUPPORTED, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString() + " Sorry, don't understand " + commandName + QUERYPREFIX + queryString);
        } else {
            // Send the command remotely using a client
            try {
                LOGGER.log(Level.FINE, "Sending command out to a remote server: {0}", jsonCommand);
                CommandPacket commandResponsePacket = getResponseUsingGNSClient(client, jsonCommand);
                return new CommandResponse(ResponseCode.NO_ERROR, // There is similar code to this other places.
                specialCaseSingleFieldRead(commandResponsePacket.getResultString(), commandType, jsonCommand));
            } catch (IOException | ClientException e) {
                return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.toString());
            //      } catch (ClientException e) {
            //        return new CommandResponse(ResponseCode.GNSProtocol.UNSPECIFIED_ERROR.toString(),
            //                GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString()
            //                + " Sorry, don't understand " + commandName + QUERYPREFIX + queryString);
            }
        }
    } catch (JSONException | UnsupportedEncodingException e) {
        return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.toString());
    }
}
Also used : AbstractCommand(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commands.AbstractCommand) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) IOException(java.io.IOException) CommandPacket(edu.umass.cs.gnscommon.packets.CommandPacket) JSONObject(org.json.JSONObject) CommandType(edu.umass.cs.gnscommon.CommandType) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException)

Aggregations

CommandType (edu.umass.cs.gnscommon.CommandType)1 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)1 CommandPacket (edu.umass.cs.gnscommon.packets.CommandPacket)1 CommandResponse (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse)1 AbstractCommand (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commands.AbstractCommand)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1