1 23 package org.archive.crawler.datamodel; 24 25 import java.lang.reflect.InvocationTargetException ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Set ; 32 import java.util.logging.Logger ; 33 34 import javax.management.AttributeNotFoundException ; 35 import javax.management.InvalidAttributeValueException ; 36 import javax.management.MBeanException ; 37 import javax.management.ReflectionException ; 38 39 import org.archive.crawler.datamodel.credential.Credential; 40 import org.archive.crawler.datamodel.credential.HtmlFormCredential; 41 import org.archive.crawler.datamodel.credential.Rfc2617Credential; 42 import org.archive.crawler.settings.CrawlerSettings; 43 import org.archive.crawler.settings.MapType; 44 import org.archive.crawler.settings.ModuleType; 45 import org.archive.crawler.settings.SettingsHandler; 46 import org.archive.crawler.settings.Type; 47 48 49 61 public class CredentialStore extends ModuleType { 62 63 private static final long serialVersionUID = -7916979754932063634L; 64 65 private static Logger logger = Logger.getLogger( 66 "org.archive.crawler.datamodel.CredentialStore"); 67 68 public static final String ATTR_NAME = "credential-store"; 69 70 73 public static final String ATTR_CREDENTIALS = "credentials"; 74 75 81 private static final List credentialTypes; 82 static { 84 Class [] tmp = {HtmlFormCredential.class, Rfc2617Credential.class}; 86 credentialTypes = Collections.unmodifiableList(Arrays.asList(tmp)); 87 } 88 89 94 public CredentialStore(String name) 95 { 96 super(name, "Credentials used by heritrix" + 97 " authenticating. See http://crawler.archive.org/proposals/auth/" + 98 " for background."); 99 100 Type t = addElementToDefinition(new MapType(ATTR_CREDENTIALS, 101 "Map of credentials.", Credential.class)); 102 t.setOverrideable(true); 103 t.setExpertSetting(true); 104 } 105 106 109 public static List getCredentialTypes() { 110 return CredentialStore.credentialTypes; 111 } 112 113 118 public static CredentialStore getCredentialStore(SettingsHandler context) { 119 120 CredentialStore cs = null; 121 122 try { 123 cs = (CredentialStore)context.getOrder(). 124 getAttribute(CredentialStore.ATTR_NAME); 125 } catch (AttributeNotFoundException e) { 126 logger.severe("Failed to get credential store: " + e.getMessage()); 127 } catch (MBeanException e) { 128 logger.severe("Failed to get credential store: " + e.getMessage()); 129 } catch (ReflectionException e) { 130 logger.severe("Failed to get credential store: " + e.getMessage()); 131 } 132 133 return cs; 134 } 135 136 142 protected MapType get(Object context) 143 throws AttributeNotFoundException { 144 145 return (MapType)getAttribute(context, ATTR_CREDENTIALS); 146 } 147 148 153 public Iterator iterator(Object context) { 154 155 MapType m = null; 156 try { 157 m = (MapType)getAttribute(context, ATTR_CREDENTIALS); 158 } catch (AttributeNotFoundException e) { 159 logger.severe("Failed get credentials: " + e.getMessage()); 160 } 161 return (m == null)? null: m.iterator(context); 162 } 163 164 174 public Credential get(Object context, String name) 175 throws AttributeNotFoundException , MBeanException , ReflectionException { 176 177 return (Credential)get(context).getAttribute(name); 178 } 179 180 195 public Credential create(CrawlerSettings context, String name, Class type) 196 throws IllegalArgumentException , InvocationTargetException , 197 InvalidAttributeValueException , AttributeNotFoundException { 198 199 Credential result = (Credential)SettingsHandler. 200 instantiateModuleTypeFromClassName(name, type.getName()); 201 get(context).addElement(context, result); 203 return result; 204 } 205 206 215 public void remove(CrawlerSettings context, Credential credential) 216 throws AttributeNotFoundException , IllegalArgumentException { 217 218 remove(context, credential.getName()); 219 } 220 221 230 public void remove(CrawlerSettings context, String name) 231 throws IllegalArgumentException , AttributeNotFoundException { 232 233 get(context).removeElement(context, name); 234 } 235 236 246 public Set subset(CrawlURI context, Class type) { 247 return subset(context, type, null); 248 } 249 250 264 public Set <Credential> subset(CrawlURI context, Class type, String rootUri) { 265 266 Set <Credential> result = null; 267 Iterator i = iterator(context); 268 if (i != null) { 269 while(i.hasNext()) { 270 Credential c = (Credential)i.next(); 271 if (!type.isInstance(c)) { 272 continue; 273 } 274 if (rootUri != null) { 275 String cd = null; 276 try { 277 cd = c.getCredentialDomain(context); 278 } 279 catch (AttributeNotFoundException e) { 280 logger.severe("Failed to get cred domain: " + 281 context + ": " + e.getMessage()); 282 } 283 if (cd == null) { 284 continue; 285 } 286 if (!rootUri.equalsIgnoreCase(cd)) { 287 continue; 288 } 289 } 290 if (result == null) { 291 result = new HashSet <Credential>(); 292 } 293 result.add(c); 294 } 295 } 296 return result; 297 } 298 } 299 | Popular Tags |