AmqPlainSaslClientFactory.java
01 /*
02  *
03  * Licensed to the Apache Software Foundation (ASF) under one
04  * or more contributor license agreements.  See the NOTICE file
05  * distributed with this work for additional information
06  * regarding copyright ownership.  The ASF licenses this file
07  * to you under the Apache License, Version 2.0 (the
08  * "License"); you may not use this file except in compliance
09  * with the License.  You may obtain a copy of the License at
10  
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  */
21 package org.apache.qpid.client.security.amqplain;
22 
23 import java.util.Map;
24 
25 import javax.security.auth.callback.CallbackHandler;
26 import javax.security.sasl.Sasl;
27 import javax.security.sasl.SaslClient;
28 import javax.security.sasl.SaslClientFactory;
29 import javax.security.sasl.SaslException;
30 
31 public class AmqPlainSaslClientFactory implements SaslClientFactory
32 {
33     public SaslClient createSaslClient(String[] mechanisms, String authorizationId, String protocol, String serverName, Map props, CallbackHandler cbhthrows SaslException
34     {
35         for (int i = 0; i < mechanisms.length; i++)
36         {
37             if (mechanisms[i].equals(AmqPlainSaslClient.MECHANISM))
38             {
39                 if (cbh == null)
40                 {
41                     throw new SaslException("CallbackHandler must not be null");
42                 }
43                 return new AmqPlainSaslClient(cbh);
44             }
45         }
46         return null;
47     }
48 
49     public String[] getMechanismNames(Map props)
50     {
51         if (props.containsKey(Sasl.POLICY_NOPLAINTEXT||
52             props.containsKey(Sasl.POLICY_NODICTIONARY||
53             props.containsKey(Sasl.POLICY_NOACTIVE))
54         {
55             // returned array must be non null according to interface documentation
56             return new String[0];
57         }
58         else
59         {
60             return new String[]{AmqPlainSaslClient.MECHANISM};
61         }
62     }
63 }