001/* 002 * Copyright 2007-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2007-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) 2008-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; 037 038 039 040import java.io.Serializable; 041import java.util.ArrayList; 042 043import com.unboundid.asn1.ASN1StreamReader; 044import com.unboundid.asn1.ASN1StreamReaderSequence; 045import com.unboundid.ldap.protocol.LDAPResponse; 046import com.unboundid.util.Debug; 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.LDAPMessages.*; 054 055 056 057/** 058 * This class provides a data structure for representing an LDAP search result 059 * reference. A search result reference consists of a set of referral URLs and 060 * may also include zero or more controls. It describes an alternate location 061 * in which additional results for the search may be found. If there are 062 * multiple referral URLs, then they should all be considered equivalent ways 063 * to access the information (e.g., referrals referencing different servers that 064 * may be contacted). 065 */ 066@NotMutable() 067@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 068public final class SearchResultReference 069 implements Serializable, LDAPResponse 070{ 071 /** 072 * The serial version UID for this serializable class. 073 */ 074 private static final long serialVersionUID = 5675961266319346053L; 075 076 077 078 // The set of controls returned with this search result reference. 079 private final Control[] controls; 080 081 // The message ID for the LDAP message containing this response. 082 private final int messageID; 083 084 // The set of referral URLs for this search result reference. 085 private final String[] referralURLs; 086 087 088 089 /** 090 * Creates a new search result reference with the provided information. 091 * 092 * @param referralURLs The set of referral URLs for this search result 093 * reference. It must not be {@code null}. 094 * @param controls The set of controls returned with this search result 095 * reference. It must not be {@code null}. 096 */ 097 public SearchResultReference(final String[] referralURLs, 098 final Control[] controls) 099 { 100 this(-1, referralURLs, controls); 101 } 102 103 104 105 /** 106 * Creates a new search result reference with the provided information. 107 * 108 * @param messageID The message ID for the LDAP message containing this 109 * response. 110 * @param referralURLs The set of referral URLs for this search result 111 * reference. It must not be {@code null}. 112 * @param controls The set of controls returned with this search result 113 * reference. It must not be {@code null}. 114 */ 115 public SearchResultReference(final int messageID, final String[] referralURLs, 116 final Control[] controls) 117 { 118 Validator.ensureNotNull(referralURLs); 119 120 this.messageID = messageID; 121 this.referralURLs = referralURLs; 122 123 if (controls == null) 124 { 125 this.controls = NO_CONTROLS; 126 } 127 else 128 { 129 this.controls = controls; 130 } 131 } 132 133 134 135 /** 136 * Creates a new search result reference object with the protocol op and 137 * controls read from the given ASN.1 stream reader. 138 * 139 * @param messageID The message ID for the LDAP message containing 140 * this response. 141 * @param messageSequence The ASN.1 stream reader sequence used in the 142 * course of reading the LDAP message elements. 143 * @param reader The ASN.1 stream reader from which to read the 144 * protocol op and controls. 145 * 146 * @return The decoded search result reference object. 147 * 148 * @throws LDAPException If a problem occurs while reading or decoding data 149 * from the ASN.1 stream reader. 150 */ 151 static SearchResultReference readSearchReferenceFrom(final int messageID, 152 final ASN1StreamReaderSequence messageSequence, 153 final ASN1StreamReader reader) 154 throws LDAPException 155 { 156 try 157 { 158 final ArrayList<String> refList = new ArrayList<>(5); 159 final ASN1StreamReaderSequence refSequence = reader.beginSequence(); 160 while (refSequence.hasMoreElements()) 161 { 162 refList.add(reader.readString()); 163 } 164 165 final String[] referralURLs = new String[refList.size()]; 166 refList.toArray(referralURLs); 167 168 Control[] controls = NO_CONTROLS; 169 if (messageSequence.hasMoreElements()) 170 { 171 final ArrayList<Control> controlList = new ArrayList<>(5); 172 final ASN1StreamReaderSequence controlSequence = reader.beginSequence(); 173 while (controlSequence.hasMoreElements()) 174 { 175 controlList.add(Control.readFrom(reader)); 176 } 177 178 controls = new Control[controlList.size()]; 179 controlList.toArray(controls); 180 } 181 182 return new SearchResultReference(messageID, referralURLs, controls); 183 } 184 catch (final LDAPException le) 185 { 186 Debug.debugException(le); 187 throw le; 188 } 189 catch (final Exception e) 190 { 191 Debug.debugException(e); 192 throw new LDAPException(ResultCode.DECODING_ERROR, 193 ERR_SEARCH_REFERENCE_CANNOT_DECODE.get( 194 StaticUtils.getExceptionMessage(e)), 195 e); 196 } 197 } 198 199 200 201 /** 202 * {@inheritDoc} 203 */ 204 @Override() 205 public int getMessageID() 206 { 207 return messageID; 208 } 209 210 211 212 /** 213 * Retrieves the set of referral URLs for this search result reference. 214 * 215 * @return The set of referral URLs for this search result reference. 216 */ 217 public String[] getReferralURLs() 218 { 219 return referralURLs; 220 } 221 222 223 224 /** 225 * Retrieves the set of controls returned with this search result reference. 226 * Individual response controls of a specific type may be retrieved and 227 * decoded using the {@code get} method in the response control class. 228 * 229 * @return The set of controls returned with this search result reference. 230 */ 231 public Control[] getControls() 232 { 233 return controls; 234 } 235 236 237 238 /** 239 * Retrieves the control with the specified OID. If there is more than one 240 * control with the given OID, then the first will be returned. 241 * 242 * @param oid The OID of the control to retrieve. 243 * 244 * @return The control with the requested OID, or {@code null} if there is no 245 * such control for this search result reference. 246 */ 247 public Control getControl(final String oid) 248 { 249 for (final Control c : controls) 250 { 251 if (c.getOID().equals(oid)) 252 { 253 return c; 254 } 255 } 256 257 return null; 258 } 259 260 261 262 /** 263 * Retrieves a string representation of this search result reference. 264 * 265 * @return A string representation of this search result reference. 266 */ 267 @Override() 268 public String toString() 269 { 270 final StringBuilder buffer = new StringBuilder(); 271 toString(buffer); 272 return buffer.toString(); 273 } 274 275 276 277 /** 278 * Appends a string representation of this search result reference to the 279 * provided buffer. 280 * 281 * @param buffer The buffer to which to append the string representation of 282 * this search result reference. 283 */ 284 @Override() 285 public void toString(final StringBuilder buffer) 286 { 287 buffer.append("SearchResultReference(referralURLs={"); 288 for (int i=0; i < referralURLs.length; i++) 289 { 290 if (i > 0) 291 { 292 buffer.append(", "); 293 } 294 buffer.append(referralURLs[i]); 295 } 296 buffer.append('}'); 297 298 if (messageID >= 0) 299 { 300 buffer.append(", messageID="); 301 buffer.append(messageID); 302 } 303 304 buffer.append(", controls={"); 305 306 for (int i=0; i < controls.length; i++) 307 { 308 if (i > 0) 309 { 310 buffer.append(", "); 311 } 312 313 controls[i].toString(buffer); 314 } 315 316 buffer.append("})"); 317 } 318}