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