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