001/* 002 * Copyright 2009-2018 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2009-2018 Ping Identity Corporation 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021package com.unboundid.ldap.protocol; 022 023 024 025import java.util.ArrayList; 026import java.util.Collections; 027import java.util.Iterator; 028import java.util.List; 029 030import com.unboundid.asn1.ASN1Buffer; 031import com.unboundid.asn1.ASN1BufferSequence; 032import com.unboundid.asn1.ASN1Element; 033import com.unboundid.asn1.ASN1OctetString; 034import com.unboundid.asn1.ASN1Sequence; 035import com.unboundid.asn1.ASN1StreamReader; 036import com.unboundid.asn1.ASN1StreamReaderSequence; 037import com.unboundid.ldap.sdk.Control; 038import com.unboundid.ldap.sdk.LDAPException; 039import com.unboundid.ldap.sdk.ResultCode; 040import com.unboundid.ldap.sdk.SearchResultReference; 041import com.unboundid.util.NotMutable; 042import com.unboundid.util.InternalUseOnly; 043import com.unboundid.util.ThreadSafety; 044import com.unboundid.util.ThreadSafetyLevel; 045 046import static com.unboundid.ldap.protocol.ProtocolMessages.*; 047import static com.unboundid.util.Debug.*; 048import static com.unboundid.util.StaticUtils.*; 049 050 051 052/** 053 * This class provides an implementation of an LDAP search result reference 054 * protocol op. 055 */ 056@InternalUseOnly() 057@NotMutable() 058@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 059public final class SearchResultReferenceProtocolOp 060 implements ProtocolOp 061{ 062 /** 063 * The serial version UID for this serializable class. 064 */ 065 private static final long serialVersionUID = -1526778443581862609L; 066 067 068 069 // The list of referral URLs for this search result reference. 070 private final List<String> referralURLs; 071 072 073 074 /** 075 * Creates a new search result reference protocol op with the provided 076 * information. 077 * 078 * @param referralURLs The list of referral URLs for this search result 079 * reference. 080 */ 081 public SearchResultReferenceProtocolOp(final List<String> referralURLs) 082 { 083 this.referralURLs = Collections.unmodifiableList(referralURLs); 084 } 085 086 087 088 /** 089 * Creates a new search result reference protocol op from the provided search 090 * result reference. 091 * 092 * @param reference The search result reference to use to create this 093 * protocol op. 094 */ 095 public SearchResultReferenceProtocolOp(final SearchResultReference reference) 096 { 097 referralURLs = toList(reference.getReferralURLs()); 098 } 099 100 101 102 /** 103 * Creates a new search result reference protocol op read from the provided 104 * ASN.1 stream reader. 105 * 106 * @param reader The ASN.1 stream reader from which to read the search 107 * result reference protocol op. 108 * 109 * @throws LDAPException If a problem occurs while reading or parsing the 110 * search result reference. 111 */ 112 SearchResultReferenceProtocolOp(final ASN1StreamReader reader) 113 throws LDAPException 114 { 115 try 116 { 117 final ArrayList<String> refs = new ArrayList<String>(5); 118 final ASN1StreamReaderSequence refSequence = reader.beginSequence(); 119 while (refSequence.hasMoreElements()) 120 { 121 refs.add(reader.readString()); 122 } 123 124 referralURLs = Collections.unmodifiableList(refs); 125 } 126 catch (final Exception e) 127 { 128 debugException(e); 129 130 throw new LDAPException(ResultCode.DECODING_ERROR, 131 ERR_SEARCH_REFERENCE_CANNOT_DECODE.get(getExceptionMessage(e)), e); 132 } 133 } 134 135 136 137 /** 138 * Retrieves the list of referral URLs for this search result reference. 139 * 140 * @return The list of referral URLs for this search result reference. 141 */ 142 public List<String> getReferralURLs() 143 { 144 return referralURLs; 145 } 146 147 148 149 /** 150 * {@inheritDoc} 151 */ 152 @Override() 153 public byte getProtocolOpType() 154 { 155 return LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_REFERENCE; 156 } 157 158 159 160 /** 161 * {@inheritDoc} 162 */ 163 @Override() 164 public ASN1Element encodeProtocolOp() 165 { 166 final ArrayList<ASN1Element> urlElements = 167 new ArrayList<ASN1Element>(referralURLs.size()); 168 for (final String url : referralURLs) 169 { 170 urlElements.add(new ASN1OctetString(url)); 171 } 172 173 return new ASN1Sequence( 174 LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_REFERENCE, 175 urlElements); 176 } 177 178 179 180 /** 181 * Decodes the provided ASN.1 element as a search result reference protocol 182 * op. 183 * 184 * @param element The ASN.1 element to be decoded. 185 * 186 * @return The decoded search result reference protocol op. 187 * 188 * @throws LDAPException If the provided ASN.1 element cannot be decoded as 189 * a search result reference protocol op. 190 */ 191 public static SearchResultReferenceProtocolOp decodeProtocolOp( 192 final ASN1Element element) 193 throws LDAPException 194 { 195 try 196 { 197 final ASN1Element[] urlElements = 198 ASN1Sequence.decodeAsSequence(element).elements(); 199 final ArrayList<String> referralURLs = 200 new ArrayList<String>(urlElements.length); 201 for (final ASN1Element e : urlElements) 202 { 203 referralURLs.add(ASN1OctetString.decodeAsOctetString(e).stringValue()); 204 } 205 206 return new SearchResultReferenceProtocolOp(referralURLs); 207 } 208 catch (final Exception e) 209 { 210 debugException(e); 211 throw new LDAPException(ResultCode.DECODING_ERROR, 212 ERR_SEARCH_REFERENCE_CANNOT_DECODE.get(getExceptionMessage(e)), 213 e); 214 } 215 } 216 217 218 219 /** 220 * {@inheritDoc} 221 */ 222 @Override() 223 public void writeTo(final ASN1Buffer buffer) 224 { 225 final ASN1BufferSequence opSequence = buffer.beginSequence( 226 LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_REFERENCE); 227 for (final String s : referralURLs) 228 { 229 buffer.addOctetString(s); 230 } 231 opSequence.end(); 232 } 233 234 235 236 /** 237 * Creates a search result reference from this protocol op. 238 * 239 * @param controls The set of controls to include in the search result 240 * reference. It may be empty or {@code null} if no 241 * controls should be included. 242 * 243 * @return The search result reference that was created. 244 */ 245 public SearchResultReference toSearchResultReference( 246 final Control... controls) 247 { 248 final String[] referralArray = new String[referralURLs.size()]; 249 referralURLs.toArray(referralArray); 250 251 return new SearchResultReference(referralArray, controls); 252 } 253 254 255 256 /** 257 * Retrieves a string representation of this protocol op. 258 * 259 * @return A string representation of this protocol op. 260 */ 261 @Override() 262 public String toString() 263 { 264 final StringBuilder buffer = new StringBuilder(); 265 toString(buffer); 266 return buffer.toString(); 267 } 268 269 270 271 /** 272 * {@inheritDoc} 273 */ 274 @Override() 275 public void toString(final StringBuilder buffer) 276 { 277 buffer.append("SearchResultReferenceProtocolOp(referralURLs={"); 278 279 final Iterator<String> iterator = referralURLs.iterator(); 280 while (iterator.hasNext()) 281 { 282 buffer.append('\''); 283 buffer.append(iterator.next()); 284 buffer.append('\''); 285 if (iterator.hasNext()) 286 { 287 buffer.append(','); 288 } 289 } 290 291 buffer.append("})"); 292 } 293}