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