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-2016 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  /** ControlDecoder implementation to decode this control from a ByteString. */
043  private static final class Decoder
044      implements ControlDecoder<PasswordExpiredControl>
045  {
046    @Override
047    public PasswordExpiredControl decode(boolean isCritical, ByteString value)
048        throws DirectoryException
049    {
050      if (value != null)
051      {
052        try
053        {
054          Integer.parseInt(value.toString());
055        }
056        catch (Exception e)
057        {
058          LocalizableMessage message = ERR_PWEXPIRED_CONTROL_INVALID_VALUE.get();
059          throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
060        }
061      }
062
063      return new PasswordExpiredControl(isCritical);
064    }
065
066    @Override
067    public String getOID()
068    {
069      return OID_NS_PASSWORD_EXPIRED;
070    }
071
072  }
073
074  /** The Control Decoder that can be used to decode this control. */
075  public static final ControlDecoder<PasswordExpiredControl> DECODER =
076    new Decoder();
077
078  /** Creates a new instance of the password expired control with the default settings. */
079  public PasswordExpiredControl()
080  {
081    this(false);
082  }
083
084  /**
085   * Creates a new instance of the password expired control with the provided
086   * information.
087   *
088   * @param  isCritical  Indicates whether support for this control should be
089   *                     considered a critical part of the client processing.
090   */
091  public PasswordExpiredControl(boolean isCritical)
092  {
093    super(OID_NS_PASSWORD_EXPIRED, isCritical);
094  }
095
096  @Override
097  public void writeValue(ASN1Writer writer) throws IOException {
098    writer.writeOctetString("0");
099  }
100
101
102
103  /**
104   * Appends a string representation of this password expired control to the
105   * provided buffer.
106   *
107   * @param  buffer  The buffer to which the information should be appended.
108   */
109  @Override
110  public void toString(StringBuilder buffer)
111  {
112    buffer.append("PasswordExpiredControl()");
113  }
114}
115