001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2015 ForgeRock AS.
016 */
017package org.opends.server.controls;
018import org.forgerock.i18n.LocalizableMessage;
019
020
021
022import org.forgerock.opendj.io.ASN1Writer;
023import org.opends.server.types.*;
024import org.forgerock.opendj.ldap.ResultCode;
025import org.forgerock.opendj.ldap.ByteString;
026import org.forgerock.i18n.slf4j.LocalizedLogger;
027import static org.opends.messages.ProtocolMessages.*;
028import static org.opends.server.util.ServerConstants.*;
029import static org.opends.server.util.StaticUtils.*;
030
031import java.io.IOException;
032
033
034/**
035 * This class implements the Netscape password expiring control, which serves as
036 * a warning to clients that the user's password is about to expire. The only
037 * element contained in the control value is a string representation of the
038 * number of seconds until expiration.
039 */
040public class PasswordExpiringControl
041       extends Control
042{
043  /**
044   * ControlDecoder implementation to decode this control from a ByteString.
045   */
046  private static final class Decoder
047      implements ControlDecoder<PasswordExpiringControl>
048  {
049    /** {@inheritDoc} */
050    public PasswordExpiringControl decode(boolean isCritical, ByteString value)
051        throws DirectoryException
052    {
053      if (value == null)
054      {
055        LocalizableMessage message = ERR_PWEXPIRING_NO_CONTROL_VALUE.get();
056        throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
057      }
058
059      int secondsUntilExpiration;
060      try
061      {
062        secondsUntilExpiration =
063            Integer.parseInt(value.toString());
064      }
065      catch (Exception e)
066      {
067        logger.traceException(e);
068
069        LocalizableMessage message = ERR_PWEXPIRING_CANNOT_DECODE_SECONDS_UNTIL_EXPIRATION.
070            get(getExceptionMessage(e));
071        throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
072      }
073
074
075      return new PasswordExpiringControl(isCritical,
076          secondsUntilExpiration);
077    }
078
079    public String getOID()
080    {
081      return OID_NS_PASSWORD_EXPIRING;
082    }
083
084  }
085
086  /**
087   * The Control Decoder that can be used to decode this control.
088   */
089  public static final ControlDecoder<PasswordExpiringControl> DECODER =
090    new Decoder();
091  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
092
093
094
095
096  /** The length of time in seconds until the password actually expires. */
097  private int secondsUntilExpiration;
098
099
100
101  /**
102   * Creates a new instance of the password expiring control with the provided
103   * information.
104   *
105   * @param  secondsUntilExpiration  The length of time in seconds until the
106   *                                 password actually expires.
107   */
108  public PasswordExpiringControl(int secondsUntilExpiration)
109  {
110    this(false, secondsUntilExpiration);
111  }
112
113
114
115  /**
116   * Creates a new instance of the password expiring control with the provided
117   * information.
118   *
119   * @param  isCritical              Indicates whether support for this control
120   *                                 should be considered a critical part of the
121   *                                 client processing.
122   * @param  secondsUntilExpiration  The length of time in seconds until the
123   *                                 password actually expires.
124   */
125  public PasswordExpiringControl(boolean isCritical, int secondsUntilExpiration)
126  {
127    super(OID_NS_PASSWORD_EXPIRING, isCritical);
128
129
130    this.secondsUntilExpiration = secondsUntilExpiration;
131  }
132
133
134  /**
135   * Writes this control's value to an ASN.1 writer. The value (if any) must be
136   * written as an ASN1OctetString.
137   *
138   * @param writer The ASN.1 output stream to write to.
139   * @throws IOException If a problem occurs while writing to the stream.
140   */
141  @Override
142  public void writeValue(ASN1Writer writer) throws IOException {
143    writer.writeOctetString(String.valueOf(secondsUntilExpiration));
144  }
145
146
147
148  /**
149   * Retrieves the length of time in seconds until the password actually
150   * expires.
151   *
152   * @return  The length of time in seconds until the password actually expires.
153   */
154  public int getSecondsUntilExpiration()
155  {
156    return secondsUntilExpiration;
157  }
158
159
160
161  /**
162   * Appends a string representation of this password expiring control to the
163   * provided buffer.
164   *
165   * @param  buffer  The buffer to which the information should be appended.
166   */
167  @Override
168  public void toString(StringBuilder buffer)
169  {
170    buffer.append("PasswordExpiringControl(secondsUntilExpiration=");
171    buffer.append(secondsUntilExpiration);
172    buffer.append(")");
173  }
174}
175