/ * * CREATED ON 2004-12-2 * WAP application restrictions on request addresses, or other * / pack org.nightkids.filter.wap; import java.io.fileinputStream; import java.io.filenotException ; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Properties; 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; import javax.servlet.http .HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; / ** * @author weidewang * / public class BlockNotMobileAccess implements Filter {private LOG _LOGGER = logfactory.getlog (this.getClass ()); private static final string mode_session_ip = "blocksession"; Private Static FilterConfig filterConfig; Priv; PRIV ate HttpServletRequest request; private HttpServletResponse response; private HttpSession session; private static Properties gatewayProperties = new Properties (); private static List gatewayList = new ArrayList (); public void init (FilterConfig fconfig) throws ServletException {filterConfig = fconfig; initGatewayList (filterConfig) ;} / ** * load list of gateways * * @param fc * / private void initGatewayList (FilterConfig fc) {String properRealPath = fc.getServletContext () getRealPath (fc.getInitParameter ( "gateway-properties"));. try {gatewayProperties .load (New FileInputStream (ProperRealPath)); _logger.debug ("has read the national gateway list configuration file:" ProperPath); ENUMERATION ELEMS =
GatewayProperties.elements (); while (elems.hasmorelements ()) {string Gateway = (String) Elems.nexTelement (); // If there is no Gateway - is a single IP, add it directly to the list, otherwise get Left arrival,-on the right to the end string, turn into a number, then traverse to add int indexof = Gateway.indexof ("-"); if (indexof == -1) {//// If it is a single IP, add it directly to GatewayList.Add (Gateway); _Logger.debug ("Add a gateway:" Gateway);} else {/ / Otherwise, it is an IP group, now only resolve the last IP group / / First get the first in the left. To the start string int indexleftPoint = Gateway.lastIndexof (".", Indexof) 1; // Get - String string string prefixstr = Gateway.substring (0, IndexleFTPoint); // Get the first string on the left. String leftstr = Gateway.Substring (indexleftpoint, indexof) .trim (); // Get the left string string rightstr = Gateway.substring (IndexOf 1) .trim (); // Get the string of the end // on the right // After you get a string, you must traverse the IPTINT = INTEGER.PARSEINT (Leftstr); int RightInt; = INTEGER.PARSEINT (RightStr); for (INT i = Leftint; i <= rightint; i ) {string grgateway = pr EFIXSTR I; GATEWAYLIST.ADD (GRGATEWAY); _Logger.debug ("Add a gateway:" GRGATEWAY);}}} _logger.debug ("Added" GatewayList.size () "gateway.") } Catch (nullpointer.deption e) {_logger.debug ("Read the National Gateway List Profile Error: (No Gateway-Properties)" E);} catch (filenotfoundexcection e) {_logger.debug ("Read the National Gateway List configuration file error: (Can't find the configuration file): " ProperPath " E);} catch (ooException e) {_logger.debug ("Read the Nationwide Gateway List Profile Error: (IO Error):" ProperPath " E);
}} / ** * * / public void doFilter (ServletRequest _servletRequest, ServletResponse _servletResponse, FilterChain filterChain) throws IOException, ServletException {boolean isActive = false; boolean allowAccess = false; request = (HttpServletRequest) _servletRequest; response = (HttpServletResponse) _servletResponse; session = request.getSession (true); if (! null = filterConfig.getInitParameter ( "active")) {if ( "true" .equalsIgnoreCase (filterConfig.getInitParameter ( "active")) || "yes" .equalsIgnoreCase (filterConfig .GetinitParameter ("Active")))) {isactive = true; _logger.debug ("activation" this.getClass (). getname ());}}}}}}}} = DOPROCESS (REQUEST);} else { allowAccess = true;} if {error () (allowAccess!); return;} filterChain.doFilter (_servletRequest, _servletResponse);} / ** * @param req * @return true through, false can not * / private boolean doProcess ( HttpservletRequest Req) {String UserAgent = NULL; Boolean Bre = true; / ** * Settings Debug mode * / String modeName = filterConfig.getInitParameter ( "mode-name"); String modeValue = filterConfig.getInitParameter ( "mode-value"); if (!! ModeName = null && modeValue = null) {if (request.getParameter (modeName) = null && modeValue.equals (request.getParameter (modeName)) || modeValue.equals (session.getAttribute (MODE_SESSION_IP)))! {session.setAttribute (MODE_SESSION_IP, modeValue); return bRe;}} / * * First, judge the browser type * / string [] blockuSefficient = {"mozilla", "apachebench"}; useERAGENT = Req.getHeader ("user-agent"). TOLOWERCASE ();