001/*
002 * Copyright 2007-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2007-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2007-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.controls;
037
038
039
040import com.unboundid.ldap.sdk.Control;
041import com.unboundid.ldap.sdk.JSONControlDecodeHelper;
042import com.unboundid.ldap.sdk.LDAPException;
043import com.unboundid.ldap.sdk.ResultCode;
044import com.unboundid.util.NotMutable;
045import com.unboundid.util.NotNull;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048import com.unboundid.util.json.JSONField;
049import com.unboundid.util.json.JSONObject;
050
051import static com.unboundid.ldap.sdk.controls.ControlMessages.*;
052
053
054
055/**
056 * This class provides an implementation of the ManageDsaIT control as described
057 * in <A HREF="http://www.ietf.org/rfc/rfc3296.txt">RFC 3296</A>.  This control
058 * may be used to request that the directory server treat all entries as if they
059 * were regular entries.
060 * <BR><BR>
061 * One of the most common uses of the ManageDsaIT control is to request that the
062 * directory server to treat an entry containing the "{@code referral}" object
063 * class as a regular entry rather than a smart referral.  Normally, when the
064 * server encounters an entry with the {@code referral} object class, it sends
065 * a referral with the URLs contained in the {@code ref} attribute of that
066 * entry.  However, if the ManageDsaIT control is included then the operation
067 * will attempt to operate on the referral definition itself rather than sending
068 * that referral to the client.
069 * <BR><BR>
070 * <H2>Example</H2>
071 * The following example demonstrates the use of the ManageDsaIT control to
072 * delete an entry that may or may not be a referral:
073 * <PRE>
074 * // Establish a connection to the directory server.  Even though it's the
075 * // default behavior, we'll explicitly configure the connection to not follow
076 * // referrals.
077 * LDAPConnectionOptions connectionOptions = new LDAPConnectionOptions();
078 * connectionOptions.setFollowReferrals(false);
079 * LDAPConnection connection = new LDAPConnection(connectionOptions,
080 *      serverAddress, serverPort, bindDN, bindPassword);
081 *
082 * // Try to delete an entry that will result in a referral.  Without the
083 * // ManageDsaIT request control, we should get an exception.
084 * DeleteRequest deleteRequest =
085 *      new DeleteRequest("ou=referral entry,dc=example,dc=com");
086 * LDAPResult deleteResult;
087 * try
088 * {
089 *   deleteResult = connection.delete(deleteRequest);
090 * }
091 * catch (LDAPException le)
092 * {
093 *   // This exception is expected because we should get a referral, and
094 *   // the connection is configured to not follow referrals.
095 *   deleteResult = le.toLDAPResult();
096 *   ResultCode resultCode = le.getResultCode();
097 *   String errorMessageFromServer = le.getDiagnosticMessage();
098 *   String[] referralURLs = le.getReferralURLs();
099 * }
100 * LDAPTestUtils.assertResultCodeEquals(deleteResult, ResultCode.REFERRAL);
101 * LDAPTestUtils.assertHasReferral(deleteResult);
102 *
103 * // Update the delete request to include the ManageDsaIT request control,
104 * // which will cause the server to try to delete the referral entry instead
105 * // of returning a referral response.  We'll assume that the delete is
106 * // successful.
107 * deleteRequest.addControl(new ManageDsaITRequestControl());
108 * try
109 * {
110 *   deleteResult = connection.delete(deleteRequest);
111 * }
112 * catch (LDAPException le)
113 * {
114 *   // The delete shouldn't trigger a referral, but it's possible that the
115 *   // operation failed for some other reason (e.g., entry doesn't exist, the
116 *   // user doesn't have permission to delete it, etc.).
117 *   deleteResult = le.toLDAPResult();
118 * }
119 * LDAPTestUtils.assertResultCodeEquals(deleteResult, ResultCode.SUCCESS);
120 * LDAPTestUtils.assertMissingReferral(deleteResult);
121 *
122 * connection.close();
123 * </PRE>
124 */
125@NotMutable()
126@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
127public final class ManageDsaITRequestControl
128       extends Control
129{
130  /**
131   * The OID (2.16.840.1.113730.3.4.2) for the ManageDsaIT request control.
132   */
133  @NotNull public static final String MANAGE_DSA_IT_REQUEST_OID =
134       "2.16.840.1.113730.3.4.2";
135
136
137
138  /**
139   * The serial version UID for this serializable class.
140   */
141  private static final long serialVersionUID = -4540943247829123783L;
142
143
144
145  /**
146   * Creates a new ManageDsaIT request control.  The control will not be marked
147   * critical.
148   */
149  public ManageDsaITRequestControl()
150  {
151    super(MANAGE_DSA_IT_REQUEST_OID, false, null);
152  }
153
154
155
156  /**
157   * Creates a new ManageDsaIT request control.
158   *
159   * @param  isCritical  Indicates whether the control should be marked
160   *                     critical.
161   */
162  public ManageDsaITRequestControl(final boolean isCritical)
163  {
164    super(MANAGE_DSA_IT_REQUEST_OID, isCritical, null);
165  }
166
167
168
169  /**
170   * Creates a new ManageDsaIT request control which is decoded from the
171   * provided generic control.
172   *
173   * @param  control  The generic control to be decoded as a ManageDsaIT request
174   *                  control.
175   *
176   * @throws  LDAPException  If the provided control cannot be decoded as a
177   *                         ManageDsaIT request control.
178   */
179  public ManageDsaITRequestControl(@NotNull final Control control)
180         throws LDAPException
181  {
182    super(control);
183
184    if (control.hasValue())
185    {
186      throw new LDAPException(ResultCode.DECODING_ERROR,
187                              ERR_MANAGE_DSA_IT_HAS_VALUE.get());
188    }
189  }
190
191
192
193  /**
194   * {@inheritDoc}
195   */
196  @Override()
197  @NotNull()
198  public String getControlName()
199  {
200    return INFO_CONTROL_NAME_MANAGE_DSAIT_REQUEST.get();
201  }
202
203
204
205  /**
206   * Retrieves a representation of this ManageDsaIT request control as a JSON
207   * object.  The JSON object uses the following fields (note that since this
208   * control does not have a value, neither the {@code value-base64} nor
209   * {@code value-json} fields may be present):
210   * <UL>
211   *   <LI>
212   *     {@code oid} -- A mandatory string field whose value is the object
213   *     identifier for this control.  For the ManageDsaIT request control, the
214   *     OID is "2.16.840.1.113730.3.4.2".
215   *   </LI>
216   *   <LI>
217   *     {@code control-name} -- An optional string field whose value is a
218   *     human-readable name for this control.  This field is only intended for
219   *     descriptive purposes, and when decoding a control, the {@code oid}
220   *     field should be used to identify the type of control.
221   *   </LI>
222   *   <LI>
223   *     {@code criticality} -- A mandatory Boolean field used to indicate
224   *     whether this control is considered critical.
225   *   </LI>
226   * </UL>
227   *
228   * @return  A JSON object that contains a representation of this control.
229   */
230  @Override()
231  @NotNull()
232  public JSONObject toJSONControl()
233  {
234    return new JSONObject(
235         new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID,
236              MANAGE_DSA_IT_REQUEST_OID),
237         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
238              INFO_CONTROL_NAME_MANAGE_DSAIT_REQUEST.get()),
239         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY,
240              isCritical()));
241  }
242
243
244
245  /**
246   * Attempts to decode the provided object as a JSON representation of a
247   * ManageDsaIT request control.
248   *
249   * @param  controlObject  The JSON object to be decoded.  It must not be
250   *                        {@code null}.
251   * @param  strict         Indicates whether to use strict mode when decoding
252   *                        the provided JSON object.  If this is {@code true},
253   *                        then this method will throw an exception if the
254   *                        provided JSON object contains any unrecognized
255   *                        fields.  If this is {@code false}, then unrecognized
256   *                        fields will be ignored.
257   *
258   * @return  The ManageDsaIT request control that was decoded from the provided
259   *          JSON object.
260   *
261   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
262   *                         valid ManageDsaIT request control.
263   */
264  @NotNull()
265  public static ManageDsaITRequestControl decodeJSONControl(
266              @NotNull final JSONObject controlObject,
267              final boolean strict)
268         throws LDAPException
269  {
270    final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper(
271         controlObject, strict, false, false);
272
273    return new ManageDsaITRequestControl(jsonControl.getCriticality());
274  }
275
276
277
278  /**
279   * {@inheritDoc}
280   */
281  @Override()
282  public void toString(@NotNull final StringBuilder buffer)
283  {
284    buffer.append("ManageDsaITRequestControl(isCritical=");
285    buffer.append(isCritical());
286    buffer.append(')');
287  }
288}