Groovy 1.7.0

groovy.servlet
Class AbstractHttpServlet

java.lang.Object
  javax.servlet.GenericServlet
      javax.servlet.http.HttpServlet
          groovy.servlet.AbstractHttpServlet
All Implemented Interfaces:
ResourceConnector

class AbstractHttpServlet
extends HttpServlet

A common ground dealing with the HTTP servlet API wrinkles.

Resource name mangling (pattern replacement)

Also implements Groovy's ResourceConnector in dynamic manner. It allows to modifiy the resource name that is searched for with a replace all operation. See Pattern and Matcher for details. The servlet init parameter names are:

 resource.name.regex = empty - defaults to null
 resource.name.replacement = empty - defaults to null
 resource.name.replace.all = true (default) | false means replaceFirst()
 
Note: If you specify a regex, you have to specify a replacement string too! Otherwise an exception gets raised.

Logging and bug-hunting options

This implementation provides a verbosity flag switching log statements. The servlet init parameter name is:

 verbose = false(default) | true
 

In order to support class-loading-troubles-debugging with Tomcat 4 or higher, you can log the class loader responsible for loading some classes. See GROOVY-861 for details. The servlet init parameter name is:

 log.GROOVY861 = false(default) | true
 

If you experience class-loading-troubles with Tomcat 4 (or higher) or any other servlet container using custom class loader setups, you can fallback to use (slower) reflection in Groovy's MetaClass implementation. Please contact the dev team with your problem! Thanks. The servlet init parameter name is:

 reflection = false(default) | true
 
author:
Christian Stein
author:
Roshan Dawrani (roshandawrani)


Field Summary
static String CONTENT_TYPE_TEXT_HTML

Content type of the HTTP response.

static String INC_PATH_INFO

Servlet API include key name: path_info

static String INC_REQUEST_URI

static String INC_SERVLET_PATH

Servlet API include key name: servlet_path

protected String encoding

Encoding to use, becomes charset part of contentType.

protected boolean reflection

Mirrors the static value of the reflection flag in MetaClass.

protected Matcher resourceNameMatcher

Null or compiled pattern matcher read from "resource.name.regex" and used in {

protected boolean resourceNameReplaceAll

The replace method to use on the matcher.

protected String resourceNameReplacement

The replacement used by the resource name matcher.

protected ServletContext servletContext

Servlet (or the web application) context.

protected boolean verbose

Controls almost all log output.

 
Constructor Summary
AbstractHttpServlet()

Initializes all fields with default values.

 
Method Summary
URLConnection getResourceConnection(String name)

Interface method for ResourceContainer.

protected String getScriptUri(HttpServletRequest request)

Returns the include-aware uri of the script or template file.

protected File getScriptUriAsFile(HttpServletRequest request)

Parses the http request for the real script or template source file.

void init(ServletConfig config)

Overrides the generic init method to set some debug flags.

protected void setVariables(ServletBinding binding)

Override this method to set your variables to the Groovy binding.

 
Methods inherited from class HttpServlet
service, log, log, init, init, destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 
Methods inherited from class GenericServlet
log, log, init, init, destroy, service, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 
Methods inherited from class Object
wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 

Field Detail

CONTENT_TYPE_TEXT_HTML

static final String CONTENT_TYPE_TEXT_HTML
Content type of the HTTP response.


INC_PATH_INFO

static final String INC_PATH_INFO
Servlet API include key name: path_info


INC_REQUEST_URI

static final String INC_REQUEST_URI


INC_SERVLET_PATH

static final String INC_SERVLET_PATH
Servlet API include key name: servlet_path


encoding

protected String encoding
Encoding to use, becomes charset part of contentType.


reflection

protected boolean reflection
Mirrors the static value of the reflection flag in MetaClass. See AbstractHttpServlet#logGROOVY861


resourceNameMatcher

protected Matcher resourceNameMatcher
Null or compiled pattern matcher read from "resource.name.regex" and used in AbstractHttpServlet#getResourceConnection(String).


resourceNameReplaceAll

protected boolean resourceNameReplaceAll
The replace method to use on the matcher.
 true - replaceAll(resourceNameReplacement); (default)
 false - replaceFirst(resourceNameReplacement);
 


resourceNameReplacement

protected String resourceNameReplacement
The replacement used by the resource name matcher.


servletContext

protected ServletContext servletContext
Servlet (or the web application) context.


verbose

protected boolean verbose
Controls almost all log output.


 
Constructor Detail

AbstractHttpServlet

public AbstractHttpServlet()
Initializes all fields with default values.


 
Method Detail

getResourceConnection

public URLConnection getResourceConnection(String name)
Interface method for ResourceContainer. This is used by the GroovyScriptEngine.


getScriptUri

protected String getScriptUri(HttpServletRequest request)
Returns the include-aware uri of the script or template file.
param:
request the http request to analyze
return:
the include-aware uri either parsed from request attributes or hints provided by the servlet container


getScriptUriAsFile

protected File getScriptUriAsFile(HttpServletRequest request)
Parses the http request for the real script or template source file.
param:
request the http request to analyze
return:
a file object using an absolute file path name


init

public void init(ServletConfig config)
Overrides the generic init method to set some debug flags.
param:
config the servlet coniguration provided by the container
throws:
ServletException if init() method defined in super class javax.servlet.GenericServlet throws it


setVariables

protected void setVariables(ServletBinding binding)
Override this method to set your variables to the Groovy binding.

All variables bound the binding are passed to the template source text, e.g. the HTML file, when the template is merged.

The binding provided by TemplateServlet does already include some default variables. As of this writing, they are (copied from ServletBinding):

And via implicite hard-coded keywords:

The binding also provides convenient methods:

Example binding all servlet context variables:


 class Mytlet extends TemplateServlet {
 
   protected void setVariables(ServletBinding binding) {
     // Bind a simple variable
     binding.setVariable("answer", new Long(42));
   
     // Bind all servlet context attributes...
     ServletContext context = (ServletContext) binding.getVariable("context");
     Enumeration enumeration = context.getAttributeNames();
     while (enumeration.hasMoreElements()) {
       String name = (String) enumeration.nextElement();
       binding.setVariable(name, context.getAttribute(name));
     }
   }
 
 }
 

param:
binding to be modified


 

Copyright © 2003-2009 The Codehaus. All rights reserved.