1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package no.feide.moria.directory.backend;
21
22 import java.util.HashMap;
23 import java.util.List;
24
25 /***
26 * Used to internally represent a user in the <code>DummyBackend</code> class.
27 */
28 public class DummyUser {
29
30 /***
31 * This user's attributes. The keys are attribute names as
32 * <code>String</code>s and the values are attribute values as
33 * <code>List</code>s.
34 */
35 private HashMap myAttributes;
36
37 /*** This user's username. */
38 private String myUsername;
39
40 /*** This user's password. */
41 private String myPassword;
42
43
44 /***
45 * Constructor.
46 * @param username
47 * The username. Cannot be <code>null</code>.
48 * @param password
49 * The password. Cannot be <code>null</code>.
50 * @param attributes
51 * The user's attributes, if any. Should contain attribute names
52 * as keys (<code>String</code>s) and attribute values as
53 * values (<code>List</code>s).
54 * @throws IllegalArgumentException
55 * If <code>username</code> or <code>password</code> is
56 * <code>null</code>.
57 */
58 public DummyUser(final String username, final String password,
59 final HashMap attributes) {
60
61 super();
62
63
64 if (username == null)
65 throw new IllegalArgumentException("Username cannot be NULL");
66 if (password == null)
67 throw new IllegalArgumentException("Password cannot be NULL");
68
69
70 myUsername = username;
71 myPassword = password;
72 if (attributes == null)
73 myAttributes = new HashMap();
74 else
75 myAttributes = new HashMap(attributes);
76 }
77
78
79 /***
80 * "Authenticates" this user, by doing a case-insensitive match on username
81 * and case-sensitive match on password.
82 * @param username
83 * The username to match. Cannot be <code>null</code>.
84 * @param password
85 * The password to match. Cannot be <code>null</code>.
86 * @return true if authentication succeeds, false otherwise.
87 * @throws IllegalArgumentException
88 * If <code>username</code> or <code>password</code> is
89 * <code>null</code>.
90 */
91 public final boolean authenticate(final String username,
92 final String password) {
93
94
95 if (username == null)
96 throw new IllegalArgumentException("Username cannot be NULL");
97 if (password == null)
98 throw new IllegalArgumentException("Password cannot be NULL");
99
100
101 return ((username.equalsIgnoreCase(myUsername)) && (password.equals(myPassword)));
102
103 }
104
105
106 /***
107 * Gets this user's attributes.
108 * @param request
109 * The attribute request. Case is ignored.
110 * @return The requested user's attributes, if any were found. Note that the
111 * attribute names returned will match the case of the attribute
112 * names in the request.
113 */
114 public final HashMap getAttributes(final String[] request) {
115
116 HashMap requestedAttributes = new HashMap();
117
118
119 if ((request != null) && (request.length > 0)) {
120
121
122 for (int i = 0; i < request.length; i++) {
123 if (myAttributes.containsKey(request[i].toLowerCase())) {
124
125
126 List requestedValues = (List) myAttributes.get(request[i].toLowerCase());
127 requestedAttributes.put(request[i], requestedValues.toArray(new String[] {}));
128
129 }
130 }
131 }
132
133
134 return requestedAttributes;
135
136 }
137
138 }