01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.auth.callback;
19:
20: import java.io.BufferedReader;
21: import java.io.IOException;
22: import java.io.InputStream;
23: import java.io.InputStreamReader;
24: import java.io.PrintStream;
25:
26: import javax.security.auth.callback.Callback;
27: import javax.security.auth.callback.CallbackHandler;
28: import javax.security.auth.callback.NameCallback;
29: import javax.security.auth.callback.PasswordCallback;
30: import javax.security.auth.callback.UnsupportedCallbackException;
31:
32: public class TextCallbackHandler implements CallbackHandler {
33:
34: private InputStream in = System.in;
35: private PrintStream out = System.out;
36:
37: public void handle(Callback[] callbacks) throws IOException,
38: UnsupportedCallbackException {
39: for (int i = 0; i < callbacks.length; i++) {
40: if (callbacks[i] instanceof NameCallback) {
41: NameCallback nameCallback = (NameCallback) callbacks[i];
42: out.print(nameCallback.getPrompt());
43: BufferedReader br = new BufferedReader(
44: new InputStreamReader(in));
45: nameCallback.setName(br.readLine());
46: } else if (callbacks[i] instanceof PasswordCallback) {
47: PasswordCallback passwordCallback = (PasswordCallback) callbacks[i];
48: out.print(passwordCallback.getPrompt());
49: //haven't implemented echo off function
50: BufferedReader br = new BufferedReader(
51: new InputStreamReader(in));
52: passwordCallback.setPassword(br.readLine()
53: .toCharArray());
54: } else {
55: throw new UnsupportedCallbackException(callbacks[i]);
56: }
57: }
58: }
59: }
|