File: Handler.java

package info (click to toggle)
cyrus-sasl2 2.1.23.dfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 8,692 kB
  • ctags: 7,265
  • sloc: ansic: 50,844; sh: 12,869; java: 1,614; xml: 1,498; makefile: 649; perl: 11
file content (111 lines) | stat: -rw-r--r-- 2,081 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

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

class Handler implements javax.security.auth.callback.CallbackHandler{

    String authid;
    String userid;
    String password;
    String realm;
    
    public Handler()
    {

    }

    public Handler(String authid, String userid, String password, String realm)
    {
	this.authid = authid;
	this.userid = userid;
	this.password = password;
	this.realm = realm;
    }



    private String getinput(String prompt)
    {
	System.out.println(prompt);
	System.out.print(">");

	String result="";
	    
	try {
	    int c;
	    do {
		c = System.in.read();
		if (c!='\n')
		    result+=(char)c;
	    } while (c!='\n');
	    
	    System.out.println("res = "+result);
	} catch (IOException e) {

	}
	
	return result;
    }

    private void getauthid(NameCallback c)
    {
	if (authid!=null) {
	    c.setName(authid);
	    return;
	}

	/*	authid = System.getProperty("user.name");
	if (authid!=null) {
	    c.setName(authid);
	    return;
	    } */

	c.setName( getinput(c.getPrompt()));
    }

    private void getpassword(PasswordCallback c)
    {
	if (password!=null) {
	    c.setPassword(password.toCharArray());
	    return;
	}

	c.setPassword( (getinput("Enter password")).toCharArray());
    }

    private void getrealm(RealmCallback c)
    {
	if (realm!=null) {
	    c.setRealm(realm);
	    return;
	}	  
	
	c.setRealm( getinput(c.getPrompt()) );
    }

    public void invokeCallback(Callback[] callbacks)
	throws java.io.IOException, UnsupportedCallbackException
    {
	for (int lup=0;lup<callbacks.length;lup++)
	{
	    Callback c = callbacks[lup];

	    if (c instanceof NameCallback) {
		getauthid((NameCallback) c);
	    } else if (c instanceof PasswordCallback) {
		getpassword((PasswordCallback) c);
	    } else if (c instanceof RealmCallback) {
		getrealm((RealmCallback) c);
	    } else {
		System.out.println("TODO!");
		System.exit(1);
	    }
	}
    }

    public void handle(Callback[] callbacks) 
	throws java.io.IOException, UnsupportedCallbackException 
    {
	invokeCallback(callbacks);
    }
}