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