1 23 package org.archive.crawler.datamodel.credential; 24 25 import java.io.Serializable ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.logging.Logger ; 29 30 import javax.management.AttributeNotFoundException ; 31 32 import org.archive.crawler.datamodel.CrawlURI; 33 import org.archive.crawler.datamodel.CredentialStore; 34 import org.archive.crawler.settings.SettingsHandler; 35 36 37 59 public class CredentialAvatar 60 implements Serializable { 61 62 private static final long serialVersionUID = 4489542750898404807L; 63 64 private static final Logger logger = 65 Logger.getLogger(CredentialAvatar.class.getName()); 66 67 70 private final String key; 71 72 75 private final Class type; 76 77 88 private final String payload; 89 90 91 96 public CredentialAvatar(Class type, String key) { 97 this(type, key, null); 98 } 99 100 108 public CredentialAvatar(Class type, String key, String payload) { 109 if (!checkType(type)) { 110 throw new IllegalArgumentException ("Type is unrecognized: " + 111 type); 112 } 113 this.key = key; 114 this.type = type; 115 this.payload = payload; 116 } 117 118 121 private CredentialAvatar() { 122 super(); 123 this.key = null; 124 this.type = null; 125 this.payload = null; 126 } 127 128 132 protected boolean checkType(Class candidateType) { 133 boolean result = false; 134 List types = CredentialStore.getCredentialTypes(); 135 for (Iterator i = types.iterator(); i.hasNext();) { 136 if (((Class )i.next()).equals(candidateType)) { 137 result = true; 138 break; 139 } 140 } 141 return result; 142 } 143 144 147 public String getPayload() { 148 return this.payload; 149 } 150 151 154 public String getKey() { 155 return this.key; 156 } 157 158 161 public Class getType() { 162 return this.type; 163 } 164 165 169 public boolean match(Class otherType) { 170 return this.type.equals(otherType); 171 } 172 173 178 public boolean match(Class otherType, String otherKey) { 179 return match(otherType) && 180 (otherKey != null && this.key != null && 181 this.key.equals(otherKey)); 182 } 183 184 public String toString() { 185 return getType() + "." + this.getKey(); 186 } 187 188 193 public Credential getCredential(SettingsHandler handler, CrawlURI curi) { 194 Credential result = null; 195 196 CredentialStore cs = CredentialStore.getCredentialStore(handler); 197 if (cs == null) { 198 logger.severe("No credential store for " + curi); 199 return result; 200 } 201 202 Iterator i = cs.iterator(curi); 203 if (i == null) { 204 logger.severe("Have CredentialAvatar " + toString() + 205 " but no iterator: " + curi); 206 return result; 207 } 208 209 while (i.hasNext()) { 210 Credential c = (Credential)i.next(); 211 if (!this.type.isInstance(c)) { 212 continue; 213 } 214 String credKey = null; 215 try { 216 credKey = c.getKey(curi); 217 } 218 catch (AttributeNotFoundException e) { 219 logger.severe("Failed to get key for " + c + " from " + curi); 220 } 221 if (credKey != null && credKey.equals(getKey())) { 222 result = c; 223 break; 224 } 225 } 226 227 if (result == null) { 228 logger.severe("Have CredentialAvatar " + toString() + 229 " but no corresponding credential: " + curi); 230 } 231 232 return result; 233 } 234 } 235 | Popular Tags |