KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > jaas > MuleCallbackHandler


1 /*
2  * $Id: MuleCallbackHandler.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.extras.jaas;
12
13 import java.io.IOException JavaDoc;
14 import javax.security.auth.callback.Callback JavaDoc;
15 import javax.security.auth.callback.CallbackHandler JavaDoc;
16 import javax.security.auth.callback.NameCallback JavaDoc;
17 import javax.security.auth.callback.PasswordCallback JavaDoc;
18 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
19 import org.mule.umo.security.UMOAuthentication;
20
21 /**
22  * @author Marie.Rizzo
23  */

24 public class MuleCallbackHandler implements CallbackHandler JavaDoc
25 {
26     private UMOAuthentication authentication;
27     private String JavaDoc username;
28     private String JavaDoc password;
29
30     /**
31      * @param authentication
32      */

33     public MuleCallbackHandler(UMOAuthentication authentication)
34     {
35         this.authentication = authentication;
36         this.username = (String JavaDoc)this.authentication.getPrincipal();
37         this.password = (String JavaDoc)this.authentication.getCredentials();
38     }
39
40     /**
41      * The handle() method handles the callbacks to be passed to the Jaas security.
42      * It makes use of two types of callbacks: the NameCallback and the
43      * PasswordCallback.
44      *
45      * @param callbacks
46      * @throws IOException
47      * @throws UnsupportedCallbackException
48      */

49     public final void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc
50     {
51         for (int i = 0; i < callbacks.length; i++)
52         {
53             if (callbacks[i] instanceof NameCallback JavaDoc)
54             {
55                 NameCallback JavaDoc nameCb = (NameCallback JavaDoc)callbacks[i];
56                 nameCb.setName(username);
57             }
58             else if (callbacks[i] instanceof PasswordCallback JavaDoc)
59             {
60                 PasswordCallback JavaDoc passCb = (PasswordCallback JavaDoc)callbacks[i];
61                 passCb.setPassword(password.toCharArray());
62             }
63             else
64             {
65                 throw (new UnsupportedCallbackException JavaDoc(callbacks[i], "Callback class not supported"));
66             }
67         }
68     }
69 }
70
Popular Tags