public class GenericTemplateAction extends CachedAction
A general purpose action that uses templates for output. This can be used on its own or subclassed in a few different ways.
Standalone use just takes some template specified in initialisation parameters, with parameter name templateKey, and gives it the default context returned by the template manager. You can add objects into the context with initialisation parameters of this action too, by prefixing the context variable name with "context.". The default context, with possible action specific additions, is passed to the template which can then produce the desired page.
To make the produced page depend on the HTTP request parameters, you can forward some of them directly into the template context. To do this, list the request parameters you would like forwarded in an initialisation parameter named forwardRequestParameters, separated by semicolons. Then the contents of the user provided request parameters are copied into the template context. Remember to sanity check any values thus forwarded. You can also set default values for such request parameters which are used if none are provided in the request. Do this by prefixing the initialisation parameter name with "defaultRequestParameter." and then adding the parameter name and put the value in the contents of the param element.
Simplest way to subclass is to just override the getTemplateContext method. It should use getDefaultTemplateContext as basis and then add whatever is needed in the context. If needed, you can then do any side effects here as well, like modifying a database or something similar.
For even more control, override the handleAction method. Then you have to call the template yourself and write the output to the HTTP response object.
This class extends CachedAction so if it is likely that the same page be generated often, it may be of use to cache the page. To do this, set the initialisation parameter caching to true, as per normal CachedAction behaviour. The default implementation will use the action name and all forwarded request parameters in the cache key. Assuming the default context doesn't contain anything dynamic, this should be adequate.
CachedAction.OutputProvider
Modifier and Type | Field and Description |
---|---|
protected java.util.HashMap<java.lang.String,java.lang.String> |
defaultRequestParameters |
protected java.util.ArrayList<java.lang.String> |
forwardRequestParameters |
protected java.lang.String |
requestContextKey |
protected java.util.HashMap<java.lang.String,java.lang.Object> |
staticContext |
protected java.lang.String |
templateKey |
protected TemplateManager |
templateManager |
cache, caching, threadPool
actionParamKey, handledActions, httpHeaders, isDefaultAction, replacements, replacementsInitialized, servletModule
engine, initScript, persistentObjects, scriptManager, startScript, stopScript
autoStart, isInitialized, isRunning, logging, loggingModule, moduleManager
Constructor and Description |
---|
GenericTemplateAction() |
Modifier and Type | Method and Description |
---|---|
protected boolean |
doOutput(javax.servlet.http.HttpServletRequest req,
ModulesServlet.HttpMethod method,
java.lang.String action,
CachedAction.OutputProvider out,
User user)
Does the output that might be either cached or returned directly.
|
protected java.lang.String |
getCacheKey(javax.servlet.http.HttpServletRequest req,
ModulesServlet.HttpMethod method,
java.lang.String action)
Should return a string that uniquely identifies this request.
|
protected java.util.LinkedHashMap<java.lang.String,java.lang.String> |
getCacheKeyParams(javax.servlet.http.HttpServletRequest req,
ModulesServlet.HttpMethod method,
java.lang.String action)
Returns the parameters used in building the cache key.
|
java.util.Collection<Module> |
getDependencies(ModuleManager manager)
Returns all the modules this module depends on.
|
protected java.util.LinkedHashMap<java.lang.String,java.lang.String> |
getForwardedParameters(javax.servlet.http.HttpServletRequest req)
Returns a map of the http request parameters that are to be
forwarded to the template context.
|
protected java.util.HashMap<java.lang.String,java.lang.Object> |
getStaticTemplateContext()
Gets the static context to for templates.
|
protected Template |
getTemplate(javax.servlet.http.HttpServletRequest req,
ModulesServlet.HttpMethod method,
java.lang.String action) |
protected java.util.HashMap<java.lang.String,java.lang.Object> |
getTemplateContext(Template template,
javax.servlet.http.HttpServletRequest req,
ModulesServlet.HttpMethod method,
java.lang.String action,
User user)
Gets the template context used for a request.
|
void |
init(ModuleManager manager,
java.util.HashMap<java.lang.String,java.lang.Object> settings)
Initialises the module.
|
void |
putStaticContext(java.lang.String key,
java.lang.Object o)
Adds a value to the action specific static template context.
|
protected void |
returnOutput(java.io.InputStream cacheIn,
javax.servlet.http.HttpServletResponse resp)
Writes a page read from an input stream into an HTTP servlet response.
|
void |
start(ModuleManager manager)
Starts the module.
|
void |
stop(ModuleManager manager)
Stops the module.
|
buildCacheKey, handleAction, readMetadata, writeMetadata
addHandledAction, doReplacements, handleRequest, isHandleAction, setActionParamKey, setHttpHeaders
getScriptEngine
isInitialized, isRunning, requireLogging, toString
protected java.lang.String requestContextKey
protected TemplateManager templateManager
protected java.lang.String templateKey
protected java.util.ArrayList<java.lang.String> forwardRequestParameters
protected java.util.HashMap<java.lang.String,java.lang.String> defaultRequestParameters
protected final java.util.HashMap<java.lang.String,java.lang.Object> staticContext
public void putStaticContext(java.lang.String key, java.lang.Object o)
key
- The name of the variable in the context.o
- The value of the context variable.protected Template getTemplate(javax.servlet.http.HttpServletRequest req, ModulesServlet.HttpMethod method, java.lang.String action)
public java.util.Collection<Module> getDependencies(ModuleManager manager) throws ModuleException
Module
getDependencies
in interface Module
getDependencies
in class CachedAction
manager
- The module manager handling this module.ModuleException
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 CachedAction
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 CachedAction
manager
- The module manager handling this module.ModuleException
public void stop(ModuleManager manager)
Module
stop
in interface Module
stop
in class CachedAction
manager
- The module manager handling this module.protected java.util.HashMap<java.lang.String,java.lang.Object> getStaticTemplateContext()
protected java.util.LinkedHashMap<java.lang.String,java.lang.String> getForwardedParameters(javax.servlet.http.HttpServletRequest req)
req
- The HTTP request.protected java.util.HashMap<java.lang.String,java.lang.Object> getTemplateContext(Template template, javax.servlet.http.HttpServletRequest req, ModulesServlet.HttpMethod method, java.lang.String action, User user) throws ActionException
Gets the template context used for a request. Contains the static context as well forwarded request parameters. The static context contains both action specific context items and the template manager default context. In addition, the logged in user, if applicable, is added with the variable name user. Usually you don't want anything user specific cached so as a safety measure, the user variable is not added if caching is turned on. If you for some reason do, you will have to override this method.
This should be the main overriding point for extending classes. You can perform any tasks needed and then add things in the context. Often you don't need to override anything else besides this and the basic Module methods.
template
- The template to be used.req
- The HTTP request.method
- The method of the HTTP request.action
- The action parameter parsed from the HTTP request.user
- The logged in user, or null if not logged in.ActionException
protected java.util.LinkedHashMap<java.lang.String,java.lang.String> getCacheKeyParams(javax.servlet.http.HttpServletRequest req, ModulesServlet.HttpMethod method, java.lang.String action)
req
- The HTTP request for which cache key parameters are resolved.method
- The method of the HTTP request.action
- The parsed action parameter from the request.protected java.lang.String getCacheKey(javax.servlet.http.HttpServletRequest req, ModulesServlet.HttpMethod method, java.lang.String action)
CachedAction
getCacheKey
in class CachedAction
req
- The HTTP request which, result of which is to be cached.method
- The method of the HTTP request.action
- The parsed action parameter from the request.protected void returnOutput(java.io.InputStream cacheIn, javax.servlet.http.HttpServletResponse resp) throws java.io.IOException
CachedAction
Writes a page read from an input stream into an HTTP servlet response. The input stream might be coming from a cached file, or directly from the doOutput method if caching is not in use. In any case, the contents of it need to be written to the response object. Note that any HTTP headers must be set before writing anything else.
If you used writeMetadata in doOutput, you have to override this method and use readMetadata correspondingly to read the metadata from the start of the stream. You can then, for example, set any HTTP headers you read from the metadata. After this, you can call this super implementation which will do the rest of the stream copying.
Make absolutely certain that the input stream is closed before returning from this method. Failing to do so may cause the caching service to become locked which can then lock the whole application. It is best to put the contents in a try-finally block and then call cacheIn.close in the finally part. Do this even if you call the super implementation as your code could possibly fail before reaching that part.
returnOutput
in class CachedAction
java.io.IOException
protected boolean doOutput(javax.servlet.http.HttpServletRequest req, ModulesServlet.HttpMethod method, java.lang.String action, CachedAction.OutputProvider out, User user) throws javax.servlet.ServletException, java.io.IOException, ActionException
CachedAction
Does the output that might be either cached or returned directly. The OutputProvider is used to get the output stream where the output should be written. Before you get this stream, you may abort the action by returning false but after the output stream is retrieved, you are committed to outputting the page that then might be cached for future use. Therefore you should do all the preparations first, and only then get the output stream when you know that you will be able to output something sensible.
If you opened the output stream using the output provider, you must make sure that the stream is properly closed. It is best to place all your code after this in a try-finally block and close the stream in the finally part. Failing to do this may cause the caching service to become locked, which in turn could potentially lock the whole application.
doOutput
in class CachedAction
javax.servlet.ServletException
java.io.IOException
ActionException
Copyright 2004-2015 Wandora Team