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.forgerock.opendj.ldap.ByteString;
024import org.opends.server.types.Control;
025import org.opends.server.types.DirectoryException;
026import org.forgerock.opendj.ldap.ResultCode;
027
028import static org.opends.messages.ProtocolMessages.*;
029import static org.opends.server.util.ServerConstants.*;
030
031import java.io.IOException;
032
033
034/**
035 * This class implements the Netscape password expired control.  The value for
036 * this control should be a string that indicates the length of time until the
037 * password expires, but because it is already expired it will always be "0".
038 */
039public class PasswordExpiredControl
040       extends Control
041{
042  /**
043   * ControlDecoder implementation to decode this control from a ByteString.
044   */
045  private static final class Decoder
046      implements ControlDecoder<PasswordExpiredControl>
047  {
048    /** {@inheritDoc} */
049    public PasswordExpiredControl decode(boolean isCritical, ByteString value)
050        throws DirectoryException
051    {
052      if (value != null)
053      {
054        try
055        {
056          Integer.parseInt(value.toString());
057        }
058        catch (Exception e)
059        {
060          LocalizableMessage message = ERR_PWEXPIRED_CONTROL_INVALID_VALUE.get();
061          throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
062        }
063      }
064
065      return new PasswordExpiredControl(isCritical);
066    }
067
068    public String getOID()
069    {
070      return OID_NS_PASSWORD_EXPIRED;
071    }
072
073  }
074
075  /**
076   * The Control Decoder that can be used to decode this control.
077   */
078  public static final ControlDecoder<PasswordExpiredControl> DECODER =
079    new Decoder();
080
081  /**
082   * Creates a new instance of the password expired control with the default
083   * settings.
084   */
085  public PasswordExpiredControl()
086  {
087    this(false);
088  }
089
090  /**
091   * Creates a new instance of the password expired control with the provided
092   * information.
093   *
094   * @param  isCritical  Indicates whether support for this control should be
095   *                     considered a critical part of the client processing.
096   */
097  public PasswordExpiredControl(boolean isCritical)
098  {
099    super(OID_NS_PASSWORD_EXPIRED, isCritical);
100  }
101
102  /**
103   * Writes this control's value to an ASN.1 writer. The value (if any) must be
104   * written as an ASN1OctetString.
105   *
106   * @param writer The ASN.1 output stream to write to.
107   * @throws IOException If a problem occurs while writing to the stream.
108   */
109  @Override
110  public void writeValue(ASN1Writer writer) throws IOException {
111    writer.writeOctetString("0");
112  }
113
114
115
116  /**
117   * Appends a string representation of this password expired control to the
118   * provided buffer.
119   *
120   * @param  buffer  The buffer to which the information should be appended.
121   */
122  @Override
123  public void toString(StringBuilder buffer)
124  {
125    buffer.append("PasswordExpiredControl()");
126  }
127}
128