jython中的servlet过滤器

时间:2015-05-08 18:47:10

标签: servlets jython

基于this java example,我在jython(精确代码)中创建了以下servlet过滤器:

from javax.servlet import Filter
from javax.servlet.http import HttpServletRequest

class HttpServletRequestWrapper(HttpServletRequest):
    def init(self, request):
        self.originalURL = self.getRequestURL()
        pathi = self.originalURL.find('/', 10) # find start of path
        qsi = self.originalURL.find('?', pathi) # find start of qs if any
        qs = self.originalURL[qsi:] if qsi > -1 else ''
        self.newURL = self.originalURL[:pathi] + '/ccc/jope.py' + qs

    def getRequestURL(self):
        return self.newURL

class Route2Jope(Filter):
    def init(self, config):
        pass

    def doFilter(self, request, response, chain):
        wrapped = HttpServletRequestWrapper(request)
        chain.doFilter(wrapped, response)

但是,我收到错误消息:

Traceback (most recent call last):
  File "c:\CCC\webapps\ccc\WEB-INF\pyfilter\Route2Jope.py", line 24, in doFilter
    wrapped = HttpServletRequestWrapper(request)
TypeError: org.python.proxies.__main__$HttpServletRequestWrapper$2(): expected 0 args; got 1

    org.python.core.Py.TypeError(Py.java:259)
    org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:209)
    org.python.core.PyReflectedFunction.throwArgCountError(PyReflectedFunction.java:262)
    org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:319)
    org.python.core.PyReflectedConstructor.__call__(PyReflectedConstructor.java:177)
    org.python.core.PyObject.__call__(PyObject.java:419)
    org.python.core.PyMethod.instancemethod___call__(PyMethod.java:237)
    org.python.core.PyMethod.__call__(PyMethod.java:228)
    org.python.core.PyMethod.__call__(PyMethod.java:223)
    org.python.core.Deriveds.dispatch__init__(Deriveds.java:19)
    org.python.core.PyObjectDerived.dispatch__init__(PyObjectDerived.java:1112)
    org.python.core.PyType.type___call__(PyType.java:1713)
    org.python.core.PyType.__call__(PyType.java:1696)
    org.python.core.PyObject.__call__(PyObject.java:461)
    org.python.core.PyObject.__call__(PyObject.java:465)
    org.python.pycode._pyx1.doFilter$6(c:\CCC\webapps\ccc\WEB-INF\pyfilter\Route2Jope.py:25)
    org.python.pycode._pyx1.call_function(c:\CCC\webapps\ccc\WEB-INF\pyfilter\Route2Jope.py)
    org.python.core.PyTableCode.call(PyTableCode.java:167)
    org.python.core.PyBaseCode.call(PyBaseCode.java:307)
    org.python.core.PyBaseCode.call(PyBaseCode.java:198)
    org.python.core.PyFunction.__call__(PyFunction.java:482)
    org.python.core.PyMethod.instancemethod___call__(PyMethod.java:237)
    org.python.core.PyMethod.__call__(PyMethod.java:228)
    org.python.core.PyMethod.__call__(PyMethod.java:218)
    org.python.core.PyMethod.__call__(PyMethod.java:213)
    org.python.core.PyObject._jcallexc(PyObject.java:3626)
    org.python.proxies.__main__$Route2Jope$3.doFilter(Unknown Source)
    org.python.util.PyFilter.doFilter(PyFilter.java:80)

我认为它告诉我不应该传递参数'request',但这对我没有意义。也许我忽略了python ad java类之间的一些映射问题?建议?

1 个答案:

答案 0 :(得分:0)

构造函数的名称必须为__init__,而不是init。 :)