1 23 package org.archive.crawler.datamodel.credential; 24 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import java.util.logging.Logger ; 28 29 import javax.management.Attribute ; 30 import javax.management.AttributeNotFoundException ; 31 import javax.management.InvalidAttributeValueException ; 32 33 import org.apache.commons.httpclient.HttpClient; 34 import org.apache.commons.httpclient.HttpMethod; 35 import org.archive.crawler.datamodel.CrawlURI; 36 import org.archive.crawler.framework.CrawlController; 37 import org.archive.crawler.settings.CrawlerSettings; 38 import org.archive.crawler.settings.ModuleType; 39 import org.archive.crawler.settings.SimpleType; 40 import org.archive.crawler.settings.Type; 41 42 43 44 57 public abstract class Credential extends ModuleType { 58 59 private static final Logger logger = 60 Logger.getLogger(Credential.class.getName()); 61 62 private static final String ATTR_CREDENTIAL_DOMAIN = "credential-domain"; 63 64 70 public Credential(String name, String description) { 71 super(name, description); 72 Type t = addElementToDefinition(new SimpleType(ATTR_CREDENTIAL_DOMAIN, 73 "The root domain this credential goes against:" + 74 " E.g. www.archive.org", "")); 75 t.setOverrideable(false); 76 t.setExpertSetting(true); 77 } 78 79 84 public String getCredentialDomain(CrawlURI context) 85 throws AttributeNotFoundException { 86 return (String )getAttribute(ATTR_CREDENTIAL_DOMAIN, context); 87 } 88 89 95 public void setCredentialDomain(CrawlerSettings context, String domain) 96 throws InvalidAttributeValueException , AttributeNotFoundException { 97 setAttribute(context, new Attribute (ATTR_CREDENTIAL_DOMAIN, domain)); 98 } 99 100 109 public void attach(CrawlURI curi) { 110 attach(curi, null); 111 } 112 113 119 public void attach(CrawlURI curi, String payload) { 120 CredentialAvatar ca = null; 121 try { 122 ca = (payload == null )? 123 new CredentialAvatar(this.getClass(), getKey(curi)): 124 new CredentialAvatar(this.getClass(), getKey(curi), payload); 125 curi.addCredentialAvatar(ca); 126 } 127 catch (AttributeNotFoundException e) { 128 logger.severe("Failed attach of " + this + " for " + curi); 129 } 130 } 131 132 138 public boolean detach(CrawlURI curi) { 139 boolean result = false; 140 if (!curi.hasCredentialAvatars()) { 141 logger.severe("This curi " + curi + 142 " has no cred when it should"); 143 } else { 144 Set avatars = curi.getCredentialAvatars(); 145 for (Iterator i = avatars.iterator(); i.hasNext();) { 146 CredentialAvatar ca = (CredentialAvatar)i.next(); 147 try { 148 if (ca.match(getClass(), getKey(curi))) { 149 result = curi.removeCredentialAvatar(ca); 150 } 151 } 152 catch (AttributeNotFoundException e) { 153 logger.severe("Failed detach of " + ca + " from " + curi); 154 } 155 } 156 } 157 return result; 158 } 159 160 166 public boolean detachAll(CrawlURI curi) { 167 boolean result = false; 168 if (!curi.hasCredentialAvatars()) { 169 logger.severe("This curi " + curi + 170 " has no creds when it should."); 171 } else { 172 Set avatars = curi.getCredentialAvatars(); 173 for (Iterator i = avatars.iterator(); i.hasNext();) { 174 CredentialAvatar ca = (CredentialAvatar)i.next(); 175 if (ca.match(getClass())) { 176 result = curi.removeCredentialAvatar(ca); 177 } 178 } 179 } 180 return result; 181 } 182 183 188 public abstract boolean isPrerequisite(CrawlURI curi); 189 190 194 public abstract boolean hasPrerequisite(CrawlURI curi); 195 196 203 public abstract String getPrerequisite(CrawlURI curi); 204 205 210 public abstract String getKey(CrawlURI context) 211 throws AttributeNotFoundException ; 212 213 217 public abstract boolean isEveryTime(); 218 219 226 public abstract boolean populate(CrawlURI curi, HttpClient http, 227 HttpMethod method, String payload); 228 229 235 public abstract boolean isPost(CrawlURI curi); 236 237 243 public boolean rootUriMatch(CrawlController controller, 244 CrawlURI curi) { 245 String cd = null; 246 try { 247 cd = getCredentialDomain(curi); 248 } 249 catch (AttributeNotFoundException e) { 250 logger.severe("Failed to get credential domain " + curi + ": " + 251 e.getMessage()); 252 } 253 254 String serverName = controller.getServerCache().getServerFor(curi). 257 getName(); 258 logger.fine("RootURI: Comparing " + serverName + " " + cd); 259 return cd != null && serverName != null && 260 serverName.equalsIgnoreCase(cd); 261 } 262 } 263 | Popular Tags |