public abstract class AbstractAction extends ScriptModule implements ServletModule.RequestListener, ActionHandler
The base class for action modules, that is modules that respond to an HTTP request and return some content. It implements ServletModule.RequestListener which makes it possible to register itself to ServletModules. In addition, it also implements ActionHandler which is needed if the action is used inside one of the context classes.
If you extend this class directly, your main point of extension is the only abstract method, handleAction, and the basic Module initialisation methods. If you wish to be notified of every HTTP request without any filtering, override the handleRequest instead.
This action reads parameters with following names from the initialisation parameters. actionParamKey can be used to specify the HTTP request parameter which contains the action id, this defaults to "action". action parameter contains the id of this action, alternatively, you can use parameter actions and provide a comma separated list of multiple ids, the default is to have no id associated with this action. defaultAction parameter can be set to "true" to make this a default action. If the action id is not specified in the request and this is a default action, it'll try to handle the action. Any parameter with its name prefixed with "httpheader." will be used to add custom HTTP headers in the response. The part following this prefix in the parameter name is not important, it only needs to be unique in the module. The actual header name and value are parsed from the contents of the element. It should contain the header name followed by a colon character (:) followed by the value.
Note that custom HTTP headers can be added in the initialisation parameters but they don't get automatically in the response. Instead, you will need to call setHttpHeaders in your handleAction code to have them added.
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
actionParamKey |
protected java.util.Collection<java.lang.String> |
handledActions |
protected java.util.HashMap<java.lang.String,java.lang.String> |
httpHeaders |
protected boolean |
isDefaultAction |
protected ReplacementsModule |
replacements |
protected boolean |
replacementsInitialized |
protected ServletModule |
servletModule |
engine, initScript, persistentObjects, scriptManager, startScript, stopScript
autoStart, isInitialized, isRunning, logging, loggingModule, moduleManager
Constructor and Description |
---|
AbstractAction() |
Modifier and Type | Method and Description |
---|---|
protected void |
addHandledAction(java.lang.String action) |
java.lang.String |
doReplacements(java.lang.String value,
javax.servlet.http.HttpServletRequest req,
ModulesServlet.HttpMethod method,
java.lang.String action,
User user)
Performs basic string replacement using a ReplacementsModule if
one is available.
|
java.util.Collection<Module> |
getDependencies(ModuleManager manager)
Returns all the modules this module depends on.
|
abstract boolean |
handleAction(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp,
ModulesServlet.HttpMethod method,
java.lang.String action,
User user)
Handles this action.
|
boolean |
handleRequest(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp,
ModulesServlet.HttpMethod method,
User user)
Handles the request, if this is the type of request the listener
is interested in.
|
void |
init(ModuleManager manager,
java.util.HashMap<java.lang.String,java.lang.Object> settings)
Initialises the module.
|
boolean |
isHandleAction(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp,
ModulesServlet.HttpMethod method)
Checks if this action handler will try to handle the given action
without handling it yet or causing any side effects.
|
protected void |
setActionParamKey(java.lang.String key) |
protected void |
setHttpHeaders(javax.servlet.http.HttpServletResponse resp,
java.util.Map<java.lang.String,java.lang.String> headers)
Sets HTTP headers from a Map of key value pairs.
|
void |
start(ModuleManager manager)
Starts the module.
|
void |
stop(ModuleManager manager)
Stops the module.
|
getScriptEngine
isInitialized, isRunning, requireLogging, toString
protected boolean isDefaultAction
protected ServletModule servletModule
protected java.lang.String actionParamKey
protected final java.util.Collection<java.lang.String> handledActions
protected final java.util.HashMap<java.lang.String,java.lang.String> httpHeaders
protected boolean replacementsInitialized
protected ReplacementsModule replacements
protected void addHandledAction(java.lang.String action)
protected void setActionParamKey(java.lang.String key)
public java.util.Collection<Module> getDependencies(ModuleManager manager) throws ModuleException
Module
getDependencies
in interface Module
getDependencies
in class AbstractModule
manager
- The module manager handling this module.ModuleException
public java.lang.String doReplacements(java.lang.String value, javax.servlet.http.HttpServletRequest req, ModulesServlet.HttpMethod method, java.lang.String action, User user)
value
- The value in which replacement is to be done.req
- The HTTP request where this call originated.method
- The method of the HTTP request.action
- The parsed action.user
- The logged in user, or null if not applicable.public void init(ModuleManager manager, java.util.HashMap<java.lang.String,java.lang.Object> settings) throws ModuleException
Module
Initialises the module. After constructor, this is the first method called in the life cycle of a module. It should not perform anything time consuming or anything with notable outside side effects. It should only read the parameters and initialise the module so that it can later be started. Note that a module being initialised doesn't mean that it necessarily will ever be started.
A ModuleException may be thrown if something vital is missing from the parameters or they are not sensible. In some cases you may not want to throw an exception even if vital initialisation information is missing. If, for example, it is possible that the module is initialised in some other way between the init and the start method calls. A ModuleException may also be thrown at the start method if the module is still not initialised.
init
in interface Module
init
in class ScriptModule
manager
- The module manager handling this module. You may keep a
reference to it if needed.ModuleException
public void start(ModuleManager manager) throws ModuleException
Module
start
in interface Module
start
in class ScriptModule
manager
- The module manager handling this module.ModuleException
public void stop(ModuleManager manager)
Module
stop
in interface Module
stop
in class ScriptModule
manager
- The module manager handling this module.protected void setHttpHeaders(javax.servlet.http.HttpServletResponse resp, java.util.Map<java.lang.String,java.lang.String> headers)
resp
- The HTTP response object.headers
- A Map containing the headers to be added.public abstract boolean handleAction(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, ModulesServlet.HttpMethod method, java.lang.String action, User user) throws javax.servlet.ServletException, java.io.IOException, ActionException
req
- The HTTP Request.resp
- The HTTP response.method
- The HTTP method of the request.action
- The parsed action id.user
- The logged in user, or null if not applicable.javax.servlet.ServletException
java.io.IOException
ActionException
public boolean isHandleAction(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, ModulesServlet.HttpMethod method)
ActionHandler
isHandleAction
in interface ActionHandler
req
- The HTTP request.resp
- The HTTP response.method
- The HTTP method of the request.public boolean handleRequest(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp, ModulesServlet.HttpMethod method, User user) throws javax.servlet.ServletException, java.io.IOException, ActionException
ServletModule.RequestListener
Handles the request, if this is the type of request the listener is interested in. If the listener does not wish to handle the request, it should return false. False return value indicates that the request should be passed to other listeners, a true return value indicates that this should not happen and a response to the request has been sent. Thus it is possible that you perform some action and still return false and let some other action write the response.
However, keep in mind that some other listener may grab any request before your listener and your listener will then never even see the request. Thus, you will not necessarily be notified of every single request. If this is your goal, you should probably make a separate context that all requests will pass through before they are passed on to other actions, see GenericContext.
handleRequest
in interface ServletModule.RequestListener
req
- The HTTP request.resp
- The HTTP response.method
- The HTTP method of the request.user
- The logged in user if applicable or null.javax.servlet.ServletException
java.io.IOException
ActionException
Copyright 2004-2015 Wandora Team