Implement an exclude Filter

Source: Internet
Author: User
When it comes to exclusion filters, everyone will be confused and cannot understand the meaning. What is exclude filter defined by myself.
The exclusive filter is relative to the filter defined by the specification. The filter in the Java EE specification is for the web. the URLs listed in XML are filtered, while the exclusive filter is the opposite. the URLs listed in XML are filtered, but the URLs other than these URLs are filtered logically.
As a Java developer who has been developing Java for many years, this is the reason for this motivation. The following describes the principle of the exclude filter, which is actually very simple. When intercepting all requests, we check whether the URLs of these requests are in the URL list. If so, we will not implement the filtering logic and directly call the chain. dofilter (XXX); otherwise, we will execute some filtering logic operations and then chain. dofilter (XXX ).
There are two methods to check the URL: exact match (equals) and fuzzy match (contains). Exact match takes precedence over fuzzy match. the URI in the Code may be changed according to the actual situation. In the code, the URI only uses the request. getrequesturi () is not a complete URL, which can be adjusted according to the actual situation.
The abstractexcludefilter class sets methods that should not be overwritten to final. The developer should implement a unique abstract method filter () and call the chain in an appropriate place within the method. dofilter (XX ).
See the implementation code:
/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package org. openthoughts. WebTools. filters;

Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. List;
Import javax. servlet. filter;
Import javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse;
Import javax. servlet. http. httpservletrequest;

/**
* An abstract filter provided for developer to filter the URLs that are not in excluded URLs list.
*
* Usage:
* 1. Write subclass of this abstract filter by implementing abstract method 'filter '.
* 2. config web. xml like this:
* <Filter>
* <Filter-Name> yourexcludefilter </filter-Name>
* <Filter-class> org. openthoughts. WebTools. Filters. yourcustomizedexcludefilter </filter-class>
* <Init-param>
* <Param-Name> exactmatchexcludedurls </param-Name>
* <Param-value>/ABC/,/ABC/index. jsp,/ABC/loginservlet </param-value>
* </Init-param>
* <Init-param>
* <Param-Name> approximatematchexcludedurls </param-Name>
* <Param-value> loginservlet, upgradeservlet </param-value>
* </Init-param>
* </Filter>
*
* <Filter-mapping>
* <Filter-Name> yourexcludefilter </filter-Name>
* <URL-pattern>/* </url-pattern>
* </Filter-mapping>
* Note: exactmatchexcludedurls means exactly match URLs that shocould not be filtered,
* I. e. Request. getrequesturi (). Equals (URL );
* Approximatematchexcludedurls means approximately math URLs that shoshould
* Not be filtered, I. e. Request. getrequesturi (). Contains (URL)
* Exactmatch is prior to approximatematch, I. e. If exactmath = true,
* Not check approximatematch
*
* @ Author <a href = "mailto: guangquanzhang@gmail.com"> Gavin. Zhang </a>
*/
Public abstract class implements actexcludefilter implements filter {
Private filterconfig config;
Private list & lt; string & gt; exactmatchexcludedurls = new arraylist & lt; string & gt ;();
Private list & lt; string & gt; approximatematchexcludedurls = new arraylist & lt; string & gt ;();

/**
* Init filter, and load all configuration.
* @ Param config
* @ Throws javax. servlet. servletexception
*/
Final public void Init (filterconfig config) throws servletexception {
This. Config = config;
This. loadconfiguration (config );
}

/**
* Do filter work according to exactmatch or approximatematch,
* This method must not be overrided.
* @ Param request
* @ Param response
* @ Param chain
* @ Throws java. Io. ioexception
* @ Throws javax. servlet. servletexception
*/
Final public void dofilter (servletrequest request, servletresponse response, filterchain chain)
Throws ioexception, servletexception {
Boolean is2befiltered = true;
String requesturi = (httpservletrequest) request). getrequesturi ();
If (! This. exactmatchexcludedurls. isempty ()){
For (string exactmatchexcludedurl: This. exactmatchexcludedurls ){
If (requesturi. Equals (exactmatchexcludedurl )){
Is2befiltered = false;
Break;
}
}
}
If (! This. approximatematchexcludedurls. isempty () & amp; is2befiltered ){
For (string approximatematchexcludedurl: This. approximatematchexcludedurls ){
If (requesturi. indexof (approximatematchexcludedurl )! =-1 ){
Is2befiltered = false;
Break;
}
}
}
If (is2befiltered ){
This. Filter (request, response, chain );
} Else {
Chain. dofilter (request, response );
}
}

/**
* Release all resources.
*/
Final public void destroy (){
This. Config = NULL;
This. exactmatchexcludedurls = NULL;
This. approximatematchexcludedurls = NULL;
}

/**
* Do customized filter work according to exactmatch or approximatematch,
* Developer shocould implement this abstract method.
* @ Param request
* @ Param response
* @ Param chain
*/
Public abstract void filter (servletrequest request, servletresponse response, filterchain chain );

/**
* Load configuration from web. xml.
* @ Param config
*/
Private void loadconfiguration (filterconfig config ){
String exactmatchedurlstring = config. getinitparameter ("exactmatchexcludedurls ");
String approximatematchedurlstring = config. getinitparameter ("approximatematchexcludedurls ");
If (null! = Exactmatchedurlstring ){
String [] TMPS = exactmatchedurlstring. Split (",");
For (string TMP: TMPS ){
TMP = TMP. Trim ();
If (TMP. Length () & gt; 0 ){
This. exactmatchexcludedurls. Add (TMP );
}
}
}
If (null! = Approximatematchedurlstring ){
String [] TMPS = approximatematchedurlstring. Split (",");
For (string TMP: TMPS ){
TMP = TMP. Trim ();
If (TMP. Length () & gt; 0 ){
This. approximatematchexcludedurls. Add (TMP );
}
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.