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