Class RequestProcessor

java.lang.Object
java.lang.Thread
org.openstreetmap.josm.io.remotecontrol.RequestProcessor
All Implemented Interfaces:
Runnable

public class RequestProcessor extends Thread
Processes HTTP "remote control" requests.
  • Field Details

  • Constructor Details

    • RequestProcessor

      public RequestProcessor(Socket request)
      Constructor
      Parameters:
      request - A socket to read the request.
  • Method Details

    • getProtocolVersion

      public static String getProtocolVersion()
      RemoteControl protocol version. Change minor number for compatible interface extensions. Change major number in case of incompatible changes.
    • processRequest

      public static void processRequest(Socket request)
      Spawns a new thread for the request
      Parameters:
      request - The request to process
    • addRequestHandlerClass

      public static void addRequestHandlerClass(String command, Class<? extends RequestHandler> handler)
      Add external request handler. Can be used by other plug-ins that want to use remote control.
      Parameters:
      command - The command to handle.
      handler - The additional request handler.
    • addRequestHandlerClass

      private static void addRequestHandlerClass(String command, Class<? extends RequestHandler> handler, boolean silent)
      Add external request handler. Message can be suppressed. (for internal use)
      Parameters:
      command - The command to handle.
      handler - The additional request handler.
      silent - Don't show message if true.
    • initialize

      public static void initialize()
      Force the class to initialize and load the handlers
    • run

      public void run()
      The work is done here.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • realRun

      private static void realRun(BufferedReader in, Writer out, Socket request) throws IOException
      Perform the actual commands
      Parameters:
      in - The reader for incoming data
      out - The writer for outgoing data
      request - The actual request
      Throws:
      IOException - Usually occurs if one of the Writer methods has problems.
    • parseHeaders

      private static Map<String,String> parseHeaders(BufferedReader in) throws IOException
      Parse the headers from the request
      Parameters:
      in - The request reader
      Returns:
      The map of headers
      Throws:
      IOException - See BufferedReader.readLine()
    • parseSender

      private static String parseSender(Map<String,String> headers, Socket request)
      Attempt to figure out who sent the request
      Parameters:
      headers - The headers (we currently look for Referer)
      request - The request to look at
      Returns:
      The sender (or "localhost" if none could be found)
    • callHandler

      private static void callHandler(String url, String command, Writer out, String sender) throws ReflectiveOperationException, IOException
      Call the handler for the command
      Parameters:
      url - The request URL
      command - The command we are using
      out - The writer to use for indicating success or failure
      sender - The sender of the request
      Throws:
      ReflectiveOperationException - If the handler class has an issue
      IOException - If one of the Writer methods has issues
    • sendError

      private static void sendError(Writer out, int errorCode, String errorName, String help) throws IOException
      Throws:
      IOException
    • sendErrorHtml

      private static void sendErrorHtml(Writer out, int errorCode, String errorName, String helpHtml) throws IOException
      Throws:
      IOException
    • sendInternalError

      private static void sendInternalError(Writer out, String help) throws IOException
      Sends a 500 error: internal server error
      Parameters:
      out - The writer where the error is written
      help - Optional HTML help content to display, can be null
      Throws:
      IOException - If the error can not be written
    • sendNotImplemented

      private static void sendNotImplemented(Writer out) throws IOException
      Sends a 501 error: not implemented
      Parameters:
      out - The writer where the error is written
      Throws:
      IOException - If the error can not be written
    • sendBadGateway

      private static void sendBadGateway(Writer out, String help) throws IOException
      Sends a 502 error: bad gateway
      Parameters:
      out - The writer where the error is written
      help - Optional HTML help content to display, can be null
      Throws:
      IOException - If the error can not be written
    • sendForbidden

      private static void sendForbidden(Writer out, String help) throws IOException
      Sends a 403 error: forbidden
      Parameters:
      out - The writer where the error is written
      help - Optional HTML help content to display, can be null
      Throws:
      IOException - If the error can not be written
    • sendBadRequest

      private static void sendBadRequest(Writer out, String help) throws IOException
      Sends a 400 error: bad request
      Parameters:
      out - The writer where the error is written
      help - Optional help content to display, can be null
      Throws:
      IOException - If the error can not be written
    • sendHeader

      private static void sendHeader(Writer out, String status, String contentType, boolean endHeaders) throws IOException
      Send common HTTP headers to the client.
      Parameters:
      out - The Writer
      status - The status string ("200 OK", "500", etc)
      contentType - The content type of the data sent
      endHeaders - If true, adds a new line, ending the headers.
      Throws:
      IOException - When error
    • getHandlersInfo

      Returns the information for the given (if null: all) handlers.
      Parameters:
      handlers - the handlers
      Returns:
      the information for the given (if null: all) handlers
    • getHandlerInfo

      public static RequestHandler getHandlerInfo(String cmd)
      Returns the information for a given handler.
      Parameters:
      cmd - handler key
      Returns:
      the information for the given handler
    • getUsageAsHtml

      Reports HTML message with the description of all available commands
      Returns:
      HTML message with the description of all available commands
      Throws:
      ReflectiveOperationException - if a reflective operation fails for one handler class