Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / sasl / ServerFactory.java
blobe9b08dbd49a40ed9e40ff17012424f7fcdee9c73
1 /* ServerFactory.java --
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.crypto.sasl;
41 import gnu.java.security.Registry;
42 import gnu.javax.crypto.sasl.anonymous.AnonymousServer;
43 import gnu.javax.crypto.sasl.crammd5.CramMD5Server;
44 import gnu.javax.crypto.sasl.plain.PlainServer;
45 import gnu.javax.crypto.sasl.srp.SRPServer;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.HashSet;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Set;
56 import javax.security.auth.callback.CallbackHandler;
57 import javax.security.sasl.Sasl;
58 import javax.security.sasl.SaslException;
59 import javax.security.sasl.SaslServer;
60 import javax.security.sasl.SaslServerFactory;
62 /**
63 * The implementation of the {@link SaslServerFactory}.
65 public class ServerFactory implements SaslServerFactory
68 // Constants and variables
69 // -------------------------------------------------------------------------
71 // Constructor(s)
72 // -------------------------------------------------------------------------
74 // implicit 0-arguments constructor
76 // Class methods
77 // -------------------------------------------------------------------------
79 public static final Set getNames()
81 return Collections.unmodifiableSet(new HashSet(
82 Arrays.asList(getNamesInternal(null))));
85 private static final String[] getNamesInternal(Map props)
87 String[] all = new String[] { Registry.SASL_SRP_MECHANISM,
88 Registry.SASL_CRAM_MD5_MECHANISM,
89 Registry.SASL_PLAIN_MECHANISM,
90 Registry.SASL_ANONYMOUS_MECHANISM };
92 List result = new ArrayList(4);
93 int i;
94 for (i = 0; i < all.length;)
96 result.add(all[i++]);
99 if (props == null)
101 return (String[]) result.toArray(new String[0]); // all
103 if (hasPolicy(Sasl.POLICY_PASS_CREDENTIALS, props))
104 { // none
105 return new String[0];
108 if (hasPolicy(Sasl.POLICY_NOPLAINTEXT, props))
110 result.remove(Registry.SASL_PLAIN_MECHANISM);
112 if (hasPolicy(Sasl.POLICY_NOACTIVE, props))
114 result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
115 result.remove(Registry.SASL_PLAIN_MECHANISM);
117 if (hasPolicy(Sasl.POLICY_NODICTIONARY, props))
119 result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
120 result.remove(Registry.SASL_PLAIN_MECHANISM);
122 if (hasPolicy(Sasl.POLICY_NOANONYMOUS, props))
124 result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
126 if (hasPolicy(Sasl.POLICY_FORWARD_SECRECY, props))
128 result.remove(Registry.SASL_CRAM_MD5_MECHANISM);
129 result.remove(Registry.SASL_ANONYMOUS_MECHANISM);
130 result.remove(Registry.SASL_PLAIN_MECHANISM);
132 return (String[]) result.toArray(new String[0]);
135 public static final ServerMechanism getInstance(String mechanism)
137 if (mechanism == null)
139 return null;
141 mechanism = mechanism.trim().toUpperCase();
142 if (mechanism.equals(Registry.SASL_SRP_MECHANISM))
144 return new SRPServer();
146 if (mechanism.equals(Registry.SASL_CRAM_MD5_MECHANISM))
148 return new CramMD5Server();
150 if (mechanism.equals(Registry.SASL_PLAIN_MECHANISM))
152 return new PlainServer();
154 if (mechanism.equals(Registry.SASL_ANONYMOUS_MECHANISM))
156 return new AnonymousServer();
158 return null;
161 // Instance methods
162 // -------------------------------------------------------------------------
164 public SaslServer createSaslServer(String mechanism, String protocol,
165 String serverName, Map props,
166 CallbackHandler cbh) throws SaslException
168 ServerMechanism result = getInstance(mechanism);
169 if (result != null)
171 HashMap attributes = new HashMap();
172 if (props != null)
174 attributes.putAll(props);
176 attributes.put(Registry.SASL_PROTOCOL, protocol);
177 attributes.put(Registry.SASL_SERVER_NAME, serverName);
178 attributes.put(Registry.SASL_CALLBACK_HANDLER, cbh);
180 result.init(attributes);
182 return result;
185 public String[] getMechanismNames(Map props)
187 return getNamesInternal(props);
190 private static boolean hasPolicy(String propertyName, Map props)
192 return "true".equalsIgnoreCase(String.valueOf(props.get(propertyName)));