use of org.apache.catalina.deploy.FilterMap in project Payara by payara.
the class CacheModule method configureResponseCache.
/**
* configure ias-web response cache
* @param app WebModule containing the cache
* @param bean ias-web app config bean
* @throws Exception
*
* read the configuration and setup the runtime support for caching in a
* application.
*/
public static CacheManager configureResponseCache(WebModule app, SunWebApp bean) throws Exception {
Cache cacheConfig = ((SunWebAppImpl) bean).getCache();
// is cache configured?
if (cacheConfig == null) {
return null;
}
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.CONFIGURE_CACHE, app.getPath());
}
// create the CacheManager object for this app
CacheManager manager = new CacheManager();
String name, value;
value = cacheConfig.getAttributeValue(Cache.ENABLED);
if (value != null) {
boolean enabled = ConfigBeansUtilities.toBoolean(value);
manager.setEnabled(enabled);
}
// set cache element's attributes and properties
value = cacheConfig.getAttributeValue(Cache.MAX_ENTRIES);
if (value != null) {
try {
int maxEntries = Integer.parseInt(value.trim());
manager.setMaxEntries(maxEntries);
} catch (NumberFormatException e) {
// XXX need error message
throw new Exception("invalid max-entries", e);
}
}
value = cacheConfig.getAttributeValue(Cache.TIMEOUT_IN_SECONDS);
if (value != null) {
try {
int defaultTimeout = Integer.parseInt(value.trim());
manager.setDefaultTimeout(defaultTimeout);
} catch (NumberFormatException e) {
// XXX need error message
throw new Exception("invalid timeout", e);
}
}
WebProperty[] props = cacheConfig.getWebProperty();
for (int i = 0; i < props.length; i++) {
name = props[i].getAttributeValue(WebProperty.NAME);
value = props[i].getAttributeValue(WebProperty.VALUE);
manager.addProperty(name, value);
}
// configure the default cache-helper
DefaultHelper defHelperConfig = cacheConfig.getDefaultHelper();
HashMap<String, String> map = new HashMap<String, String>();
if (defHelperConfig != null) {
props = defHelperConfig.getWebProperty();
for (int i = 0; i < props.length; i++) {
name = props[i].getAttributeValue(WebProperty.NAME);
value = props[i].getAttributeValue(WebProperty.VALUE);
map.put(name, value);
}
}
manager.setDefaultHelperProps(map);
// configure custom cache-helper classes
for (int i = 0; i < cacheConfig.sizeCacheHelper(); i++) {
CacheHelper helperConfig = cacheConfig.getCacheHelper(i);
String helperName = helperConfig.getAttributeValue(CacheHelper.NAME);
HashMap<String, String> helperProps = new HashMap<String, String>();
props = helperConfig.getWebProperty();
for (int j = 0; j < props.length; j++) {
name = props[i].getAttributeValue(WebProperty.NAME);
value = props[i].getAttributeValue(WebProperty.VALUE);
helperProps.put(name, value);
}
helperProps.put("class-name", helperConfig.getAttributeValue(CacheHelper.CLASS_NAME));
manager.addCacheHelperDef(helperName, helperProps);
}
// for each cache-mapping, create CacheMapping, setup the filter
for (int i = 0; i < cacheConfig.sizeCacheMapping(); i++) {
org.glassfish.web.deployment.runtime.CacheMapping mapConfig = cacheConfig.getCacheMapping(i);
CacheMapping mapping = new CacheMapping();
configureCacheMapping(mapConfig, mapping, logger);
// use filter's name to refer to setup the filter
String filterName = CACHING_FILTER_CLASSNAME + i;
/**
* all cache-mapings are indexed by the unique filter-name;
* DefaultCacheHelper uses this name to access the mapping.
*/
manager.addCacheMapping(filterName, mapping);
// setup the ias CachingFilter definition with the context
FilterDef filterDef = new FilterDef();
filterDef.setFilterName(filterName);
filterDef.setFilterClassName(CACHING_FILTER_CLASSNAME);
if (mapping.getServletName() != null) {
filterDef.addInitParameter("servletName", mapping.getServletName());
}
if (mapping.getURLPattern() != null) {
filterDef.addInitParameter("URLPattern", mapping.getURLPattern());
}
app.addFilterDef(filterDef);
// setup the mapping for the specified servlet-name or url-pattern
FilterMap filterMap = new FilterMap();
filterMap.setServletName(mapping.getServletName());
filterMap.setURLPattern(mapping.getURLPattern());
String[] dispatchers = mapConfig.getDispatcher();
if (dispatchers != null) {
EnumSet<DispatcherType> dispatcherTypes = null;
for (String dispatcher : dispatchers) {
// calls to FilterMap.setDispatcher are cumulative
if (dispatcherTypes == null) {
dispatcherTypes = EnumSet.of(Enum.valueOf(DispatcherType.class, dispatcher));
} else {
dispatcherTypes.add(Enum.valueOf(DispatcherType.class, dispatcher));
}
}
filterMap.setDispatcherTypes(dispatcherTypes);
}
filterMap.setFilterName(filterName);
app.addFilterMap(filterMap);
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogFacade.CACHING_FILTER_ADDED, new Object[] { mapping.getServletName(), mapping.getURLPattern() });
}
}
manager.setServletContext(app.getServletContext());
return manager;
}
use of org.apache.catalina.deploy.FilterMap in project Payara by payara.
the class ApplicationFilterFactory method createFilterChain.
/**
* Construct and return a FilterChain implementation that will wrap the
* execution of the specified servlet instance. If we should not execute
* a filter chain at all, return <code>null</code>.
*
* @param request The servlet request we are processing
* @param servlet The servlet instance to be wrapped
*/
public ApplicationFilterChain createFilterChain(ServletRequest request, Wrapper wrapper, Servlet servlet) {
// If there is no servlet to execute, return null
if (servlet == null)
return (null);
// Create and initialize a filter chain object
ApplicationFilterChain filterChain = null;
/**
* IASRI 4665318
* if ((securityManager == null) && (request instanceof Request)) {
* Request req = (Request) request;
* filterChain = (ApplicationFilterChain) req.getFilterChain();
* if (filterChain == null) {
* filterChain = new ApplicationFilterChain();
* req.setFilterChain(filterChain);
* }
* } else {
* // Security: Do not recycle
* filterChain = new ApplicationFilterChain();
* }
*
* filterChain.setServlet(servlet);
*
* filterChain.setSupport
* (((StandardWrapper)wrapper).getInstanceSupport());
*/
// Acquire the filter mappings for this Context
StandardContext context = (StandardContext) wrapper.getParent();
List<FilterMap> filterMaps = context.findFilterMaps();
// If there are no filter mappings, we are done
if (filterMaps.isEmpty()) {
return (filterChain);
}
// get the dispatcher type
DispatcherType dispatcher = request.getDispatcherType();
String requestPath = null;
Object attribute = request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR);
if (attribute != null) {
requestPath = attribute.toString();
}
// 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
Iterator<FilterMap> i = filterMaps.iterator();
while (i.hasNext()) {
FilterMap filterMap = i.next();
if (!filterMap.getDispatcherTypes().contains(dispatcher)) {
continue;
}
// START SJSWS 6324431
if (!matchFiltersURL(filterMap, requestPath, context.isCaseSensitiveMapping()))
continue;
// END SJSWS 6324431
ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) context.findFilterConfig(filterMap.getFilterName());
if (filterConfig == null) {
// FIXME - log configuration problem
continue;
}
// Create a filter chain only when there are filters to add
if (filterChain == null)
filterChain = internalCreateFilterChain(request, wrapper, servlet);
// END IASRI 4665318
filterChain.addFilter(filterConfig);
n++;
}
// Add filters that match on servlet name second
i = filterMaps.iterator();
while (i.hasNext()) {
FilterMap filterMap = i.next();
if (!filterMap.getDispatcherTypes().contains(dispatcher)) {
continue;
}
if (!matchFiltersServlet(filterMap, servletName))
continue;
ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) context.findFilterConfig(filterMap.getFilterName());
if (filterConfig == null) {
// FIXME - log configuration problem
continue;
}
// Create a filter chain only when there are filters to add
if (filterChain == null)
filterChain = internalCreateFilterChain(request, wrapper, servlet);
// END IASRI 4665318
filterChain.addFilter(filterConfig);
n++;
}
// Return the completed filter chain
return (filterChain);
}
use of org.apache.catalina.deploy.FilterMap in project Payara by payara.
the class FilterRegistrationImpl method addMappingForUrlPatterns.
@Override
public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) {
if (ctx.isContextInitializedCalled()) {
String msg = MessageFormat.format(rb.getString(LogFacade.FILTER_REGISTRATION_ALREADY_INIT), new Object[] { "url-pattern mapping", filterDef.getFilterName(), ctx.getName() });
throw new IllegalStateException(msg);
}
if ((urlPatterns == null) || (urlPatterns.length == 0)) {
String msg = MessageFormat.format(rb.getString(LogFacade.FILTER_REGISTRATION_MAPPING_URL_PATTERNS_EXCEPTION), new Object[] { filterDef.getFilterName(), ctx.getName() });
throw new IllegalArgumentException(msg);
}
for (String urlPattern : urlPatterns) {
FilterMap fmap = new FilterMap();
fmap.setFilterName(filterDef.getFilterName());
fmap.setURLPattern(urlPattern);
fmap.setDispatcherTypes(dispatcherTypes);
ctx.addFilterMap(fmap, isMatchAfter);
}
}
use of org.apache.catalina.deploy.FilterMap in project hadoop by apache.
the class AuthenticatorTestCase method startTomcat.
protected void startTomcat() throws Exception {
tomcat = new Tomcat();
File base = new File(System.getProperty("java.io.tmpdir"));
org.apache.catalina.Context ctx = tomcat.addContext("/foo", base.getAbsolutePath());
FilterDef fd = new FilterDef();
fd.setFilterClass(TestFilter.class.getName());
fd.setFilterName("TestFilter");
FilterMap fm = new FilterMap();
fm.setFilterName("TestFilter");
fm.addURLPattern("/*");
fm.addServletName("/bar");
ctx.addFilterDef(fd);
ctx.addFilterMap(fm);
tomcat.addServlet(ctx, "/bar", TestServlet.class.getName());
ctx.addServletMapping("/bar", "/bar");
host = "localhost";
port = getLocalPort();
tomcat.setHostname(host);
tomcat.setPort(port);
tomcat.start();
}
use of org.apache.catalina.deploy.FilterMap in project Payara by payara.
the class FilterRegistrationImpl method addMappingForServletNames.
@Override
public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) {
if (ctx.isContextInitializedCalled()) {
String msg = MessageFormat.format(rb.getString(LogFacade.FILTER_REGISTRATION_ALREADY_INIT), new Object[] { "servlet-name mapping", filterDef.getFilterName(), ctx.getName() });
throw new IllegalStateException(msg);
}
if ((servletNames == null) || (servletNames.length == 0)) {
String msg = MessageFormat.format(rb.getString(LogFacade.FILTER_REGISTRATION_MAPPING_SERVLET_NAME_EXCEPTION), new Object[] { filterDef.getFilterName(), ctx.getName() });
throw new IllegalArgumentException(msg);
}
for (String servletName : servletNames) {
FilterMap fmap = new FilterMap();
fmap.setFilterName(filterDef.getFilterName());
fmap.setServletName(servletName);
fmap.setDispatcherTypes(dispatcherTypes);
ctx.addFilterMap(fmap, isMatchAfter);
}
}
Aggregations