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