Search in sources :

Example 1 with FilterMappingInfo

use of io.undertow.servlet.api.FilterMappingInfo in project undertow by undertow-io.

the class FilterRegistrationImpl method getServletNameMappings.

@Override
public Collection<String> getServletNameMappings() {
    DeploymentInfo deploymentInfo = deployment.getDeploymentInfo();
    final List<String> ret = new ArrayList<>();
    for (final FilterMappingInfo mapping : deploymentInfo.getFilterMappings()) {
        if (mapping.getMappingType() == FilterMappingInfo.MappingType.SERVLET) {
            if (mapping.getFilterName().equals(filterInfo.getName())) {
                ret.add(mapping.getMapping());
            }
        }
    }
    return ret;
}
Also used : FilterMappingInfo(io.undertow.servlet.api.FilterMappingInfo) ArrayList(java.util.ArrayList) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo)

Example 2 with FilterMappingInfo

use of io.undertow.servlet.api.FilterMappingInfo in project indy by Commonjava.

the class DeploymentInfoUtils method merge.

public static void merge(final DeploymentInfo into, final DeploymentInfo from) {
    final Map<String, AuthenticationMechanismFactory> authMechs = from.getAuthenticationMechanisms();
    if (authMechs != null) {
        for (final Map.Entry<String, AuthenticationMechanismFactory> entry : authMechs.entrySet()) {
            into.addAuthenticationMechanism(entry.getKey(), entry.getValue());
        }
    }
    if (from.getAuthorizationManager() != null) {
        into.setAuthorizationManager(from.getAuthorizationManager());
    }
    if (from.getConfidentialPortManager() != null) {
        into.setConfidentialPortManager(from.getConfidentialPortManager());
    }
    final List<ErrorPage> errorPages = from.getErrorPages();
    if (errorPages != null) {
        into.addErrorPages(errorPages);
    }
    if (from.getExceptionHandler() != null) {
        into.setExceptionHandler(from.getExceptionHandler());
    }
    final List<FilterMappingInfo> filterMappings = from.getFilterMappings();
    if (filterMappings != null) {
        for (final FilterMappingInfo fmi : filterMappings) {
            switch(fmi.getMappingType()) {
                case SERVLET:
                    {
                        into.addFilterServletNameMapping(fmi.getFilterName(), fmi.getMapping(), fmi.getDispatcher());
                        break;
                    }
                default:
                    {
                        into.addFilterUrlMapping(fmi.getFilterName(), fmi.getMapping(), fmi.getDispatcher());
                    }
            }
        }
    }
    final Map<String, FilterInfo> filterInfos = from.getFilters();
    if (filterInfos != null) {
        into.addFilters(filterInfos.values());
    }
    if (from.getIdentityManager() != null) {
        into.setIdentityManager(from.getIdentityManager());
    }
    final Map<String, String> initParameters = from.getInitParameters();
    if (initParameters != null) {
        for (final Map.Entry<String, String> entry : initParameters.entrySet()) {
            logger.debug("Init-Param: {} = {} from: {}", entry.getKey(), entry.getValue(), from);
            into.addInitParameter(entry.getKey(), entry.getValue());
        }
    }
    final List<LifecycleInterceptor> lifecycleInterceptors = from.getLifecycleInterceptors();
    if (lifecycleInterceptors != null) {
        for (final LifecycleInterceptor lifecycleInterceptor : lifecycleInterceptors) {
            into.addLifecycleInterceptor(lifecycleInterceptor);
        }
    }
    final List<ListenerInfo> listeners = from.getListeners();
    if (listeners != null) {
        into.addListeners(listeners);
    }
    if (from.getMetricsCollector() != null) {
        into.setMetricsCollector(from.getMetricsCollector());
    }
    final List<MimeMapping> mimeMappings = from.getMimeMappings();
    if (mimeMappings != null) {
        into.addMimeMappings(mimeMappings);
    }
    final List<NotificationReceiver> notificationReceivers = from.getNotificationReceivers();
    if (notificationReceivers != null) {
        into.addNotificationReceivers(notificationReceivers);
    }
    final Map<String, Set<String>> principalVersusRolesMap = from.getPrincipalVersusRolesMap();
    if (principalVersusRolesMap != null) {
        for (final Map.Entry<String, Set<String>> entry : principalVersusRolesMap.entrySet()) {
            into.addPrincipalVsRoleMappings(entry.getKey(), entry.getValue());
        }
    }
    final List<SecurityConstraint> securityConstraints = from.getSecurityConstraints();
    if (securityConstraints != null) {
        if (logger.isDebugEnabled()) {
            for (final SecurityConstraint sc : securityConstraints) {
                logger.debug("Security Constraint: {} from: {}", sc, from);
            }
        }
        into.addSecurityConstraints(securityConstraints);
    }
    final LoginConfig loginConfig = from.getLoginConfig();
    if (loginConfig != null) {
        logger.debug("Login Config with realm: {} and mechanism: {} from: {}", loginConfig.getRealmName(), loginConfig.getAuthMethods(), from);
        if (into.getLoginConfig() != null) {
            throw new IllegalStateException("Two or more deployment providers are attempting to provide login configurations! Enable debug logging to see more.");
        }
        into.setLoginConfig(loginConfig);
    }
    if (from.getSecurityContextFactory() != null) {
        into.setSecurityContextFactory(from.getSecurityContextFactory());
    }
    final Set<String> securityRoles = from.getSecurityRoles();
    if (securityRoles != null) {
        into.addSecurityRoles(securityRoles);
    }
    final List<ServletContainerInitializerInfo> servletContainerInitializers = from.getServletContainerInitializers();
    if (servletContainerInitializers != null) {
        into.addServletContainerInitalizers(servletContainerInitializers);
    }
    final Map<String, Object> servletContextAttributes = from.getServletContextAttributes();
    if (servletContextAttributes != null) {
        for (final Map.Entry<String, Object> entry : servletContextAttributes.entrySet()) {
            into.addServletContextAttribute(entry.getKey(), entry.getValue());
        }
    }
    final List<ServletExtension> servletExtensions = from.getServletExtensions();
    if (servletExtensions != null) {
        for (final ServletExtension servletExtension : servletExtensions) {
            into.addServletExtension(servletExtension);
        }
    }
    final Map<String, ServletInfo> servletInfos = from.getServlets();
    if (servletInfos != null) {
        into.addServlets(servletInfos.values());
    }
    final List<SessionListener> sessionListeners = from.getSessionListeners();
    if (sessionListeners != null) {
        for (final SessionListener sessionListener : sessionListeners) {
            into.addSessionListener(sessionListener);
        }
    }
    if (from.getSessionManagerFactory() != null) {
        into.setSessionManagerFactory(from.getSessionManagerFactory());
    }
    if (from.getSessionPersistenceManager() != null) {
        into.setSessionPersistenceManager(from.getSessionPersistenceManager());
    }
    if (from.getTempDir() != null) {
        into.setTempDir(from.getTempDir());
    }
    final List<String> welcomePages = from.getWelcomePages();
    if (welcomePages != null) {
        into.addWelcomePages(welcomePages);
    }
    final List<HandlerWrapper> initWrappers = from.getInitialHandlerChainWrappers();
    if (initWrappers != null) {
        for (final HandlerWrapper wrapper : initWrappers) {
            into.addInitialHandlerChainWrapper(wrapper);
        }
    }
    final List<HandlerWrapper> outerWrappers = from.getOuterHandlerChainWrappers();
    if (outerWrappers != null) {
        for (final HandlerWrapper wrapper : outerWrappers) {
            into.addOuterHandlerChainWrapper(wrapper);
        }
    }
    final List<HandlerWrapper> innerWrappers = from.getInnerHandlerChainWrappers();
    if (innerWrappers != null) {
        for (final HandlerWrapper wrapper : innerWrappers) {
            into.addInnerHandlerChainWrapper(wrapper);
        }
    }
}
Also used : ErrorPage(io.undertow.servlet.api.ErrorPage) FilterMappingInfo(io.undertow.servlet.api.FilterMappingInfo) Set(java.util.Set) HandlerWrapper(io.undertow.server.HandlerWrapper) SecurityConstraint(io.undertow.servlet.api.SecurityConstraint) LifecycleInterceptor(io.undertow.servlet.api.LifecycleInterceptor) ServletInfo(io.undertow.servlet.api.ServletInfo) LoginConfig(io.undertow.servlet.api.LoginConfig) FilterInfo(io.undertow.servlet.api.FilterInfo) MimeMapping(io.undertow.servlet.api.MimeMapping) ServletContainerInitializerInfo(io.undertow.servlet.api.ServletContainerInitializerInfo) ListenerInfo(io.undertow.servlet.api.ListenerInfo) NotificationReceiver(io.undertow.security.api.NotificationReceiver) SessionListener(io.undertow.server.session.SessionListener) AuthenticationMechanismFactory(io.undertow.security.api.AuthenticationMechanismFactory) Map(java.util.Map) ServletExtension(io.undertow.servlet.ServletExtension)

