Example usage for javax.servlet FilterRegistration.Dynamic setInitParameters

List of usage examples for javax.servlet FilterRegistration.Dynamic setInitParameters

Introduction

In this page you can find the example usage for javax.servlet FilterRegistration.Dynamic setInitParameters.

Prototype

public Set<String> setInitParameters(Map<String, String> initParameters);

Source Link

Document

Sets the given initialization parameters on the Servlet or Filter that is represented by this Registration.

Usage

From source file:com.hortonworks.streamline.webservice.StreamlineApplication.java

@SuppressWarnings("unchecked")
private void addServletFilters(StreamlineConfiguration configuration, Environment environment) {
    List<ServletFilterConfiguration> servletFilterConfigurations = configuration.getServletFilters();
    if (servletFilterConfigurations != null && !servletFilterConfigurations.isEmpty()) {
        for (ServletFilterConfiguration servletFilterConfiguration : servletFilterConfigurations) {
            try {
                FilterRegistration.Dynamic dynamic = environment.servlets().addFilter(
                        servletFilterConfiguration.getClassName(),
                        (Class<? extends Filter>) Class.forName(servletFilterConfiguration.getClassName()));
                dynamic.setInitParameters(servletFilterConfiguration.getParams());
                dynamic.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
                LOG.info("Added servlet filter with configuration {}", servletFilterConfiguration);
            } catch (Exception e) {
                LOG.error("Error occurred while adding servlet filter {}", servletFilterConfiguration);
                throw new RuntimeException(e);
            }/*from w w  w  .  j  a  v a 2s. c  o m*/
        }
    } else {
        LOG.info("No servlet filters configured");
    }
}

From source file:org.ops4j.pax.web.service.tomcat.internal.TomcatServerWrapper.java

@Override
public void addFilter(final FilterModel filterModel) {
    LOG.debug("add filter [{}]", filterModel);

    final Context context = findOrCreateContext(filterModel);
    LifecycleState state = ((HttpServiceContext) context).getState();
    boolean restartContext = false;
    if ((LifecycleState.STARTING.equals(state) || LifecycleState.STARTED.equals(state))
            && !filterModel.getContextModel().isWebBundle()) {
        try {/*from w ww.j  a v  a2s.c  o m*/
            restartContext = true;
            ((HttpServiceContext) context).stop();
        } catch (LifecycleException e) {
            LOG.warn("Can't reset the Lifecycle ... ", e);
        }
    }
    context.addLifecycleListener(new LifecycleListener() {

        @Override
        public void lifecycleEvent(LifecycleEvent event) {
            if (Lifecycle.BEFORE_START_EVENT.equalsIgnoreCase(event.getType())) {
                FilterRegistration.Dynamic filterRegistration = null;
                if (filterModel.getFilter() != null) {
                    filterRegistration = context.getServletContext().addFilter(filterModel.getName(),
                            filterModel.getFilter());

                } else if (filterModel.getFilterClass() != null) {
                    filterRegistration = context.getServletContext().addFilter(filterModel.getName(),
                            filterModel.getFilterClass());
                }

                if (filterRegistration == null) {
                    filterRegistration = (Dynamic) context.getServletContext()
                            .getFilterRegistration(filterModel.getName());
                    if (filterRegistration == null) {
                        LOG.error("Can't register Filter due to unknown reason!");
                    }
                }

                filterRegistration.setAsyncSupported(filterModel.isAsyncSupported());

                if (filterModel.getServletNames() != null && filterModel.getServletNames().length > 0) {
                    filterRegistration.addMappingForServletNames(getDispatcherTypes(filterModel), /*
                                                                                                  * TODO get
                                                                                                  * asynch
                                                                                                  * supported?
                                                                                                  */false,
                            filterModel.getServletNames());
                } else if (filterModel.getUrlPatterns() != null && filterModel.getUrlPatterns().length > 0) {
                    filterRegistration.addMappingForUrlPatterns(getDispatcherTypes(filterModel), /*
                                                                                                 * TODO get
                                                                                                 * asynch
                                                                                                 * supported?
                                                                                                 */false,
                            filterModel.getUrlPatterns());
                } else {
                    throw new AddFilterException(
                            "cannot add filter to the context; at least a not empty list of servlet names or URL patterns in exclusive mode must be provided: "
                                    + filterModel);
                }
                filterRegistration.setInitParameters(filterModel.getInitParams());
            }
        }
    });

    if (restartContext) {
        try {
            ((HttpServiceContext) context).start();
        } catch (LifecycleException e) {
            LOG.warn("Can't reset the Lifecycle ... ", e);
        }
    }

}