| Method from freemarker.ext.servlet.FreemarkerServlet Detail: |
protected Configuration createConfiguration() {
return new Configuration();
}
This method is called from #init() to create the
FreeMarker configuration object that this servlet will use
for template loading. This is a hook that allows you
to custom-configure the configuration object in a subclass.
The default implementation returns a new Configuration
instance. |
protected TemplateModel createModel(ObjectWrapper wrapper,
ServletContext servletContext,
HttpServletRequest request,
HttpServletResponse response) throws TemplateModelException {
try {
AllHttpScopesHashModel params = new AllHttpScopesHashModel(wrapper, servletContext, request);
// Create hash model wrapper for servlet context (the application)
ServletContextHashModel servletContextModel =
(ServletContextHashModel) servletContext.getAttribute(
ATTR_APPLICATION_MODEL);
if (servletContextModel == null)
{
servletContextModel = new ServletContextHashModel(this, wrapper);
servletContext.setAttribute(
ATTR_APPLICATION_MODEL,
servletContextModel);
TaglibFactory taglibs = new TaglibFactory(servletContext);
servletContext.setAttribute(
ATTR_JSP_TAGLIBS_MODEL,
taglibs);
initializeServletContext(request, response);
}
params.putUnlistedModel(KEY_APPLICATION, servletContextModel);
params.putUnlistedModel(KEY_APPLICATION_PRIVATE, servletContextModel);
params.putUnlistedModel(KEY_JSP_TAGLIBS, (TemplateModel)servletContext.getAttribute(ATTR_JSP_TAGLIBS_MODEL));
// Create hash model wrapper for session
HttpSessionHashModel sessionModel;
HttpSession session = request.getSession(false);
if(session != null) {
sessionModel = (HttpSessionHashModel) session.getAttribute(ATTR_SESSION_MODEL);
if (sessionModel == null || sessionModel.isZombie()) {
sessionModel = new HttpSessionHashModel(session, wrapper);
session.setAttribute(ATTR_SESSION_MODEL, sessionModel);
if(!sessionModel.isZombie()) {
initializeSession(request, response);
}
}
}
else {
sessionModel = new HttpSessionHashModel(this, request, response, wrapper);
}
params.putUnlistedModel(KEY_SESSION, sessionModel);
// Create hash model wrapper for request
HttpRequestHashModel requestModel =
(HttpRequestHashModel) request.getAttribute(ATTR_REQUEST_MODEL);
if (requestModel == null || requestModel.getRequest() != request)
{
requestModel = new HttpRequestHashModel(request, response, wrapper);
request.setAttribute(ATTR_REQUEST_MODEL, requestModel);
request.setAttribute(
ATTR_REQUEST_PARAMETERS_MODEL,
createRequestParametersHashModel(request));
}
params.putUnlistedModel(KEY_REQUEST, requestModel);
params.putUnlistedModel(KEY_REQUEST_PRIVATE, requestModel);
// Create hash model wrapper for request parameters
HttpRequestParametersHashModel requestParametersModel =
(HttpRequestParametersHashModel) request.getAttribute(
ATTR_REQUEST_PARAMETERS_MODEL);
params.putUnlistedModel(KEY_REQUEST_PARAMETERS, requestParametersModel);
return params;
} catch (ServletException e) {
throw new TemplateModelException(e);
} catch (IOException e) {
throw new TemplateModelException(e);
}
}
|
protected ObjectWrapper createObjectWrapper() {
String wrapper = getServletConfig().getInitParameter(DEPR_INITPARAM_OBJECT_WRAPPER);
if (wrapper != null) { // BC
if (getInitParameter(Configurable.OBJECT_WRAPPER_KEY) != null) {
throw new RuntimeException("Conflicting init-params: "
+ Configurable.OBJECT_WRAPPER_KEY + " and "
+ DEPR_INITPARAM_OBJECT_WRAPPER);
}
if (DEPR_INITPARAM_WRAPPER_BEANS.equals(wrapper)) {
return ObjectWrapper.BEANS_WRAPPER;
}
if(DEPR_INITPARAM_WRAPPER_SIMPLE.equals(wrapper)) {
return ObjectWrapper.SIMPLE_WRAPPER;
}
if(DEPR_INITPARAM_WRAPPER_JYTHON.equals(wrapper)) {
// Avoiding compile-time dependency on Jython package
try {
return (ObjectWrapper) Class.forName("freemarker.ext.jython.JythonWrapper")
.newInstance();
} catch (InstantiationException e) {
throw new InstantiationError(e.getMessage());
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
} catch (ClassNotFoundException e) {
throw new NoClassDefFoundError(e.getMessage());
}
}
// return BeansWrapper.getDefaultInstance();
return ObjectWrapper.DEFAULT_WRAPPER;
} else {
wrapper = getInitParameter(Configurable.OBJECT_WRAPPER_KEY);
if (wrapper == null) {
// return BeansWrapper.getDefaultInstance();
return ObjectWrapper.DEFAULT_WRAPPER;
} else {
try {
config.setSetting(Configurable.OBJECT_WRAPPER_KEY, wrapper);
} catch (TemplateException e) {
throw new RuntimeException(e.toString());
}
return config.getObjectWrapper();
}
}
}
This method is called from #init() to create the
FreeMarker object wrapper object that this servlet will use
for adapting request, session, and servlet context attributes into
template models.. This is a hook that allows you
to custom-configure the wrapper object in a subclass.
The default implementation returns a wrapper that depends on the value
of ObjectWrapper init parameter. If simple is
specified, ObjectWrapper#SIMPLE_WRAPPER is used; if jython
is specified, freemarker.ext.jython.JythonWrapper is used. In
every other case ObjectWrapper#DEFAULT_WRAPPER is used. |
protected HttpRequestParametersHashModel createRequestParametersHashModel(HttpServletRequest request) {
return new HttpRequestParametersHashModel(request);
}
|
protected TemplateLoader createTemplateLoader(String templatePath) throws IOException {
if (templatePath.startsWith("class://")) {
// substring(7) is intentional as we "reuse" the last slash
return new ClassTemplateLoader(getClass(), templatePath.substring(7));
} else {
if (templatePath.startsWith("file://")) {
templatePath = templatePath.substring(7);
return new FileTemplateLoader(new File(templatePath));
} else {
return new WebappTemplateLoader(this.getServletContext(), templatePath);
}
}
}
|
protected Locale deduceLocale(String templatePath,
HttpServletRequest request,
HttpServletResponse response) {
return config.getLocale();
}
Returns the locale used for the
Configuration#getTemplate(String, Locale) call.
The base implementation simply returns the locale setting of the
configuration. Override this method to provide different behaviour, i.e.
to use the locale indicated in the request. |
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
process(request, response);
}
|
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
process(request, response);
}
|
protected Configuration getConfiguration() {
return config;
}
|
protected ObjectWrapper getObjectWrapper() {
return wrapper;
}
|
protected final String getTemplatePath() {
return templatePath;
}
|
public void init() throws ServletException {
try {
config = createConfiguration();
// Set defaults:
config.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
contentType = DEFAULT_CONTENT_TYPE;
// Process object_wrapper init-param out of order:
wrapper = createObjectWrapper();
if (logger.isDebugEnabled()) {
logger.debug("Using object wrapper of class " + wrapper.getClass().getName());
}
config.setObjectWrapper(wrapper);
// Process TemplatePath init-param out of order:
templatePath = getInitParameter(INITPARAM_TEMPLATE_PATH);
if (templatePath == null)
templatePath = "class://";
config.setTemplateLoader(createTemplateLoader(templatePath));
// Process all other init-params:
Enumeration initpnames = getServletConfig().getInitParameterNames();
while (initpnames.hasMoreElements()) {
String name = (String) initpnames.nextElement();
String value = getInitParameter(name);
if (name == null) {
throw new ServletException(
"init-param without param-name. "
+ "Maybe the web.xml is not well-formed?");
}
if (value == null) {
throw new ServletException(
"init-param without param-value. "
+ "Maybe the web.xml is not well-formed?");
}
if (name.equals(DEPR_INITPARAM_OBJECT_WRAPPER)
|| name.equals(Configurable.OBJECT_WRAPPER_KEY)
|| name.equals(INITPARAM_TEMPLATE_PATH)) {
// ignore: we have already processed these
} else if (name.equals(DEPR_INITPARAM_ENCODING)) { // BC
if (getInitParameter(Configuration.DEFAULT_ENCODING_KEY) != null) {
throw new ServletException(
"Conflicting init-params: "
+ Configuration.DEFAULT_ENCODING_KEY + " and "
+ DEPR_INITPARAM_ENCODING);
}
config.setDefaultEncoding(value);
} else if (name.equals(DEPR_INITPARAM_TEMPLATE_DELAY)) { // BC
if (getInitParameter(Configuration.TEMPLATE_UPDATE_DELAY_KEY) != null) {
throw new ServletException(
"Conflicting init-params: "
+ Configuration.TEMPLATE_UPDATE_DELAY_KEY + " and "
+ DEPR_INITPARAM_TEMPLATE_DELAY);
}
try {
config.setTemplateUpdateDelay(Integer.parseInt(value));
} catch (NumberFormatException e) {
// Intentionally ignored
}
} else if (name.equals(DEPR_INITPARAM_TEMPLATE_EXCEPTION_HANDLER)) { // BC
if (getInitParameter(Configurable.TEMPLATE_EXCEPTION_HANDLER_KEY) != null) {
throw new ServletException(
"Conflicting init-params: "
+ Configurable.TEMPLATE_EXCEPTION_HANDLER_KEY + " and "
+ DEPR_INITPARAM_TEMPLATE_EXCEPTION_HANDLER);
}
if (DEPR_INITPARAM_TEMPLATE_EXCEPTION_HANDLER_RETHROW.equals(value)) {
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
} else if (DEPR_INITPARAM_TEMPLATE_EXCEPTION_HANDLER_DEBUG.equals(value)) {
config.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
} else if (DEPR_INITPARAM_TEMPLATE_EXCEPTION_HANDLER_HTML_DEBUG.equals(value)) {
config.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
} else if (DEPR_INITPARAM_TEMPLATE_EXCEPTION_HANDLER_IGNORE.equals(value)) {
config.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
} else {
throw new ServletException(
"Invalid value for servlet init-param "
+ DEPR_INITPARAM_TEMPLATE_EXCEPTION_HANDLER + ": " + value);
}
} else if (name.equals(INITPARAM_NOCACHE)) {
nocache = StringUtil.getYesNo(value);
} else if (name.equals(DEPR_INITPARAM_DEBUG)) { // BC
if (getInitParameter(INITPARAM_DEBUG) != null) {
throw new ServletException(
"Conflicting init-params: "
+ INITPARAM_DEBUG + " and "
+ DEPR_INITPARAM_DEBUG);
}
debug = StringUtil.getYesNo(value);
} else if (name.equals(INITPARAM_DEBUG)) {
debug = StringUtil.getYesNo(value);
} else if (name.equals(INITPARAM_CONTENT_TYPE)) {
contentType = value;
} else {
config.setSetting(name, value);
}
} // while initpnames
noCharsetInContentType = true;
int i = contentType.toLowerCase().indexOf("charset=");
if (i != -1) {
char c = ' ";
i--;
while (i >= 0) {
c = contentType.charAt(i);
if (!Character.isWhitespace(c)) break;
i--;
}
if (i == -1 || c == ';") {
noCharsetInContentType = false;
}
}
} catch (ServletException e) {
throw e;
} catch (Exception e) {
throw new ServletException(e);
}
}
|
protected void initializeServletContext(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
}
Called when servlet detects in a request processing that
application-global (that is, ServletContext-specific) attributes are not yet
set.
This is a generic hook you might use in subclasses to perform a specific
action on first request in the context. By default it does nothing. |
protected void initializeSession(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
}
Called when servlet detects in a request processing that session-global
(that is, HttpSession-specific) attributes are not yet set.
This is a generic hook you might use in subclasses to perform a specific
action on first request in the session. By default it does nothing. It
is only invoked on newly created sessions; it is not invoked when a
replicated session is reinstantiated in another servlet container. |
protected void postTemplateProcess(HttpServletRequest request,
HttpServletResponse response,
Template template,
TemplateModel data) throws IOException, ServletException {
}
Called after the execution returns from template.process().
This is a generic hook you might use in subclasses to perform a specific
action after the template is processed. It will be invoked even if the
template processing throws an exception. By default does nothing. |
protected boolean preTemplateProcess(HttpServletRequest request,
HttpServletResponse response,
Template template,
TemplateModel data) throws IOException, ServletException {
return true;
}
((SimpleHash) data).put("baseDir", request.getContextPath() + "/");
return true;
|
protected boolean preprocessRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
return false;
}
Called as the first step in request processing, before the templating mechanism
is put to work. By default does nothing and returns false. This method is
typically overridden to manage serving of non-template resources (i.e. images)
that reside in the template directory. |
protected String requestUrlToTemplatePath(HttpServletRequest request) {
// First, see if it is an included request
String includeServletPath = (String) request.getAttribute("javax.servlet.include.servlet_path");
if(includeServletPath != null)
{
// Try path info; only if that's null (servlet is mapped to an
// URL extension instead of to prefix) use servlet path.
String includePathInfo = (String) request.getAttribute("javax.servlet.include.path_info");
return includePathInfo == null ? includeServletPath : includePathInfo;
}
// Seems that the servlet was not called as the result of a
// RequestDispatcher.include(...). Try pathInfo then servletPath again,
// only now directly on the request object:
String path = request.getPathInfo();
if (path != null) return path;
path = request.getServletPath();
if (path != null) return path;
// Seems that it is a servlet mapped with prefix, and there was no extra path info.
return "";
}
Maps the request URL to a template path that is passed to
Configuration#getTemplate(String, Locale) . You can override it
(i.e. to provide advanced rewriting capabilities), but you are strongly
encouraged to call the overridden method first, then only modify its
return value. |