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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
-
-
Field Summary
Fields Modifier and Type Field Description private static Map<String,Class<? extends RequestHandler>>
handlers
Collection of request handlers.static String
JOSM_REMOTE_CONTROL
The string "JOSM RemoteControl"static String
PROTOCOLVERSION
RemoteControl protocol version.private Socket
request
The socket this processor listens onprivate static Charset
RESPONSE_CHARSET
private static String
RESPONSE_TEMPLATE
-
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
-
Constructor Summary
Constructors Constructor Description RequestProcessor(Socket request)
Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
addRequestHandlerClass(String command, Class<? extends RequestHandler> handler)
Add external request handler.private static void
addRequestHandlerClass(String command, Class<? extends RequestHandler> handler, boolean silent)
Add external request handler.static RequestHandler
getHandlerInfo(String cmd)
Returns the information for a given handler.static Stream<RequestHandler>
getHandlersInfo(Collection<String> handlers)
Returns the information for the given (if null: all) handlers.static String
getUsageAsHtml()
Reports HTML message with the description of all available commandsstatic void
initialize()
Force the class to initialize and load the handlersstatic void
processRequest(Socket request)
Spawns a new thread for the requestvoid
run()
The work is done here.private static void
sendBadGateway(Writer out, String help)
Sends a 502 error: bad gatewayprivate static void
sendBadRequest(Writer out, String help)
Sends a 400 error: bad requestprivate static void
sendError(Writer out, int errorCode, String errorName, String help)
private static void
sendErrorHtml(Writer out, int errorCode, String errorName, String helpHtml)
private static void
sendForbidden(Writer out, String help)
Sends a 403 error: forbiddenprivate static void
sendHeader(Writer out, String status, String contentType, boolean endHeaders)
Send common HTTP headers to the client.private static void
sendInternalError(Writer out, String help)
Sends a 500 error: internal server errorprivate static void
sendNotImplemented(Writer out)
Sends a 501 error: not implemented-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
-
-
-
Field Detail
-
RESPONSE_CHARSET
private static final Charset RESPONSE_CHARSET
-
RESPONSE_TEMPLATE
private static final String RESPONSE_TEMPLATE
-
JOSM_REMOTE_CONTROL
public static final String JOSM_REMOTE_CONTROL
The string "JOSM RemoteControl"- See Also:
- Constant Field Values
-
PROTOCOLVERSION
public static final String PROTOCOLVERSION
RemoteControl protocol version. Change minor number for compatible interface extensions. Change major number in case of incompatible changes.
-
handlers
private static final Map<String,Class<? extends RequestHandler>> handlers
Collection of request handlers. Will be initialized with default handlers here. Other plug-ins can extend this list by using @see addRequestHandler
-
-
Constructor Detail
-
RequestProcessor
public RequestProcessor(Socket request)
Constructor- Parameters:
request
- A socket to read the request.
-
-
Method Detail
-
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.
-
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 writtenhelp
- 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 writtenhelp
- 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 writtenhelp
- 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 writtenhelp
- 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 Writerstatus
- The status string ("200 OK", "500", etc)contentType
- The content type of the data sentendHeaders
- If true, adds a new line, ending the headers.- Throws:
IOException
- When error
-
getHandlersInfo
public static Stream<RequestHandler> getHandlersInfo(Collection<String> handlers)
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
public static String getUsageAsHtml() throws ReflectiveOperationException
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
-
-