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