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;
020import java.util.List;
021
022import org.forgerock.i18n.LocalizableMessage;
023import org.forgerock.i18n.slf4j.LocalizedLogger;
024import org.forgerock.opendj.io.ASN1Writer;
025import org.forgerock.util.Utils;
026import org.forgerock.opendj.ldap.DN;
027
028import static org.opends.server.protocols.ldap.LDAPConstants.*;
029import static org.opends.server.util.ServerConstants.*;
030
031/**
032 * This class defines the structures and methods for an LDAP delete response
033 * protocol op, which is used to provide information about the result of
034 * processing a delete request.
035 */
036public class DeleteResponseProtocolOp
037       extends ProtocolOp
038{
039  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
040
041  /** The matched DN for this response. */
042  private DN matchedDN;
043  /** The result code for this response. */
044  private int resultCode;
045  /** The set of referral URLs for this response. */
046  private List<String> referralURLs;
047  /** The error message for this response. */
048  private LocalizableMessage errorMessage;
049
050  /**
051   * Creates a new delete response protocol op with the provided result code.
052   *
053   * @param  resultCode  The result code for this response.
054   */
055  public DeleteResponseProtocolOp(int resultCode)
056  {
057    this.resultCode = resultCode;
058  }
059
060  /**
061   * Creates a new delete response protocol op with the provided result code and
062   * error message.
063   *
064   * @param  resultCode    The result code for this response.
065   * @param  errorMessage  The error message for this response.
066   */
067  public DeleteResponseProtocolOp(int resultCode, LocalizableMessage errorMessage)
068  {
069    this.resultCode   = resultCode;
070    this.errorMessage = errorMessage;
071  }
072
073  /**
074   * Creates a new delete response protocol op with the provided information.
075   *
076   * @param  resultCode    The result code for this response.
077   * @param  errorMessage  The error message for this response.
078   * @param  matchedDN     The matched DN for this response.
079   * @param  referralURLs  The referral URLs for this response.
080   */
081  public DeleteResponseProtocolOp(int resultCode, LocalizableMessage errorMessage,
082                                  DN matchedDN, List<String> referralURLs)
083  {
084    this.resultCode   = resultCode;
085    this.errorMessage = errorMessage;
086    this.matchedDN    = matchedDN;
087    this.referralURLs = referralURLs;
088  }
089
090  /**
091   * Retrieves the result code for this response.
092   *
093   * @return  The result code for this response.
094   */
095  public int getResultCode()
096  {
097    return resultCode;
098  }
099
100  /**
101   * Retrieves the error message for this response.
102   *
103   * @return  The error message for this response, or <CODE>null</CODE> if none
104   *          is available.
105   */
106  public LocalizableMessage getErrorMessage()
107  {
108    return errorMessage;
109  }
110
111  /**
112   * Retrieves the matched DN for this response.
113   *
114   * @return  The matched DN for this response, or <CODE>null</CODE> if none is
115   *          available.
116   */
117  public DN getMatchedDN()
118  {
119    return matchedDN;
120  }
121
122  /**
123   * Retrieves the set of referral URLs for this response.
124   *
125   * @return  The set of referral URLs for this response, or <CODE>null</CODE>
126   *          if none are available.
127   */
128  public List<String> getReferralURLs()
129  {
130    return referralURLs;
131  }
132
133  @Override
134  public byte getType()
135  {
136    return OP_TYPE_DELETE_RESPONSE;
137  }
138
139  @Override
140  public String getProtocolOpName()
141  {
142    return "Delete Response";
143  }
144
145  @Override
146  public void write(ASN1Writer stream) throws IOException
147  {
148    stream.writeStartSequence(OP_TYPE_DELETE_RESPONSE);
149    stream.writeEnumerated(resultCode);
150
151    if(matchedDN == null)
152    {
153      stream.writeOctetString((String)null);
154    }
155    else
156    {
157      stream.writeOctetString(matchedDN.toString());
158    }
159
160    if(errorMessage == null)
161    {
162      stream.writeOctetString((String)null);
163    }
164    else
165    {
166      stream.writeOctetString(errorMessage.toString());
167    }
168
169    if (referralURLs != null && !referralURLs.isEmpty())
170    {
171      stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE);
172      for (String s : referralURLs)
173      {
174        stream.writeOctetString(s);
175      }
176      stream.writeEndSequence();
177    }
178
179    stream.writeEndSequence();
180  }
181
182  @Override
183  public void toString(StringBuilder buffer)
184  {
185    buffer.append("DeleteResponse(resultCode=");
186    buffer.append(resultCode);
187
188    if (errorMessage != null && errorMessage.length() > 0)
189    {
190      buffer.append(", errorMessage=");
191      buffer.append(errorMessage);
192    }
193    if (matchedDN != null)
194    {
195      buffer.append(", matchedDN=");
196      buffer.append(matchedDN);
197    }
198    if (referralURLs != null && !referralURLs.isEmpty())
199    {
200      buffer.append(", referralURLs={");
201      Utils.joinAsString(buffer, ", ", referralURLs);
202      buffer.append("}");
203    }
204
205    buffer.append(")");
206  }
207
208  @Override
209  public void toString(StringBuilder buffer, int indent)
210  {
211    StringBuilder indentBuf = new StringBuilder(indent);
212    for (int i=0 ; i < indent; i++)
213    {
214      indentBuf.append(' ');
215    }
216
217    buffer.append(indentBuf);
218    buffer.append("Delete Response");
219    buffer.append(EOL);
220
221    buffer.append(indentBuf);
222    buffer.append("  Result Code:  ");
223    buffer.append(resultCode);
224    buffer.append(EOL);
225
226    if (errorMessage != null)
227    {
228      buffer.append(indentBuf);
229      buffer.append("  Error LocalizableMessage:  ");
230      buffer.append(errorMessage);
231      buffer.append(EOL);
232    }
233
234    if (matchedDN != null)
235    {
236      buffer.append(indentBuf);
237      buffer.append("  Matched DN:  ");
238      buffer.append(matchedDN);
239      buffer.append(EOL);
240    }
241
242    if (referralURLs != null && !referralURLs.isEmpty())
243    {
244      buffer.append(indentBuf);
245      buffer.append("  Referral URLs:  ");
246      buffer.append(EOL);
247
248      for (String s : referralURLs)
249      {
250        buffer.append(indentBuf);
251        buffer.append("  ");
252        buffer.append(s);
253        buffer.append(EOL);
254      }
255    }
256  }
257}