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-2015 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  public byte getType()
106  {
107    return OP_TYPE_SEARCH_RESULT_REFERENCE;
108  }
109
110
111
112  /**
113   * Retrieves the name for this protocol op type.
114   *
115   * @return  The name for this protocol op type.
116   */
117  public String getProtocolOpName()
118  {
119    return "Search Result Reference";
120  }
121
122  /**
123   * Writes this protocol op to an ASN.1 output stream.
124   *
125   * @param stream The ASN.1 output stream to write to.
126   * @throws IOException If a problem occurs while writing to the stream.
127   */
128  public void write(ASN1Writer stream) throws IOException
129  {
130    stream.writeStartSequence(OP_TYPE_SEARCH_RESULT_REFERENCE);
131    for(String url : referralURLs)
132    {
133      stream.writeOctetString(url);
134    }
135    stream.writeEndSequence();
136  }
137
138
139
140  /**
141   * Appends a string representation of this LDAP protocol op to the provided
142   * buffer.
143   *
144   * @param  buffer  The buffer to which the string should be appended.
145   */
146  public void toString(StringBuilder buffer)
147  {
148    buffer.append("SearchReference(referralURLs={");
149
150    if (! referralURLs.isEmpty())
151    {
152      Iterator<String> iterator = referralURLs.iterator();
153      buffer.append(iterator.next());
154
155      while (iterator.hasNext())
156      {
157        buffer.append(", ");
158        buffer.append(iterator.next());
159      }
160    }
161
162    buffer.append("})");
163  }
164
165
166
167  /**
168   * Appends a multi-line string representation of this LDAP protocol op to the
169   * provided buffer.
170   *
171   * @param  buffer  The buffer to which the information should be appended.
172   * @param  indent  The number of spaces from the margin that the lines should
173   *                 be indented.
174   */
175  public void toString(StringBuilder buffer, int indent)
176  {
177    StringBuilder indentBuf = new StringBuilder(indent);
178    for (int i=0 ; i < indent; i++)
179    {
180      indentBuf.append(' ');
181    }
182
183    buffer.append(indentBuf);
184    buffer.append("Search Result Reference");
185    buffer.append(EOL);
186
187    buffer.append(indentBuf);
188    buffer.append("  Referral URLs:");
189    buffer.append(EOL);
190
191    for (String url : referralURLs)
192    {
193      buffer.append(indentBuf);
194      buffer.append("    ");
195      buffer.append(url);
196      buffer.append(EOL);
197    }
198  }
199}
200