]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e8fff8c2587d9c5beae3b54ac6bc181b19b1e38a
[tmp/jakarta-migration.git] /
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package org.collectionspace.hello.services.nuxeo;
6
7 /**
8  *
9  * @author sanjaydalal
10  */
11 import javax.security.auth.callback.Callback;
12 import javax.security.auth.callback.CallbackHandler;
13 import javax.security.auth.callback.NameCallback;
14 import javax.security.auth.callback.PasswordCallback;
15 import javax.security.auth.callback.UnsupportedCallbackException;
16
17 /**
18  * Copied from jbossx.
19  *
20  * @author Scott.Stark@jboss.org
21  */
22 public class NuxeoCallbackHandler implements CallbackHandler {
23
24     private final String username;
25     private char[] password;
26     private final Object credential;
27
28     /**
29      * Initializes the UsernamePasswordHandler with the username and password to
30      * use.
31      *
32      * @param username the user name
33      * @param password the password for this user
34      */
35     public NuxeoCallbackHandler(String username, char[] password) {
36         this.username = username;
37         this.password = password;
38         credential = password;
39     }
40
41     public NuxeoCallbackHandler(String username, Object credential) {
42         this.username = username;
43         this.credential = credential;
44         if (credential instanceof char[]) {
45             password = (char[]) credential;
46         } else if (credential instanceof CharSequence) {
47             password = credential.toString().toCharArray();
48         }
49     }
50
51     /**
52      * Sets any NameCallback name property to the instance username, sets any
53      * PasswordCallback password property to the instance, and any password.
54      *
55      * @exception UnsupportedCallbackException,
56      *                thrown if any callback of type other than NameCallback or
57      *                PasswordCallback are seen.
58      */
59     public void handle(Callback[] callbacks)
60             throws UnsupportedCallbackException {
61         for (Callback c : callbacks) {
62             if (c instanceof NameCallback) {
63                 NameCallback nc = (NameCallback) c;
64                 nc.setName(username);
65             } else if (c instanceof PasswordCallback) {
66                 PasswordCallback pc = (PasswordCallback) c;
67                 if (password == null) {
68                     // We were given an opaque Object credential but a char[] is
69                     // requested?
70                     if (credential != null) {
71                         String tmp = credential.toString();
72                         password = tmp.toCharArray();
73                     }
74                 }
75                 pc.setPassword(password);
76             } else if (c instanceof NuxeoCallback) {
77                 NuxeoCallback oc = (NuxeoCallback) c;
78                 oc.setCredential(credential);
79             } else {
80                 throw new UnsupportedCallbackException(c,
81                         "Unrecognized Callback");
82             }
83         }
84     }
85 }