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.forgerock.opendj.ldap.ByteString;
034
035import org.forgerock.i18n.slf4j.LocalizedLogger;
036import static org.opends.server.protocols.ldap.LDAPConstants.*;
037import static org.opends.server.util.ServerConstants.*;
038
039
040/**
041 * This class defines the structures and methods for an LDAP intermediate
042 * response protocol op, which is used to provide information to a client before
043 * the final response for an operation.
044 */
045public class IntermediateResponseProtocolOp
046       extends ProtocolOp
047{
048  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
049
050  /** The value for this intermediate response. */
051  private ByteString value;
052
053  /** The OID for this intermediate response. */
054  private String oid;
055
056
057
058  /**
059   * Creates a new intermediate protocol op with the specified OID and no
060   * value.
061   *
062   * @param  oid  The OID for this intermediate response.
063   */
064  public IntermediateResponseProtocolOp(String oid)
065  {
066    this.oid   = oid;
067    this.value = null;
068  }
069
070
071
072  /**
073   * Creates a new intermediate response protocol op with the specified OID and
074   * value.
075   *
076   * @param  oid    The OID for this intermediate response.
077   * @param  value  The value for this intermediate response.
078   */
079  public IntermediateResponseProtocolOp(String oid, ByteString value)
080  {
081    this.oid   = oid;
082    this.value = value;
083  }
084
085
086
087  /**
088   * Retrieves the OID for this intermediate response.
089   *
090   * @return  The OID for this intermediate response, or <CODE>null</CODE> if
091   *          there is no OID.
092   */
093  public String getOID()
094  {
095    return oid;
096  }
097
098
099  /**
100   * Retrieves the value for this intermediate response.
101   *
102   * @return  The value for this intermediate response, or <CODE>null</CODE> if
103   *          there is no value.
104   */
105  public ByteString getValue()
106  {
107    return value;
108  }
109
110
111
112  /**
113   * Retrieves the BER type for this protocol op.
114   *
115   * @return  The BER type for this protocol op.
116   */
117  public byte getType()
118  {
119    return OP_TYPE_INTERMEDIATE_RESPONSE;
120  }
121
122
123
124  /**
125   * Retrieves the name for this protocol op type.
126   *
127   * @return  The name for this protocol op type.
128   */
129  public String getProtocolOpName()
130  {
131    return "Intermediate Response";
132  }
133
134  /**
135   * Writes this protocol op to an ASN.1 output stream.
136   *
137   * @param stream The ASN.1 output stream to write to.
138   * @throws IOException If a problem occurs while writing to the stream.
139   */
140  public void write(ASN1Writer stream) throws IOException
141  {
142    stream.writeStartSequence(OP_TYPE_INTERMEDIATE_RESPONSE);
143
144    if (oid != null)
145    {
146      stream.writeOctetString(TYPE_INTERMEDIATE_RESPONSE_OID, oid);
147    }
148
149    if (value != null)
150    {
151      stream.writeOctetString(TYPE_INTERMEDIATE_RESPONSE_VALUE, value);
152    }
153
154    stream.writeEndSequence();
155  }
156
157
158
159  /**
160   * Appends a string representation of this LDAP protocol op to the provided
161   * buffer.
162   *
163   * @param  buffer  The buffer to which the string should be appended.
164   */
165  public void toString(StringBuilder buffer)
166  {
167    buffer.append("IntermediateResponse(oid=").append(oid);
168    if (value != null)
169    {
170      buffer.append(", value=").append(value);
171    }
172    buffer.append(")");
173  }
174
175
176
177  /**
178   * Appends a multi-line string representation of this LDAP protocol op to the
179   * provided buffer.
180   *
181   * @param  buffer  The buffer to which the information should be appended.
182   * @param  indent  The number of spaces from the margin that the lines should
183   *                 be indented.
184   */
185  public void toString(StringBuilder buffer, int indent)
186  {
187    StringBuilder indentBuf = new StringBuilder(indent);
188    for (int i=0 ; i < indent; i++)
189    {
190      indentBuf.append(' ');
191    }
192
193    buffer.append(indentBuf);
194    buffer.append("Intermediate Response");
195    buffer.append(EOL);
196
197    if (oid != null)
198    {
199      buffer.append(indentBuf);
200      buffer.append("  OID:  ");
201      buffer.append(oid);
202      buffer.append(EOL);
203    }
204
205    if (value != null)
206    {
207      buffer.append(indentBuf);
208      buffer.append("  Value:");
209      buffer.append(EOL);
210      buffer.append(value.toHexPlusAsciiString(indent+4));
211    }
212  }
213}
214