Search in sources :

Example 1 with PathMappings

use of org.eclipse.jetty.http.pathmap.PathMappings in project jetty.project by eclipse.

the class ServletHandler method updateMappings.

/* ------------------------------------------------------------ */
protected synchronized void updateMappings() {
    // update filter mappings
    if (_filterMappings == null) {
        _filterPathMappings = null;
        _filterNameMappings = null;
    } else {
        _filterPathMappings = new ArrayList<>();
        _filterNameMappings = new MultiMap<>();
        for (FilterMapping filtermapping : _filterMappings) {
            FilterHolder filter_holder = _filterNameMap.get(filtermapping.getFilterName());
            if (filter_holder == null)
                throw new IllegalStateException("No filter named " + filtermapping.getFilterName());
            filtermapping.setFilterHolder(filter_holder);
            if (filtermapping.getPathSpecs() != null)
                _filterPathMappings.add(filtermapping);
            if (filtermapping.getServletNames() != null) {
                String[] names = filtermapping.getServletNames();
                for (String name : names) {
                    if (name != null)
                        _filterNameMappings.add(name, filtermapping);
                }
            }
        }
    }
    // Map servlet paths to holders
    if (_servletMappings == null || _servletNameMap == null) {
        _servletPathMap = null;
    } else {
        PathMappings<ServletHolder> pm = new PathMappings<>();
        Map<String, ServletMapping> servletPathMappings = new HashMap<>();
        //create a map of paths to set of ServletMappings that define that mapping
        HashMap<String, List<ServletMapping>> sms = new HashMap<>();
        for (ServletMapping servletMapping : _servletMappings) {
            String[] pathSpecs = servletMapping.getPathSpecs();
            if (pathSpecs != null) {
                for (String pathSpec : pathSpecs) {
                    List<ServletMapping> mappings = sms.get(pathSpec);
                    if (mappings == null) {
                        mappings = new ArrayList<>();
                        sms.put(pathSpec, mappings);
                    }
                    mappings.add(servletMapping);
                }
            }
        }
        //evaluate path to servlet map based on servlet mappings
        for (String pathSpec : sms.keySet()) {
            //for each path, look at the mappings where it is referenced
            //if a mapping is for a servlet that is not enabled, skip it
            List<ServletMapping> mappings = sms.get(pathSpec);
            ServletMapping finalMapping = null;
            for (ServletMapping mapping : mappings) {
                //Get servlet associated with the mapping and check it is enabled
                ServletHolder servlet_holder = _servletNameMap.get(mapping.getServletName());
                if (servlet_holder == null)
                    throw new IllegalStateException("No such servlet: " + mapping.getServletName());
                //if the servlet related to the mapping is not enabled, skip it from consideration
                if (!servlet_holder.isEnabled())
                    continue;
                //only accept a default mapping if we don't have any other
                if (finalMapping == null)
                    finalMapping = mapping;
                else {
                    //if the candidate is a default, or we're allowing duplicate mappings
                    if (finalMapping.isDefault())
                        finalMapping = mapping;
                    else if (isAllowDuplicateMappings()) {
                        LOG.warn("Multiple servlets map to path {}: {} and {}, choosing {}", pathSpec, finalMapping.getServletName(), mapping.getServletName(), mapping);
                        finalMapping = mapping;
                    } else {
                        //existing candidate isn't a default, if the one we're looking at isn't a default either, then its an error
                        if (!mapping.isDefault()) {
                            ServletHolder finalMappedServlet = _servletNameMap.get(finalMapping.getServletName());
                            throw new IllegalStateException("Multiple servlets map to path " + pathSpec + ": " + finalMappedServlet.getName() + "[mapped:" + finalMapping.getSource() + "]," + mapping.getServletName() + "[mapped:" + mapping.getSource() + "]");
                        }
                    }
                }
            }
            if (finalMapping == null)
                throw new IllegalStateException("No acceptable servlet mappings for " + pathSpec);
            if (LOG.isDebugEnabled())
                LOG.debug("Path={}[{}] mapped to servlet={}[{}]", pathSpec, finalMapping.getSource(), finalMapping.getServletName(), _servletNameMap.get(finalMapping.getServletName()).getSource());
            servletPathMappings.put(pathSpec, finalMapping);
            pm.put(new ServletPathSpec(pathSpec), _servletNameMap.get(finalMapping.getServletName()));
        }
        _servletPathMap = pm;
    }
    // flush filter chain cache
    if (_chainCache != null) {
        for (int i = _chainCache.length; i-- > 0; ) {
            if (_chainCache[i] != null)
                _chainCache[i].clear();
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("filterNameMap=" + _filterNameMap);
        LOG.debug("pathFilters=" + _filterPathMappings);
        LOG.debug("servletFilterMap=" + _filterNameMappings);
        LOG.debug("servletPathMap=" + _servletPathMap);
        LOG.debug("servletNameMap=" + _servletNameMap);
    }
    try {
        if (_contextHandler != null && _contextHandler.isStarted() || _contextHandler == null && isStarted())
            initialize();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ServletPathSpec(org.eclipse.jetty.http.pathmap.ServletPathSpec) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ServletException(javax.servlet.ServletException) MultiException(org.eclipse.jetty.util.MultiException) IOException(java.io.IOException) List(java.util.List) ArrayList(java.util.ArrayList) LazyList(org.eclipse.jetty.util.LazyList) PathMappings(org.eclipse.jetty.http.pathmap.PathMappings)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ServletException (javax.servlet.ServletException)1 PathMappings (org.eclipse.jetty.http.pathmap.PathMappings)1 ServletPathSpec (org.eclipse.jetty.http.pathmap.ServletPathSpec)1 LazyList (org.eclipse.jetty.util.LazyList)1 MultiException (org.eclipse.jetty.util.MultiException)1