001/*
002 * Copyright 2009-2014 UnboundID Corp.
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-2014 UnboundID Corp.
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  public String getID()
097  {
098    return r.getOID();
099  }
100
101
102
103  /**
104   * Retrieves the encoded value for this extended request (including the BER
105   * type and length), if available.
106   *
107   * @return  The encoded value for this extended request, or {@code null} if
108   *          there is no value.
109   */
110  public byte[] getEncodedValue()
111  {
112    final ASN1OctetString value = r.getValue();
113    if (value == null)
114    {
115      return null;
116    }
117    else
118    {
119      return value.encode();
120    }
121  }
122
123
124
125  /**
126   * Creates a JNDI extended response with the provided information.
127   *
128   * @param  id        The object identifier for the response, or {@code null}
129   *                   if there should not be a value.
130   * @param  berValue  A byte array containing the encoded value (including BER
131   *                   type and length), or {@code null} if the response should
132   *                   not have a value.
133   * @param  offset    The offset within the provided array at which the value
134   *                   should begin.
135   * @param  length    The number of bytes contained in the value.
136   *
137   * @return  The created JNDI extended response.
138   *
139   * @throws  NamingException  If a problem occurs while creating the response.
140   */
141  public JNDIExtendedResponse createExtendedResponse(final String id,
142                                   final byte[] berValue, final int offset,
143                                   final int length)
144         throws NamingException
145  {
146    return new JNDIExtendedResponse(id, berValue, offset, length);
147  }
148
149
150
151  /**
152   * Retrieves an LDAP SDK extended request that is the equivalent of this JNDI
153   * extended request.
154   *
155   * @return  An LDAP SDK extended request that is the equivalent of this JNDI
156   *          extended request.
157   */
158  public ExtendedRequest toSDKExtendedRequest()
159  {
160    return r;
161  }
162
163
164
165  /**
166   * Retrieves an LDAP SDK extended request that is the equivalent of the
167   * provided JNDI extended request.
168   *
169   * @param  r  The JNDI extended request to convert to an LDAP SDK extended
170   *            request.
171   *
172   * @return  The LDAP SDK extended request converted from the provided JNDI
173   *          extended request.
174   *
175   * @throws  NamingException  If a problem occurs while decoding the provided
176   *                           JNDI extended request as an SDK extended request.
177   */
178  public static ExtendedRequest toSDKExtendedRequest(
179                                     final javax.naming.ldap.ExtendedRequest r)
180         throws NamingException
181  {
182    if (r == null)
183    {
184      return null;
185    }
186
187    final ASN1OctetString value;
188    final byte[] valueBytes = r.getEncodedValue();
189    if (valueBytes == null)
190    {
191      value = null;
192    }
193    else
194    {
195      try
196      {
197        value = ASN1OctetString.decodeAsOctetString(valueBytes);
198      }
199      catch (ASN1Exception ae)
200      {
201        throw new NamingException(StaticUtils.getExceptionMessage(ae));
202      }
203    }
204
205    return new ExtendedRequest(r.getID(), value);
206  }
207
208
209
210  /**
211   * Retrieves a string representation of this JNDI extended request.
212   *
213   * @return  A string representation of this JNDI request.
214   */
215  @Override()
216  public String toString()
217  {
218    return r.toString();
219  }
220}