com.thoughtworks.webstub.server.utils.JettyServletRemover 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.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[]{}));
}
}