1 package com.atlassian.plugin.servlet.filter;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.LinkedList;
7 import java.util.List;
8
9 import javax.servlet.Filter;
10 import javax.servlet.FilterChain;
11 import javax.servlet.FilterConfig;
12 import javax.servlet.ServletException;
13 import javax.servlet.ServletRequest;
14 import javax.servlet.ServletResponse;
15
16 public final class FilterTestUtils
17 {
18 public static class FilterAdapter implements Filter
19 {
20 public void init(FilterConfig filterConfig) throws ServletException {}
21 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {}
22 public void destroy() {}
23 }
24
25 public final static class SoundOffFilter extends FilterAdapter
26 {
27 private final List<Integer> filterCallOrder;
28 private final int filterId;
29
30 public SoundOffFilter(List<Integer> filterCallOrder, int filterId)
31 {
32 this.filterCallOrder = filterCallOrder;
33 this.filterId = filterId;
34 }
35
36 @Override
37 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
38 throws IOException, ServletException
39 {
40 filterCallOrder.add(filterId);
41 chain.doFilter(request, response);
42 filterCallOrder.add(filterId);
43 }
44 }
45
46 public static final FilterChain emptyChain = new FilterChain()
47 {
48 public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {}
49 };
50
51 static <T> List<T> newList(T first, T... rest)
52 {
53 List<T> list = new LinkedList<T>();
54 list.add(first);
55 for (T element : rest)
56 {
57 list.add(element);
58 }
59 return list;
60 }
61
62
63
64
65 static FilterChain singletonFilterChain(final Filter filter)
66 {
67 return new FilterChain()
68 {
69 boolean called = false;
70
71 public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException
72 {
73 if (!called)
74 {
75 called = true;
76 filter.doFilter(request, response, this);
77 }
78 }
79 };
80 }
81
82 public static <T> List<T> immutableList(List<T> list)
83 {
84 List<T> copy = new ArrayList<T>(list.size());
85 copy.addAll(list);
86 return Collections.unmodifiableList(copy);
87 }
88 }