2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
5 package org.collectionspace.hello.services.nuxeo;
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;
20 * @author Scott.Stark@jboss.org
22 public class NuxeoCallbackHandler implements CallbackHandler {
24 private final String username;
25 private char[] password;
26 private final Object credential;
29 * Initializes the UsernamePasswordHandler with the username and password to
32 * @param username the user name
33 * @param password the password for this user
35 public NuxeoCallbackHandler(String username, char[] password) {
36 this.username = username;
37 this.password = password;
38 credential = password;
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();
52 * Sets any NameCallback name property to the instance username, sets any
53 * PasswordCallback password property to the instance, and any password.
55 * @exception UnsupportedCallbackException,
56 * thrown if any callback of type other than NameCallback or
57 * PasswordCallback are seen.
59 public void handle(Callback[] callbacks)
60 throws UnsupportedCallbackException {
61 for (Callback c : callbacks) {
62 if (c instanceof NameCallback) {
63 NameCallback nc = (NameCallback) c;
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
70 if (credential != null) {
71 String tmp = credential.toString();
72 password = tmp.toCharArray();
75 pc.setPassword(password);
76 } else if (c instanceof NuxeoCallback) {
77 NuxeoCallback oc = (NuxeoCallback) c;
78 oc.setCredential(credential);
80 throw new UnsupportedCallbackException(c,
81 "Unrecognized Callback");