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.protocols.ldap;
028
029
030import java.io.IOException;
031
032import org.forgerock.opendj.io.*;
033import org.opends.server.types.Control;
034import org.forgerock.opendj.ldap.ByteString;
035
036import static org.opends.server.util.ServerConstants.*;
037
038
039/**
040 * This class defines the data structures and methods to use when interacting
041 * with a generic LDAP control.
042 */
043public class LDAPControl extends Control
044{
045  /** The control value. */
046  private ByteString value;
047
048
049
050  /**
051   * Creates a new LDAP control with the specified OID.  It will not be
052   * critical, and will not have a value.
053   *
054   * @param  oid  The OID for this LDAP control.
055   */
056  public LDAPControl(String oid)
057  {
058    super(oid, false);
059  }
060
061
062
063  /**
064   * Creates a new LDAP control with the specified OID and criticality.  It will
065   * not have a value.
066   *
067   * @param  oid         The OID for this LDAP control.
068   * @param  isCritical  Indicates whether this control should be considered
069   *                     critical.
070   */
071  public LDAPControl(String oid, boolean isCritical)
072  {
073    super(oid, isCritical);
074  }
075
076
077
078  /**
079   * Creates a new LDAP control with the specified OID, criticality, and value.
080   *
081   * @param  oid         The OID for this LDAP control.
082   * @param  isCritical  Indicates whether this control should be considered
083   *                     critical.
084   * @param  value       The value for this LDAP control.
085   */
086  public LDAPControl(String oid, boolean isCritical, ByteString value)
087  {
088    super(oid, isCritical);
089    this.value = value;
090  }
091
092
093  /**
094   * Retrieves the value for this control.
095   *
096   * @return  The value for this control, or <CODE>null</CODE> if
097   *          there is no value.
098   */
099  public final ByteString getValue()
100  {
101    return value;
102  }
103
104
105
106  /**
107   * Indicates whether this control has a value.
108   *
109   * @return  <CODE>true</CODE> if this control has a value, or
110   *          <CODE>false</CODE> if it does not.
111   */
112  public final boolean hasValue()
113  {
114    return value != null;
115  }
116
117  /** {@inheritDoc} */
118  public void writeValue(ASN1Writer stream) throws IOException
119  {
120    if (value != null)
121    {
122      stream.writeOctetString(value);
123    }
124  }
125
126
127
128  /**
129   * Appends a string representation of this LDAP control to the provided
130   * buffer.
131   *
132   * @param  buffer  The buffer to which the information should be appended.
133   */
134  public void toString(StringBuilder buffer)
135  {
136    buffer.append("LDAPControl(oid=");
137    buffer.append(getOID());
138    buffer.append(", criticality=");
139    buffer.append(isCritical());
140
141    if (value != null)
142    {
143      buffer.append(", value=");
144      buffer.append(value.toHexPlusAsciiString(4));
145    }
146
147    buffer.append(")");
148  }
149
150
151
152  /**
153   * Appends a multi-line string representation of this LDAP control to the
154   * provided buffer.
155   *
156   * @param  buffer  The buffer to which the information should be appended.
157   * @param  indent  The number of spaces to indent the information.
158   */
159  public void toString(StringBuilder buffer, int indent)
160  {
161    StringBuilder indentBuf = new StringBuilder(indent);
162    for (int i=0 ; i < indent; i++)
163    {
164      indentBuf.append(' ');
165    }
166
167    buffer.append(indentBuf);
168    buffer.append("LDAP Control");
169    buffer.append(EOL);
170
171    buffer.append(indentBuf);
172    buffer.append("  OID:  ");
173    buffer.append(getOID());
174    buffer.append(EOL);
175
176    buffer.append(indentBuf);
177    buffer.append("  Criticality:  ");
178    buffer.append(isCritical());
179    buffer.append(EOL);
180
181    if (value != null)
182    {
183      buffer.append(indentBuf);
184      buffer.append("  Value:");
185      buffer.append(value.toHexPlusAsciiString(indent+4));
186    }
187  }
188}
189