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