001/*
002 * Copyright 2009-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-2019 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk.migrate.jndi;
022
023
024
025import javax.naming.NamingException;
026
027import com.unboundid.asn1.ASN1Exception;
028import com.unboundid.asn1.ASN1OctetString;
029import com.unboundid.ldap.sdk.ExtendedRequest;
030import com.unboundid.util.NotMutable;
031import com.unboundid.util.StaticUtils;
032import com.unboundid.util.ThreadSafety;
033import com.unboundid.util.ThreadSafetyLevel;
034
035
036
037/**
038 * This class provides a mechanism for converting between an LDAP extended
039 * request as used in JNDI and one used in the UnboundID LDAP SDK for Java.
040 *
041 * @see  ExtendedRequest
042 */
043@NotMutable()
044@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
045public final class JNDIExtendedRequest
046       implements javax.naming.ldap.ExtendedRequest
047{
048  /**
049   * The serial version UID for this serializable class.
050   */
051  private static final long serialVersionUID = -8502230539753937274L;
052
053
054
055  // The SDK extended request that backs this JNDI extended request.
056  private final ExtendedRequest r;
057
058
059
060  /**
061   * Creates a new JNDI extended request from the provided SDK extended request.
062   *
063   * @param  r  The SDK extended request to use to create this JNDI extended
064   *            request.
065   */
066  public JNDIExtendedRequest(final ExtendedRequest r)
067  {
068    this.r = r;
069  }
070
071
072
073  /**
074   * Creates a new JNDI extended request from the provided JNDI extended
075   * request.
076   *
077   * @param  r  The JNDI extended request to use to create this JNDI extended
078   *            request.
079   *
080   * @throws  NamingException  If a problem occurs while trying to create this
081   *                           JNDI extended request.
082   */
083  public JNDIExtendedRequest(final javax.naming.ldap.ExtendedRequest r)
084         throws NamingException
085  {
086    this.r = toSDKExtendedRequest(r);
087  }
088
089
090
091  /**
092   * Retrieves the object identifier for this extended request.
093   *
094   * @return  The object identifier for this extended request.
095   */
096  @Override()
097  public String getID()
098  {
099    return r.getOID();
100  }
101
102
103
104  /**
105   * Retrieves the encoded value for this extended request (including the BER
106   * type and length), if available.
107   *
108   * @return  The encoded value for this extended request, or {@code null} if
109   *          there is no value.
110   */
111  @Override()
112  public byte[] getEncodedValue()
113  {
114    final ASN1OctetString value = r.getValue();
115    if (value == null)
116    {
117      return null;
118    }
119    else
120    {
121      return value.encode();
122    }
123  }
124
125
126
127  /**
128   * Creates a JNDI extended response with the provided information.
129   *
130   * @param  id        The object identifier for the response, or {@code null}
131   *                   if there should not be a value.
132   * @param  berValue  A byte array containing the encoded value (including BER
133   *                   type and length), or {@code null} if the response should
134   *                   not have a value.
135   * @param  offset    The offset within the provided array at which the value
136   *                   should begin.
137   * @param  length    The number of bytes contained in the value.
138   *
139   * @return  The created JNDI extended response.
140   *
141   * @throws  NamingException  If a problem occurs while creating the response.
142   */
143  @Override()
144  public JNDIExtendedResponse createExtendedResponse(final String id,
145                                   final byte[] berValue, final int offset,
146                                   final int length)
147         throws NamingException
148  {
149    return new JNDIExtendedResponse(id, berValue, offset, length);
150  }
151
152
153
154  /**
155   * Retrieves an LDAP SDK extended request that is the equivalent of this JNDI
156   * extended request.
157   *
158   * @return  An LDAP SDK extended request that is the equivalent of this JNDI
159   *          extended request.
160   */
161  public ExtendedRequest toSDKExtendedRequest()
162  {
163    return r;
164  }
165
166
167
168  /**
169   * Retrieves an LDAP SDK extended request that is the equivalent of the
170   * provided JNDI extended request.
171   *
172   * @param  r  The JNDI extended request to convert to an LDAP SDK extended
173   *            request.
174   *
175   * @return  The LDAP SDK extended request converted from the provided JNDI
176   *          extended request.
177   *
178   * @throws  NamingException  If a problem occurs while decoding the provided
179   *                           JNDI extended request as an SDK extended request.
180   */
181  public static ExtendedRequest toSDKExtendedRequest(
182                                     final javax.naming.ldap.ExtendedRequest r)
183         throws NamingException
184  {
185    if (r == null)
186    {
187      return null;
188    }
189
190    final ASN1OctetString value;
191    final byte[] valueBytes = r.getEncodedValue();
192    if (valueBytes == null)
193    {
194      value = null;
195    }
196    else
197    {
198      try
199      {
200        value = ASN1OctetString.decodeAsOctetString(valueBytes);
201      }
202      catch (final ASN1Exception ae)
203      {
204        throw new NamingException(StaticUtils.getExceptionMessage(ae));
205      }
206    }
207
208    return new ExtendedRequest(r.getID(), value);
209  }
210
211
212
213  /**
214   * Retrieves a string representation of this JNDI extended request.
215   *
216   * @return  A string representation of this JNDI request.
217   */
218  @Override()
219  public String toString()
220  {
221    return r.toString();
222  }
223}