import java.io.*;
import java.security.*;
import javax.security.auth.*;
import javax.security.auth.callback.*;

/**
 *	CallbackHandler que maneja usuarios y passwords.
 */
public class UserPasswordCallbackHandler
implements CallbackHandler {

	private String mUsuario;
	private char[] mPassword;

	/**
	 *	Constructor especifica usuario y password.
	 */
	public UserPasswordCallbackHandler(String usuario, char[] password) {
		mUsuario = usuario;
		mPassword = password;
	}

	/**
	 *	Manejar callbacks: NameCallbacks y PasswordCallbacks
	 */
	public void handle(Callback[] callbacks)
	throws UnsupportedCallbackException {
		// Iterar los callbacks
		for(int i=0;i<callbacks.length;i++) {
			Callback callback = callbacks[i];
			// Manejar callback según su tipo.
			if (callback instanceof NameCallback) {
				NameCallback nameCallback = (NameCallback)callback;
				nameCallback.setName(mUsuario);
			} else if (callback instanceof PasswordCallback) {
				PasswordCallback passwordCallback = (PasswordCallback)callback;
				passwordCallback.setPassword(mPassword);
			} else {
				throw new UnsupportedCallbackException(callback, "Tipo de callback no soportado");
			}
		}
	}

}
