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 ServerFactory
66: implements SaslServerFactory
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: List result = new ArrayList(4);
83: int i;
84: for (i = 0; i < all.length;)
85: result.add(all[i++]);
86: if (props == null)
87: return (String[]) result.toArray(new String[0]);
88: if (hasPolicy(Sasl.POLICY_PASS_CREDENTIALS, props))
89: return new String[0];
90: if (hasPolicy(Sasl.POLICY_NOPLAINTEXT, props))
91: result.remove(Registry.SASL_PLAIN_MECHANISM);
92: if (hasPolicy(Sasl.POLICY_NOACTIVE, props))
93: {
94: result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
95: result.remove(Registry.SASL_PLAIN_MECHANISM);
96: }
97: if (hasPolicy(Sasl.POLICY_NODICTIONARY, props))
98: {
99: result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
100: result.remove(Registry.SASL_PLAIN_MECHANISM);
101: }
102: if (hasPolicy(Sasl.POLICY_NOANONYMOUS, props))
103: {
104: result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
105: }
106: if (hasPolicy(Sasl.POLICY_FORWARD_SECRECY, props))
107: {
108: result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
109: result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
110: result.remove(Registry.SASL_PLAIN_MECHANISM);
111: }
112: return (String[]) result.toArray(new String[0]);
113: }
114:
115: public static final ServerMechanism getInstance(String mechanism)
116: {
117: if (mechanism == null)
118: return null;
119: mechanism = mechanism.trim().toUpperCase();
120: if (mechanism.equals(Registry.SASL_SRP_MECHANISM))
121: return new SRPServer();
122: if (mechanism.equals(Registry.SASL_CRAM_MD5_MECHANISM))
123: return new CramMD5Server();
124: if (mechanism.equals(Registry.SASL_PLAIN_MECHANISM))
125: return new PlainServer();
126: if (mechanism.equals(Registry.SASL_ANONYMOUS_MECHANISM))
127: return new AnonymousServer();
128: return null;
129: }
130:
131: public SaslServer createSaslServer(String mechanism, String protocol,
132: String serverName, Map props,
133: CallbackHandler cbh) throws SaslException
134: {
135: ServerMechanism result = getInstance(mechanism);
136: if (result != null)
137: {
138: HashMap attributes = new HashMap();
139: if (props != null)
140: attributes.putAll(props);
141: attributes.put(Registry.SASL_PROTOCOL, protocol);
142: attributes.put(Registry.SASL_SERVER_NAME, serverName);
143: attributes.put(Registry.SASL_CALLBACK_HANDLER, cbh);
144: result.init(attributes);
145: }
146: return result;
147: }
148:
149: public String[] getMechanismNames(Map props)
150: {
151: return getNamesInternal(props);
152: }
153:
154: private static boolean hasPolicy(String propertyName, Map props)
155: {
156: return "true".equalsIgnoreCase(String.valueOf(props.get(propertyName)));
157: }
158: }