1 30 31 package org.apache.commons.httpclient.auth; 32 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.List ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 58 public abstract class AuthPolicy { 59 60 private static final HashMap SCHEMES = new HashMap (); 61 private static final ArrayList SCHEME_LIST = new ArrayList (); 62 63 79 public static final String AUTH_SCHEME_PRIORITY = "http.auth.scheme-priority"; 80 81 86 public static final String NTLM = "NTLM"; 87 88 91 public static final String DIGEST = "Digest"; 92 93 97 public static final String BASIC = "Basic"; 98 99 static { 100 AuthPolicy.registerAuthScheme(NTLM, NTLMScheme.class); 101 AuthPolicy.registerAuthScheme(DIGEST, DigestScheme.class); 102 AuthPolicy.registerAuthScheme(BASIC, BasicScheme.class); 103 } 104 105 106 protected static final Log LOG = LogFactory.getLog(AuthPolicy.class); 107 108 125 public static synchronized void registerAuthScheme(final String id, Class clazz) { 126 if (id == null) { 127 throw new IllegalArgumentException ("Id may not be null"); 128 } 129 if (clazz == null) { 130 throw new IllegalArgumentException ("Authentication scheme class may not be null"); 131 } 132 SCHEMES.put(id.toLowerCase(), clazz); 133 SCHEME_LIST.add(id.toLowerCase()); 134 } 135 136 142 public static synchronized void unregisterAuthScheme(final String id) { 143 if (id == null) { 144 throw new IllegalArgumentException ("Id may not be null"); 145 } 146 SCHEMES.remove(id.toLowerCase()); 147 SCHEME_LIST.remove(id.toLowerCase()); 148 } 149 150 159 public static synchronized AuthScheme getAuthScheme(final String id) 160 throws IllegalStateException { 161 162 if (id == null) { 163 throw new IllegalArgumentException ("Id may not be null"); 164 } 165 Class clazz = (Class )SCHEMES.get(id.toLowerCase()); 166 if (clazz != null) { 167 try { 168 return (AuthScheme)clazz.newInstance(); 169 } catch (Exception e) { 170 LOG.error("Error initializing authentication scheme: " + id, e); 171 throw new IllegalStateException (id + 172 " authentication scheme implemented by " + 173 clazz.getName() + " could not be initialized"); 174 } 175 } else { 176 throw new IllegalStateException ("Unsupported authentication scheme " + id); 177 } 178 } 179 180 186 public static synchronized List getDefaultAuthPrefs() { 187 return (List )SCHEME_LIST.clone(); 188 } 189 } 190 | Popular Tags |