Class RequestHandler
- java.lang.Object
-
- org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler
-
- Direct Known Subclasses:
AddNodeHandler
,AddWayHandler
,AuthorizationHandler
,FeaturesHandler
,LoadAndZoomHandler
,LoadDataHandler
,LoadObjectHandler
,OpenApiHandler
,OpenFileHandler
,RequestHandler.RawURLParseRequestHandler
,VersionHandler
public abstract class RequestHandler extends java.lang.Object
This is the parent of all classes that handle a specific remote control command
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
RequestHandler.PermissionCache
static class
RequestHandler.RawURLParseRequestHandler
Handler that takes a URL as a parameter.static class
RequestHandler.RequestHandlerBadRequestException
Error raised for bad requests.static class
RequestHandler.RequestHandlerErrorException
Error raised when a runtime error occurred.static class
RequestHandler.RequestHandlerException
Base exception of remote control handler errors.static class
RequestHandler.RequestHandlerForbiddenException
Error raised for forbidden usage.static class
RequestHandler.RequestHandlerOsmApiException
Error raised for OSM API errors.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,java.lang.String>
args
The GET request argumentsprotected java.lang.String
content
default responseprotected java.lang.String
contentType
default content typestatic BooleanProperty
GLOBAL_CONFIRMATION
preference to determine if all Remote Control actions must be confirmed manuallystatic BooleanProperty
LOAD_IN_NEW_LAYER
preference to determine if remote control loads data in a new layerprotected java.lang.String
myCommand
will be filled with the command assigned to the subclassstatic IntegerProperty
OSM_DOWNLOAD_TIMEOUT
preference to define OSM download timeout in secondsprotected static RequestHandler.PermissionCache
PERMISSIONS
past confirmationsprotected java.lang.String
request
The request URL without "GET".protected java.lang.String
sender
who sent the request? the host from referer header or IP of request senderprotected static java.util.regex.Pattern
SPLITTER_COMMA
protected static java.util.regex.Pattern
SPLITTER_SEMIC
-
Constructor Summary
Constructors Constructor Description RequestHandler()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) void
checkMandatoryParams()
void
checkPermission()
Check permissions in preferences and display error message or ask for permission.private boolean
get(java.lang.String key)
private <T> T
get(java.lang.String key, java.util.function.Function<java.lang.String,T> parser, java.util.function.Supplier<T> defaultSupplier)
java.lang.String
getCommand()
Returns the command associated with this handler.java.lang.String
getContent()
Returns the response content.java.lang.String
getContentType()
Returns the response content type.protected DownloadParams
getDownloadParams()
abstract java.lang.String[]
getMandatoryParams()
Returns the mandatory parameters.java.lang.String[]
getOptionalParams()
Returns the optional parameters.abstract java.lang.String
getPermissionMessage()
Get a specific message to ask the user for permission for the operation requested via remote control.abstract PermissionPrefWithDefault
getPermissionPref()
Get a PermissionPref object containing the name of a special permission preference to individually allow the requested operation and an error message to be displayed when a disabled operation is requested.(package private) static java.util.Map<java.lang.String,java.lang.String>
getRequestParameter(java.net.URI uri)
Returns the request parameters.java.lang.String
getUsage()
Returns usage description, for bad requests and documentation.java.lang.String[]
getUsageExamples()
Returns usage examples, for bad requests and documentation.java.lang.String[]
getUsageExamples(java.lang.String cmd)
Returns usage examples for the given command.void
handle()
Check permission and parameters and handle request.protected abstract void
handleRequest()
Handle a specific command sent as remote control.private boolean
isLoadInNewLayer()
protected void
parseArgs()
Parse the request parameters as key=value pairs.void
setCommand(java.lang.String command)
Save command associated with this handler.void
setSender(java.lang.String sender)
Sets who sent the request (the host from referer header or IP of request sender)void
setUrl(java.lang.String url)
Set request URL and parse args.protected java.lang.String[]
splitArg(java.lang.String arg, java.util.regex.Pattern splitter)
protected void
validateDownloadParams()
protected abstract void
validateRequest()
Validates the request before attempting to perform it.
-
-
-
Field Detail
-
GLOBAL_CONFIRMATION
public static final BooleanProperty GLOBAL_CONFIRMATION
preference to determine if all Remote Control actions must be confirmed manually
-
LOAD_IN_NEW_LAYER
public static final BooleanProperty LOAD_IN_NEW_LAYER
preference to determine if remote control loads data in a new layer
-
OSM_DOWNLOAD_TIMEOUT
public static final IntegerProperty OSM_DOWNLOAD_TIMEOUT
preference to define OSM download timeout in seconds
-
SPLITTER_COMMA
protected static final java.util.regex.Pattern SPLITTER_COMMA
-
SPLITTER_SEMIC
protected static final java.util.regex.Pattern SPLITTER_SEMIC
-
PERMISSIONS
protected static final RequestHandler.PermissionCache PERMISSIONS
past confirmations
-
args
protected java.util.Map<java.lang.String,java.lang.String> args
The GET request arguments
-
request
protected java.lang.String request
The request URL without "GET".
-
content
protected java.lang.String content
default response
-
contentType
protected java.lang.String contentType
default content type
-
myCommand
protected java.lang.String myCommand
will be filled with the command assigned to the subclass
-
sender
protected java.lang.String sender
who sent the request? the host from referer header or IP of request sender
-
-
Constructor Detail
-
RequestHandler
public RequestHandler()
-
-
Method Detail
-
handle
public final void handle() throws RequestHandler.RequestHandlerForbiddenException, RequestHandler.RequestHandlerBadRequestException, RequestHandler.RequestHandlerErrorException
Check permission and parameters and handle request.- Throws:
RequestHandler.RequestHandlerForbiddenException
- if request is forbidden by preferencesRequestHandler.RequestHandlerBadRequestException
- if request is invalidRequestHandler.RequestHandlerErrorException
- if an error occurs while processing request
-
validateRequest
protected abstract void validateRequest() throws RequestHandler.RequestHandlerBadRequestException
Validates the request before attempting to perform it.- Throws:
RequestHandler.RequestHandlerBadRequestException
- if request is invalid- Since:
- 5678
-
handleRequest
protected abstract void handleRequest() throws RequestHandler.RequestHandlerErrorException, RequestHandler.RequestHandlerBadRequestException
Handle a specific command sent as remote control. Any time-consuming operation must be performed asynchronously to avoid delaying the HTTP response.This method of the subclass will do the real work.
- Throws:
RequestHandler.RequestHandlerErrorException
- if an error occurs while processing requestRequestHandler.RequestHandlerBadRequestException
- if request is invalid
-
getPermissionMessage
public abstract java.lang.String getPermissionMessage()
Get a specific message to ask the user for permission for the operation requested via remote control.This message will be displayed to the user if the preference remotecontrol.always-confirm is true.
- Returns:
- the message
-
getPermissionPref
public abstract PermissionPrefWithDefault getPermissionPref()
Get a PermissionPref object containing the name of a special permission preference to individually allow the requested operation and an error message to be displayed when a disabled operation is requested.Default is not to check any special preference. Override this in a subclass to define permission preference and error message.
- Returns:
- the preference name and error message or null
-
getMandatoryParams
public abstract java.lang.String[] getMandatoryParams()
Returns the mandatory parameters. Both used to enforce their presence at runtime and for documentation.- Returns:
- the mandatory parameters
-
getOptionalParams
public java.lang.String[] getOptionalParams()
Returns the optional parameters. Both used to enforce their presence at runtime and for documentation.- Returns:
- the optional parameters
-
getUsage
public java.lang.String getUsage()
Returns usage description, for bad requests and documentation.- Returns:
- usage description
-
getUsageExamples
public java.lang.String[] getUsageExamples()
Returns usage examples, for bad requests and documentation.- Returns:
- Usage examples
-
getUsageExamples
public java.lang.String[] getUsageExamples(java.lang.String cmd)
Returns usage examples for the given command. To be overridden only my handlers that define several commands.- Parameters:
cmd
- The command asked- Returns:
- Usage examples for the given command
- Since:
- 6332
-
checkPermission
public final void checkPermission() throws RequestHandler.RequestHandlerForbiddenException
Check permissions in preferences and display error message or ask for permission.- Throws:
RequestHandler.RequestHandlerForbiddenException
- if request is forbidden by preferences
-
setUrl
public void setUrl(java.lang.String url) throws RequestHandler.RequestHandlerBadRequestException
Set request URL and parse args.- Parameters:
url
- The request URL.- Throws:
RequestHandler.RequestHandlerBadRequestException
- if request URL is invalid
-
parseArgs
protected void parseArgs() throws java.net.URISyntaxException
Parse the request parameters as key=value pairs. The result will be stored inthis.args
.Can be overridden by subclass.
- Throws:
java.net.URISyntaxException
- if request URL is invalid
-
splitArg
protected final java.lang.String[] splitArg(java.lang.String arg, java.util.regex.Pattern splitter)
-
getRequestParameter
static java.util.Map<java.lang.String,java.lang.String> getRequestParameter(java.net.URI uri)
Returns the request parameters.- Parameters:
uri
- URI as string- Returns:
- map of request parameters
- See Also:
- What every web developer must know about URL encoding
-
checkMandatoryParams
void checkMandatoryParams() throws RequestHandler.RequestHandlerBadRequestException
-
setCommand
public void setCommand(java.lang.String command)
Save command associated with this handler.- Parameters:
command
- The command.
-
getCommand
public java.lang.String getCommand()
Returns the command associated with this handler.- Returns:
- the command associated with this handler.
-
getContent
public java.lang.String getContent()
Returns the response content.- Returns:
- the response content
-
getContentType
public java.lang.String getContentType()
Returns the response content type.- Returns:
- the response content type
-
get
private <T> T get(java.lang.String key, java.util.function.Function<java.lang.String,T> parser, java.util.function.Supplier<T> defaultSupplier)
-
get
private boolean get(java.lang.String key)
-
isLoadInNewLayer
private boolean isLoadInNewLayer()
-
getDownloadParams
protected DownloadParams getDownloadParams()
-
validateDownloadParams
protected void validateDownloadParams() throws RequestHandler.RequestHandlerBadRequestException
-
setSender
public void setSender(java.lang.String sender)
Sets who sent the request (the host from referer header or IP of request sender)- Parameters:
sender
- the host from referer header or IP of request sender
-
-