| java.lang.Object net.homeip.donaldm.httpdbase4j.StringTemplateHandler
All known Subclasses: net.homeip.donaldm.httpdbase4j.FileStringTemplateHandler, net.homeip.donaldm.httpdbase4j.ArchiveStringTemplateHandler,
StringTemplateHandler | abstract public class StringTemplateHandler implements HttpHandleable,Postable,Templatable(Code) | | Provides a base handler class for templating based on the StringTemplate
library (http://www.stringtemplate.org/)
The Templates (which must have an extension of '.st') may be
processed in three ways:
-
The user may specify a template processing class that implements the
Templatable interface. The Templatable.processTemplate method of that class
will then be called for each template.
-
If a template processing class is not specified in the constructor then
the StringTemplateHandler class uses itself as a template processing
class. The default Templatable handler method loads a class from
the Java package in m_templateJavaPackage (which must be specified in the
constructor) with the same name as the template. This class must have an
empty public constructor and must implement the Templatable interface.
The class name (excluding the extension) must either be exactly the same
as the template (ie have the same case too) or the first letter can be
uppercase. For example if the the template is listpersons.st then
listpersons.class or Listpersons.class will be found.
-
The user can overide the StringTemplateHTTPD class and provide his own
processTemplate method
The StringTemplate library (@see http://www.stringtemplate.org) requires
stringtemplate.jar and antlr-2.7.7.jar be in the classpath.
See Also: FileStringTemplateHandler See Also: ArchiveStringTemplateHandler author: Donald Munro |
Method Summary | |
protected Class | _instantiateTemplateClass(String className) | public boolean | getCacheable() | abstract protected StringTemplate | getTemplate(Request request) | abstract protected Templatable | getTemplateInstance(String templateName) | public Request | onFileNotFound(long id, HttpExchange ex, Request request) | public Object | onHandlePost(long id, HttpExchange ex, Request request, HttpResponse response, File dir, Object... extraParameters) | public String | onListDirectory(Request request) | public void | onPostServe(long id, HttpExchange ex, Request request, boolean isOK) | public boolean | onPreServe(long id, HttpExchange ex, Request request) | public InputStream | onServeBody(long id, HttpExchange ex, Request request) | public HttpResponse | onServeHeaders(long id, HttpExchange ex, Request request) | public void | setCacheable(boolean isCacheable) By default the StringTemplateHandler derived classes return m_isCacheable
which defaults to false to disallow cacheing for template based resources. | public void | setDebug(boolean b) | public File | templateFile(StringTemplate template, Request request, StringBuffer mimeType, File dir) The method processes templates and returns the file generated from the
template. | public InputStream | templateStream(StringTemplate template, Request request, StringBuffer mimeType) The method processes templates and returns the file generated from the
template. | public String | templateString(StringTemplate template, Request request, StringBuffer mimeType) The method processes templates and returns the file generated from the
template. |
m_isCacheable | protected boolean m_isCacheable(Code) | | |
m_templateGroup | protected StringTemplateGroup m_templateGroup(Code) | | Template group for the home directory (also handles sub-dirs of the
home dir
|
m_templateJavaPackage | protected String m_templateJavaPackage(Code) | | The package name for per-template processing and generation Java classes.
|
m_templateProcessor | protected Templatable m_templateProcessor(Code) | | Template processing and generation class.
|
StringTemplateHandler | public StringTemplateHandler(Httpd httpd, String templateJavaPackage) throws IOException(Code) | | Constructor with template package.
Parameters: httpd - The Httpd instance Parameters: templateJavaPackage - The Java package where template processing classes are located. throws: java.io.IOException - Throws an IO exception if the homeDir directory is not accessible. |
StringTemplateHandler | public StringTemplateHandler(Httpd httpd, Templatable templateProcessor) throws IOException, IllegalArgumentException(Code) | | Constructor with a template processor class that implements
the Templatable interface.
Parameters: httpd - The Httpd instance Parameters: templateProcessor - A Java class that implements the Templatable interface. throws: java.io.IOException - Throws an IO exception if the homeDir directory not accessible. throws: java.lang.IllegalArgumentException - Throws an IllegalArgumentException exception if templateProcessor is null |
_instantiateTemplateClass | protected Class _instantiateTemplateClass(String className)(Code) | | |
getCacheable | public boolean getCacheable()(Code) | | true if cacheing is enabled for StringTemplate resources, elsefalse |
getTemplate | abstract protected StringTemplate getTemplate(Request request)(Code) | | |
onPostServe | public void onPostServe(long id, HttpExchange ex, Request request, boolean isOK)(Code) | | |
onPreServe | public boolean onPreServe(long id, HttpExchange ex, Request request)(Code) | | |
setCacheable | public void setCacheable(boolean isCacheable)(Code) | | By default the StringTemplateHandler derived classes return m_isCacheable
which defaults to false to disallow cacheing for template based resources.
Use this method to enable/disable caching for this handler.
Parameters: isCacheable - true to enable cahceing for this handler. |
setDebug | public void setDebug(boolean b)(Code) | | Parameters: b - true to enable debug mode (Disables use of template cache) |
templateFile | public File templateFile(StringTemplate template, Request request, StringBuffer mimeType, File dir)(Code) | | The method processes templates and returns the file generated from the
template. The default behaviour when this class is the template processor
is to load a class from m_templateJavaPackage with the same name as the
template. This class must have an empty public constructor and must
implement the Templatable interface.
Overiding classes can modify this behaviour to change the way templates
content is generated.
Parameters: template - The template Parameters: request - The Request instance. The file generated from the template. |
templateStream | public InputStream templateStream(StringTemplate template, Request request, StringBuffer mimeType)(Code) | | The method processes templates and returns the file generated from the
template. The default behaviour when this class is the template processor
is to load a class from m_templateJavaPackage with the same name as the
template. This class must have an empty public constructor and must
implement the Templatable interface.
Overiding classes can modify this behaviour to change the way templates
content is generated.
Note With the current implementation only templateString (and
templateFile for POST processing) is used so this method need not be
implemented, although if the implementation changes for example to use
chunked encoding and not to generate a string in onServeHeaders, but
instead to generate a stream in onServeBody then templateStream would be
required.
Parameters: template - The template Parameters: request - The Request instance. The InputStream generated from the template. |
templateString | public String templateString(StringTemplate template, Request request, StringBuffer mimeType)(Code) | | The method processes templates and returns the file generated from the
template. The default behaviour when this class is the template processor
is to load a class from m_templateJavaPackage with the same name as the
template. This class must have an empty public constructor and must
implement the Templatable interface.
Overiding classes can modify this behaviour to change the way templates
content is generated.
Parameters: template - The template Parameters: request - The Request instance. The String generated from the template. |
|
|