📄 applicationfilterfactory.java
字号:
// Acquire the information we will need to match filter mappings
String servletName = wrapper.getName();
int n = 0;
// Add the relevant path-mapped filters to this filter chain
for (int i = 0; i < filterMaps.length; i++) {
if (!matchDispatcher(filterMaps[i] ,dispatcher)) {
continue;
}
if (!matchFiltersURL(filterMaps[i], requestPath))
continue;
ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
context.findFilterConfig(filterMaps[i].getFilterName());
if (filterConfig == null) {
; // FIXME - log configuration problem
continue;
}
filterChain.addFilter(filterConfig);
n++;
}
// Add filters that match on servlet name second
for (int i = 0; i < filterMaps.length; i++) {
if (!matchDispatcher(filterMaps[i] ,dispatcher)) {
continue;
}
if (!matchFiltersServlet(filterMaps[i], servletName))
continue;
ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
context.findFilterConfig(filterMaps[i].getFilterName());
if (filterConfig == null) {
; // FIXME - log configuration problem
continue;
}
filterChain.addFilter(filterConfig);
n++;
}
// Return the completed filter chain
return (filterChain);
}
// -------------------------------------------------------- Private Methods
/**
* Return <code>true</code> if the context-relative request path
* matches the requirements of the specified filter mapping;
* otherwise, return <code>null</code>.
*
* @param filterMap Filter mapping being checked
* @param requestPath Context-relative request path of this request
*/
private boolean matchFiltersURL(FilterMap filterMap, String requestPath) {
if (requestPath == null)
return (false);
// Match on context relative request path
String testPath = filterMap.getURLPattern();
if (testPath == null)
return (false);
// Case 1 - Exact Match
if (testPath.equals(requestPath))
return (true);
// Case 2 - Path Match ("/.../*")
if (testPath.equals("/*"))
return (true);
if (testPath.endsWith("/*")) {
if (testPath.regionMatches(0, requestPath, 0,
testPath.length() - 2)) {
if (requestPath.length() == (testPath.length() - 2)) {
return (true);
} else if ('/' == requestPath.charAt(testPath.length() - 2)) {
return (true);
}
}
return (false);
}
// Case 3 - Extension Match
if (testPath.startsWith("*.")) {
int slash = requestPath.lastIndexOf('/');
int period = requestPath.lastIndexOf('.');
if ((slash >= 0) && (period > slash)
&& (period != requestPath.length() - 1)
&& ((requestPath.length() - period)
== (testPath.length() - 1))) {
return (testPath.regionMatches(2, requestPath, period + 1,
testPath.length() - 2));
}
}
// Case 4 - "Default" Match
return (false); // NOTE - Not relevant for selecting filters
}
/**
* Return <code>true</code> if the specified servlet name matches
* the requirements of the specified filter mapping; otherwise
* return <code>false</code>.
*
* @param filterMap Filter mapping being checked
* @param servletName Servlet name being checked
*/
private boolean matchFiltersServlet(FilterMap filterMap,
String servletName) {
if (servletName == null) {
return (false);
} else {
if (servletName.equals(filterMap.getServletName())) {
return (true);
} else {
return false;
}
}
}
/**
* Convienience method which returns true if the dispatcher type
* matches the dispatcher types specified in the FilterMap
*/
private boolean matchDispatcher(FilterMap filterMap, int dispatcher) {
switch (dispatcher) {
case FORWARD : {
if (filterMap.getDispatcherMapping() == FilterMap.FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.FORWARD_ERROR ||
filterMap.getDispatcherMapping() == FilterMap.INCLUDE_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD_INCLUDE) {
return true;
}
break;
}
case INCLUDE : {
if (filterMap.getDispatcherMapping() == FilterMap.INCLUDE ||
filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR ||
filterMap.getDispatcherMapping() == FilterMap.INCLUDE_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_INCLUDE ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_INCLUDE ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD_INCLUDE) {
return true;
}
break;
}
case REQUEST : {
if (filterMap.getDispatcherMapping() == FilterMap.REQUEST ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_INCLUDE ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_INCLUDE ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD_INCLUDE ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE) {
return true;
}
break;
}
case ERROR : {
if (filterMap.getDispatcherMapping() == FilterMap.ERROR ||
filterMap.getDispatcherMapping() == FilterMap.FORWARD_ERROR ||
filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR ||
filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE ||
filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_INCLUDE) {
return true;
}
break;
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -