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.jndi;
037
038
039
040import javax.naming.NamingException;
041
042import com.unboundid.asn1.ASN1Exception;
043import com.unboundid.asn1.ASN1OctetString;
044import com.unboundid.ldap.sdk.ExtendedRequest;
045import com.unboundid.util.NotMutable;
046import com.unboundid.util.NotNull;
047import com.unboundid.util.Nullable;
048import com.unboundid.util.StaticUtils;
049import com.unboundid.util.ThreadSafety;
050import com.unboundid.util.ThreadSafetyLevel;
051
052
053
054/**
055 * This class provides a mechanism for converting between an LDAP extended
056 * request as used in JNDI and one used in the UnboundID LDAP SDK for Java.
057 *
058 * @see  ExtendedRequest
059 */
060@NotMutable()
061@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
062public final class JNDIExtendedRequest
063       implements javax.naming.ldap.ExtendedRequest
064{
065  /**
066   * The serial version UID for this serializable class.
067   */
068  private static final long serialVersionUID = -8502230539753937274L;
069
070
071
072  // The SDK extended request that backs this JNDI extended request.
073  @NotNull private final ExtendedRequest r;
074
075
076
077  /**
078   * Creates a new JNDI extended request from the provided SDK extended request.
079   *
080   * @param  r  The SDK extended request to use to create this JNDI extended
081   *            request.
082   */
083  public JNDIExtendedRequest(@NotNull final ExtendedRequest r)
084  {
085    this.r = r;
086  }
087
088
089
090  /**
091   * Creates a new JNDI extended request from the provided JNDI extended
092   * request.
093   *
094   * @param  r  The JNDI extended request to use to create this JNDI extended
095   *            request.
096   *
097   * @throws  NamingException  If a problem occurs while trying to create this
098   *                           JNDI extended request.
099   */
100  public JNDIExtendedRequest(@NotNull final javax.naming.ldap.ExtendedRequest r)
101         throws NamingException
102  {
103    this.r = toSDKExtendedRequest(r);
104  }
105
106
107
108  /**
109   * Retrieves the object identifier for this extended request.
110   *
111   * @return  The object identifier for this extended request.
112   */
113  @Override()
114  @NotNull()
115  public String getID()
116  {
117    return r.getOID();
118  }
119
120
121
122  /**
123   * Retrieves the encoded value for this extended request (including the BER
124   * type and length), if available.
125   *
126   * @return  The encoded value for this extended request, or {@code null} if
127   *          there is no value.
128   */
129  @Override()
130  @Nullable()
131  public byte[] getEncodedValue()
132  {
133    final ASN1OctetString value = r.getValue();
134    if (value == null)
135    {
136      return null;
137    }
138    else
139    {
140      if (JNDIConverter.includeTypeAndLengthInExtendedOpValues())
141      {
142        return value.encode();
143      }
144      else
145      {
146        return value.getValue();
147      }
148    }
149  }
150
151
152
153  /**
154   * Creates a JNDI extended response with the provided information.
155   *
156   * @param  id        The object identifier for the response, or {@code null}
157   *                   if there should not be a value.
158   * @param  berValue  A byte array containing the encoded value (including BER
159   *                   type and length), or {@code null} if the response should
160   *                   not have a value.
161   * @param  offset    The offset within the provided array at which the value
162   *                   should begin.
163   * @param  length    The number of bytes contained in the value.
164   *
165   * @return  The created JNDI extended response.
166   *
167   * @throws  NamingException  If a problem occurs while creating the response.
168   */
169  @Override()
170  @NotNull()
171  public JNDIExtendedResponse createExtendedResponse(@Nullable final String id,
172                                   @Nullable final byte[] berValue,
173                                   final int offset, final int length)
174         throws NamingException
175  {
176    return new JNDIExtendedResponse(id, berValue, offset, length);
177  }
178
179
180
181  /**
182   * Retrieves an LDAP SDK extended request that is the equivalent of this JNDI
183   * extended request.
184   *
185   * @return  An LDAP SDK extended request that is the equivalent of this JNDI
186   *          extended request.
187   */
188  @NotNull()
189  public ExtendedRequest toSDKExtendedRequest()
190  {
191    return r;
192  }
193
194
195
196  /**
197   * Retrieves an LDAP SDK extended request that is the equivalent of the
198   * provided JNDI extended request.
199   *
200   * @param  r  The JNDI extended request to convert to an LDAP SDK extended
201   *            request.
202   *
203   * @return  The LDAP SDK extended request converted from the provided JNDI
204   *          extended request.
205   *
206   * @throws  NamingException  If a problem occurs while decoding the provided
207   *                           JNDI extended request as an SDK extended request.
208   */
209  @Nullable()
210  public static ExtendedRequest toSDKExtendedRequest(
211                     @Nullable final javax.naming.ldap.ExtendedRequest r)
212         throws NamingException
213  {
214    if (r == null)
215    {
216      return null;
217    }
218
219    final ASN1OctetString value;
220    final byte[] valueBytes = r.getEncodedValue();
221    if (valueBytes == null)
222    {
223      value = null;
224    }
225    else
226    {
227      if (JNDIConverter.includeTypeAndLengthInExtendedOpValues())
228      {
229        try
230        {
231          value = ASN1OctetString.decodeAsOctetString(valueBytes);
232        }
233        catch (final ASN1Exception ae)
234        {
235          throw new NamingException(StaticUtils.getExceptionMessage(ae));
236        }
237      }
238      else
239      {
240        value = new ASN1OctetString(valueBytes);
241      }
242    }
243
244    return new ExtendedRequest(r.getID(), value);
245  }
246
247
248
249  /**
250   * Retrieves a string representation of this JNDI extended request.
251   *
252   * @return  A string representation of this JNDI request.
253   */
254  @Override()
255  @NotNull()
256  public String toString()
257  {
258    return r.toString();
259  }
260}