Search in sources :

Example 31 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomee by apache.

the class TomcatRsRegistry method createRsHttpListener.

@Override
public AddressInfo createRsHttpListener(final String appId, final String webContext, final HttpListener listener, final ClassLoader classLoader, final String completePath, final String virtualHost, final String auth, final String realm) {
    String path = webContext;
    if (path == null) {
        throw new NullPointerException("contextRoot is null");
    }
    if (listener == null) {
        throw new NullPointerException("listener is null");
    }
    // find the existing host (we do not auto-create hosts)
    Container host;
    Context context = null;
    if (virtualHost == null) {
        host = hosts.getDefault();
    } else {
        host = hosts.get(virtualHost);
    }
    if (host == null) {
        for (final Host h : hosts) {
            context = findContext(h, appId, webContext);
            if (context != null) {
                host = h;
                if (classLoader != null && classLoader.equals(context.getLoader().getClassLoader())) {
                    break;
                }
            // else try next to find something better
            }
        }
        if (host == null) {
            throw new IllegalArgumentException("Invalid virtual host '" + virtualHost + "'.  Do you have a matching Host entry in the server.xml?");
        }
    } else {
        context = findContext(host, appId, webContext);
    }
    if (context == null) {
        throw new IllegalStateException("Invalid context '" + webContext + "'.  Cannot find context in host " + host.getName());
    }
    final CxfRsHttpListener cxfRsHttpListener = findCxfRsHttpListener(listener);
    final String description = "tomee-jaxrs-" + listener;
    String mapping = completePath;
    if (!completePath.endsWith("/*")) {
        // respect servlet spec (!= from our embedded listeners)
        if (completePath.endsWith("*")) {
            mapping = completePath.substring(0, completePath.length() - 1);
        }
        mapping = mapping + "/*";
    }
    final String urlPattern = removeWebContext(webContext, mapping);
    cxfRsHttpListener.setUrlPattern(urlPattern.substring(0, urlPattern.length() - 1));
    final FilterDef filterDef = new FilterDef();
    filterDef.setAsyncSupported("true");
    filterDef.setDescription(description);
    filterDef.setFilterName(description);
    filterDef.setDisplayName(description);
    filterDef.setFilter(new CXFJAXRSFilter(cxfRsHttpListener, context.findWelcomeFiles()));
    filterDef.setFilterClass(CXFJAXRSFilter.class.getName());
    // just keep base path
    filterDef.addInitParameter("mapping", urlPattern.substring(0, urlPattern.length() - "/*".length()));
    context.addFilterDef(filterDef);
    final FilterMap filterMap = new FilterMap();
    filterMap.addURLPattern(urlPattern);
    for (final DispatcherType type : DispatcherType.values()) {
        filterMap.setDispatcher(type.name());
    }
    filterMap.setFilterName(filterDef.getFilterName());
    context.addFilterMap(filterMap);
    Registrations.addFilterConfig(context, filterDef);
    path = address(connectors, host.getName(), webContext);
    final String key = address(connectors, host.getName(), completePath);
    listeners.put(new Key(appId, key), listener);
    return new AddressInfo(path, key);
}
Also used : Context(org.apache.catalina.Context) CxfRsHttpListener(org.apache.openejb.server.cxf.rs.CxfRsHttpListener) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) Host(org.apache.catalina.Host) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Container(org.apache.catalina.Container) DispatcherType(javax.servlet.DispatcherType)

Example 32 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomee by apache.

the class TomcatWebAppBuilder method beforeStart.

/**
     * {@inheritDoc}
     */
@Override
public void beforeStart(final StandardContext standardContext) {
    if (standardContext.getResources() != null && LazyStopStandardRoot.class.isInstance(standardContext.getResources())) {
        // reset after reload
        Reflections.set(standardContext, "resources", LazyStopStandardRoot.class.cast(standardContext.getResources()).getDelegate());
    }
    final ServletContext sc = standardContext.getServletContext();
    if (sc != null && !SystemInstance.get().getOptions().get(OPENEJB_JSESSION_ID_SUPPORT, true)) {
        final Set<SessionTrackingMode> defaultTrackingModes = sc.getEffectiveSessionTrackingModes();
        if (defaultTrackingModes.contains(SessionTrackingMode.URL)) {
            final Set<SessionTrackingMode> newModes = new HashSet<>();
            newModes.remove(SessionTrackingMode.URL);
            sc.setSessionTrackingModes(newModes);
        }
    }
    initContextLoader(standardContext);
    // used to add custom filters first - our arquillian integration uses it for instance
    // needs to be done now (= before start event) because of addFilterMapBefore() usage
    final String filters = SystemInstance.get().getProperty("org.apache.openejb.servlet.filters");
    if (filters != null) {
        final String[] names = filters.split(",");
        for (final String name : names) {
            final String[] clazzMapping = name.split("=");
            final FilterDef filterDef = new FilterDef();
            filterDef.setFilterClass(clazzMapping[0]);
            filterDef.setFilterName(clazzMapping[0]);
            standardContext.addFilterDef(filterDef);
            final FilterMap filterMap = new FilterMap();
            filterMap.setFilterName(clazzMapping[0]);
            filterMap.addURLPattern(clazzMapping[1]);
            standardContext.addFilterMapBefore(filterMap);
        }
    }
    // mainly to get back compatibility with tomcat <= 8.0
    final String cookieProcessor = SystemInstance.get().getProperty("tomee.tomcat.cookieProcessor");
    if (cookieProcessor != null) {
        // not that important for now if we use the container loader, we mainly want to be able to access
        // the legacy one
        final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            final Class<?> cookieProcessorClass = contextClassLoader.loadClass(cookieProcessor.trim());
            standardContext.setCookieProcessor(CookieProcessor.class.cast(cookieProcessorClass.newInstance()));
        } catch (final Exception e) {
            throw new IllegalArgumentException("Cannot set CookieProcessor: " + cookieProcessor);
        }
    }
}
Also used : FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) SessionTrackingMode(javax.servlet.SessionTrackingMode) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) LifecycleException(org.apache.catalina.LifecycleException) NameNotFoundException(javax.naming.NameNotFoundException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) CookieProcessor(org.apache.tomcat.util.http.CookieProcessor) ServletContext(javax.servlet.ServletContext) HashSet(java.util.HashSet)

Aggregations

FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)32 Test (org.junit.Test)16 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)14 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Context (org.apache.catalina.Context)8 HashMap (java.util.HashMap)7 Tomcat (org.apache.catalina.startup.Tomcat)5 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)4 File (java.io.File)3 Container (org.apache.catalina.Container)3 JavaClassCacheEntry (org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry)3 IOException (java.io.IOException)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Wrapper (org.apache.catalina.Wrapper)2 ErrorPage (org.apache.tomcat.util.descriptor.web.ErrorPage)2 WebXml (org.apache.tomcat.util.descriptor.web.WebXml)2