com.thoughtworks.webstub.server.utils.JettyHandlerRemover Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of web-stub Show documentation
Show all versions of web-stub Show documentation
Library for stubbing external HTTP dependencies
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);
}