Commit | Line | Data |
---|---|---|
7131093c FT |
1 | package dolda.dolcon; |
2 | ||
3 | import java.util.*; | |
4 | import dolda.dolcon.protocol.*; | |
5 | ||
6 | public class Session { | |
7 | private Connection conn; | |
8 | ||
9 | public Session(String aspec, String username, List<Authenticator> auth) throws AuthException, ProtocolException, InterruptedException { | |
10 | conn = new Connection(aspec); | |
11 | conn.expectVersion(2); | |
12 | try { | |
13 | conn.syncConnect(); | |
14 | } catch(ConnectException e) { | |
15 | throw(new ProtocolException(e)); | |
16 | } | |
17 | authenticate(username, auth); | |
18 | } | |
19 | ||
20 | public Session(String aspec, String username, Authenticator... auth) throws AuthException, ProtocolException, InterruptedException { | |
21 | this(aspec, username, Arrays.asList(auth)); | |
22 | } | |
23 | ||
24 | private void authenticate(String username, List<Authenticator> auth) throws AuthException, ProtocolException, InterruptedException { | |
25 | Response resp; | |
26 | ||
27 | try { | |
28 | resp = ResponseException.check(conn.ecmd("lsauth"), 200); | |
29 | List<String> mechs = new LinkedList<String>(); | |
30 | for(List<String> mech : resp.lines) | |
31 | mechs.add(mech.get(0).intern()); | |
32 | String use = null; | |
33 | Authenticator au = null; | |
34 | for(Authenticator a : auth) { | |
35 | System.out.println(a); | |
36 | use = a.handles(mechs); | |
37 | if(use != null) { | |
38 | au = a; | |
39 | break; | |
40 | } | |
41 | } | |
42 | if(use == null) | |
43 | throw(new NoMechException()); | |
44 | resp = conn.ecmd("login", use, username); | |
45 | while(true) { | |
46 | if(resp.code == 200) { | |
47 | return; | |
48 | } else if((resp.code / 100) == 3) { | |
49 | resp = conn.ecmd(au.step(resp)); | |
50 | } else if((resp.code / 100) == 5) { | |
51 | throw(new AuthException(resp.token(0, 0))); | |
52 | } else { | |
53 | throw(new ResponseException(resp, 0)); | |
54 | } | |
55 | } | |
56 | } catch(ClosedException e) { | |
57 | throw(new ProtocolException(e)); | |
58 | } | |
59 | } | |
60 | } |