Class RequestProcessor
java.lang.Object
java.lang.Thread
org.openstreetmap.josm.io.remotecontrol.RequestProcessor
- All Implemented Interfaces:
Runnable
Processes HTTP "remote control" requests.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Map<String,Class<? extends RequestHandler>> Collection of request handlers.static final StringThe string "JOSM RemoteControl"private static final ReentrantLockThis is purely used to ensure that remote control commands are executed in the order in which they are receivedprivate final SocketThe socket this processor listens onprivate static final Charsetprivate static final StringFields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddRequestHandlerClass(String command, Class<? extends RequestHandler> handler) Add external request handler.private static voidaddRequestHandlerClass(String command, Class<? extends RequestHandler> handler, boolean silent) Add external request handler.private static voidcallHandler(String url, String command, Writer out, String sender) Call the handler for the commandstatic RequestHandlergetHandlerInfo(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 StringRemoteControl protocol version.static StringReports HTML message with the description of all available commandsstatic voidForce the class to initialize and load the handlersParse the headers from the requestprivate static StringparseSender(Map<String, String> headers, Socket request) Attempt to figure out who sent the requeststatic voidprocessRequest(Socket request) Spawns a new thread for the requestprivate static voidrealRun(BufferedReader in, Writer out, Socket request) Perform the actual commandsvoidrun()The work is done here.private static voidsendBadGateway(Writer out, String help) Sends a 502 error: bad gatewayprivate static voidsendBadRequest(Writer out, String help) Sends a 400 error: bad requestprivate static voidprivate static voidsendErrorHtml(Writer out, int errorCode, String errorName, String helpHtml) private static voidsendForbidden(Writer out, String help) Sends a 403 error: forbiddenprivate static voidsendHeader(Writer out, String status, String contentType, boolean endHeaders) Send common HTTP headers to the client.private static voidsendInternalError(Writer out, String help) Sends a 500 error: internal server errorprivate static voidsendNotImplemented(Writer out) Sends a 501 error: not implementedMethods 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 Details
-
ORDER_LOCK
This is purely used to ensure that remote control commands are executed in the order in which they are received -
RESPONSE_CHARSET
-
RESPONSE_TEMPLATE
-
JOSM_REMOTE_CONTROL
The string "JOSM RemoteControl"- See Also:
-
request
The socket this processor listens on -
handlers
Collection of request handlers. Will be initialized with default handlers here. Other plug-ins can extend this list by using @see addRequestHandler
-
-
Constructor Details
-
RequestProcessor
Constructor- Parameters:
request- A socket to read the request.
-
-
Method Details
-
getProtocolVersion
RemoteControl protocol version. Change minor number for compatible interface extensions. Change major number in case of incompatible changes. -
processRequest
Spawns a new thread for the request- Parameters:
request- The request to process
-
addRequestHandlerClass
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
Force the class to initialize and load the handlers -
run
The work is done here. -
realRun
Perform the actual commands- Parameters:
in- The reader for incoming dataout- The writer for outgoing datarequest- The actual request- Throws:
IOException- Usually occurs if one of theWritermethods has problems.
-
parseHeaders
Parse the headers from the request- Parameters:
in- The request reader- Returns:
- The map of headers
- Throws:
IOException- SeeBufferedReader.readLine()
-
parseSender
Attempt to figure out who sent the request- Parameters:
headers- The headers (we currently look forReferer)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 URLcommand- The command we are usingout- The writer to use for indicating success or failuresender- The sender of the request- Throws:
ReflectiveOperationException- If the handler class has an issueIOException- If one of theWritermethods 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
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
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
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
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
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
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
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
-