use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.
the class WebFilterAnnotation method apply.
/**
* @see DiscoveredAnnotation#apply()
*/
public void apply() {
// TODO verify against rules for annotation v descriptor
Class clazz = getTargetClass();
if (clazz == null) {
LOG.warn(_className + " cannot be loaded");
return;
}
//Servlet Spec 8.1.2
if (!Filter.class.isAssignableFrom(clazz)) {
LOG.warn(clazz.getName() + " is not assignable from javax.servlet.Filter");
return;
}
MetaData metaData = _context.getMetaData();
WebFilter filterAnnotation = (WebFilter) clazz.getAnnotation(WebFilter.class);
if (filterAnnotation.value().length > 0 && filterAnnotation.urlPatterns().length > 0) {
LOG.warn(clazz.getName() + " defines both @WebFilter.value and @WebFilter.urlPatterns");
return;
}
String name = (filterAnnotation.filterName().equals("") ? clazz.getName() : filterAnnotation.filterName());
String[] urlPatterns = filterAnnotation.value();
if (urlPatterns.length == 0)
urlPatterns = filterAnnotation.urlPatterns();
FilterHolder holder = _context.getServletHandler().getFilter(name);
if (holder == null) {
//Filter with this name does not already exist, so add it
holder = _context.getServletHandler().newFilterHolder(new Source(Source.Origin.ANNOTATION, clazz.getName()));
holder.setName(name);
holder.setHeldClass(clazz);
metaData.setOrigin(name + ".filter.filter-class", filterAnnotation, clazz);
holder.setDisplayName(filterAnnotation.displayName());
metaData.setOrigin(name + ".filter.display-name", filterAnnotation, clazz);
for (WebInitParam ip : filterAnnotation.initParams()) {
holder.setInitParameter(ip.name(), ip.value());
metaData.setOrigin(name + ".filter.init-param." + ip.name(), ip, clazz);
}
FilterMapping mapping = new FilterMapping();
mapping.setFilterName(holder.getName());
if (urlPatterns.length > 0) {
ArrayList<String> paths = new ArrayList<String>();
for (String s : urlPatterns) {
paths.add(ServletPathSpec.normalize(s));
}
mapping.setPathSpecs(paths.toArray(new String[paths.size()]));
}
if (filterAnnotation.servletNames().length > 0) {
ArrayList<String> names = new ArrayList<String>();
for (String s : filterAnnotation.servletNames()) {
names.add(s);
}
mapping.setServletNames(names.toArray(new String[names.size()]));
}
EnumSet<DispatcherType> dispatcherSet = EnumSet.noneOf(DispatcherType.class);
for (DispatcherType d : filterAnnotation.dispatcherTypes()) {
dispatcherSet.add(d);
}
mapping.setDispatcherTypes(dispatcherSet);
metaData.setOrigin(name + ".filter.mappings", filterAnnotation, clazz);
holder.setAsyncSupported(filterAnnotation.asyncSupported());
metaData.setOrigin(name + ".filter.async-supported", filterAnnotation, clazz);
_context.getServletHandler().addFilter(holder);
_context.getServletHandler().addFilterMapping(mapping);
} else {
//init-params of the same name.
for (WebInitParam ip : filterAnnotation.initParams()) {
//if (holder.getInitParameter(ip.name()) == null)
if (metaData.getOrigin(name + ".filter.init-param." + ip.name()) == Origin.NotSet) {
holder.setInitParameter(ip.name(), ip.value());
metaData.setOrigin(name + ".filter.init-param." + ip.name(), ip, clazz);
}
}
FilterMapping[] mappings = _context.getServletHandler().getFilterMappings();
boolean mappingExists = false;
if (mappings != null) {
for (FilterMapping m : mappings) {
if (m.getFilterName().equals(name)) {
mappingExists = true;
break;
}
}
}
//from the annotation
if (!mappingExists) {
FilterMapping mapping = new FilterMapping();
mapping.setFilterName(holder.getName());
if (urlPatterns.length > 0) {
ArrayList<String> paths = new ArrayList<String>();
for (String s : urlPatterns) {
paths.add(ServletPathSpec.normalize(s));
}
mapping.setPathSpecs(paths.toArray(new String[paths.size()]));
}
if (filterAnnotation.servletNames().length > 0) {
ArrayList<String> names = new ArrayList<String>();
for (String s : filterAnnotation.servletNames()) {
names.add(s);
}
mapping.setServletNames(names.toArray(new String[names.size()]));
}
EnumSet<DispatcherType> dispatcherSet = EnumSet.noneOf(DispatcherType.class);
for (DispatcherType d : filterAnnotation.dispatcherTypes()) {
dispatcherSet.add(d);
}
mapping.setDispatcherTypes(dispatcherSet);
_context.getServletHandler().addFilterMapping(mapping);
metaData.setOrigin(name + ".filter.mappings", filterAnnotation, clazz);
}
}
}
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 jetty.project by eclipse.
the class PutFilterTest method setUp.
@Before
public void setUp() throws Exception {
_dir = File.createTempFile("testPutFilter", null);
assertTrue(_dir.delete());
assertTrue(_dir.mkdir());
_dir.deleteOnExit();
assertTrue(_dir.isDirectory());
tester = new ServletTester("/context");
tester.setResourceBase(_dir.getCanonicalPath());
tester.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/");
FilterHolder holder = tester.addFilter(PutFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
holder.setInitParameter("delAllowed", "true");
// Bloody Windows does not allow file renaming
if (!System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows"))
holder.setInitParameter("putAtomic", "true");
tester.start();
}
Aggregations