SpiderContextImpl.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:10k
源码类别:

搜索引擎

开发平台:

Java

  1. /**
  2.  * $Id: SpiderContextImpl.java,v 1.32 2003/04/10 16:19:05 vanrogu Exp $
  3.  */
  4. package net.javacoding.jspider.core.impl;
  5. import net.javacoding.jspider.Constants;
  6. import net.javacoding.jspider.spi.Rule;
  7. import net.javacoding.jspider.api.event.site.UserAgentObeyedEvent;
  8. import net.javacoding.jspider.api.model.Cookie;
  9. import net.javacoding.jspider.api.model.Site;
  10. import net.javacoding.jspider.core.Agent;
  11. import net.javacoding.jspider.core.SpiderContext;
  12. import net.javacoding.jspider.core.dispatch.EventDispatcher;
  13. import net.javacoding.jspider.core.logging.Log;
  14. import net.javacoding.jspider.core.logging.LogFactory;
  15. import net.javacoding.jspider.core.model.SiteInternal;
  16. import net.javacoding.jspider.core.rule.*;
  17. import net.javacoding.jspider.core.rule.impl.*;
  18. import net.javacoding.jspider.core.storage.Storage;
  19. import net.javacoding.jspider.core.throttle.Throttle;
  20. import net.javacoding.jspider.core.throttle.ThrottleFactory;
  21. import net.javacoding.jspider.core.util.Base64Encoder;
  22. import net.javacoding.jspider.core.util.URLUtil;
  23. import net.javacoding.jspider.core.util.config.*;
  24. import net.javacoding.jspider.core.util.http.CookieUtil;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.net.URL;
  28. import java.net.URLConnection;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. /**
  32.  *
  33.  * $Id: SpiderContextImpl.java,v 1.32 2003/04/10 16:19:05 vanrogu Exp $
  34.  *
  35.  * @author G黱ther Van Roey
  36.  */
  37. public class SpiderContextImpl implements SpiderContext {
  38.     protected Agent agent;
  39.     protected URL baseURL;
  40.     protected EventDispatcher eventDispatcher;
  41.     protected ThrottleFactory throttleFactory;
  42.     protected Map throttles;
  43.     protected Map spiderRules;
  44.     protected Map parserRules;
  45.     protected Map robotsTXTRules;
  46.     protected Storage storage;
  47.     protected CookieUtil cookieUtil;
  48.     protected String authenticationString;
  49.     protected boolean useProxyAuthentication;
  50.     protected boolean useProxy;
  51.     protected Ruleset generalSpiderRules;
  52.     protected Ruleset generalParserRules;
  53.     protected String defaultUserAgent;
  54.     protected Log log;
  55.     public SpiderContextImpl(URL baseURL, EventDispatcher eventDispatcher, ThrottleFactory throttleFactory, Storage storage) {
  56.         this.baseURL = URLUtil.normalize(baseURL);
  57.         this.eventDispatcher = eventDispatcher;
  58.         this.throttleFactory = throttleFactory;
  59.         this.storage = storage;
  60.         this.cookieUtil = new CookieUtil();
  61.         this.throttles = new HashMap();
  62.         this.spiderRules = new HashMap ( );
  63.         this.parserRules = new HashMap ( );
  64.         this.robotsTXTRules = new HashMap ( );
  65.         this.generalSpiderRules = RuleFactory.createGeneralSpiderRules();
  66.         this.generalParserRules = RuleFactory.createGeneralParserRules();
  67.         this.log = LogFactory.getLog(SpiderContext.class);
  68.         PropertySet props = ConfigurationFactory.getConfiguration().getJSpiderConfiguration();
  69.         this.defaultUserAgent = props.getString(ConfigConstants.CONFIG_USERAGENT, Constants.USERAGENT );
  70.         log.info("default user Agent is '" + defaultUserAgent + "'");
  71.         PropertySet proxyProps = new MappedPropertySet ( ConfigConstants.CONFIG_PROXY, props );
  72.         useProxy = proxyProps.getBoolean(ConfigConstants.CONFIG_PROXY_USE, false);
  73.         if (useProxy) {
  74.             String proxyHost = proxyProps.getString(ConfigConstants.CONFIG_PROXY_HOST, "");
  75.             String proxyPort = proxyProps.getString(ConfigConstants.CONFIG_PROXY_PORT, "");
  76.             System.setProperty("http.proxyHost", proxyHost);
  77.             System.setProperty("http.proxyPort", proxyPort);
  78.             log.info ("Using proxy " + proxyHost + ":" + proxyPort );
  79.             useProxyAuthentication = proxyProps.getBoolean(ConfigConstants.CONFIG_PROXY_AUTHENTICATE, false);
  80.             if (useProxyAuthentication) {
  81.                 String proxyUser = proxyProps.getString(ConfigConstants.CONFIG_PROXY_USERNAME, "");
  82.                 String proxyPwd = proxyProps.getString(ConfigConstants.CONFIG_PROXY_PASSWORD, "");
  83.                 String plain = proxyUser + ":" + proxyPwd;
  84.                 authenticationString = "Basic " + Base64Encoder.base64Encode(plain);
  85.                 log.info("Authenticating against proxy, user:" + proxyUser);
  86.             }
  87.         }
  88.     }
  89.     public void setAgent(Agent agent) {
  90.         this.agent = agent;
  91.     }
  92.     public synchronized void setCookies(Site site, Cookie[] cookies) {
  93.         if (cookies != null && cookies.length > 0) {
  94.           storage.getCookieDAO().save(site, cookies);
  95.         }
  96.     }
  97.     public void preHandle(URLConnection connection, Site site) {
  98.         connection.setDefaultUseCaches(false);
  99.         connection.setUseCaches(false);
  100.         connection.setRequestProperty("Cache-Control","max-age=0,no-cache");
  101.         connection.setRequestProperty("Pragma","no-cache");
  102.         if (useProxyAuthentication) {
  103.             connection.setRequestProperty("Proxy-Authorization", authenticationString);
  104.         }
  105.         String cookieString = site.getCookieString();
  106.         boolean useCookies = site.getUseCookies();
  107.         if (useCookies && cookieString != null) {
  108.             connection.setRequestProperty("Cookie", cookieString);
  109.         }
  110.     }
  111.     public void postHandle(URLConnection connection, Site site) {
  112.         setCookies(site, cookieUtil.getCookies(connection));
  113.         storage.getSiteDAO().save(site);
  114.     }
  115.     public Agent getAgent() {
  116.         return agent;
  117.     }
  118.     public URL getBaseURL() {
  119.         return baseURL;
  120.     }
  121.     public EventDispatcher getEventDispatcher() {
  122.         return eventDispatcher;
  123.     }
  124.     public void throttle(Site site) {
  125.         Throttle throttle = null;
  126.         throttle = (Throttle) throttles.get(site.getHost());
  127.         if (throttle == null) {
  128.             throttle = throttleFactory.createThrottle(site);
  129.             throttles.put(site.getHost(), throttle);
  130.         }
  131.         throttle.throttle();
  132.     }
  133.     public Ruleset getGeneralSpiderRules ( ) {
  134.         return generalSpiderRules;
  135.     }
  136.     public Ruleset getGeneralParserRules ( ) {
  137.         return generalParserRules;
  138.     }
  139.     public Ruleset getSiteSpiderRules(Site site) {
  140.         Ruleset ruleSet =(Ruleset)spiderRules.get(site);
  141.         if ( ruleSet == null) {
  142.             return generalSpiderRules;
  143.         } else {
  144.             return ruleSet;
  145.         }
  146.     }
  147.     public Rule getSiteRobotsTXTRule(Site site) {
  148.         Rule rule = (Rule) robotsTXTRules.get(site);
  149.         if ( rule == null ) {
  150.             rule = new RobotsTXTSkippedRule();
  151.         }
  152.         return rule;
  153.     }
  154.     public Ruleset getSiteParserRules(Site site) {
  155.         Ruleset ruleSet =(Ruleset)parserRules.get(site);
  156.         if ( ruleSet == null) {
  157.             return generalParserRules;
  158.         } else {
  159.             return ruleSet;
  160.         }
  161.     }
  162.     public Storage getStorage() {
  163.         return storage;
  164.     }
  165.     public void registerRobotsTXT(Site site, InputStream inputStream) {
  166.         try {
  167.             RobotsTXTRule robotsTxtRule = new RobotsTXTRule(defaultUserAgent, inputStream);
  168.             ((Ruleset)spiderRules.get(site)).addRule(robotsTxtRule);
  169.             robotsTXTRules.put(site, robotsTxtRule);
  170.             eventDispatcher.dispatch(new UserAgentObeyedEvent(site, robotsTxtRule.getObeyedUserAgent()));
  171.         } catch (IOException e) {
  172.             log.error("i/o exception while reading robots.txt", e);
  173.         }
  174.     }
  175.     public void registerRobotsTXTError(Site site) {
  176.         RobotsTXTErrorRule robotsTxtRule = new RobotsTXTErrorRule();
  177.         ((Ruleset)spiderRules.get(site)).addRule(robotsTxtRule);
  178.         robotsTXTRules.put(site, robotsTxtRule);
  179.     }
  180.     public void registerRobotsTXTSkipped(Site site) {
  181.         RobotsTXTSkippedRule robotsTxtRule = new RobotsTXTSkippedRule();
  182.         ((Ruleset)spiderRules.get(site)).addRule(robotsTxtRule);
  183.         robotsTXTRules.put(site, robotsTxtRule);
  184.     }
  185.     public void registerNewSite(Site site) {
  186.         SiteInternal sitei = (SiteInternal) site;
  187.         sitei.setBaseSite(URLUtil.getSiteURL(baseURL).equals(site.getURL()));
  188.         PropertySet siteProps = ConfigurationFactory.getConfiguration().getSiteConfiguration(site);
  189.         sitei.setUseCookies(siteProps.getBoolean(ConfigConstants.SITE_COOKIES_USE, true));
  190.         sitei.setUseProxy (siteProps.getBoolean(ConfigConstants.SITE_PROXY_USE, true));
  191.         sitei.setObeyRobotsTXT (siteProps.getBoolean(ConfigConstants.SITE_ROBOTSTXT_OBEY, true));
  192.         sitei.setFetchRobotsTXT (siteProps.getBoolean(ConfigConstants.SITE_ROBOTSTXT_FETCH, true));
  193.         sitei.setUserAgent(siteProps.getString(ConfigConstants.SITE_USERAGENT, defaultUserAgent));
  194.         sitei.setHandle(siteProps.getBoolean(ConfigConstants.SITE_HANDLE, false));
  195.         if ( sitei.mustHandle () ) {
  196.         log.info("using userAgent '" + sitei.getUserAgent() + "' for site '" + site.getURL() + "'");
  197.         if ((!siteProps.getBoolean(ConfigConstants.SITE_PROXY_USE, true)) && getUseProxy()) {
  198.             log.info("Using no proxy for " + site.getURL());
  199.             String nonProxyHosts = System.getProperty("http.nonProxyHosts");
  200.             if (nonProxyHosts == null) {
  201.                 nonProxyHosts = site.getURL().getHost();
  202.             } else {
  203.                 nonProxyHosts += "|" + site.getURL().getHost();
  204.             }
  205.             System.setProperty("http.nonProxyHosts", nonProxyHosts);
  206.         } else {
  207.             if (getUseProxy()) {
  208.                 log.info("Using proxy for " + site.getURL());
  209.             }
  210.         }
  211.         } else {
  212.             log.info("site " + sitei.getURL() + " must not be handled.");
  213.         }
  214.         spiderRules.put(site, RuleFactory.createSiteSpiderRules(site));
  215.         parserRules.put(site, RuleFactory.createSiteParserRules(site));
  216.     }
  217.     public boolean getUseProxy() {
  218.         return useProxy;
  219.     }
  220.     public String getUserAgent() {
  221.         return defaultUserAgent;
  222.     }
  223. }