001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-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) 2009-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.migrate.ldapjdk;
037
038
039
040import java.io.Serializable;
041
042import com.unboundid.asn1.ASN1OctetString;
043import com.unboundid.ldap.sdk.ExtendedRequest;
044import com.unboundid.util.NotExtensible;
045import com.unboundid.util.NotMutable;
046import com.unboundid.util.NotNull;
047import com.unboundid.util.Nullable;
048import com.unboundid.util.ThreadSafety;
049import com.unboundid.util.ThreadSafetyLevel;
050
051
052
053/**
054 * This class provides a data structure that represents an LDAP extended
055 * request.
056 * <BR><BR>
057 * This class is primarily intended to be used in the process of updating
058 * applications which use the Netscape Directory SDK for Java to switch to or
059 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
060 * using the Netscape Directory SDK for Java, the {@link ExtendedRequest} class
061 * should be used instead.
062 */
063@NotExtensible()
064@NotMutable()
065@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
066public class LDAPExtendedOperation
067       implements Serializable
068{
069  /**
070   * The serial version UID for this serializable class.
071   */
072  private static final long serialVersionUID = 9207085503424216431L;
073
074
075
076  // The value for this extended operation, if available.
077  @Nullable private final byte[] value;
078
079  // The OID for this extended operation.
080  @NotNull private final String oid;
081
082
083
084  /**
085   * Creates a new LDAP extended operation with the provided OID and value.
086   *
087   * @param  id    The OID for this extended request.
088   * @param  vals  The encoded value for this extended request, or {@code null}
089   *               if there is none.
090   */
091  public LDAPExtendedOperation(@NotNull final String id,
092                               @Nullable final byte[] vals)
093  {
094    oid   = id;
095    value = vals;
096  }
097
098
099
100  /**
101   * Creates a new LDAP extended operation from the provided extended request.
102   *
103   * @param  extendedRequest  The extended request to use to create this LDAP
104   *                          extended operation.
105   */
106  public LDAPExtendedOperation(@NotNull final ExtendedRequest extendedRequest)
107  {
108    oid = extendedRequest.getOID();
109
110    final ASN1OctetString v = extendedRequest.getValue();
111    if (v == null)
112    {
113      value = null;
114    }
115    else
116    {
117      value = v.getValue();
118    }
119  }
120
121
122
123  /**
124   * Retrieves the OID for this LDAP extended operation.
125   *
126   * @return  The OID for this LDaP extended operation.
127   */
128  @NotNull()
129  public String getID()
130  {
131    return oid;
132  }
133
134
135
136  /**
137   * Retrieves the encoded value for this LDAP extended operation, if
138   * available.
139   *
140   * @return  The encoded value for this LDAP extended operation, or
141   *          {@code null} if there is none.
142   */
143  @Nullable()
144  public byte[] getValue()
145  {
146    return value;
147  }
148
149
150
151  /**
152   * Converts this LDAP extended operation to an {@link ExtendedRequest}.
153   *
154   * @return  The {@code ExtendedRequest} object that is the equivalent of this
155   *          LDAP extended response.
156   */
157  @NotNull()
158  public final ExtendedRequest toExtendedRequest()
159  {
160    if (value == null)
161    {
162      return new ExtendedRequest(oid);
163    }
164    else
165    {
166      return new ExtendedRequest(oid, new ASN1OctetString(value));
167    }
168  }
169
170
171
172  /**
173   * Retrieves a string representation of this extended operation.
174   *
175   * @return  A string representation of this extended operation.
176   */
177  @Override()
178  @NotNull()
179  public String toString()
180  {
181    final StringBuilder buffer = new StringBuilder();
182
183    buffer.append("LDAPExtendedOperation(id=");
184    buffer.append(oid);
185
186    if (value != null)
187    {
188      buffer.append(", value=byte[");
189      buffer.append(value.length);
190      buffer.append(']');
191    }
192
193    buffer.append(')');
194
195    return buffer.toString();
196  }
197}