Example 3 with FilterMappingInfo

use of io.undertow.servlet.api.FilterMappingInfo in project undertow by undertow-io.

the class ServletPathMatches method setupServletChains.

/**
     * Sets up the handlers in the servlet chain. We setup a chain for every path + extension match possibility.
     * (i.e. if there a m path mappings and n extension mappings we have n*m chains).
     * <p/>
     * If a chain consists of only the default servlet then we add it as an async handler, so that resources can be
     * served up directly without using blocking operations.
     * <p/>
     * TODO: this logic is a bit convoluted at the moment, we should look at simplifying it
     */
private ServletPathMatchesData setupServletChains() {
    //create the default servlet
    ServletHandler defaultServlet = null;
    final ManagedServlets servlets = deployment.getServlets();
    final ManagedFilters filters = deployment.getFilters();
    final Map<String, ServletHandler> extensionServlets = new HashMap<>();
    final Map<String, ServletHandler> pathServlets = new HashMap<>();
    final Set<String> pathMatches = new HashSet<>();
    final Set<String> extensionMatches = new HashSet<>();
    DeploymentInfo deploymentInfo = deployment.getDeploymentInfo();
    //loop through all filter mappings, and add them to the set of known paths
    for (FilterMappingInfo mapping : deploymentInfo.getFilterMappings()) {
        if (mapping.getMappingType() == FilterMappingInfo.MappingType.URL) {
            String path = mapping.getMapping();
            if (path.equals("*")) {
                //UNDERTOW-95, support this non-standard filter mapping
                path = "/*";
            }
            if (!path.startsWith("*.")) {
                pathMatches.add(path);
            } else {
                extensionMatches.add(path.substring(2));
            }
        }
    }
    //now loop through all servlets.
    for (Map.Entry<String, ServletHandler> entry : servlets.getServletHandlers().entrySet()) {
        final ServletHandler handler = entry.getValue();
        //add the servlet to the approprite path maps
        for (String path : handler.getManagedServlet().getServletInfo().getMappings()) {
            if (path.equals("/")) {
                //the default servlet
                pathMatches.add("/*");
                if (defaultServlet != null) {
                    throw UndertowServletMessages.MESSAGES.twoServletsWithSameMapping(path);
                }
                defaultServlet = handler;
            } else if (!path.startsWith("*.")) {
                //either an exact or a /* based path match
                if (path.isEmpty()) {
                    path = "/";
                }
                pathMatches.add(path);
                if (pathServlets.containsKey(path)) {
                    throw UndertowServletMessages.MESSAGES.twoServletsWithSameMapping(path);
                }
                pathServlets.put(path, handler);
            } else {
                //an extension match based servlet
                String ext = path.substring(2);
                extensionMatches.add(ext);
                if (extensionServlets.containsKey(ext)) {
                    throw UndertowServletMessages.MESSAGES.twoServletsWithSameMapping(path);
                }
                extensionServlets.put(ext, handler);
            }
        }
    }
    ServletHandler managedDefaultServlet = servlets.getServletHandler(DEFAULT_SERVLET_NAME);
    if (managedDefaultServlet == null) {
        //we always create a default servlet, even if it is not going to have any path mappings registered
        managedDefaultServlet = servlets.addServlet(new ServletInfo(DEFAULT_SERVLET_NAME, DefaultServlet.class));
    }
    if (defaultServlet == null) {
        //no explicit default servlet was specified, so we register our mapping
        pathMatches.add("/*");
        defaultServlet = managedDefaultServlet;
    }
    final ServletPathMatchesData.Builder builder = ServletPathMatchesData.builder();
    //these paths contain both /* and exact matches.
    for (final String path : pathMatches) {
        //resolve the target servlet, will return null if this is the default servlet
        MatchData targetServletMatch = resolveServletForPath(path, pathServlets, extensionServlets, defaultServlet);
        final Map<DispatcherType, List<ManagedFilter>> noExtension = new EnumMap<>(DispatcherType.class);
        final Map<String, Map<DispatcherType, List<ManagedFilter>>> extension = new HashMap<>();
        //any filters that match the extension key
        for (String ext : extensionMatches) {
            extension.put(ext, new EnumMap<DispatcherType, List<ManagedFilter>>(DispatcherType.class));
        }
        //loop over all the filters, and add them to the appropriate map in the correct order
        for (final FilterMappingInfo filterMapping : deploymentInfo.getFilterMappings()) {
            ManagedFilter filter = filters.getManagedFilter(filterMapping.getFilterName());
            if (filterMapping.getMappingType() == FilterMappingInfo.MappingType.SERVLET) {
                if (targetServletMatch.handler != null) {
                    if (filterMapping.getMapping().equals(targetServletMatch.handler.getManagedServlet().getServletInfo().getName()) || filterMapping.getMapping().equals("*")) {
                        addToListMap(noExtension, filterMapping.getDispatcher(), filter);
                    }
                }
                for (Map.Entry<String, Map<DispatcherType, List<ManagedFilter>>> entry : extension.entrySet()) {
                    ServletHandler pathServlet = targetServletMatch.handler;
                    boolean defaultServletMatch = targetServletMatch.defaultServlet;
                    if (defaultServletMatch && extensionServlets.containsKey(entry.getKey())) {
                        pathServlet = extensionServlets.get(entry.getKey());
                    }
                    if (filterMapping.getMapping().equals(pathServlet.getManagedServlet().getServletInfo().getName()) || filterMapping.getMapping().equals("*")) {
                        addToListMap(extension.get(entry.getKey()), filterMapping.getDispatcher(), filter);
                    }
                }
            } else {
                if (filterMapping.getMapping().isEmpty() || !filterMapping.getMapping().startsWith("*.")) {
                    if (isFilterApplicable(path, filterMapping.getMapping())) {
                        addToListMap(noExtension, filterMapping.getDispatcher(), filter);
                        for (Map<DispatcherType, List<ManagedFilter>> l : extension.values()) {
                            addToListMap(l, filterMapping.getDispatcher(), filter);
                        }
                    }
                } else {
                    addToListMap(extension.get(filterMapping.getMapping().substring(2)), filterMapping.getDispatcher(), filter);
                }
            }
        }
        //resolve any matches and add them to the builder
        if (path.endsWith("/*")) {
            String prefix = path.substring(0, path.length() - 2);
            //add the default non-extension match
            builder.addPrefixMatch(prefix, createHandler(deploymentInfo, targetServletMatch.handler, noExtension, targetServletMatch.matchedPath, targetServletMatch.defaultServlet, targetServletMatch.mappingMatch, targetServletMatch.userPath), targetServletMatch.defaultServlet || targetServletMatch.handler.getManagedServlet().getServletInfo().isRequireWelcomeFileMapping());
            //build up the chain for each non-extension match
            for (Map.Entry<String, Map<DispatcherType, List<ManagedFilter>>> entry : extension.entrySet()) {
                ServletHandler pathServlet = targetServletMatch.handler;
                String pathMatch = targetServletMatch.matchedPath;
                boolean defaultServletMatch = targetServletMatch.defaultServlet;
                if (defaultServletMatch && extensionServlets.containsKey(entry.getKey())) {
                    defaultServletMatch = false;
                    pathServlet = extensionServlets.get(entry.getKey());
                }
                HttpHandler handler = pathServlet;
                if (!entry.getValue().isEmpty()) {
                    handler = new FilterHandler(entry.getValue(), deploymentInfo.isAllowNonStandardWrappers(), handler);
                }
                builder.addExtensionMatch(prefix, entry.getKey(), servletChain(handler, pathServlet.getManagedServlet(), pathMatch, deploymentInfo, defaultServletMatch, defaultServletMatch ? MappingMatch.DEFAULT : MappingMatch.EXTENSION, defaultServletMatch ? "/" : "*." + entry.getKey()));
            }
        } else if (path.isEmpty()) {
            //the context root match
            builder.addExactMatch("/", createHandler(deploymentInfo, targetServletMatch.handler, noExtension, targetServletMatch.matchedPath, targetServletMatch.defaultServlet, targetServletMatch.mappingMatch, targetServletMatch.userPath));
        } else {
            //we need to check for an extension match, so paths like /exact.txt will have the correct filter applied
            int lastSegmentIndex = path.lastIndexOf('/');
            String lastSegment;
            if (lastSegmentIndex > 0) {
                lastSegment = path.substring(lastSegmentIndex);
            } else {
                lastSegment = path;
            }
            if (lastSegment.contains(".")) {
                String ext = lastSegment.substring(lastSegment.lastIndexOf('.') + 1);
                if (extension.containsKey(ext)) {
                    Map<DispatcherType, List<ManagedFilter>> extMap = extension.get(ext);
                    builder.addExactMatch(path, createHandler(deploymentInfo, targetServletMatch.handler, extMap, targetServletMatch.matchedPath, targetServletMatch.defaultServlet, targetServletMatch.mappingMatch, targetServletMatch.userPath));
                } else {
                    builder.addExactMatch(path, createHandler(deploymentInfo, targetServletMatch.handler, noExtension, targetServletMatch.matchedPath, targetServletMatch.defaultServlet, targetServletMatch.mappingMatch, targetServletMatch.userPath));
                }
            } else {
                builder.addExactMatch(path, createHandler(deploymentInfo, targetServletMatch.handler, noExtension, targetServletMatch.matchedPath, targetServletMatch.defaultServlet, targetServletMatch.mappingMatch, targetServletMatch.userPath));
            }
        }
    }
    //these are used for name based dispatch
    for (Map.Entry<String, ServletHandler> entry : servlets.getServletHandlers().entrySet()) {
        final Map<DispatcherType, List<ManagedFilter>> filtersByDispatcher = new EnumMap<>(DispatcherType.class);
        for (final FilterMappingInfo filterMapping : deploymentInfo.getFilterMappings()) {
            ManagedFilter filter = filters.getManagedFilter(filterMapping.getFilterName());
            if (filterMapping.getMappingType() == FilterMappingInfo.MappingType.SERVLET) {
                if (filterMapping.getMapping().equals(entry.getKey())) {
                    addToListMap(filtersByDispatcher, filterMapping.getDispatcher(), filter);
                }
            }
        }
        if (filtersByDispatcher.isEmpty()) {
            builder.addNameMatch(entry.getKey(), servletChain(entry.getValue(), entry.getValue().getManagedServlet(), null, deploymentInfo, false, MappingMatch.UNKNOWN, ""));
        } else {
            builder.addNameMatch(entry.getKey(), servletChain(new FilterHandler(filtersByDispatcher, deploymentInfo.isAllowNonStandardWrappers(), entry.getValue()), entry.getValue().getManagedServlet(), null, deploymentInfo, false, MappingMatch.UNKNOWN, ""));
        }
    }
    return builder.build();
}
Also used : ManagedServlets(io.undertow.servlet.core.ManagedServlets) FilterMappingInfo(io.undertow.servlet.api.FilterMappingInfo) HashMap(java.util.HashMap) ServletInfo(io.undertow.servlet.api.ServletInfo) ArrayList(java.util.ArrayList) List(java.util.List) ManagedFilter(io.undertow.servlet.core.ManagedFilter) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) DispatcherType(javax.servlet.DispatcherType) EnumMap(java.util.EnumMap) ManagedFilters(io.undertow.servlet.core.ManagedFilters) HashSet(java.util.HashSet) HttpHandler(io.undertow.server.HttpHandler) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap)

Example 4 with FilterMappingInfo

use of io.undertow.servlet.api.FilterMappingInfo in project undertow by undertow-io.

the class FilterRegistrationImpl method getUrlPatternMappings.

@Override
public Collection<String> getUrlPatternMappings() {
    DeploymentInfo deploymentInfo = deployment.getDeploymentInfo();
    final List<String> ret = new ArrayList<>();
    for (final FilterMappingInfo mapping : deploymentInfo.getFilterMappings()) {
        if (mapping.getMappingType() == FilterMappingInfo.MappingType.URL) {
            if (mapping.getFilterName().equals(filterInfo.getName())) {
                ret.add(mapping.getMapping());
            }
        }
    }
    return ret;
}
Also used : FilterMappingInfo(io.undertow.servlet.api.FilterMappingInfo) ArrayList(java.util.ArrayList) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo)

Aggregations

FilterMappingInfo (io.undertow.servlet.api.FilterMappingInfo)4 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 ArrayList (java.util.ArrayList)3 ServletInfo (io.undertow.servlet.api.ServletInfo)2 Map (java.util.Map)2 AuthenticationMechanismFactory (io.undertow.security.api.AuthenticationMechanismFactory)1 NotificationReceiver (io.undertow.security.api.NotificationReceiver)1 HandlerWrapper (io.undertow.server.HandlerWrapper)1 HttpHandler (io.undertow.server.HttpHandler)1 SessionListener (io.undertow.server.session.SessionListener)1 ServletExtension (io.undertow.servlet.ServletExtension)1 ErrorPage (io.undertow.servlet.api.ErrorPage)1 FilterInfo (io.undertow.servlet.api.FilterInfo)1 LifecycleInterceptor (io.undertow.servlet.api.LifecycleInterceptor)1 ListenerInfo (io.undertow.servlet.api.ListenerInfo)1 LoginConfig (io.undertow.servlet.api.LoginConfig)1 MimeMapping (io.undertow.servlet.api.MimeMapping)1 SecurityConstraint (io.undertow.servlet.api.SecurityConstraint)1 ServletContainerInitializerInfo (io.undertow.servlet.api.ServletContainerInitializerInfo)1 ManagedFilter (io.undertow.servlet.core.ManagedFilter)1