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 package org.melati.login;
45
46 import java.io.BufferedReader;
47 import java.io.IOException;
48 import java.io.InputStream;
49 import java.io.InputStreamReader;
50 import java.io.PrintStream;
51
52 import org.melati.Melati;
53 import org.melati.poem.AccessPoemException;
54 import org.melati.poem.PoemThread;
55 import org.melati.poem.User;
56 import org.melati.poem.util.ArrayUtils;
57 import org.melati.util.MelatiException;
58
59 /**
60 * A handler invoked when an {@link AccessPoemException} is thrown.
61 *
62 * If the application is invoked without a username and password on the command
63 * line then the user will be prompted for them.
64 *
65 * If the usename is supplied then the user will not be prompted as this migth
66 * interfere with use in a scripting environment.
67 *
68 * @see org.melati.login.AccessHandler
69 */
70 public class CommandLineAccessHandler implements AccessHandler {
71
72 private PrintStream output = System.out;
73
74 private InputStream input = System.in;
75
76 private boolean commandLineUserCredentialsSet = false;
77
78 BufferedReader inputReader = null;
79
80 /**
81 * Constructor.
82 */
83 public CommandLineAccessHandler() {
84 super();
85 commandLineUserCredentialsSet = false;
86 }
87
88
89 /**
90 * Actually handle the {@link AccessPoemException}.
91 *
92 * {@inheritDoc}
93 *
94 * @see org.melati.login.AccessHandler#handleAccessException
95 * (org.melati.Melati, org.melati.poem.AccessPoemException)
96 */
97 public void handleAccessException(Melati melati,
98 AccessPoemException accessException) throws Exception {
99 Authorization auth = null;
100 boolean loggedIn = false;
101 if (!commandLineUserCredentialsSet) {
102 inputReader = new BufferedReader(new InputStreamReader(input));
103 System.err.println(accessException.getMessage());
104 int goes = 0;
105 while (goes < 3 && loggedIn == false) {
106 goes++;
107 auth = Authorization.from(inputReader, output);
108 if (auth != null) {
109
110 User user = null;
111
112 if (auth.username != null) {
113 user = (User) melati.getDatabase().getUserTable().getLoginColumn()
114 .firstWhereEq(auth.username);
115 }
116 if (user != null && user.getPassword_unsafe().equals(auth.password)) {
117
118
119 String[] args = melati.getArguments();
120 args = (String[]) ArrayUtils.added(args, "-u");
121 args = (String[]) ArrayUtils.added(args, auth.username);
122 args = (String[]) ArrayUtils.added(args, "-p");
123 args = (String[]) ArrayUtils.added(args, auth.password);
124 melati.setArguments(args);
125 loggedIn = true;
126 } else {
127 System.err.println("Unknown username");
128 }
129 }
130 }
131 }
132 if (!loggedIn)
133 throw accessException;
134 }
135
136 /**
137 * Called when the PoemTask is initialised, recalled after a login.
138 * {@inheritDoc}
139 *
140 * @see org.melati.login.AccessHandler#establishUser(org.melati.Melati)
141 */
142 public Melati establishUser(Melati melati) {
143 Authorization auth = Authorization.from(melati.getArguments());
144 if (auth == null) {
145
146 PoemThread.setAccessToken(melati.getDatabase().guestAccessToken());
147 return melati;
148 } else {
149 commandLineUserCredentialsSet = true;
150
151 User user = null;
152 user = (User) melati.getDatabase().getUserTable().getLoginColumn()
153 .firstWhereEq(auth.username);
154 if (user == null || !user.getPassword_unsafe().equals(auth.password)) {
155
156
157
158 PoemThread.setAccessToken(melati.getDatabase().guestAccessToken());
159 return melati;
160 } else {
161
162 PoemThread.setAccessToken(user);
163 return melati;
164 }
165 }
166 }
167
168 /**
169 * A no-op in a command line application.
170 *
171 * {@inheritDoc}
172 *
173 * @see org.melati.login.AccessHandler#buildRequest(org.melati.Melati)
174 */
175 public void buildRequest(Melati melati) throws MelatiException, IOException {
176
177 }
178
179 /**
180 * @param input
181 * The input to set.
182 */
183 public void setInput(InputStream input) {
184 this.input = input;
185 }
186
187 /**
188 * @param output
189 * The output to set.
190 */
191 public void setOutput(PrintStream output) {
192 this.output = output;
193 }
194
195 }