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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.controls;
028import org.forgerock.i18n.LocalizableMessage;
029
030
031
032import org.forgerock.opendj.io.ASN1Writer;
033import org.forgerock.opendj.ldap.ByteString;
034import org.opends.server.types.Control;
035import org.opends.server.types.DirectoryException;
036import org.forgerock.opendj.ldap.ResultCode;
037
038import static org.opends.messages.ProtocolMessages.*;
039import static org.opends.server.util.ServerConstants.*;
040
041import java.io.IOException;
042
043
044/**
045 * This class implements the Netscape password expired control.  The value for
046 * this control should be a string that indicates the length of time until the
047 * password expires, but because it is already expired it will always be "0".
048 */
049public class PasswordExpiredControl
050       extends Control
051{
052  /**
053   * ControlDecoder implementation to decode this control from a ByteString.
054   */
055  private static final class Decoder
056      implements ControlDecoder<PasswordExpiredControl>
057  {
058    /** {@inheritDoc} */
059    public PasswordExpiredControl decode(boolean isCritical, ByteString value)
060        throws DirectoryException
061    {
062      if (value != null)
063      {
064        try
065        {
066          Integer.parseInt(value.toString());
067        }
068        catch (Exception e)
069        {
070          LocalizableMessage message = ERR_PWEXPIRED_CONTROL_INVALID_VALUE.get();
071          throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
072        }
073      }
074
075      return new PasswordExpiredControl(isCritical);
076    }
077
078    public String getOID()
079    {
080      return OID_NS_PASSWORD_EXPIRED;
081    }
082
083  }
084
085  /**
086   * The Control Decoder that can be used to decode this control.
087   */
088  public static final ControlDecoder<PasswordExpiredControl> DECODER =
089    new Decoder();
090
091  /**
092   * Creates a new instance of the password expired control with the default
093   * settings.
094   */
095  public PasswordExpiredControl()
096  {
097    this(false);
098  }
099
100  /**
101   * Creates a new instance of the password expired control with the provided
102   * information.
103   *
104   * @param  isCritical  Indicates whether support for this control should be
105   *                     considered a critical part of the client processing.
106   */
107  public PasswordExpiredControl(boolean isCritical)
108  {
109    super(OID_NS_PASSWORD_EXPIRED, isCritical);
110  }
111
112  /**
113   * Writes this control's value to an ASN.1 writer. The value (if any) must be
114   * written as an ASN1OctetString.
115   *
116   * @param writer The ASN.1 output stream to write to.
117   * @throws IOException If a problem occurs while writing to the stream.
118   */
119  @Override
120  public void writeValue(ASN1Writer writer) throws IOException {
121    writer.writeOctetString("0");
122  }
123
124
125
126  /**
127   * Appends a string representation of this password expired control to the
128   * provided buffer.
129   *
130   * @param  buffer  The buffer to which the information should be appended.
131   */
132  @Override
133  public void toString(StringBuilder buffer)
134  {
135    buffer.append("PasswordExpiredControl()");
136  }
137}
138