001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.protocols.ldap; 018 019 020import java.util.ArrayList; 021import java.util.List; 022import java.util.Iterator; 023import java.io.IOException; 024 025import org.forgerock.opendj.io.*; 026import org.opends.server.types.SearchResultReference; 027 028import org.forgerock.i18n.slf4j.LocalizedLogger; 029import static org.opends.server.protocols.ldap.LDAPConstants.*; 030import static org.opends.server.util.ServerConstants.*; 031 032 033 034/** 035 * This class defines the structures and methods for an LDAP search result 036 * reference protocol op, which is used to indicate to the client that an 037 * alternate location or server may hold more matching entries. 038 */ 039public class SearchResultReferenceProtocolOp 040 extends ProtocolOp 041{ 042 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 043 044 /** The set of referral URLs for this search result reference. */ 045 private List<String> referralURLs; 046 047 048 049 /** 050 * Creates a new search result reference protocol op with the provided set of 051 * referral URLs. 052 * 053 * @param referralURLs The set of URLs for this search result reference. 054 */ 055 public SearchResultReferenceProtocolOp(List<String> referralURLs) 056 { 057 if (referralURLs == null) 058 { 059 this.referralURLs = new ArrayList<>(); 060 } 061 else 062 { 063 this.referralURLs = referralURLs; 064 } 065 } 066 067 068 069 /** 070 * Creates a new search result reference protocol op from the provided search 071 * result reference object. 072 * 073 * @param searchReference The search result reference object to use to 074 * create this search result reference protocol op. 075 */ 076 public SearchResultReferenceProtocolOp(SearchResultReference searchReference) 077 { 078 referralURLs = searchReference.getReferralURLs(); 079 if (referralURLs == null) 080 { 081 referralURLs = new ArrayList<>(); 082 } 083 } 084 085 086 087 /** 088 * Retrieves the set of referral URLs for this search result reference 089 * protocol op. The returned list may be altered by the caller. 090 * 091 * @return The set of referral URLs for this search result reference protocol op. 092 */ 093 public List<String> getReferralURLs() 094 { 095 return referralURLs; 096 } 097 098 099 100 /** 101 * Retrieves the BER type for this protocol op. 102 * 103 * @return The BER type for this protocol op. 104 */ 105 @Override 106 public byte getType() 107 { 108 return OP_TYPE_SEARCH_RESULT_REFERENCE; 109 } 110 111 112 113 /** 114 * Retrieves the name for this protocol op type. 115 * 116 * @return The name for this protocol op type. 117 */ 118 @Override 119 public String getProtocolOpName() 120 { 121 return "Search Result Reference"; 122 } 123 124 /** 125 * Writes this protocol op to an ASN.1 output stream. 126 * 127 * @param stream The ASN.1 output stream to write to. 128 * @throws IOException If a problem occurs while writing to the stream. 129 */ 130 @Override 131 public void write(ASN1Writer stream) throws IOException 132 { 133 stream.writeStartSequence(OP_TYPE_SEARCH_RESULT_REFERENCE); 134 for(String url : referralURLs) 135 { 136 stream.writeOctetString(url); 137 } 138 stream.writeEndSequence(); 139 } 140 141 142 143 /** 144 * Appends a string representation of this LDAP protocol op to the provided 145 * buffer. 146 * 147 * @param buffer The buffer to which the string should be appended. 148 */ 149 @Override 150 public void toString(StringBuilder buffer) 151 { 152 buffer.append("SearchReference(referralURLs={"); 153 154 if (! referralURLs.isEmpty()) 155 { 156 Iterator<String> iterator = referralURLs.iterator(); 157 buffer.append(iterator.next()); 158 159 while (iterator.hasNext()) 160 { 161 buffer.append(", "); 162 buffer.append(iterator.next()); 163 } 164 } 165 166 buffer.append("})"); 167 } 168 169 170 171 /** 172 * Appends a multi-line string representation of this LDAP protocol op to the 173 * provided buffer. 174 * 175 * @param buffer The buffer to which the information should be appended. 176 * @param indent The number of spaces from the margin that the lines should 177 * be indented. 178 */ 179 @Override 180 public void toString(StringBuilder buffer, int indent) 181 { 182 StringBuilder indentBuf = new StringBuilder(indent); 183 for (int i=0 ; i < indent; i++) 184 { 185 indentBuf.append(' '); 186 } 187 188 buffer.append(indentBuf); 189 buffer.append("Search Result Reference"); 190 buffer.append(EOL); 191 192 buffer.append(indentBuf); 193 buffer.append(" Referral URLs:"); 194 buffer.append(EOL); 195 196 for (String url : referralURLs) 197 { 198 buffer.append(indentBuf); 199 buffer.append(" "); 200 buffer.append(url); 201 buffer.append(EOL); 202 } 203 } 204} 205