001/* 002 * Copyright 2009-2022 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2009-2022 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-2022 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 return value.encode(); 141 } 142 } 143 144 145 146 /** 147 * Creates a JNDI extended response with the provided information. 148 * 149 * @param id The object identifier for the response, or {@code null} 150 * if there should not be a value. 151 * @param berValue A byte array containing the encoded value (including BER 152 * type and length), or {@code null} if the response should 153 * not have a value. 154 * @param offset The offset within the provided array at which the value 155 * should begin. 156 * @param length The number of bytes contained in the value. 157 * 158 * @return The created JNDI extended response. 159 * 160 * @throws NamingException If a problem occurs while creating the response. 161 */ 162 @Override() 163 @NotNull() 164 public JNDIExtendedResponse createExtendedResponse(@Nullable final String id, 165 @Nullable final byte[] berValue, 166 final int offset, final int length) 167 throws NamingException 168 { 169 return new JNDIExtendedResponse(id, berValue, offset, length); 170 } 171 172 173 174 /** 175 * Retrieves an LDAP SDK extended request that is the equivalent of this JNDI 176 * extended request. 177 * 178 * @return An LDAP SDK extended request that is the equivalent of this JNDI 179 * extended request. 180 */ 181 @NotNull() 182 public ExtendedRequest toSDKExtendedRequest() 183 { 184 return r; 185 } 186 187 188 189 /** 190 * Retrieves an LDAP SDK extended request that is the equivalent of the 191 * provided JNDI extended request. 192 * 193 * @param r The JNDI extended request to convert to an LDAP SDK extended 194 * request. 195 * 196 * @return The LDAP SDK extended request converted from the provided JNDI 197 * extended request. 198 * 199 * @throws NamingException If a problem occurs while decoding the provided 200 * JNDI extended request as an SDK extended request. 201 */ 202 @Nullable() 203 public static ExtendedRequest toSDKExtendedRequest( 204 @Nullable final javax.naming.ldap.ExtendedRequest r) 205 throws NamingException 206 { 207 if (r == null) 208 { 209 return null; 210 } 211 212 final ASN1OctetString value; 213 final byte[] valueBytes = r.getEncodedValue(); 214 if (valueBytes == null) 215 { 216 value = null; 217 } 218 else 219 { 220 try 221 { 222 value = ASN1OctetString.decodeAsOctetString(valueBytes); 223 } 224 catch (final ASN1Exception ae) 225 { 226 throw new NamingException(StaticUtils.getExceptionMessage(ae)); 227 } 228 } 229 230 return new ExtendedRequest(r.getID(), value); 231 } 232 233 234 235 /** 236 * Retrieves a string representation of this JNDI extended request. 237 * 238 * @return A string representation of this JNDI request. 239 */ 240 @Override() 241 @NotNull() 242 public String toString() 243 { 244 return r.toString(); 245 } 246}