All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thoughtworks.webstub.server.utils.JettyServletRemover Maven / Gradle / Ivy

The newest version!
package com.thoughtworks.webstub.server.utils;

import com.thoughtworks.webstub.utils.Predicate;
import com.thoughtworks.webstub.utils.PredicatedPartition;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping;

import java.util.Collection;

import static com.thoughtworks.webstub.utils.CollectionUtils.partition;
import static java.util.Arrays.asList;
import static org.apache.commons.lang.ArrayUtils.contains;

public class JettyServletRemover extends JettyHandlerRemover {
    public JettyServletRemover(org.eclipse.jetty.servlet.ServletContextHandler contextHandler) {
        super(contextHandler);
    }

    @Override
    PredicatedPartition partitionMappingsBy(final String pathSpec) {
        ServletMapping[] servletMappings = servletHandler().getServletMappings();
        if (servletMappings == null)
            return PredicatedPartition.empty();

        return partition(asList(servletMappings), new Predicate() {
            @Override
            public boolean satisfies(ServletMapping mapping) {
                return contains(mapping.getPathSpecs(), pathSpec);
            }
        });
    }

    @Override
    PredicatedPartition partitionHoldersBy(final Collection handlerNames) {
        return partition(asList(servletHandler().getServlets()), new Predicate() {
            @Override
            public boolean satisfies(ServletHolder holder) {
                return handlerNames.contains(holder.getName());
            }
        });
    }

    @Override
    String getHandlerName(ServletMapping servletMapping) {
        return servletMapping.getServletName();
    }

    @Override
    void setNewMappings(Collection mappings) {
        servletHandler().setServletMappings(mappings.toArray(new ServletMapping[]{}));
    }

    @Override
    void setNewHolders(Collection holders) {
        servletHandler().setServlets(holders.toArray(new ServletHolder[]{}));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy