1 package com.atlassian.johnson.filters;
2
3 import com.atlassian.johnson.Johnson;
4 import com.atlassian.johnson.JohnsonEventContainer;
5 import com.atlassian.johnson.config.JohnsonConfig;
6 import com.atlassian.johnson.event.Event;
7 import com.atlassian.johnson.event.RequestEventCheck;
8 import com.atlassian.johnson.setup.SetupConfig;
9 import org.apache.commons.lang.StringUtils;
10
11 import javax.servlet.*;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import java.io.IOException;
15 import java.util.Collection;
16
17
18
19
20 public abstract class AbstractJohnsonFilter implements Filter
21 {
22 protected static final String TEXT_XML_UTF8_CONTENT_TYPE = "text/xml;charset=utf-8";
23
24 protected FilterConfig filterConfig;
25 protected JohnsonConfig config;
26
27
28
29
30 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
31 {
32 String alreadyFilteredKey = getClass().getName() + "_already_filtered";
33 if (servletRequest.getAttribute(alreadyFilteredKey) != null)
34 {
35 filterChain.doFilter(servletRequest, servletResponse);
36 return;
37 }
38 else
39 {
40 servletRequest.setAttribute(alreadyFilteredKey, Boolean.TRUE);
41 }
42
43 HttpServletRequest request = (HttpServletRequest) servletRequest;
44 HttpServletResponse response = (HttpServletResponse) servletResponse;
45
46
47 String servletPath = getServletPath(request);
48
49
50 JohnsonEventContainer appEventContainer = getContainerAndRunEventChecks(request);
51
52 SetupConfig setup = config.getSetupConfig();
53
54
55 boolean ignoreUri = ignoreURI(servletPath);
56 if (appEventContainer.hasEvents() && !ignoreUri)
57 {
58 handleError(appEventContainer, request, response);
59 }
60
61 else if (!ignoreUri && !setup.isSetup() && !setup.isSetupPage(servletPath))
62 {
63 handleNotSetup(request, response);
64 }
65 else
66 {
67 filterChain.doFilter(servletRequest, servletResponse);
68 }
69 }
70
71 public void destroy()
72 {
73 }
74
75 public void init(FilterConfig filterConfig)
76 {
77 this.filterConfig = filterConfig;
78
79 config = Johnson.getConfig();
80 }
81
82 protected JohnsonEventContainer getContainerAndRunEventChecks(HttpServletRequest req)
83 {
84
85 JohnsonEventContainer appEventContainer = Johnson.getEventContainer(filterConfig.getServletContext());
86
87 for (RequestEventCheck requestEventCheck : config.getRequestEventChecks())
88 {
89 requestEventCheck.check(appEventContainer, req);
90 }
91 return appEventContainer;
92 }
93
94
95
96
97
98
99
100
101
102 protected static String getServletPath(HttpServletRequest request)
103 {
104 String servletPath = request.getServletPath();
105 if (StringUtils.isNotEmpty(servletPath))
106 {
107 return servletPath;
108 }
109
110 String requestUri = request.getRequestURI();
111 int startIndex = request.getContextPath().equals("") ? 0 : request.getContextPath().length();
112 int endIndex = request.getPathInfo() == null ? requestUri.length() : requestUri.lastIndexOf(request.getPathInfo());
113 if (startIndex > endIndex)
114 {
115
116 endIndex = startIndex;
117 }
118
119 return requestUri.substring(startIndex, endIndex);
120 }
121
122 protected String getStringForEvents(Collection<Event> events)
123 {
124 StringBuilder message = new StringBuilder();
125 for (Event event : events)
126 {
127 if (message.length() > 0)
128 {
129 message.append("\n");
130 }
131 message.append(event.getDesc());
132 }
133 return message.toString();
134 }
135
136
137
138
139
140
141
142
143
144 protected abstract void handleError(JohnsonEventContainer appEventContainer, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException;
145
146
147
148
149
150
151
152
153 protected abstract void handleNotSetup(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException;
154
155 protected boolean ignoreURI(String uri)
156 {
157 return uri.equalsIgnoreCase(config.getErrorPath()) || config.isIgnoredPath(uri);
158 }
159 }