-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathPyFilter.java
More file actions
144 lines (129 loc) · 4.64 KB
/
PyFilter.java
File metadata and controls
144 lines (129 loc) · 4.64 KB
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
package org.python.servlet;
import java.io.File;
import java.io.IOException;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.python.core.PyException;
import org.python.util.PythonInterpreter;
/**
* Enables you to write Jython modules that inherit from <code>jakarta.servlet.Filter</code>, and to insert them in your
* servlet container's filter chain, like any Java <code>Filter</code>.
*
* <p>
* Example:
*
* <p>
* <b>/WEB-INF/filters/HeaderFilter.py:</b>
*
* <pre>
* from jakarta.servlet import Filter
*
* # Module must contain a class with the same name as the module
* # which in turn must implement jakarta.servlet.Filter
* class HeaderFilter (Filter):
* def init(self, config):
* self.header = config.getInitParameter('header')
*
* def doFilter(self, request, response, chain):
* response.setHeader(self.header, "Yup")
* chain.doFilter(request, response)
* </pre>
*
* <p>
* <b>web.xml:</b>
* </p>
*
* <pre>
* <!-- Initialize the Jython runtime -->
* <listener>
* <listener-class>org.python.servlet.PyServletInitializer</listener-class>
* <load-on-startup>1</load-on-startup>
* </listener>
*
* <!-- Declare a uniquely-named PyFilter -->
* <filter>
* <filter-name>HeaderFilter</filter-name>
* <filter-class>org.python.servlet.PyFilter</filter-class>
*
* <!-- The special param __filter__ gives the context-relative path to the Jython source file -->
* <init-param>
* <param-name>__filter__</param-name>
* <param-value>/WEB-INF/pyfilter/HeaderFilter.py</param-value>
* </init-param>
*
* <!-- Other params are passed on the the Jython filter -->
* <init-param>
* <param-name>header</param-name>
* <param-value>X-LookMaNoJava</param-value>
* </init-param>
* </filter>
* <filter-mapping>
* <filter-name>HeaderFilter</filter-name>
* <url-pattern>/*</url-pattern>
* </filter-mapping>
* </pre>
*/
public class PyFilter implements Filter {
public static final String FILTER_PATH_PARAM = "__filter__";
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setAttribute("pyfilter", this);
getFilter().doFilter(request, response, chain);
}
public void init(FilterConfig config) throws ServletException {
if (config.getServletContext().getAttribute(PyServlet.INIT_ATTR) == null) {
throw new ServletException("Jython has not been initialized. Add "
+ "org.python.servlet.PyServletInitializer as a listener to your "
+ "web.xml.");
}
this.config = config;
String filterPath = config.getInitParameter(FILTER_PATH_PARAM);
if (filterPath == null) {
throw new ServletException("Missing required param '" + FILTER_PATH_PARAM + "'");
}
source = new File(getRealPath(config.getServletContext(), filterPath));
if (!source.exists()) {
throw new ServletException(source.getAbsolutePath() + " does not exist.");
}
interp = PyServlet.createInterpreter(config.getServletContext());
}
private String getRealPath(ServletContext context, String appPath) {
String realPath = context.getRealPath(appPath);
// This madness seems to be necessary on Windows
return realPath.replaceAll("\\\\", "/");
}
private Filter getFilter() throws ServletException, IOException {
if (cached == null || source.lastModified() > loadedMtime) {
return loadFilter();
}
return cached;
}
private Filter loadFilter() throws ServletException, IOException {
loadedMtime = source.lastModified();
cached = PyServlet.createInstance(interp, source, Filter.class);
try {
cached.init(config);
} catch (PyException e) {
throw new ServletException(e);
}
return cached;
}
public void destroy() {
if (cached != null) {
cached.destroy();
}
if (interp != null) {
interp.cleanup();
}
}
private PythonInterpreter interp;
private FilterConfig config;
private File source;
private Filter cached;
private long loadedMtime;
}