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) 2015-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.unboundidds.controls; 037 038 039 040import java.io.Serializable; 041 042import com.unboundid.asn1.ASN1Element; 043import com.unboundid.asn1.ASN1Null; 044import com.unboundid.asn1.ASN1OctetString; 045import com.unboundid.ldap.sdk.LDAPException; 046import com.unboundid.ldap.sdk.ResultCode; 047import com.unboundid.util.NotMutable; 048import com.unboundid.util.StaticUtils; 049import com.unboundid.util.ThreadSafety; 050import com.unboundid.util.ThreadSafetyLevel; 051import com.unboundid.util.Validator; 052 053import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*; 054 055 056 057/** 058 * This class provides a data structure which may be used to indicate the base 059 * DN to use for a join request. See the class-level documentation for the 060 * {@link JoinRequestControl} class for additional information and an example 061 * demonstrating its use. 062 * <BR> 063 * <BLOCKQUOTE> 064 * <B>NOTE:</B> This class, and other classes within the 065 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 066 * supported for use against Ping Identity, UnboundID, and 067 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 068 * for proprietary functionality or for external specifications that are not 069 * considered stable or mature enough to be guaranteed to work in an 070 * interoperable way with other types of LDAP servers. 071 * </BLOCKQUOTE> 072 */ 073@NotMutable() 074@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 075public final class JoinBaseDN 076 implements Serializable 077{ 078 /** 079 * The base type which indicates that the base DN for join processing should 080 * be the same as the base DN from the search request. 081 */ 082 public static final byte BASE_TYPE_SEARCH_BASE = (byte) 0x80; 083 084 085 086 /** 087 * The base type which indicates that the base DN for join processing should 088 * be the DN of the source entry. 089 */ 090 public static final byte BASE_TYPE_SOURCE_ENTRY_DN = (byte) 0x81; 091 092 093 094 /** 095 * The base type which indicates that the base DN for join processing should 096 * be a custom base DN. 097 */ 098 public static final byte BASE_TYPE_CUSTOM = (byte) 0x82; 099 100 101 102 /** 103 * The singleton instance that will be used for the "useSearchBaseDN" type. 104 */ 105 private static final JoinBaseDN USE_SEARCH_BASE_DN = new JoinBaseDN( 106 BASE_TYPE_SEARCH_BASE, null); 107 108 109 110 /** 111 * The singleton instance that will be used for the "useSourceEntryDN" type. 112 */ 113 private static final JoinBaseDN USE_SOURCE_ENTRY_DN = new JoinBaseDN( 114 BASE_TYPE_SOURCE_ENTRY_DN, null); 115 116 117 118 /** 119 * The serial version UID for this serializable class. 120 */ 121 private static final long serialVersionUID = -330303461586380445L; 122 123 124 125 // The base type value for this join base DN. 126 private final byte type; 127 128 // The base DN value to use if the custom type is used. 129 private final String customBaseDN; 130 131 132 133 /** 134 * Creates a new join base DN with the provided information. 135 * 136 * @param type The base type value for this join base DN. 137 * @param customBaseDN The custom base DN to use, if appropriate. 138 */ 139 private JoinBaseDN(final byte type, final String customBaseDN) 140 { 141 this.type = type; 142 this.customBaseDN = customBaseDN; 143 } 144 145 146 147 /** 148 * Creates a join base DN object which indicates that join processing should 149 * use the base DN from the search request. 150 * 151 * @return A join base DN object which indicates that join processing should 152 * use the base DN from the search request. 153 */ 154 public static JoinBaseDN createUseSearchBaseDN() 155 { 156 return USE_SEARCH_BASE_DN; 157 } 158 159 160 161 /** 162 * Creates a join base DN object which indicates that join processing should 163 * use the DN of the source entry. 164 * 165 * @return A join base DN object which indicates that join processing should 166 * use the DN of the source entry. 167 */ 168 public static JoinBaseDN createUseSourceEntryDN() 169 { 170 return USE_SOURCE_ENTRY_DN; 171 } 172 173 174 175 /** 176 * Creates a join base DN object which indicates that join processing should 177 * use the provided base DN. 178 * 179 * @param baseDN The custom base DN to use. It must not be {@code null}. 180 * 181 * @return A join base DN object which indicates that join processing should 182 * use the provided base DN. 183 */ 184 public static JoinBaseDN createUseCustomBaseDN(final String baseDN) 185 { 186 Validator.ensureNotNull(baseDN); 187 return new JoinBaseDN(BASE_TYPE_CUSTOM, baseDN); 188 } 189 190 191 192 /** 193 * Retrieves the base type for this join base DN. 194 * 195 * @return The base type for this join base DN. 196 */ 197 public byte getType() 198 { 199 return type; 200 } 201 202 203 204 /** 205 * Retrieves the base DN value to use for the custom base DN type. 206 * 207 * @return The base DN value to use for the custom base DN type, or 208 * {@code null} if the base DN should be the search base DN or the 209 * source entry DN. 210 */ 211 public String getCustomBaseDN() 212 { 213 return customBaseDN; 214 } 215 216 217 218 /** 219 * Encodes this join base DN as appropriate for inclusion in an LDAP join 220 * request control. 221 * 222 * @return The encoded representation of this join base DN. 223 */ 224 ASN1Element encode() 225 { 226 switch (type) 227 { 228 case BASE_TYPE_SEARCH_BASE: 229 case BASE_TYPE_SOURCE_ENTRY_DN: 230 return new ASN1Null(type); 231 232 case BASE_TYPE_CUSTOM: 233 return new ASN1OctetString(type, customBaseDN); 234 235 default: 236 // This should never happen. 237 return null; 238 } 239 } 240 241 242 243 /** 244 * Decodes the provided ASN.1 element as a join rule. 245 * 246 * @param element The element to be decoded. 247 * 248 * @return The decoded join rule. 249 * 250 * @throws LDAPException If a problem occurs while attempting to decode the 251 * provided element as a join rule. 252 */ 253 static JoinBaseDN decode(final ASN1Element element) 254 throws LDAPException 255 { 256 switch (element.getType()) 257 { 258 case BASE_TYPE_SEARCH_BASE: 259 return USE_SEARCH_BASE_DN; 260 261 case BASE_TYPE_SOURCE_ENTRY_DN: 262 return USE_SOURCE_ENTRY_DN; 263 264 case BASE_TYPE_CUSTOM: 265 return new JoinBaseDN(element.getType(), 266 element.decodeAsOctetString().stringValue()); 267 268 default: 269 throw new LDAPException(ResultCode.DECODING_ERROR, 270 ERR_JOIN_BASE_DECODE_INVALID_TYPE.get( 271 StaticUtils.toHex(element.getType()))); 272 } 273 } 274 275 276 277 /** 278 * Retrieves a string representation of this join base DN. 279 * 280 * @return A string representation of this join base DN. 281 */ 282 @Override() 283 public String toString() 284 { 285 final StringBuilder buffer = new StringBuilder(); 286 toString(buffer); 287 return buffer.toString(); 288 } 289 290 291 292 /** 293 * Appends a string representation of this join base DN to the provided 294 * buffer. 295 * 296 * @param buffer The buffer to which the information should be appended. 297 */ 298 public void toString(final StringBuilder buffer) 299 { 300 switch (type) 301 { 302 case BASE_TYPE_SEARCH_BASE: 303 buffer.append("useSearchBaseDN"); 304 break; 305 306 case BASE_TYPE_SOURCE_ENTRY_DN: 307 buffer.append("useSourceEntryDN"); 308 break; 309 310 case BASE_TYPE_CUSTOM: 311 buffer.append("useCustomBaseDN(baseDN='"); 312 buffer.append(customBaseDN); 313 buffer.append("')"); 314 break; 315 } 316 } 317}