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 2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.controls;
028import org.forgerock.i18n.LocalizableMessage;
029
030import java.io.IOException;
031
032import org.forgerock.opendj.io.*;
033import org.forgerock.i18n.slf4j.LocalizedLogger;
034import org.opends.server.types.*;
035import org.forgerock.opendj.ldap.ResultCode;
036import org.forgerock.opendj.ldap.ByteString;
037import static org.opends.messages.ProtocolMessages.*;
038import static org.opends.server.util.ServerConstants.*;
039import static org.opends.server.util.StaticUtils.*;
040
041/**
042 * This class implements Subentries Control as defined in RFC 3672. It makes
043 * it possible to control the visibility of entries and subentries which are
044 * within scope of specific operation.
045 */
046public class SubentriesControl
047       extends Control
048{
049  /**
050   * ControlDecoder implementation to decode this control from a ByteString.
051   */
052  private static final class Decoder
053      implements ControlDecoder<SubentriesControl>
054  {
055    /** {@inheritDoc} */
056    public SubentriesControl decode(boolean isCritical, ByteString value)
057        throws DirectoryException
058    {
059      if (value == null)
060      {
061        LocalizableMessage message = ERR_SUBENTRIES_NO_CONTROL_VALUE.get();
062        throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
063      }
064
065      ASN1Reader reader = ASN1.getReader(value);
066      boolean visibility;
067      try
068      {
069        visibility = reader.readBoolean();
070      }
071      catch (Exception e)
072      {
073        logger.traceException(e);
074
075        LocalizableMessage message =
076            ERR_SUBENTRIES_CANNOT_DECODE_VALUE.get(getExceptionMessage(e));
077        throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message, e);
078      }
079
080      return new SubentriesControl(isCritical, visibility);
081    }
082
083    public String getOID()
084    {
085      return OID_LDAP_SUBENTRIES;
086    }
087
088  }
089
090  /**
091   * The Control Decoder that can be used to decode this control.
092   */
093  public static final ControlDecoder<SubentriesControl> DECODER =
094    new Decoder();
095  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
096
097  /** The visibility from the control value. */
098  private boolean visibility;
099
100  /**
101   * Creates a new instance of the Subentries Control with the provided
102   * information.
103   *
104   * @param  isCritical       Indicates whether support for this control
105   *                          should be considered a critical part of the
106   *                          server processing.
107   * @param  visibility       The visibility flag from the control value.
108   */
109  public SubentriesControl(boolean isCritical, boolean visibility)
110  {
111    super(OID_LDAP_SUBENTRIES, isCritical);
112    this.visibility = visibility;
113  }
114
115  /**
116   * Writes this control's value to an ASN.1 writer. The value must be
117   * written as an ASN1OctetString.
118   *
119   * @param writer The ASN.1 writer to use.
120   * @throws IOException If a problem occurs while writing to the stream.
121   */
122  @Override
123  protected void writeValue(ASN1Writer writer) throws IOException
124  {
125    writer.writeStartSequence(ASN1.UNIVERSAL_OCTET_STRING_TYPE);
126    writer.writeBoolean(visibility);
127    writer.writeEndSequence();
128  }
129
130  /**
131   * Retrieves the visibility for this Subentries Control.
132   *
133   * @return  The visibility for this Subentries Control.
134   */
135  public boolean getVisibility()
136  {
137    return visibility;
138  }
139
140  /**
141   * Appends a string representation of this Subentries Control to the
142   * provided buffer.
143   *
144   * @param  buffer  The buffer to which the information should be appended.
145   */
146  @Override
147  public void toString(StringBuilder buffer)
148  {
149    buffer.append("SubentriesControl(visibility=\"");
150    buffer.append(visibility);
151    buffer.append("\")");
152  }
153}