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

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

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

import com.thoughtworks.webstub.utils.Mapper;
import com.thoughtworks.webstub.utils.PredicatedPartition;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;

import java.util.Collection;

import static com.thoughtworks.webstub.utils.CollectionUtils.map;

public abstract class JettyHandlerRemover {
    private ServletHandler servletHandler;

    protected JettyHandlerRemover(ServletContextHandler contextHandler) {
        servletHandler = contextHandler.getServletHandler();
    }

    public void remove(String pathSpec) {
        PredicatedPartition mappings = partitionMappingsBy(pathSpec);
        if (mappings.satisfying().isEmpty())
            return;

        Collection handlerNames = handlerNames(mappings.satisfying());
        PredicatedPartition holders = partitionHoldersBy(handlerNames);

        setNewHolders(holders.notSatisfying());
        setNewMappings(mappings.notSatisfying());
    }

    private Collection handlerNames(Collection mappings) {
        return map(mappings, new Mapper() {
            @Override
            public String map(Mapping mapping) {
                return getHandlerName(mapping);
            }
        });
    }

    protected ServletHandler servletHandler() {
        return servletHandler;
    }

    abstract PredicatedPartition partitionMappingsBy(String pathSpec);

    abstract void setNewMappings(Collection mappings);

    abstract PredicatedPartition partitionHoldersBy(Collection handlerNames);

    abstract void setNewHolders(Collection holders);

    abstract String getHandlerName(Mapping mapping);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy