1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62:
65: public class ClientFactory
66: implements SaslClientFactory
67: {
68:
69:
70: public static final Set getNames()
71: {
72: return Collections.unmodifiableSet(new HashSet(Arrays.asList(getNamesInternal(null))));
73: }
74:
75: private static final String[] getNamesInternal(Map props)
76: {
77: String[] all = new String[] {
78: Registry.SASL_SRP_MECHANISM,
79: Registry.SASL_CRAM_MD5_MECHANISM,
80: Registry.SASL_PLAIN_MECHANISM,
81: Registry.SASL_ANONYMOUS_MECHANISM };
82: if (props == null)
83: return all;
84: if (hasPolicy(Sasl.POLICY_PASS_CREDENTIALS, props))
85: return new String[0];
86: List result = new ArrayList(all.length);
87: for (int i = 0; i < all.length;)
88: result.add(all[i++]);
89: if (hasPolicy(Sasl.POLICY_NOPLAINTEXT, props))
90: result.remove(Registry.SASL_PLAIN_MECHANISM);
91: if (hasPolicy(Sasl.POLICY_NOACTIVE, props))
92: {
93: result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
94: result.remove(Registry.SASL_PLAIN_MECHANISM);
95: }
96: if (hasPolicy(Sasl.POLICY_NODICTIONARY, props))
97: {
98: result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
99: result.remove(Registry.SASL_PLAIN_MECHANISM);
100: }
101: if (hasPolicy(Sasl.POLICY_NOANONYMOUS, props))
102: {
103: result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
104: }
105: if (hasPolicy(Sasl.POLICY_FORWARD_SECRECY, props))
106: {
107: result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
108: result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
109: result.remove(Registry.SASL_PLAIN_MECHANISM);
110: }
111: return (String[]) result.toArray(new String[0]);
112: }
113:
114: public static final ClientMechanism getInstance(String mechanism)
115: {
116: if (mechanism == null)
117: return null;
118: mechanism = mechanism.trim().toUpperCase();
119: if (mechanism.equals(Registry.SASL_SRP_MECHANISM))
120: return new SRPClient();
121: if (mechanism.equals(Registry.SASL_CRAM_MD5_MECHANISM))
122: return new CramMD5Client();
123: if (mechanism.equals(Registry.SASL_PLAIN_MECHANISM))
124: return new PlainClient();
125: if (mechanism.equals(Registry.SASL_ANONYMOUS_MECHANISM))
126: return new AnonymousClient();
127: return null;
128: }
129:
130: public SaslClient createSaslClient(String[] mechanisms,
131: String authorisationID, String protocol,
132: String serverName, Map props,
133: CallbackHandler cbh) throws SaslException
134: {
135: ClientMechanism result = null;
136: String mechanism;
137: for (int i = 0; i < mechanisms.length; i++)
138: {
139: mechanism = mechanisms[i];
140: result = getInstance(mechanism);
141: if (result != null)
142: break;
143: }
144: if (result != null)
145: {
146: HashMap attributes = new HashMap();
147: if (props != null)
148: attributes.putAll(props);
149: attributes.put(Registry.SASL_AUTHORISATION_ID, authorisationID);
150: attributes.put(Registry.SASL_PROTOCOL, protocol);
151: attributes.put(Registry.SASL_SERVER_NAME, serverName);
152: attributes.put(Registry.SASL_CALLBACK_HANDLER, cbh);
153: result.init(attributes);
154: return result;
155: }
156: throw new SaslException("No supported mechanism found in given mechanism list");
157: }
158:
159: public String[] getMechanismNames(Map props)
160: {
161: return getNamesInternal(props);
162: }
163:
164: private static boolean hasPolicy(String propertyName, Map props)
165: {
166: return "true".equalsIgnoreCase(String.valueOf(props.get(propertyName)));
167: }
168: }