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-2015 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  /**
040   * ControlDecoder implementation to decode this control from a ByteString.
041   */
042  private static final class Decoder
043      implements ControlDecoder<SubentriesControl>
044  {
045    /** {@inheritDoc} */
046    public SubentriesControl decode(boolean isCritical, ByteString value)
047        throws DirectoryException
048    {
049      if (value == null)
050      {
051        LocalizableMessage message = ERR_SUBENTRIES_NO_CONTROL_VALUE.get();
052        throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message);
053      }
054
055      ASN1Reader reader = ASN1.getReader(value);
056      boolean visibility;
057      try
058      {
059        visibility = reader.readBoolean();
060      }
061      catch (Exception e)
062      {
063        logger.traceException(e);
064
065        LocalizableMessage message =
066            ERR_SUBENTRIES_CANNOT_DECODE_VALUE.get(getExceptionMessage(e));
067        throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message, e);
068      }
069
070      return new SubentriesControl(isCritical, visibility);
071    }
072
073    public String getOID()
074    {
075      return OID_LDAP_SUBENTRIES;
076    }
077
078  }
079
080  /**
081   * The Control Decoder that can be used to decode this control.
082   */
083  public static final ControlDecoder<SubentriesControl> DECODER =
084    new Decoder();
085  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
086
087  /** The visibility from the control value. */
088  private boolean visibility;
089
090  /**
091   * Creates a new instance of the Subentries Control with the provided
092   * information.
093   *
094   * @param  isCritical       Indicates whether support for this control
095   *                          should be considered a critical part of the
096   *                          server processing.
097   * @param  visibility       The visibility flag from the control value.
098   */
099  public SubentriesControl(boolean isCritical, boolean visibility)
100  {
101    super(OID_LDAP_SUBENTRIES, isCritical);
102    this.visibility = visibility;
103  }
104
105  /**
106   * Writes this control's value to an ASN.1 writer. The value must be
107   * written as an ASN1OctetString.
108   *
109   * @param writer The ASN.1 writer to use.
110   * @throws IOException If a problem occurs while writing to the stream.
111   */
112  @Override
113  protected void writeValue(ASN1Writer writer) throws IOException
114  {
115    writer.writeStartSequence(ASN1.UNIVERSAL_OCTET_STRING_TYPE);
116    writer.writeBoolean(visibility);
117    writer.writeEndSequence();
118  }
119
120  /**
121   * Retrieves the visibility for this Subentries Control.
122   *
123   * @return  The visibility for this Subentries Control.
124   */
125  public boolean getVisibility()
126  {
127    return visibility;
128  }
129
130  /**
131   * Appends a string representation of this Subentries Control to the
132   * provided buffer.
133   *
134   * @param  buffer  The buffer to which the information should be appended.
135   */
136  @Override
137  public void toString(StringBuilder buffer)
138  {
139    buffer.append("SubentriesControl(visibility=\"");
140    buffer.append(visibility);
141    buffer.append("\")");
142  }
143}