// Iterate to do filter matching the current request servlet
// path. Remove bundle's web prefix from servlet path
// temporarily before filter invoke.
ClonedExecutableServletObject<FilterDelegate> clonedFilterInstance = it
.next();
FilterDelegate filterDelegate = clonedFilterInstance
.getOriginalObj();
String urlPattern = filterDelegate.getURLPatterns()[clonedFilterInstance
.getId()];
// Notes: decide the url pattern according to the dispatchers
// set if any. If dispatcher type check failed, a null url
// pattern returned. So do the next while iterate.
urlPattern = decideUrlPattern(req, urlPattern);
while (clonedFilterInstance != null
&& (clonedFilterInstance.isExecuted() || !(!StringUtils
.isEmpty(urlPattern) && fwkContainerHelper
.checkNeedFilter((HttpServletRequest) request,
urlPattern)))) {
if (it.hasNext()) {
clonedFilterInstance = it.next();
filterDelegate = clonedFilterInstance.getOriginalObj();
urlPattern = filterDelegate.getURLPatterns()[clonedFilterInstance
.getId()];
urlPattern = decideUrlPattern(req, urlPattern);
} else {
clonedFilterInstance = null;
}
}
// If any filter matched in the chain, do it.
if (clonedFilterInstance != null) {
try {
// Switch request's current bundle to the filter's
// bundle. And modify the servlet path of the request
// delivering to the filter.
fwkContainerHelper
.switchReqBundleContext(filterDelegate
.getBundleContext().getBundle());
switchReqPath(filterDelegate.getBundleContext());
// set the executed mark early.
clonedFilterInstance.setExecuted();
filterDelegate.doFilter(request, response, this);
} finally {
// revert to the former bundle.
clonedFilterInstance.refresh();
// revertReqPath(); // no need to revert req path at
// last for it is not be used later.