use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.
the class TestAddCharSetFilter method doTest.
private void doTest(String encoding, String expected, int mode) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Add the Servlet
CharsetServlet servlet = new CharsetServlet(mode);
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMappingDecoded("/", "servlet");
// Add the Filter
FilterDef filterDef = new FilterDef();
filterDef.setFilterClass(AddDefaultCharsetFilter.class.getName());
filterDef.setFilterName("filter");
if (encoding != null) {
filterDef.addInitParameter("encoding", encoding);
}
ctx.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setFilterName("filter");
filterMap.addServletName("servlet");
ctx.addFilterMap(filterMap);
tomcat.start();
Map<String, List<String>> headers = new HashMap<>();
getUrl("http://localhost:" + getPort() + "/", new ByteChunk(), headers);
List<String> ctHeaders = headers.get("Content-Type");
assertEquals(1, ctHeaders.size());
String ct = ctHeaders.get(0);
assertEquals("text/plain;charset=" + expected, ct);
}
use of org.apache.tomcat.util.descriptor.web.FilterDef in project nutzboot by nutzam.
the class TomcatStarter method addFilter.
private void addFilter(WebFilterFace filterFace) {
if (filterFace == null || filterFace.getFilter() == null) {
return;
}
log.debugf("[NutzBoot] add filter name=%s pathSpec=%s", filterFace.getName(), filterFace.getPathSpec());
FilterDef filterDef = new FilterDef();
filterDef.setFilter(filterFace.getFilter());
filterDef.setFilterName(filterFace.getName());
filterDef.setFilterClass(filterFace.getFilter().getClass().getName());
if (filterFace.getInitParameters() != null && !filterFace.getInitParameters().isEmpty()) {
for (Map.Entry<String, String> entry : filterFace.getInitParameters().entrySet()) {
filterDef.addInitParameter(entry.getKey(), entry.getValue());
}
}
this.tomcatContext.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.addURLPatternDecoded(filterFace.getPathSpec());
filterMap.setFilterName(filterFace.getName());
this.tomcatContext.addFilterMap(filterMap);
}
use of org.apache.tomcat.util.descriptor.web.FilterDef in project ofbiz-framework by apache.
the class CatalinaContainer method prepareContext.
private StandardContext prepareContext(Host host, ContainerConfig.Configuration configuration, ComponentConfig.WebappInfo appInfo, Property clusterProp) throws ContainerException {
StandardContext context = new StandardContext();
Tomcat.initWebappDefaults(context);
String location = getWebappRootLocation(appInfo);
boolean contextIsDistributable = isContextDistributable(configuration, location);
context.setParent(host);
context.setDocBase(location);
context.setPath(getWebappMountPoint(appInfo));
context.addLifecycleListener(new ContextConfig());
context.setJ2EEApplication("OFBiz");
context.setJ2EEServer("OFBiz Container");
context.setLoader(new WebappLoader(Thread.currentThread().getContextClassLoader()));
context.setDisplayName(appInfo.name);
context.setDocBase(location);
context.setReloadable(ContainerConfig.getPropertyValue(configuration, "apps-context-reloadable", false));
context.setDistributable(contextIsDistributable);
context.setCrossContext(ContainerConfig.getPropertyValue(configuration, "apps-cross-context", true));
context.setPrivileged(appInfo.privileged);
context.getServletContext().setAttribute("_serverId", appInfo.server);
context.getServletContext().setAttribute("componentName", appInfo.componentConfig.getComponentName());
if (clusterProp != null && contextIsDistributable) {
context.setManager(prepareClusterManager(clusterProp));
}
StandardRoot resources = new StandardRoot(context);
resources.setAllowLinking(true);
context.setResources(resources);
JarScanner jarScanner = context.getJarScanner();
if (jarScanner instanceof StandardJarScanner) {
StandardJarScanner standardJarScanner = (StandardJarScanner) jarScanner;
standardJarScanner.setJarScanFilter(new FilterJars());
standardJarScanner.setScanClassPath(true);
}
Map<String, String> initParameters = appInfo.getInitParameters();
// request dumper filter
if ("true".equals(initParameters.get("enableRequestDump"))) {
FilterDef requestDumperFilterDef = new FilterDef();
requestDumperFilterDef.setFilterClass(RequestDumperFilter.class.getName());
requestDumperFilterDef.setFilterName("RequestDumper");
context.addFilterDef(requestDumperFilterDef);
FilterMap requestDumperFilterMap = new FilterMap();
requestDumperFilterMap.setFilterName("RequestDumper");
requestDumperFilterMap.addURLPattern("*");
context.addFilterMap(requestDumperFilterMap);
}
// set the init parameters
initParameters.entrySet().forEach(entry -> context.addParameter(entry.getKey(), entry.getValue()));
return context;
}
use of org.apache.tomcat.util.descriptor.web.FilterDef in project spring-framework by spring-projects.
the class TomcatWebSocketTestServer method deployConfig.
@Override
public void deployConfig(WebApplicationContext wac, Filter... filters) {
Assert.state(this.port != -1, "setup() was never called.");
this.context = this.tomcatServer.addContext("", System.getProperty("java.io.tmpdir"));
this.context.addApplicationListener(WsContextListener.class.getName());
Tomcat.addServlet(this.context, "dispatcherServlet", new DispatcherServlet(wac)).setAsyncSupported(true);
this.context.addServletMappingDecoded("/", "dispatcherServlet");
for (Filter filter : filters) {
FilterDef filterDef = new FilterDef();
filterDef.setFilterName(filter.getClass().getName());
filterDef.setFilter(filter);
filterDef.setAsyncSupported("true");
this.context.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setFilterName(filter.getClass().getName());
filterMap.addURLPattern("/*");
filterMap.setDispatcher("REQUEST,FORWARD,INCLUDE,ASYNC");
this.context.addFilterMap(filterMap);
}
}
use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomee by apache.
the class OpenEJBContextConfig method webConfig.
@Override
protected void webConfig() {
TomcatHelper.configureJarScanner(context);
// read the real config
super.webConfig();
if (IgnoredStandardContext.class.isInstance(context)) {
// no need of jsf
return;
}
if (AppFinder.findAppContextOrWeb(context.getLoader().getClassLoader(), AppFinder.WebBeansContextTransformer.INSTANCE) != null) {
final FilterDef asyncOwbFilter = new FilterDef();
asyncOwbFilter.setAsyncSupported("true");
asyncOwbFilter.setDescription("OpenEJB CDI Filter - to propagate @RequestScoped in async tasks");
asyncOwbFilter.setDisplayName("OpenEJB CDI");
asyncOwbFilter.setFilterClass(EEFilter.class.getName());
asyncOwbFilter.setFilterName(EEFilter.class.getName());
context.addFilterDef(asyncOwbFilter);
final FilterMap asyncOwbMapping = new FilterMap();
asyncOwbMapping.setFilterName(asyncOwbFilter.getFilterName());
asyncOwbMapping.addURLPattern("/*");
context.addFilterMap(asyncOwbMapping);
}
if ("true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.jsp-development", "false"))) {
for (final Container c : context.findChildren()) {
if (Wrapper.class.isInstance(c)) {
final Wrapper servlet = Wrapper.class.cast(c);
if ("org.apache.jasper.servlet.JspServlet".equals(servlet.getServletClass())) {
servlet.addInitParameter("development", "true");
}
}
}
}
final ClassLoader classLoader = context.getLoader().getClassLoader();
// add myfaces auto-initializer if mojarra is not present
try {
classLoader.loadClass("com.sun.faces.context.SessionMap");
return;
} catch (final Throwable ignored) {
// no-op
}
try {
final Class<?> myfacesInitializer = Class.forName(MYFACES_TOMEEM_CONTAINER_INITIALIZER, true, classLoader);
final ServletContainerInitializer instance = (ServletContainerInitializer) myfacesInitializer.newInstance();
context.addServletContainerInitializer(instance, getJsfClasses(context));
// cleanup listener
context.addApplicationListener(TOMEE_MYFACES_CONTEXT_LISTENER);
} catch (final Exception | NoClassDefFoundError ignored) {
// no-op
}
}
Aggregations