use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.
the class ProxyServletTest method testResponseHeadersAreNotRemoved.
@Test
public void testResponseHeadersAreNotRemoved() throws Exception {
startServer(new EmptyHttpServlet());
startProxy();
proxyContext.stop();
final String headerName = "X-Test";
final String headerValue = "test-value";
proxyContext.addFilter(new FilterHolder(new Filter() {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
((HttpServletResponse) response).addHeader(headerName, headerValue);
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}), "/*", EnumSet.of(DispatcherType.REQUEST));
proxyContext.start();
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(headerValue, response.getHeaders().get(headerName));
}
use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.
the class StandardDescriptorProcessor method start.
/**
* {@inheritDoc}
*/
public void start(WebAppContext context, Descriptor descriptor) {
for (FilterHolder h : context.getServletHandler().getFilters()) {
_filterHolderMap.put(h.getName(), h);
_filterHolders.add(h);
}
if (context.getServletHandler().getFilterMappings() != null)
_filterMappings.addAll(Arrays.asList(context.getServletHandler().getFilterMappings()));
for (ServletHolder h : context.getServletHandler().getServlets()) {
_servletHolderMap.put(h.getName(), h);
_servletHolders.add(h);
}
if (context.getServletHandler().getServletMappings() != null)
_servletMappings.addAll(Arrays.asList(context.getServletHandler().getServletMappings()));
}
use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.
the class StandardDescriptorProcessor method visitFilter.
public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser.Node node) {
String name = node.getString("filter-name", false, true);
FilterHolder holder = _filterHolderMap.get(name);
if (holder == null) {
holder = context.getServletHandler().newFilterHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString()));
holder.setName(name);
_filterHolderMap.put(name, holder);
_filterHolders.add(holder);
}
String filter_class = node.getString("filter-class", false, true);
if (filter_class != null) {
((WebDescriptor) descriptor).addClassName(filter_class);
switch(context.getMetaData().getOrigin(name + ".filter.filter-class")) {
case NotSet:
{
//no class set yet
holder.setClassName(filter_class);
context.getMetaData().setOrigin(name + ".filter.filter-class", descriptor);
break;
}
case WebXml:
case WebDefaults:
case WebOverride:
{
//filter class was set in web.xml, only allow other web xml descriptors (override/default) to change it
if (!(descriptor instanceof FragmentDescriptor)) {
holder.setClassName(filter_class);
context.getMetaData().setOrigin(name + ".filter.filter-class", descriptor);
}
break;
}
case WebFragment:
{
//the filter class was set up by a web fragment, all fragments must be the same
if (!holder.getClassName().equals(filter_class))
throw new IllegalStateException("Conflicting filter-class for filter " + name + " in " + descriptor.getResource());
break;
}
default:
// TODO throw ISE?
LOG.warn(new Throwable());
}
}
Iterator<XmlParser.Node> iter = node.iterator("init-param");
while (iter.hasNext()) {
XmlParser.Node paramNode = iter.next();
String pname = paramNode.getString("param-name", false, true);
String pvalue = paramNode.getString("param-value", false, true);
switch(context.getMetaData().getOrigin(name + ".filter.init-param." + pname)) {
case NotSet:
{
//init-param not already set, so set it
holder.setInitParameter(pname, pvalue);
context.getMetaData().setOrigin(name + ".filter.init-param." + pname, descriptor);
break;
}
case WebXml:
case WebDefaults:
case WebOverride:
{
//otherwise just ignore it
if (!(descriptor instanceof FragmentDescriptor)) {
holder.setInitParameter(pname, pvalue);
context.getMetaData().setOrigin(name + ".filter.init-param." + pname, descriptor);
}
break;
}
case WebFragment:
{
//previously set by a web-fragment, make sure that the value matches, otherwise its an error
if (!holder.getInitParameter(pname).equals(pvalue))
throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor.getResource());
break;
}
default:
// TODO throw ISE?
LOG.warn(new Throwable());
}
}
String async = node.getString("async-supported", false, true);
if (async != null)
holder.setAsyncSupported(async.length() == 0 || Boolean.valueOf(async));
if (async != null) {
boolean val = async.length() == 0 || Boolean.valueOf(async);
switch(context.getMetaData().getOrigin(name + ".filter.async-supported")) {
case NotSet:
{
//set it
holder.setAsyncSupported(val);
context.getMetaData().setOrigin(name + ".filter.async-supported", descriptor);
break;
}
case WebXml:
case WebDefaults:
case WebOverride:
{
//async-supported set by previous web xml descriptor, only allow override if we're parsing another web descriptor(web.xml/web-override.xml/web-default.xml)
if (!(descriptor instanceof FragmentDescriptor)) {
holder.setAsyncSupported(val);
context.getMetaData().setOrigin(name + ".filter.async-supported", descriptor);
}
break;
}
case WebFragment:
{
//async-supported set by another fragment, this fragment's value must match
if (holder.isAsyncSupported() != val)
throw new IllegalStateException("Conflicting async-supported=" + async + " for filter " + name + " in " + descriptor.getResource());
break;
}
default:
// TODO throw ISE?
LOG.warn(new Throwable());
}
}
}
use of org.eclipse.jetty.servlet.FilterHolder in project killbill by killbill.
the class HttpServer method createServletContextHandler.
private ServletContextHandler createServletContextHandler(final String resourceBase, final Iterable<EventListener> eventListeners, final Map<FilterHolder, String> filterHolders) {
final ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.NO_SESSIONS);
context.setContextPath("/");
if (resourceBase != null) {
// Required if you want a webapp directory. See ContextHandler#getResource and http://docs.codehaus.org/display/JETTY/Embedding+Jetty
final String webapp = this.getClass().getClassLoader().getResource(resourceBase).toExternalForm();
context.setResourceBase(webapp);
}
// Jersey insists on using java.util.logging (JUL)
final EventListener listener = new JULServletContextListener();
context.addEventListener(listener);
for (final EventListener eventListener : eventListeners) {
context.addEventListener(eventListener);
}
for (final FilterHolder filterHolder : filterHolders.keySet()) {
context.addFilter(filterHolder, filterHolders.get(filterHolder), EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
}
// Make sure Guice filter all requests
final FilterHolder filterHolder = new FilterHolder(GuiceFilter.class);
context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
// Backend servlet for Guice - never used
final ServletHolder sh = new ServletHolder(DefaultServlet.class);
context.addServlet(sh, "/*");
return context;
}
use of org.eclipse.jetty.servlet.FilterHolder in project neo4j by neo4j.
the class Jetty9WebServer method loadStaticContent.
private void loadStaticContent(SessionManager sm, String mountPoint) {
String contentLocation = staticContent.get(mountPoint);
try {
SessionHandler sessionHandler = new SessionHandler(sm);
sessionHandler.setServer(getJetty());
final WebAppContext staticContext = new WebAppContext();
staticContext.setServer(getJetty());
staticContext.setContextPath(mountPoint);
staticContext.setSessionHandler(sessionHandler);
staticContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
URL resourceLoc = getClass().getClassLoader().getResource(contentLocation);
if (resourceLoc != null) {
URL url = resourceLoc.toURI().toURL();
final Resource resource = Resource.newResource(url);
staticContext.setBaseResource(resource);
addFiltersTo(staticContext);
staticContext.addFilter(new FilterHolder(new NoCacheHtmlFilter()), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD));
handlers.addHandler(staticContext);
} else {
log.warn("No static content available for Neo Server at %s, management console may not be available.", jettyAddress);
}
} catch (Exception e) {
log.error("Unknown error loading static content", e);
e.printStackTrace();
throw new RuntimeException(e);
}
}
Aggregations