KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > auth > callback > NoInputCallbackHandler


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent Benoit
22  * --------------------------------------------------------------------------
23  * $Id: NoInputCallbackHandler.java,v 1.3 2004/05/25 15:13:29 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.security.auth.callback;
28
29 import java.io.IOException JavaDoc;
30 import javax.security.auth.callback.CallbackHandler JavaDoc;
31 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
32 import javax.security.auth.callback.NameCallback JavaDoc;
33 import javax.security.auth.callback.PasswordCallback JavaDoc;
34 import javax.security.auth.callback.Callback JavaDoc;
35 import java.security.cert.Certificate JavaDoc;
36
37 /**
38  * The username and password are given by the constructor No JNDI, datasource or
39  * file checks.
40  * @author Florent Benoit (initial developer)
41  * @author Alexandre Thaveau (add the use of certificates)
42  * @author Marc-Antoine Bourgeot (add the use of certificates)
43  */

44 public class NoInputCallbackHandler implements CallbackHandler JavaDoc {
45
46     /**
47      * Username to use
48      */

49     private String JavaDoc username = null;
50
51     /**
52      * Password to use
53      */

54     private String JavaDoc password = null;
55
56     /**
57      * Certificate to use, optionnal
58      */

59     private Certificate JavaDoc cert = null;
60
61     /**
62      * No default Constructor : must use the one with username and password
63      * @throws Exception if someone try to use it
64      */

65     public NoInputCallbackHandler() throws Exception JavaDoc {
66         throw new Exception JavaDoc(
67                 "This class could only be used with the constructor NoInputCallbackHandler(String username, String password)");
68     }
69
70     /**
71      * Constructor
72      * @param username username to store for the authentication
73      * @param password password to store for the authentication
74      */

75     public NoInputCallbackHandler(String JavaDoc username, String JavaDoc password) {
76         this.username = username;
77         this.password = password;
78     }
79
80     /**
81      * Constructor
82      * @param username username to store for the authentication
83      * @param password password to store for the authentication
84      * @param cert the certificate for the authentication
85      */

86     public NoInputCallbackHandler(String JavaDoc username, String JavaDoc password, Certificate JavaDoc cert) {
87         this(username, password);
88         this.cert = cert;
89     }
90
91     /**
92      * Invoke an array of Callbacks.
93      * @param callbacks an array of <code>Callback</code> objects which
94      * contain the information requested by an underlying security
95      * service to be retrieved or displayed.
96      * @throws IOException if an input or output error occurs. <p>
97      * @throws UnsupportedCallbackException if the implementation of this method
98      * does not support one or more of the Callbacks specified in the
99      * <code>callbacks</code> parameter.
100      */

101     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
102
103         for (int i = 0; i < callbacks.length; i++) {
104             if (callbacks[i] instanceof NameCallback JavaDoc) {
105                 // set the username to the username given in the constructor
106
NameCallback JavaDoc nc = (NameCallback JavaDoc) callbacks[i];
107                 nc.setName(username);
108             } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
109                 // set the password to the password given in the constructor
110
PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc) callbacks[i];
111                 pc.setPassword(password.toCharArray());
112             } else if (callbacks[i] instanceof CertificateCallback) {
113                 CertificateCallback cc = (CertificateCallback) callbacks[i];
114                 cc.setUserCertificate(cert);
115             } else {
116                 throw new UnsupportedCallbackException JavaDoc(callbacks[i], "Unrecognized Callback");
117             }
118         }
119     }
120 }
Popular Tags