Class RequestHandler
- java.lang.Object
-
- org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler
-
- Direct Known Subclasses:
AddNodeHandler,AddWayHandler,AuthorizationHandler,ExportHandler,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 classRequestHandler.PermissionCachestatic classRequestHandler.RawURLParseRequestHandlerHandler that takes a URL as a parameter.static classRequestHandler.RequestHandlerBadRequestExceptionError raised for bad requests.static classRequestHandler.RequestHandlerErrorExceptionError raised when a runtime error occurred.static classRequestHandler.RequestHandlerExceptionBase exception of remote control handler errors.static classRequestHandler.RequestHandlerForbiddenExceptionError raised for forbidden usage.static classRequestHandler.RequestHandlerOsmApiExceptionError raised for OSM API errors.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,java.lang.String>argsThe GET request argumentsprotected java.lang.Stringcontentdefault responseprotected java.lang.StringcontentTypedefault content typestatic BooleanPropertyGLOBAL_CONFIRMATIONpreference to determine if all Remote Control actions must be confirmed manuallystatic BooleanPropertyLOAD_IN_NEW_LAYERpreference to determine if remote control loads data in a new layerprotected java.lang.StringmyCommandwill be filled with the command assigned to the subclassstatic IntegerPropertyOSM_DOWNLOAD_TIMEOUTpreference to define OSM download timeout in secondsprotected static RequestHandler.PermissionCachePERMISSIONSpast confirmationsprotected java.lang.StringrequestThe request URL without "GET".protected java.lang.Stringsenderwho sent the request? the host from referer header or IP of request senderprotected static java.util.regex.PatternSPLITTER_COMMAprotected static java.util.regex.PatternSPLITTER_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) voidcheckMandatoryParams()voidcheckPermission()Check permissions in preferences and display error message or ask for permission.private booleanget(java.lang.String key)private <T> Tget(java.lang.String key, java.util.function.Function<java.lang.String,T> parser, java.util.function.Supplier<T> defaultSupplier)java.lang.StringgetCommand()Returns the command associated with this handler.java.lang.StringgetContent()Returns the response content.java.lang.StringgetContentType()Returns the response content type.protected DownloadParamsgetDownloadParams()abstract java.lang.String[]getMandatoryParams()Returns the mandatory parameters.java.lang.String[]getOptionalParams()Returns the optional parameters.abstract java.lang.StringgetPermissionMessage()Get a specific message to ask the user for permission for the operation requested via remote control.abstract PermissionPrefWithDefaultgetPermissionPref()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.StringgetUsage()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.voidhandle()Check permission and parameters and handle request.protected abstract voidhandleRequest()Handle a specific command sent as remote control.private booleanisLoadInNewLayer()protected voidparseArgs()Parse the request parameters as key=value pairs.voidsetCommand(java.lang.String command)Save command associated with this handler.voidsetSender(java.lang.String sender)Sets who sent the request (the host from referer header or IP of request sender)voidsetUrl(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 voidvalidateDownloadParams()protected abstract voidvalidateRequest()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
-
-