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