001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.types;
028    
029    
030    
031    import java.util.ArrayList;
032    import java.util.Date;
033    import java.util.HashMap;
034    import java.util.List;
035    import java.util.Map;
036    
037    import org.opends.messages.Message;
038    import org.opends.server.core.PasswordPolicy;
039    import org.opends.server.core.PasswordPolicyState;
040    
041    import static org.opends.server.types.
042                       AccountStatusNotificationProperty.*;
043    import static org.opends.server.util.StaticUtils.*;
044    
045    
046    
047    /**
048     * This class defines a data type for storing information associated
049     * with an account status notification.
050     */
051    @org.opends.server.types.PublicAPI(
052         stability=org.opends.server.types.StabilityLevel.VOLATILE,
053         mayInstantiate=false,
054         mayExtend=false,
055         mayInvoke=true)
056    public final class AccountStatusNotification
057    {
058      // The notification type for this account status notification.
059      private AccountStatusNotificationType notificationType;
060    
061      // The entry for the user to whom this notification applies.
062      private Entry userEntry;
063    
064      // A set of additional properties that may be useful for this
065      // notification.
066      private Map<AccountStatusNotificationProperty,List<String>>
067                   notificationProperties;
068    
069      // A message that provides additional information for this account
070      // status notification.
071      private Message message;
072    
073    
074    
075      /**
076       * Creates a new account status notification object with the
077       * provided information.
078       *
079       * @param  notificationType        The type for this account status
080       *                                 notification.
081       * @param  userEntry               The entry for the user to whom
082       *                                 this notification applies.
083       * @param  message                 The human-readable message for
084       *                                 this notification.
085       * @param  notificationProperties  A set of properties that may
086       *                                 include additional information
087       *                                 about this notification.
088       */
089      public AccountStatusNotification(
090                  AccountStatusNotificationType notificationType,
091                  Entry userEntry, Message message,
092                  Map<AccountStatusNotificationProperty,List<String>>
093                       notificationProperties)
094      {
095        this.notificationType = notificationType;
096        this.userEntry        = userEntry;
097        this.message          = message;
098    
099        if (notificationProperties == null)
100        {
101          this.notificationProperties =
102               new HashMap<AccountStatusNotificationProperty,
103                           List<String>>(0);
104        }
105        else
106        {
107          this.notificationProperties = notificationProperties;
108        }
109      }
110    
111    
112    
113      /**
114       * Retrieves the notification type for this account status
115       * notification.
116       *
117       * @return  The notification type for this account status
118       *          notification.
119       */
120      public AccountStatusNotificationType getNotificationType()
121      {
122        return notificationType;
123      }
124    
125    
126    
127      /**
128       * Retrieves the DN of the user entry to which this notification
129       * applies.
130       *
131       * @return  The DN of the user entry to which this notification
132       *          applies.
133       */
134      public DN getUserDN()
135      {
136        return userEntry.getDN();
137      }
138    
139    
140    
141      /**
142       * Retrieves user entry for whom this notification applies.
143       *
144       * @return  The user entry for whom this notification applies.
145       */
146      public Entry getUserEntry()
147      {
148        return userEntry;
149      }
150    
151    
152    
153      /**
154       * Retrieves a message that provides additional information for this
155       * account status notification.
156       *
157       * @return  A message that provides additional information for this
158       *          account status notification.
159       */
160      public Message getMessage()
161      {
162        return message;
163      }
164    
165    
166    
167      /**
168       * Retrieves a set of properties that may provide additional
169       * information for this account status notification.
170       *
171       * @return  A set of properties that may provide additional
172       *          information for this account status notification.
173       */
174      public Map<AccountStatusNotificationProperty,List<String>>
175                  getNotificationProperties()
176      {
177        return notificationProperties;
178      }
179    
180    
181    
182      /**
183       * Retrieves the set of values for the specified account status
184       * notification property.
185       *
186       * @param  property  The account status notification property for
187       *                   which to retrieve the associated values.
188       *
189       * @return  The set of values for the specified account status
190       *          notification property, or {@code null} if the specified
191       *          property is not defined for this account status
192       *          notification.
193       */
194      public List<String> getNotificationProperty(
195                               AccountStatusNotificationProperty property)
196      {
197        return notificationProperties.get(property);
198      }
199    
200    
201    
202      /**
203       * Creates a set of account status notification properties from the
204       * provided information.
205       *
206       * @param  pwPolicyState     The password policy state for the user
207       *                           associated with the notification.
208       * @param  tempLocked        Indicates whether the user's account
209       *                           has been temporarily locked.
210       * @param  timeToExpiration  The length of time in seconds until the
211       *                           user's password expires, or -1 if it's
212       *                           not about to expire.
213       * @param  oldPasswords      The set of old passwords for the user,
214       *                           or {@code null} if this is not
215       *                           applicable.
216       * @param  newPasswords      The set of new passwords for the user,
217       *                           or {@code null} if this is not
218       *                           applicable.
219       *
220       * @return  The created set of account status notification
221       *          properties.
222       */
223      @org.opends.server.types.PublicAPI(
224           stability=org.opends.server.types.StabilityLevel.PRIVATE,
225           mayInstantiate=false,
226           mayExtend=false,
227           mayInvoke=false)
228      public static Map<AccountStatusNotificationProperty,List<String>>
229                         createProperties(
230                              PasswordPolicyState pwPolicyState,
231                              boolean tempLocked, int timeToExpiration,
232                              List<AttributeValue> oldPasswords,
233                              List<AttributeValue> newPasswords)
234      {
235        HashMap<AccountStatusNotificationProperty,List<String>> props =
236             new HashMap<AccountStatusNotificationProperty,
237                         List<String>>(4);
238    
239        PasswordPolicy policy = pwPolicyState.getPolicy();
240    
241        ArrayList<String> propList = new ArrayList<String>(1);
242        propList.add(policy.getConfigEntryDN().toString());
243        props.put(PASSWORD_POLICY_DN, propList);
244    
245        if (tempLocked)
246        {
247          int secondsUntilUnlock = policy.getLockoutDuration();
248          if (secondsUntilUnlock > 0)
249          {
250            propList = new ArrayList<String>(1);
251            propList.add(String.valueOf(secondsUntilUnlock));
252            props.put(SECONDS_UNTIL_UNLOCK, propList);
253    
254            propList = new ArrayList<String>(1);
255            propList.add(
256                 secondsToTimeString(secondsUntilUnlock).toString());
257            props.put(TIME_UNTIL_UNLOCK, propList);
258    
259            long unlockTime = System.currentTimeMillis() +
260                              (1000*secondsUntilUnlock);
261            propList = new ArrayList<String>(1);
262            propList.add(new Date(unlockTime).toString());
263            props.put(ACCOUNT_UNLOCK_TIME, propList);
264          }
265        }
266    
267        if (timeToExpiration >= 0)
268        {
269            propList = new ArrayList<String>(1);
270            propList.add(String.valueOf(timeToExpiration));
271            props.put(SECONDS_UNTIL_EXPIRATION, propList);
272    
273            propList = new ArrayList<String>(1);
274            propList.add(
275                 secondsToTimeString(timeToExpiration).toString());
276            props.put(TIME_UNTIL_EXPIRATION, propList);
277    
278            long expTime = System.currentTimeMillis() +
279                           (1000*timeToExpiration);
280            propList = new ArrayList<String>(1);
281            propList.add(new Date(expTime).toString());
282            props.put(PASSWORD_EXPIRATION_TIME, propList);
283        }
284    
285        if ((oldPasswords != null) && (! oldPasswords.isEmpty()))
286        {
287          propList = new ArrayList<String>(oldPasswords.size());
288          for (AttributeValue v : oldPasswords)
289          {
290            propList.add(v.getStringValue());
291          }
292    
293          props.put(OLD_PASSWORD, propList);
294        }
295    
296        if ((newPasswords != null) && (! newPasswords.isEmpty()))
297        {
298          propList = new ArrayList<String>(newPasswords.size());
299          for (AttributeValue v : newPasswords)
300          {
301            propList.add(v.getStringValue());
302          }
303    
304          props.put(NEW_PASSWORD, propList);
305        }
306    
307        return props;
308      }
309    
310    
311    
312      /**
313       * Retrieves a string representation of this account status
314       * notification.
315       *
316       * @return  A string representation of this account status
317       *          notification.
318       */
319      public String toString()
320      {
321        return "AccountStatusNotification(type=" +
322               notificationType.getName() + ",dn=" + userEntry.getDN() +
323               ",message=" + message + ")";
324      }
325    }
326