01: package net.javacoding.jspider.mod.rule;
02:
03: import net.javacoding.jspider.core.util.config.PropertySet;
04: import net.javacoding.jspider.core.SpiderContext;
05: import net.javacoding.jspider.core.rule.impl.BaseRuleImpl;
06: import net.javacoding.jspider.core.model.DecisionInternal;
07: import net.javacoding.jspider.api.model.Decision;
08: import net.javacoding.jspider.api.model.Site;
09:
10: import java.net.URL;
11:
12: /**
13: * $Id: ForbiddenPathRule.java,v 1.1 2003/04/03 16:10:51 vanrogu Exp $
14: */
15: public class ForbiddenPathRule extends BaseRuleImpl {
16:
17: public static final String PATH = "path";
18:
19: protected String forbiddenPath;
20:
21: public ForbiddenPathRule(PropertySet config) {
22: forbiddenPath = config.getString(PATH, "/");
23: }
24:
25: /**
26: * Applies the rule to a given URL
27: * @param context the spider context we're working in
28: * @param currentSite the site we're spidering
29: * @param url the url of the resource to be tested for spider permission
30: * @return Decision object expressing this rule's decision on the resource
31: */
32: public Decision apply(SpiderContext context, Site currentSite,
33: URL url) {
34: String path = url.getPath();
35: Decision decision = new DecisionInternal();
36:
37: if (matches(url, forbiddenPath)) {
38: decision = new DecisionInternal(Decision.RULE_FORBIDDEN,
39: "access to '" + path + "' forbidden");
40: }
41: return decision;
42: }
43:
44: public boolean matches(URL url, String forbiddenPath) {
45: String path = url.getPath();
46: if (path.length() == 0 && forbiddenPath.equals("/")) {
47: return true;
48: } else {
49: return url.getPath().startsWith(forbiddenPath);
50: }
51: }
52:
53: }
|