1 19 20 package com.maverick.http; 21 22 import java.text.MessageFormat ; 23 24 30 public class HttpAuthenticatorFactory { 31 32 public static final String NTLM = "NTLM"; public static final String BASIC = "Basic"; public static final String DIGEST = "Digest"; public static final String NONE = "None"; 37 static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(HttpAuthenticatorFactory.class); 39 40 42 public static HttpAuthenticator createAuthenticator(HttpConnection con, String [] challenges, String authenticationHeader, 43 String authorizationHeader, String pref, String uri) 44 throws UnsupportedAuthenticationException { 45 46 HttpAuthenticator authenticator = null; 47 48 String actualChallenge = ""; 50 if (pref != null) { 51 52 boolean prefAvailable = false; 53 for (int x = 0; pref != null && x < challenges.length; x++) { 54 if (challenges[x].toLowerCase().startsWith(pref.toLowerCase())) { 55 prefAvailable = true; 56 actualChallenge = challenges[x]; 57 break; 58 } 59 } 60 61 if (prefAvailable) { 62 if (pref.equalsIgnoreCase(BASIC)) { 63 authenticator = new BasicAuthentication(uri, con.getHost(), con.getPort(), con.isSecure()); 64 } else if (pref.equalsIgnoreCase(NTLM)) { 65 authenticator = new NTLMAuthentication(uri, con.getHost(), con.getPort(), con.isSecure()); 66 } else if (pref.equalsIgnoreCase(DIGEST)) { 67 authenticator = new DigestAuthentication(uri, con.getHost(), con.getPort(), con.isSecure()); 68 } 69 } 70 } 71 72 if(authenticator==null) { 73 74 for (int i = 0; i < challenges.length; i++) { 76 String method = getAuthenticationMethod(challenges[i]); 77 78 if (method.equalsIgnoreCase(BASIC)) { 79 authenticator = new BasicAuthentication(uri, con.getHost(), con.getPort(), con.isSecure()); 80 } else if (method.equalsIgnoreCase(NTLM)) { 81 authenticator = new NTLMAuthentication(uri, con.getHost(), con.getPort(), con.isSecure()); 82 } else if (method.equalsIgnoreCase(DIGEST)) { 83 authenticator = new DigestAuthentication(uri, con.getHost(), con.getPort(), con.isSecure()); 84 } 85 86 if (authenticator != null) { 87 actualChallenge = challenges[i]; 88 break; 89 } 90 } 91 } 92 if (authenticator != null) { 93 log.info(MessageFormat.format(Messages.getString("HttpAuthenticatorFactory.created"), new Object [] { authenticator.getScheme() })); 97 authenticator.setConnection(con); 98 authenticator.setChallenge(actualChallenge); 99 authenticator.setAuthenicationHeader(authenticationHeader); 100 authenticator.setAuthorizationHeader(authorizationHeader); 101 102 return authenticator; 103 } 104 105 if (pref == null) 106 throw new UnsupportedAuthenticationException(challenges); 107 else 108 throw new UnsupportedAuthenticationException(challenges, 109 MessageFormat.format(Messages.getString("HttpAuthenticatorFactory.notSupported"), new Object [] { pref })); } 111 112 public static String getAuthenticationMethod(String challenge) { 113 String method = challenge; 114 115 if (method != null) { 116 int n = method.indexOf(' '); 117 if (n > -1) 118 method = method.substring(0, n); 119 } 120 121 return method; 122 } 123 124 public String getAuthenticationRealm(String challenge) { 125 String auth = challenge; 126 String realm = ""; 128 if (auth != null) { 129 int l; 130 int r = auth.indexOf('='); 131 132 while (r >= 0) { 133 l = auth.lastIndexOf(' ', r); 134 realm = auth.substring(l + 1, r); 135 136 if (realm.equalsIgnoreCase("realm")) { l = r + 2; 138 r = auth.indexOf('"', l); 139 realm = auth.substring(l, r); 140 141 break; 142 } 143 144 r = auth.indexOf('=', r + 1); 145 } 146 } 147 148 return realm; 149 } 150 } 151 | Popular Tags |