public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (Callback cb : callbacks) { if (cb instanceof TextInputCallback) { TextInputCallback ticb = (TextInputCallback) cb; String prompt = ticb.getPrompt(); if (prompt == "clientID") { ticb.setText(clientID); } else if (prompt == "clientSecret") { ticb.setText(clientSecret); } else if (prompt == "accessToken") { ticb.setText(accessToken); } else if (prompt == "refreshToken") { ticb.setText(refreshToken); } else { throw new UnsupportedCallbackException(ticb, "Unrecognized prompt: " + ticb.getPrompt()); } } else { throw new UnsupportedCallbackException(cb, "Unsupported callback type."); } } }
@Override public synchronized void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { final Console console = System.console(); if (console == null) throw new IOException("Console not available"); for (Callback callback : callbacks) { if (callback instanceof PasswordCallback) { final PasswordCallback password = (PasswordCallback) callback; final String prompt = getDefault(password.getPrompt(), "Enter password"); char[] answer = console.readPassword("\u001b[34m%s\u001b[0m: ", prompt); if (answer == null) throw new EOFException(); password.setPassword(answer); } else if (callback instanceof NameCallback) { final NameCallback name = (NameCallback) callback; final String prompt = getDefault(name.getPrompt(), "Enter name"); final String defaultName = name.getDefaultName(); if (defaultName == null) while (true) { final String answer = console.readLine("\u001b[34m%s\u001b[0m: ", prompt); if (answer == null) throw new EOFException(); if (answer.length() == 0) continue; name.setName(answer); break; } else { final String answer = console.readLine( "\u001b[34m%s\u001b[0m [\u001b[36m%s\u001b[0m]: ", prompt, defaultName); if (answer == null) throw new EOFException(); if (answer.length() == 0) name.setName(defaultName); else name.setName(answer); } } else if (callback instanceof TextInputCallback) { final TextInputCallback input = (TextInputCallback) callback; final String prompt = getDefault(input.getPrompt(), "Enter value"); final String defaultText = input.getDefaultText(); if (defaultText == null) while (true) { final String answer = console.readLine("\u001b[34m%s\u001b[0m: ", prompt); if (answer == null) throw new EOFException(); if (answer.length() == 0) continue; input.setText(answer); break; } else { final String answer = console.readLine( "\u001b[34m%s\u001b[0m [\u001b[36m%s\u001b[0m]: ", prompt, defaultText); if (answer == null) throw new EOFException(); if (answer.length() == 0) input.setText(defaultText); else input.setText(answer); } } else if (callback instanceof TextOutputCallback) { final TextOutputCallback output = (TextOutputCallback) callback; final String message = output.getMessage(); switch (output.getMessageType()) { case TextOutputCallback.INFORMATION: console.format("\u001b[32m%s\u001b[0m\n", message); break; case TextOutputCallback.WARNING: console.format("\u001b[33mWARNING: %s\u001b[0m\n", message); break; case TextOutputCallback.ERROR: console.format("\u001b[31mERROR: %s\u001b[0m\n", message); break; default: console.format("\u001b[35m??? [%d]: %s\u001b[0m\n", output.getMessageType(), message); break; } } else { throw new UnsupportedCallbackException(callback, "Callback type not supported"); } } }
/** * Retrieve the information requested in the provided <code>Callbacks</code>. This implementation * only recognizes {@link NameCallback}, {@link PasswordCallback} and {@link TextInputCallback}. * {@link TextInputCallback} is used to pass the various additional parameters required for DIGEST * authentication. * * @param callbacks The set of <code>Callback</code>s to be processed * @exception IOException if an input/output error occurs * @exception UnsupportedCallbackException if the login method requests an unsupported callback * type */ @Override public void handle(Callback callbacks[]) throws IOException, UnsupportedCallbackException { try { for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof NameCallback) { if (realm.getContainer().getLogger().isTraceEnabled()) realm.getContainer().getLogger().trace(sm.getString("jaasCallback.username", username)); ((NameCallback) callbacks[i]).setName(username); } else if (callbacks[i] instanceof PasswordCallback) { final char[] passwordcontents; if (password != null) { passwordcontents = password.toCharArray(); } else { passwordcontents = new char[0]; } ((PasswordCallback) callbacks[i]).setPassword(passwordcontents); } else if (callbacks[i] instanceof TextInputCallback) { TextInputCallback cb = ((TextInputCallback) callbacks[i]); if (cb.getPrompt().equals("nonce")) { cb.setText(nonce); } else if (cb.getPrompt().equals("nc")) { cb.setText(nc); } else if (cb.getPrompt().equals("cnonce")) { cb.setText(cnonce); } else if (cb.getPrompt().equals("qop")) { cb.setText(qop); } else if (cb.getPrompt().equals("realmName")) { cb.setText(realmName); } else if (cb.getPrompt().equals("md5a2")) { cb.setText(md5a2); } else if (cb.getPrompt().equals("authMethod")) { cb.setText(authMethod); } else { throw new UnsupportedCallbackException(callbacks[i]); } } else { throw new UnsupportedCallbackException(callbacks[i]); } } } catch (Exception excp) { excp.printStackTrace(); } }