/ * * CREATED ON 2004-12-2 * WAP Application Filter for the request address, or other * / pack org.nightkids.filter.wap;
import java.io.FileInputStream; import java.io.FileNotFoundException; 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 us _Logger = logfactory.getlog (this.getclass ());
Private static final string mode_session_ip = "blocknotemion";
Private static filterconfig filterconfig;
Private httpservletRequest request;
Private httpservletResponse response
PRIVATE HTTPSESSION session;
Private statçerties GatewayProperties = new profmentness ();
Private static list gatewaylist = new arriselist ();
Public void init (filterconfig fconfig) throws servletexception {filterconfig = fconfig; initgatewayList (filterConfig);}
/ * ** loading a 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 ( "National gateway has read the list of profiles:" properRealPath); Enumeration elems = gatewayProperties.elements (); while (elems.hasMoreElements ()) {String gateway = (String (); // If there is no in Gateway -, it is a single IP, add it directly to the list, otherwise get - the left side., - the string of the end to the end, turn to a number, then travers it Add IP to INDEXOF = GATEWAY.INDEXOF ("-"); if is a single IP, directly add GatewayList.Add (Gateway); _logger.debug ("Add a gateway:" gateway); LastIndexof (".", indexof) 1; // Get the string string string string string string prefixstr = Gateway.substring (0, indexleftPoint); // Get the first string on the left String Leftstr = Gateway.substring IndexLEFTPOINT, INDEXOF) .trim (); // Get the string string rightstr = Gateway.Substring (INDEXOF 1) .trim (); // Get the string of the end // After you get the string, It is necessary to traverse the IP to separate IP to add INT leftint = integer.Parseint (Leftstr); int rightint = integer.parseint (Rightstr); for (int i = left; i <= rightint; i ) {string grgateway = PrefixStr I; GatewayList.Add (GRGATEWAY); _Logger.debug ("Add a gateway:" GRGATEWAY);}}} _logger.debug ("Added" GatewayList.Size () "gateway." ); Catch (NullPointersException E) {_logger.debug ("
Read the national gateway list profile error: (no Gateway-property) " E);} catch (filenotfoundexcection e) {_logger.debug (" Read the National Gateway List Profile Error: (Can't find the configuration file): " ProperPath " E);} catch (ioException e) {_logger.debug ("Read the National Gateway List Profile Error: (IO Error):" ProperRPath " 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 ());}}
IF (isactive) {allowaccess = doprocess;} else {allowaccess = true;
IF (! allowaccess) {error (); return;} filterchain.dofilter (_servetRequest, _servletResponse);
/ ** * @param req * @return true through, false can not * / private boolean doProcess (HttpServletRequest req) {String userAgent = null; boolean bRe = true; / ** * Set the 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;}} / * * determined first browser type * / String [ ] blockuseragent = {"mozilla", "apachebench"}; useERAGENT = Req.getHeader ("user-agent"). TOLOWERCASE (); int indexof = useERAGENT.INDEXOF ("/"); if (INDEXOF == -1) {INDEXOF = UserAgent.indexOf ("*"); if (indexof == -1) {indexof = useERAGENT.LENGTH ();}} useERAGENT = useERAGENT.SUBSTRING (0, indexof); for (int i = 0; i Public void destroy () { } private void error () throws IOException {response.setContentType (filterConfig.getInitParameter ( "content-type")); StringBuffer sb = new StringBuffer (); String errorMessage = "Sorry, please use the phone to access."; if (filterConfig.getInitParameter ("ERROR_MESSAGE")! = Null) {ErrorMessage = filterconfig.getinitParameter ("Error_Message");} sb.append (" errorMessage " Dreamnet Home p> card> wml> "); Writer Out = response.GetWriter (); Out.write (SB .toString ()); out.close ();}}