1 9 10 package com.opensymphony.module.sitemesh.mapper; 11 12 import com.opensymphony.module.sitemesh.Config; 13 import com.opensymphony.module.sitemesh.Decorator; 14 import com.opensymphony.module.sitemesh.DecoratorMapper; 15 import com.opensymphony.module.sitemesh.Page; 16 import com.opensymphony.module.sitemesh.RequestConstants; 17 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpSession ; 20 import java.util.Properties ; 21 22 34 public final class RobotDecoratorMapper extends AbstractDecoratorMapper { 35 private String decoratorName = null; 36 37 38 private static final String [] botHosts = {"alltheweb.com", "alta-vista.net", "altavista.com", 39 "atext.com", "euroseek.net", "excite.com", 40 "fast-search.net", "google.com", "googlebot.com", 41 "infoseek.co.jp", "infoseek.com", "inktomi.com", 42 "inktomisearch.com", "linuxtoday.com.au", "lycos.com", 43 "lycos.com", "northernlight.com", "pa-x.dec.com"}; 44 45 57 private static final String [] botAgents = { 58 "acme.spider", "ahoythehomepagefinder", "alkaline", "appie", "arachnophilia", 59 "architext", "aretha", "ariadne", "aspider", "atn.txt", "atomz", "auresys", 60 "backrub", "bigbrother", "bjaaland", "blackwidow", "blindekuh", "bloodhound", 61 "brightnet", "bspider", "cactvschemistryspider", "calif", "cassandra", 62 "cgireader", "checkbot", "churl", "cmc", "collective", "combine", "conceptbot", 63 "core", "cshkust", "cusco", "cyberspyder", "deweb", "dienstspider", "diibot", 64 "direct_hit", "dnabot", "download_express", "dragonbot", "dwcp", "ebiness", 65 "eit", "emacs", "emcspider", "esther", "evliyacelebi", "fdse", "felix", 66 "ferret", "fetchrover", "fido", "finnish", "fireball", "fish", "fouineur", 67 "francoroute", "freecrawl", "funnelweb", "gazz", "gcreep", "getbot", "geturl", 68 "golem", "googlebot", "grapnel", "griffon", "gromit", "gulliver", "hambot", 69 "harvest", "havindex", "hometown", "wired-digital", "htdig", "htmlgobble", 70 "hyperdecontextualizer", "ibm", "iconoclast", "ilse", "imagelock", "incywincy", 71 "informant", "infoseek", "infoseeksidewinder", "infospider", "inspectorwww", 72 "intelliagent", "iron33", "israelisearch", "javabee", "jcrawler", "jeeves", 73 "jobot", "joebot", "jubii", "jumpstation", "katipo", "kdd", "kilroy", 74 "ko_yappo_robot", "labelgrabber.txt", "larbin", "legs", "linkscan", 75 "linkwalker", "lockon", "logo_gif", "lycos", "macworm", "magpie", "mediafox", 76 "merzscope", "meshexplorer", "mindcrawler", "moget", "momspider", "monster", 77 "motor", "muscatferret", "mwdsearch", "myweb", "netcarta", "netmechanic", 78 "netscoop", "newscan-online", "nhse", "nomad", "northstar", "nzexplorer", 79 "occam", "octopus", "orb_search", "packrat", "pageboy", "parasite", "patric", 80 "perignator", "perlcrawler", "phantom", "piltdownman", "pioneer", "pitkow", 81 "pjspider", "pka", "plumtreewebaccessor", "poppi", "portalb", "puu", "python", 82 "raven", "rbse", "resumerobot", "rhcs", "roadrunner", "robbie", "robi", 83 "roverbot", "safetynetrobot", "scooter", "search_au", "searchprocess", 84 "senrigan", "sgscout", "shaggy", "shaihulud", "sift", "simbot", "site-valet", 85 "sitegrabber", "sitetech", "slurp", "smartspider", "snooper", "solbot", 86 "spanner", "speedy", "spider_monkey", "spiderbot", "spiderman", "spry", 87 "ssearcher", "suke", "sven", "tach_bw", "tarantula", "tarspider", "tcl", 88 "techbot", "templeton", "titin", "titan", "tkwww", "tlspider", "ucsd", 89 "udmsearch", "urlck", "valkyrie", "victoria", "visionsearch", "voyager", 90 "vwbot", "w3index", "w3m2", "wanderer", "webbandit", "webcatcher", "webcopy", 91 "webfetcher", "webfoot", "weblayers", "weblinker", "webmirror", "webmoose", 92 "webquest", "webreader", "webreaper", "websnarf", "webspider", "webvac", 93 "webwalk", "webwalker", "webwatch", "wget", "whowhere", "wmir", "wolp", 94 "wombat", "worm", "wwwc", "wz101", "xget", "nederland.zoek" 95 }; 96 97 public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException { 98 super.init(config, properties, parent); 99 decoratorName = properties.getProperty("decorator"); 100 } 101 102 public Decorator getDecorator(HttpServletRequest request, Page page) { 103 Decorator result = null; 104 105 if (decoratorName != null && isBot(request)) { 106 result = getNamedDecorator(request, decoratorName); 107 } 108 109 return result == null ? super.getDecorator(request, page) : result; 110 } 111 112 113 private static boolean isBot(HttpServletRequest request) { 114 if (request == null) return false; 115 116 HttpSession session = request.getSession(true); 118 119 if (Boolean.FALSE.equals(session.getAttribute(RequestConstants.ROBOT))) { 120 return false; 121 } 122 else if (Boolean.TRUE.equals(session.getAttribute(RequestConstants.ROBOT))) { 123 return true; 125 } 126 else { 127 if ("robots.txt".indexOf(request.getRequestURI()) != -1) { 128 131 session.setAttribute(RequestConstants.ROBOT, Boolean.TRUE); 134 return true; 135 } 136 else { 137 String userAgent = request.getHeader("User-Agent"); 138 139 if (userAgent != null && userAgent.trim().length() > 2) { 140 if (userAgent.indexOf("MSIE") != -1 || userAgent.indexOf("Gecko") != -1 || userAgent.indexOf("Opera") != -1 || userAgent.indexOf("iCab") != -1 || userAgent.indexOf("Konqueror") != -1 || userAgent.indexOf("KMeleon") != -1 || userAgent.indexOf("4.7") != -1 || userAgent.indexOf("Lynx") != -1) { session.setAttribute(RequestConstants.ROBOT, Boolean.FALSE); 148 return false; 149 } 150 151 for (int i = 0; i < botAgents.length; i++) { 152 if (userAgent.indexOf(botAgents[i]) != -1) { 153 session.setAttribute(RequestConstants.ROBOT, Boolean.TRUE); 156 return true; 157 } 158 } 159 } 160 161 String remoteHost = request.getRemoteHost(); 164 if (remoteHost != null && remoteHost.length() > 0 && remoteHost.charAt(remoteHost.length() - 1) > 64) { 168 for (int i = 0; i < botHosts.length; i++) { 169 if (remoteHost.indexOf(botHosts[i]) != -1) { 170 session.setAttribute(RequestConstants.ROBOT, Boolean.TRUE); 173 return true; 174 } 175 } 176 } 177 178 181 session.setAttribute(RequestConstants.ROBOT, Boolean.FALSE); 183 return false; 184 } 185 } 186 } 187 } | Popular Tags |