View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
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          // Sanity checks.
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          // Assignments.
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          // Sanity checks.
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         // "Authentication".
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         // Do we have a non-empty attribute request?
119         if ((request != null) && (request.length > 0)) {
120 
121             // Some attributes were requested.
122             for (int i = 0; i < request.length; i++) {
123                 if (myAttributes.containsKey(request[i].toLowerCase())) {
124 
125                     // Requested attribute found.
126                     List requestedValues = (List) myAttributes.get(request[i].toLowerCase());
127                     requestedAttributes.put(request[i], requestedValues.toArray(new String[] {}));
128 
129                 }
130             }
131         }
132 
133         // Return requested attributes.
134         return requestedAttributes;
135 
136     }
137 
138 }