|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.stripes.controller.DynamicMappingFilter
public class DynamicMappingFilter
A servlet filter that dynamically maps URLs to ActionBean
s. This filter can be used to
allow Stripes to dispatch requests to ActionBean
s based on their URL binding, even if the
URL to which they are bound is not explicitly mapped in web.xml
.
One caveat must be observed when using this filter. This filter MUST be the last filter
in the filter chain. When it dynamically maps an ActionBean
to a URL, the filter chain is
interrupted.
StripesFilter
and/or DispatcherServlet
may be declared in web.xml
, but
neither is required for this filter to work. If you choose not to declare StripesFilter
in web.xml
, then this filter should be configured the way you would normally configure
StripesFilter
. However, some resources, such as JSPs, may require access to the Stripes
Configuration
through StripesFilter
. If you intend to access JSPs directly, then
StripesFilter
should be explicitly mapped to *.jsp
.
This filter takes the following approach to determining when to dispatch an ActionBean
:
404
was returned then send the error through. Otherwise ...ActionResolver
to see if an ActionBean
is mapped to the URL. If
not, then send the 404
error through. Otherwise...StripesFilter
and DispatcherServlet
One benefit of this approach is that static resources can be delivered from the same namespace to
which an ActionBean
is mapped using clean URLs. (For more information on clean URLs, see
UrlBinding
.) For example, if your UserActionBean
is mapped to
@UrlBinding("/user/{id}/{$event}")
and you have a static file at /user/icon.gif
,
then your icon will be delivered correctly because the initial request will not have returned a
404
error.
The IncludeBufferSize
initialization parameter (optional, default 1024) sets the number
of characters to be buffered by DynamicMappingFilter.TempBufferWriter
for include requests. See
DynamicMappingFilter.TempBufferWriter
for more information.
This is the suggested mapping for this filter in web.xml
.
<filter> <description>Dynamically maps URLs to ActionBeans.</description> <display-name>Stripes Dynamic Mapping Filter</display-name> <filter-name>DynamicMappingFilter</filter-name> <filter-class> net.sourceforge.stripes.controller.DynamicMappingFilter </filter-class> <init-param> <param-name>ActionResolver.Packages</param-name> <param-value>com.yourcompany.stripes.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>DynamicMappingFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping>
UrlBinding
Nested Class Summary | |
---|---|
static class |
DynamicMappingFilter.ErrorTrappingResponseWrapper
An HttpServletResponseWrapper that traps HTTP errors by overriding
sendError(int, ..) . |
static class |
DynamicMappingFilter.TempBufferWriter
A Writer that passes characters to a PrintWriter . |
Field Summary | |
---|---|
static String |
CONTEXT_KEY_STRIPES_FILTER
The attribute name used to store a reference to StripesFilter in the servlet context. |
static String |
INCLUDE_BUFFER_SIZE_PARAM
The name of the init-param that can be used to set the size of the buffer used by DynamicMappingFilter.TempBufferWriter before it overflows. |
Constructor Summary | |
---|---|
DynamicMappingFilter()
|
Method Summary | ||
---|---|---|
protected void |
createStripesFilter(FilterConfig config)
Create and initialize an instance of StripesFilter with the given configuration. |
|
void |
destroy()
|
|
void |
doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
|
|
protected
|
eval(String expression,
Node source,
QName returnType)
Evaluate an xpath expression against a DOM Node and return the result. |
|
protected Map<String,String> |
getFilterParameters(Node filterNode)
Get the initialization parameters for a filter declared in web.xml . |
|
protected List<String> |
getFilterUrlPatterns(Node filterNode)
Get all the URL patterns to which a filter is mapped in web.xml . |
|
protected String |
getRequestURI(HttpServletRequest request)
Deprecated. Use HttpUtil.getRequestedPath(HttpServletRequest) instead. |
|
protected StripesFilter |
getStripesFilter()
Get a reference to StripesFilter . |
|
void |
init(FilterConfig config)
|
|
protected void |
initStripesFilter(HttpServletRequest request,
HttpServletResponse response)
The servlet spec allows a container to wait until a filter is required to process a request before it initializes the filter. |
|
protected void |
issueRequests(List<String> patterns,
HttpServletRequest request,
HttpServletResponse response)
Issue a series of requests in an attempt to force an invocation (and initialization) of StripesFilter in the application context. |
|
protected Document |
parseWebXml()
Parse the application's web.xml file and return a DOM Document . |
|
void |
requestRemotely(HttpServletRequest request,
String relativePath)
Issue a new request to a path relative to the request's context. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String INCLUDE_BUFFER_SIZE_PARAM
DynamicMappingFilter.TempBufferWriter
before it overflows.
public static final String CONTEXT_KEY_STRIPES_FILTER
StripesFilter
in the servlet context.
Constructor Detail |
---|
public DynamicMappingFilter()
Method Detail |
---|
public void init(FilterConfig config) throws ServletException
init
in interface Filter
ServletException
public void destroy()
destroy
in interface Filter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
doFilter
in interface Filter
IOException
ServletException
@Deprecated protected String getRequestURI(HttpServletRequest request)
HttpUtil.getRequestedPath(HttpServletRequest)
instead.
protected StripesFilter getStripesFilter()
StripesFilter
. The first time this method is called, the reference
will be looked up in the servlet context and cached in the stripesFilter
field.
protected void initStripesFilter(HttpServletRequest request, HttpServletResponse response) throws ServletException
StripesFilter
from the servlet context, we really need StripesFilter
to have been initialized at
the time we process our first request. If that didn't happen automatically, this method does
its best to force it to happen.
request
- The current requestresponse
- The current response
ServletException
- If anything goes wrong that simply can't be ignored.protected Document parseWebXml() throws SAXException, IOException, ParserConfigurationException
web.xml
file and return a DOM Document
.
ParserConfigurationException
- If thrown by the XML parser
IOException
- If thrown by the XML parser
SAXException
- If thrown by the XML parserprotected <T> T eval(String expression, Node source, QName returnType) throws XPathExpressionException
Node
and return the result.
expression
- The expression to evaluatesource
- The node against which the expression will be evaluatedreturnType
- One of the constants defined in XPathConstants
XPath.evaluate(String, Object, QName)
XPathExpressionException
- If the xpath expression is invalidprotected List<String> getFilterUrlPatterns(Node filterNode) throws XPathExpressionException
web.xml
. This includes direct
mappings using filter-mapping/url-pattern
and indirect mappings using
filter-mapping/servlet-name
and servlet-mapping/url-pattern
.
filterNode
- The DOM (<filter>
) Node
containing the filter
declaration from web.xml
XPathExpressionException
- In case of failure evaluating an xpath expressionprotected Map<String,String> getFilterParameters(Node filterNode) throws XPathExpressionException
web.xml
.
filterNode
- The DOM (<filter>
) Node
containing the filter
declaration from web.xml
XPathExpressionException
- In case of failure evaluation an xpath expressionprotected void createStripesFilter(FilterConfig config) throws ServletException
StripesFilter
with the given configuration.
config
- The filter configuration
ServletException
- If initialization of the filter failsprotected void issueRequests(List<String> patterns, HttpServletRequest request, HttpServletResponse response)
StripesFilter
in the application context. All patterns will be requested first with
an internal forward, then an include and finally with a brand new request to the address and
port returned by ServletRequest.getLocalAddr()
and
ServletRequest.getLocalPort()
, respectively.
patterns
- The list of patterns to request, as specified by url-pattern
elements
in web.xml
request
- The current request, required to process a forward or includeresponse
- The current response, required to process a forward or includepublic void requestRemotely(HttpServletRequest request, String relativePath)
ServletRequest.getLocalAddr()
and
ServletRequest.getLocalPort()
, respectively.
request
- The current requestrelativePath
- The context-relative path to request
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |