-
Notifications
You must be signed in to change notification settings - Fork 2
/
RackFilter.java
169 lines (145 loc) · 4.84 KB
/
RackFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package org.jrack;
import org.jrack.logging.JRackLogger;
import org.jrack.logging.Slf4jLogger;
import org.jrack.utils.ClassUtilities;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
* @since $Revision$ (created: 2012-12-17 7:19 PM)
*/
public class RackFilter implements Filter {
private FilterConfig filterConfig;
private ServletContext servletContext;
private JRack rack;
/**
* set of paths that should be ignored
*/
private final Set<String> ignorePaths = new HashSet<String>();
/**
* Filter initialization
*
* @param filterConfig
* @throws ServletException
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
servletContext = filterConfig.getServletContext();
String rackClass = filterConfig.getInitParameter("rack");
String paths = filterConfig.getInitParameter("ignore");
if (paths != null) {
String[] parts = paths.split(",");
for (String path : parts) {
path = path.trim();
if (path.startsWith("/")) {
path = path.substring(1);
}
ignorePaths.add(path);
}
}
try {
setRack((JRack) ClassUtilities.loadClass(rackClass).newInstance());
} catch (Exception e) {
throw new ServletException("Cannot load: " + rackClass);
}
}
/**
* @param request
* @param response
* @param chain
* @throws IOException
* @throws ServletException
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public final void doFilter(
ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
response.setCharacterEncoding(RackResponse.DEFAULT_ENCODING);
if (shouldIgnorePath(httpRequest)) {
if (chain != null) {
chain.doFilter(request, response);
}
} else {
try {
Context<String> resp = rack.call(rack.getEnvironment(httpRequest));
httpResponse.setStatus((Integer) resp.getObject(Rack.MESSAGE_STATUS));
for (Map.Entry<String, Object> entry : resp) {
if (entry.getKey().startsWith(Rack.HTTP_)) {
httpResponse.setHeader(entry.getKey().substring(Rack.HTTP_.length()),
(String) entry.getValue());
}
}
RackBody body = (RackBody) resp.getObject(Rack.MESSAGE_BODY);
//httpResponse.getWriter().print(rackResponse.getResponse());
httpResponse.getOutputStream().write(body.getBytes(RackResponse.DEFAULT_ENCODING));
} catch (Exception e) {
RackServlet.throwAsError(e);
}
}
}
/**
* Filter destroy event interceptor
*/
public void destroy() {
this.filterConfig = null;
}
/**
* Filter configuration getter
*
* @return FilterConfiguration
*/
public FilterConfig getFilterConfig() {
return filterConfig;
}
public ServletContext getServletContext() {
return servletContext;
}
public JRack getRack() {
return rack;
}
public void setRack(JRack rack) {
this.rack = rack;
}
/**
* Returns a relative path to the filter path and context root from an HttpServletRequest
*/
public String getRelativePath(HttpServletRequest request) {
String path = request.getPathInfo();
if (path == null) {
path = request.getServletPath();
}
if (path.length() > 0) {
path = path.substring(1);
}
return path;
}
private boolean shouldIgnorePath(HttpServletRequest request) {
boolean ignore = false;
if (ignorePaths.size() > 0) {
String relativePath = getRelativePath(request);
if (relativePath != null && relativePath.trim().length() > 0) {
for (String path : ignorePaths) {
if (relativePath.startsWith(path)) {
ignore = true;
break;
}
}
}
}
return ignore;
}
/**
* todo: refactor
*
* @param req the HttpServletRequest
* @return
*/
}