PyFilter (Jython API documentation)
Date: 2024-08-18

org.python.util

## Class PyFilter

- java.lang.Object
- - org.python.util.PyFilter

- All Implemented Interfaces:
    javax.servlet.Filter

      public class PyFilter
      extends java.lang.Object
      implements javax.servlet.Filter

  Enables you to write Jython modules that inherit from `javax.servlet.Filter`, and to insert them in your servlet container's filter chain, like any Java `Filter`.

  Example:

  /WEB-INF/filters/HeaderFilter.py:

       from javax.servlet import Filter

       # Module must contain a class with the same name as the module
       # which in turn must implement javax.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)

  web.xml:

       <!-- Initialize the Jython runtime -->
         <listener>
             <listener-class>org.python.util.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.util.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>

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static java.lang.String` | `FILTER_PATH_PARAM`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PyFilter()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `destroy()`
    `void` | `doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)`
    `void` | `init(javax.servlet.FilterConfig config)`

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### FILTER_PATH_PARAM

          public static final java.lang.String FILTER_PATH_PARAM

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### PyFilter

          public PyFilter()

  - ### Method Detail

    - #### doFilter

          public void doFilter(javax.servlet.ServletRequest request,
                               javax.servlet.ServletResponse response,
                               javax.servlet.FilterChain chain)
                        throws java.io.IOException,
                               javax.servlet.ServletException

      Specified by:
        `doFilter` in interface `javax.servlet.Filter`
      Throws:
        `java.io.IOException`
        `javax.servlet.ServletException`

    - #### init

          public void init(javax.servlet.FilterConfig config)
                    throws javax.servlet.ServletException

      Specified by:
        `init` in interface `javax.servlet.Filter`
      Throws:
        `javax.servlet.ServletException`

    - #### destroy

          public void destroy()

      Specified by:
        `destroy` in interface `javax.servlet.Filter`

Jython homepage