Search in sources :

Example 1 with FilterDef

use of org.apache.catalina.deploy.FilterDef 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;
}
Also used : FilterDef(org.apache.catalina.deploy.FilterDef) HashMap(java.util.HashMap) org.glassfish.web.deployment.runtime(org.glassfish.web.deployment.runtime) FilterMap(org.apache.catalina.deploy.FilterMap) ValueConstraint(com.sun.appserv.web.cache.mapping.ValueConstraint) CacheManager(com.sun.appserv.web.cache.CacheManager) CacheMapping(com.sun.appserv.web.cache.mapping.CacheMapping) DispatcherType(javax.servlet.DispatcherType)

Example 2 with FilterDef

use of org.apache.catalina.deploy.FilterDef in project Payara by payara.

the class ContextFacade method addFilterFacade.

public FilterRegistration.Dynamic addFilterFacade(String filterName, String className) {
    DynamicFilterRegistrationImpl regis = (DynamicFilterRegistrationImpl) filterRegisMap.get(filterName);
    FilterDef filterDef = null;
    if (null == regis) {
        filterDef = new FilterDef();
    } else {
        filterDef = regis.getFilterDefinition();
    }
    filterDef.setFilterName(filterName);
    filterDef.setFilterClassName(className);
    regis = new DynamicFilterRegistrationImpl(filterDef, this);
    filterRegisMap.put(filterDef.getFilterName(), regis);
    filters.put(filterName, className);
    return regis;
}
Also used : FilterDef(org.apache.catalina.deploy.FilterDef)

Example 3 with FilterDef

use of org.apache.catalina.deploy.FilterDef 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();
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.catalina.deploy.FilterDef) File(java.io.File) FilterMap(org.apache.catalina.deploy.FilterMap)

Example 4 with FilterDef

use of org.apache.catalina.deploy.FilterDef in project Payara by payara.

the class StandardContext method addFilter.

/*
     * Registers the given filter instance with this ServletContext
     * under the given <tt>filterName</tt>.
     */
@Override
public FilterRegistration.Dynamic addFilter(String filterName, Filter filter) {
    if (isContextInitializedCalled) {
        String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_CONTEXT_ALREADY_INIT_EXCEPTION), new Object[] { "addFilter", getName() });
        throw new IllegalStateException(msg);
    }
    if (filterName == null || filterName.length() == 0) {
        throw new IllegalArgumentException(rb.getString(LogFacade.NULL_EMPTY_FILTER_NAME_EXCEPTION));
    }
    if (filter == null) {
        throw new IllegalArgumentException(rb.getString(LogFacade.NULL_FILTER_INSTANCE_EXCEPTION));
    }
    /*
         * Make sure the given Filter instance is unique across all deployed
         * contexts
         */
    Container host = getParent();
    if (host != null) {
        for (Container child : host.findChildren()) {
            if (child == this) {
                // Our own context will be checked further down
                continue;
            }
            if (((StandardContext) child).hasFilter(filter)) {
                return null;
            }
        }
    }
    /*
         * Make sure the given Filter name and instance are unique within
         * this context
         */
    synchronized (filterDefs) {
        for (Map.Entry<String, FilterDef> e : filterDefs.entrySet()) {
            if (filterName.equals(e.getKey()) || filter == e.getValue().getFilter()) {
                return null;
            }
        }
        DynamicFilterRegistrationImpl regis = (DynamicFilterRegistrationImpl) filterRegisMap.get(filterName);
        FilterDef filterDef = null;
        if (null == regis) {
            filterDef = new FilterDef();
        } else {
            // Complete preliminary filter registration
            filterDef = regis.getFilterDefinition();
        }
        filterDef.setFilterName(filterName);
        filterDef.setFilter(filter);
        addFilterDef(filterDef, true, (regis == null));
        if (null == regis) {
            regis = (DynamicFilterRegistrationImpl) filterRegisMap.get(filterName);
        }
        return regis;
    }
}
Also used : Container(org.apache.catalina.Container) FilterDef(org.apache.catalina.deploy.FilterDef) FilterMap(org.apache.catalina.deploy.FilterMap) ServletMap(org.apache.catalina.deploy.ServletMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 5 with FilterDef

use of org.apache.catalina.deploy.FilterDef in project Payara by payara.

the class StandardContext method addFilter.

/**
 * Adds the filter with the given name and class type to this servlet
 * context.
 */
@Override
public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass) {
    if (isContextInitializedCalled) {
        String msg = MessageFormat.format(rb.getString(LogFacade.SERVLET_CONTEXT_ALREADY_INIT_EXCEPTION), new Object[] { "addFilter", getName() });
        throw new IllegalStateException(msg);
    }
    if (filterName == null || filterName.length() == 0) {
        throw new IllegalArgumentException(rb.getString(LogFacade.NULL_EMPTY_FILTER_NAME_EXCEPTION));
    }
    synchronized (filterDefs) {
        if (findFilterDef(filterName) != null) {
            return null;
        }
        DynamicFilterRegistrationImpl regis = (DynamicFilterRegistrationImpl) filterRegisMap.get(filterName);
        FilterDef filterDef = null;
        if (null == regis) {
            filterDef = new FilterDef();
        } else {
            // Complete preliminary filter registration
            filterDef = regis.getFilterDefinition();
        }
        filterDef.setFilterName(filterName);
        filterDef.setFilterClass(filterClass);
        addFilterDef(filterDef, true, (regis == null));
        if (null == regis) {
            regis = (DynamicFilterRegistrationImpl) filterRegisMap.get(filterName);
        }
        return regis;
    }
}
Also used : FilterDef(org.apache.catalina.deploy.FilterDef)

Aggregations

FilterDef (org.apache.catalina.deploy.FilterDef)7 FilterMap (org.apache.catalina.deploy.FilterMap)3 HashMap (java.util.HashMap)2 CacheManager (com.sun.appserv.web.cache.CacheManager)1 CacheMapping (com.sun.appserv.web.cache.mapping.CacheMapping)1 ValueConstraint (com.sun.appserv.web.cache.mapping.ValueConstraint)1 File (java.io.File)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 DispatcherType (javax.servlet.DispatcherType)1 Container (org.apache.catalina.Container)1 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)1 ServletMap (org.apache.catalina.deploy.ServletMap)1 Tomcat (org.apache.catalina.startup.Tomcat)1 org.glassfish.web.deployment.runtime (org.glassfish.web.deployment.runtime)1