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.types;
018
019import java.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022
023import org.forgerock.opendj.ldap.ByteString;
024
025/**
026 * This class defines a data structure for holding information that
027 * may be sent to the client in the form of an intermediate response.
028 * It may contain an OID, value, and/or set of controls.
029 */
030@org.opends.server.types.PublicAPI(
031     stability=org.opends.server.types.StabilityLevel.VOLATILE,
032     mayInstantiate=true,
033     mayInvoke=true)
034public final class IntermediateResponse
035{
036  /** The value for this intermediate response. */
037  private ByteString value;
038  /** The set of controls for this intermediate response. */
039  private final List<Control> controls;
040  /** The operation with which this intermediate response is associated. */
041  private final Operation operation;
042  /** The OID for this intermediate response. */
043  private String oid;
044
045  /**
046   * Creates a new intermediate response with the provided
047   * information.
048   *
049   * @param  operation  The operation with which this intermediate
050   *                    response is associated.
051   * @param  oid        The OID for this intermediate response.
052   * @param  value      The value for this intermediate response.
053   * @param  controls   The set of controls to for this intermediate
054   *                    response.
055   */
056  public IntermediateResponse(Operation operation, String oid,
057                              ByteString value,
058                              List<Control> controls)
059  {
060    this.operation = operation;
061    this.oid       = oid;
062    this.value     = value;
063
064    if (controls != null)
065    {
066      this.controls = controls;
067    }
068    else
069    {
070      this.controls = new ArrayList<>(0);
071    }
072  }
073
074  /**
075   * Retrieves the operation with which this intermediate response
076   * message is associated.
077   *
078   * @return  The operation with which this intermediate response
079   *          message is associated.
080   */
081  public Operation getOperation()
082  {
083    return operation;
084  }
085
086  /**
087   * Retrieves the OID for this intermediate response.
088   *
089   * @return  The OID for this intermediate response, or
090   *          <CODE>null</CODE> if there is none.
091   */
092  public String getOID()
093  {
094    return oid;
095  }
096
097  /**
098   * Specifies the OID for this intermediate response.
099   *
100   * @param  oid  The OID for this intermediate response.
101   */
102  public void setOID(String oid)
103  {
104    this.oid = oid;
105  }
106
107  /**
108   * Retrieves the value for this intermediate response.
109   *
110   * @return  The value for this intermediate response, or
111   *          <CODE>null</CODE> if there is none.
112   */
113  public ByteString getValue()
114  {
115    return value;
116  }
117
118  /**
119   * Specifies the value for this intermediate response.
120   *
121   * @param  value  The value for this intermediate response.
122   */
123  public void setValue(ByteString value)
124  {
125    this.value = value;
126  }
127
128  /**
129   * Retrieves the set of controls for this intermediate response.
130   * The contents of the list may be altered by intermediate response
131   * plugins.
132   *
133   * @return  The set of controls for this intermediate response.
134   */
135  public List<Control> getControls()
136  {
137    return controls;
138  }
139
140  /**
141   * Retrieves a string representation of this intermediate response.
142   *
143   * @return  A string representation of this intermediate response.
144   */
145  @Override
146  public String toString()
147  {
148    StringBuilder buffer = new StringBuilder();
149    toString(buffer);
150    return buffer.toString();
151  }
152
153  /**
154   * Appends a string representation of this intermediate response to
155   * the provided buffer.
156   *
157   * @param  buffer  The buffer to which the information should be
158   *                 appended.
159   */
160  private void toString(StringBuilder buffer)
161  {
162    buffer.append("IntermediateResponse(operation=");
163    operation.toString(buffer);
164    buffer.append(",oid=").append(oid);
165    buffer.append(",value=").append(buffer);
166
167    if (! controls.isEmpty())
168    {
169      buffer.append(",controls={");
170
171      Iterator<Control> iterator = controls.iterator();
172      iterator.next().toString(buffer);
173
174      while (iterator.hasNext())
175      {
176        buffer.append(",");
177        iterator.next().toString(buffer);
178      }
179
180      buffer.append("}");
181    }
182
183    buffer.append(")");
184  }
185}