Friday, November 6, 2015

WSO2 Carbon Extension Points - Tomcat Extensions

Valves

A Valve element represents a component that will be inserted into the request processing pipeline for the associated Catalina container (Engine, Host, or Context). Individual Valves have distinct processing capabilities. Simply a valves provide a way of inserting custom functionality within the Tomcat request/response stream.
In WSO2 products users can either extend Tomcat generic org.apache.catalina.valve.ValveBase abstract class or carbon specific org.wso2.carbon.tomcat.ext.valves.CarbonTomcatValve in order to create a custom valve.
You can refer to Tomcat Valve documentation  for further info regarding the Tomcat specific valve implementation https://tomcat.apache.org/tomcat-7.0-doc/config/valve.html

Sample Valve

 public class CarbonSimpleValve extends ValveBase {  
   public CarbonSimpleValve() {  
     //enable async support  
     super(true);  
   }  
   @Override  
   public void invoke(Request request, Response response) throws IOException, ServletException {  
     //valve specific logic    
   }  
 }  

You can refer to the following valves in the carbon kernel to get a better idea


If you extend CarbonTomcatValve and implement your own Valve you need to add that to the Valve pipe using the TomcatValveContainer. You can refer to how the GhostWebappValve and TenantLazyLoaderValve added in the WebappManagementServiceComponent

If you extend the ValveBase then the valve need to be added to the Engine, Host or to the Context.
e.g. Adding a Host level valve in the $PRODUCT_HOME/repository/conf/tomcat/catalina-server.xml
 <Server>  
  <Service>  
    <Engine>  
     <Host>  
       …..  
       <Valve className="org.wso2.carbon.sample.CarbonSimpleValve"/>  
       …..  
     </Host>  
    </Engine>  
  </Service>  
 </Server>  

Filters

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext.

Sample Filter

 public class MyFilter implements Filter {  
  public void init(FilterConfig filterConfig) throws ServletException {  
  }  
  public void destroy() {  
  }  
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)  
   throws IOException, ServletException {  
  servletResponse.setContentType("text/html");  
  //Pre filter logic  
  filterChain.doFilter(servletRequest, servletResponse);  
                //post filter logic  
  }  
 }  

This filter can be defined in the web.xml in order to use this. e,g. in the $Product_Home/repository/conf/tomcat/web.xml for product wide filter or you can use the web.xml in your application.

 <?xml version="1.0" encoding="ISO-8859-1"?>  
 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  <filter>  
  <filter-name>MyFilter</filter-name>  
  <filter-class>MyFilter</filter-class>  
  <init-param>  
   <param-name>my-param</param-name>  
   <param-value>my-param-value</param-value>  
  </init-param>  
  </filter>  
  <filter-mapping>  
  <filter-name>MyFilter</filter-name>  
  <url-pattern>/*</url-pattern>  
  </filter-mapping>  
 </web-app>  

You can refer to following filters in the carbon kernel for more implementation details.

  • org.wso2.carbon.ui.filters.CSRFPreventionFilter
  • org.wso2.carbon.ui.filters.CRLFPreventionFilter
  • org.wso2.carbon.tomcat.ext.filter.CharacterSetFilter

No comments:

Post a Comment