1 23 package org.archive.crawler.datamodel.credential; 24 25 26 import java.util.Iterator ; 27 import java.util.Set ; 28 import java.util.logging.Logger ; 29 30 import javax.management.AttributeNotFoundException ; 31 32 import org.apache.commons.httpclient.HttpClient; 33 import org.apache.commons.httpclient.HttpMethod; 34 import org.apache.commons.httpclient.URIException; 35 import org.apache.commons.httpclient.UsernamePasswordCredentials; 36 import org.apache.commons.httpclient.auth.AuthScope; 37 import org.archive.crawler.datamodel.CrawlURI; 38 import org.archive.crawler.settings.SimpleType; 39 import org.archive.crawler.settings.Type; 40 41 42 43 49 public class Rfc2617Credential extends Credential { 50 51 private static final long serialVersionUID = -1909614285968756188L; 52 53 private static Logger logger = 54 Logger.getLogger(Rfc2617Credential.class.getName()); 55 56 private static final String ATTR_REALM = "realm"; 57 private static final String ATTR_LOGIN = "login"; 58 private static final String ATTR_PASSWORD = "password"; 59 60 61 69 public Rfc2617Credential(String name) { 70 super(name, "Basic/Digest Auth type credential."); 71 72 Type t = addElementToDefinition(new SimpleType(ATTR_REALM, 73 "Basic/Digest Auth realm.", "Realm")); 74 t.setOverrideable(false); 75 t.setExpertSetting(true); 76 77 t = addElementToDefinition(new SimpleType(ATTR_LOGIN, "Login.", 78 "login")); 79 t.setOverrideable(false); 80 t.setExpertSetting(true); 81 82 t = addElementToDefinition(new SimpleType(ATTR_PASSWORD, "Password.", 83 "password")); 84 t.setOverrideable(false); 85 t.setExpertSetting(true); 86 } 87 88 93 public String getRealm(CrawlURI context) 94 throws AttributeNotFoundException { 95 return (String )getAttribute(ATTR_REALM, context); 96 } 97 98 103 public String getLogin(CrawlURI context) 104 throws AttributeNotFoundException { 105 return (String )getAttribute(ATTR_LOGIN, context); 106 } 107 108 113 public String getPassword(CrawlURI context) 114 throws AttributeNotFoundException { 115 return (String )getAttribute(ATTR_PASSWORD, context); 116 } 117 118 public boolean isPrerequisite(CrawlURI curi) { 119 return false; 122 } 123 124 public boolean hasPrerequisite(CrawlURI curi) { 125 return false; 128 } 129 130 public String getPrerequisite(CrawlURI curi) { 131 return null; 134 } 135 136 public String getKey(CrawlURI context) throws AttributeNotFoundException { 137 return getRealm(context); 138 } 139 140 public boolean isEveryTime() { 141 return true; 142 } 143 144 public boolean populate(CrawlURI curi, HttpClient http, HttpMethod method, 145 String payload) { 146 boolean result = false; 147 String authRealm = payload; 148 if (authRealm == null) { 149 logger.severe("No authscheme though creds: " + curi); 150 return result; 151 } 152 153 UsernamePasswordCredentials upc = null; 166 try { 167 upc = new UsernamePasswordCredentials(getLogin(curi), 168 getPassword(curi)); 169 http.getState().setCredentials(new AuthScope(curi.getUURI().getHost(), 170 curi.getUURI().getPort(), authRealm), upc); 171 logger.fine("Credentials for realm " + authRealm + 172 " for CrawlURI " + curi.toString() + " added to request: " + 173 result); 174 result = true; 175 } catch (AttributeNotFoundException e1) { 176 logger.severe("Failed to get login and password for " + 177 curi + " and " + authRealm); 178 } catch (URIException e) { 179 logger.severe("Failed to parse host from " + curi + ": " + 180 e.getMessage()); 181 } 182 183 return result; 184 } 185 186 public boolean isPost(CrawlURI curi) { 187 return false; 190 } 191 192 203 public static Rfc2617Credential getByRealm(Set rfc2617Credentials, 204 String realm, CrawlURI context) { 205 206 Rfc2617Credential result = null; 207 if (rfc2617Credentials == null || rfc2617Credentials.size() <= 0) { 208 return result; 209 } 210 if (rfc2617Credentials != null && rfc2617Credentials.size() > 0) { 211 for (Iterator i = rfc2617Credentials.iterator(); i.hasNext();) { 212 Rfc2617Credential c = (Rfc2617Credential)i.next(); 213 try { 214 if (c.getRealm(context).equals(realm)) { 215 result = c; 216 break; 217 } 218 } catch (AttributeNotFoundException e) { 219 logger.severe("Failed look up by realm " + realm + " " + e); 220 } 221 } 222 } 223 return result; 224 } 225 } 226 | Popular Tags |