Class ServerAPI

java.lang.Object
za.co.wethinkcode.robots.api.ServerAPI

public class ServerAPI extends Object
The Server API class encapsulates all behaviour related to outputting Command Results to the Client
  • Field Details

    • DELIMITER

      public static String DELIMITER
      Custom delimiter used to send multiple new line characters over the network.
  • Constructor Details

    • ServerAPI

      public ServerAPI()
  • Method Details

    • parsejson

      public static Command parsejson(String json)
    • webAPIOut

      public static String webAPIOut(com.google.gson.JsonObject data)
      Utility method to send a cleaned JSON object over the HTTP web layer
    • ok

      public static com.google.gson.JsonObject ok(com.google.gson.JsonObject data)
      Take in a JsonObject and return and OK message as a String
      Parameters:
      data - the JSON Object being wrapped in OK
      Returns:
      the wrapped JSON object represented as a string
    • error

      public static com.google.gson.JsonObject error(String message)
      Takes in an error message and wraps into a JSON structured error
      Parameters:
      message - the error message
      Returns:
      the error JSON represented a raw string.
    • error

      public static com.google.gson.JsonObject error(Exception e)
      Return an Error From an exception
      Parameters:
      e - the exception being passed in
      Returns:
      a JsonObject structured as an error
    • error

      public static com.google.gson.JsonObject error(PurgedRobotException e)
      Overload to send back a specific error message for the custom UnsupportedRobotWorldCommand exception
      Parameters:
      e - the exception being passed in
      Returns:
      a JsonObject structured as an error
    • error

      public static com.google.gson.JsonObject error(UnsupportedRobotWorldCommand e)
      Overload to send back a specific error message for the custom UnsupportedRobotWorldCommand exception
      Parameters:
      e - the exception being passed in
      Returns:
      a JsonObject structured as an error
    • error

      public static com.google.gson.JsonObject error(TooManyOfYouException e)
      Overload to send back a specific error message for the custom TooManyOfYouException exception
      Parameters:
      e - the exception being passed in
      Returns:
      a JsonObject structured as an error
    • error

      public static com.google.gson.JsonObject error(WorldFullException e)
      Overload to send back a specific error message for the custom TooManyOfYouException exception
      Parameters:
      e - the exception being passed in
      Returns:
      a JsonObject structured as an error
    • error

      public static com.google.gson.JsonObject error(RobotCannotActException e)
      Overload to send back a specific error message for the custom RobotCannotActException

      RobotCannotActException has an internal constructor for RobotCannotActException.constructTimeError(za.co.wethinkcode.robots.domain.Status, za.co.wethinkcode.robots.server.robots.Robot, za.co.wethinkcode.robots.server.world.World) method.
      This method allows for smarter errors to be sent to client regarding when next they will be able to act.

      Parameters:
      e - the exception being passed in
      Returns:
      a JsonObject structured as an error
    • cleanForNetworkRaw

      public static String cleanForNetworkRaw(String raw)
      Simple utility method to clean raw strings up for transmission over a network

      This involves replacing \n characters with a custom delimiter

      Parameters:
      raw - the unclean string
      Returns:
      the cleaned string
    • cleanForNetworkJSONtoJSON

      public static com.google.gson.JsonObject cleanForNetworkJSONtoJSON(com.google.gson.JsonObject raw)
      Simple utility method to clean Json Objects for transmission. Shallow call to cleanForNetworkRaw(String)
      Parameters:
      raw - the unclean JSON
      Returns:
      the cleaned JSON