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.types;
028
029import java.util.ArrayList;
030import java.util.List;
031import java.util.Iterator;
032
033import org.opends.server.util.CollectionUtils;
034
035/**
036 * This class defines a data structure for storing information about a
037 * referral returned while processing a search request.
038 */
039@org.opends.server.types.PublicAPI(
040     stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
041     mayInstantiate=false,
042     mayExtend=false,
043     mayInvoke=true)
044public final class SearchResultReference
045{
046  /** The set of controls associated with this search result reference. */
047  private List<Control> controls;
048
049  /** The set of referral URLs for this search result reference. */
050  private List<String> referralURLs;
051
052
053
054  /**
055   * Creates a new search result reference with the provided referral
056   * URL.
057   *
058   * @param  referralURL  The referral URL for this search result
059   *                      reference.
060   */
061  public SearchResultReference(String referralURL)
062  {
063    referralURLs = CollectionUtils.newArrayList(referralURL);
064    this.controls = new ArrayList<>(0);
065  }
066
067
068
069  /**
070   * Creates a new search result reference with the provided set of
071   * referral URLs and no controls.
072   *
073   * @param  referralURLs  The referral URLs for this search result
074   *                       reference.
075   */
076  public SearchResultReference(List<String> referralURLs)
077  {
078    if (referralURLs == null)
079    {
080      this.referralURLs = new ArrayList<>();
081    }
082    else
083    {
084      this.referralURLs = referralURLs;
085    }
086
087    this.controls = new ArrayList<>(0);
088  }
089
090
091
092  /**
093   * Creates a new search result reference with the provided set of
094   * referral URLs and no controls.
095   *
096   * @param  referralURLs  The referral URLs for this search result
097   *                       reference.
098   * @param  controls      The set of controls for this search result
099   *                       reference.
100   */
101  public SearchResultReference(List<String> referralURLs,
102                               List<Control> controls)
103  {
104    if (referralURLs == null)
105    {
106      this.referralURLs = new ArrayList<>();
107    }
108    else
109    {
110      this.referralURLs = referralURLs;
111    }
112
113    if (controls == null)
114    {
115      this.controls = new ArrayList<>(0);
116    }
117    else
118    {
119      this.controls = controls;
120    }
121  }
122
123
124
125  /**
126   * Retrieves the set of referral URLs for this search result
127   * reference.  It may be modified by the caller.
128   *
129   * @return  The set of referral URLs for this search result
130   *          reference.
131   */
132  public List<String> getReferralURLs()
133  {
134    return referralURLs;
135  }
136
137
138
139  /**
140   * Retrieves a string representation of the referral URL(s) for this
141   * search result reference.
142   *
143   * @return  A string representation of the referral URL(s) for this
144   *          search result reference.
145   */
146  public String getReferralURLString()
147  {
148    if (referralURLs == null || referralURLs.isEmpty())
149    {
150      return "";
151    }
152    else if (referralURLs.size() == 1)
153    {
154      return referralURLs.get(0);
155    }
156    else
157    {
158      Iterator<String> iterator = referralURLs.iterator();
159      StringBuilder    buffer   = new StringBuilder();
160      buffer.append("{ ");
161      buffer.append(iterator.next());
162
163      while (iterator.hasNext())
164      {
165        buffer.append(", ");
166        buffer.append(iterator.next());
167      }
168
169      buffer.append(" }");
170      return buffer.toString();
171    }
172  }
173
174
175
176  /**
177   * Retrieves the set of controls to include with this search result
178   * reference when it is sent to the client.  This set may be
179   * modified by the caller.
180   *
181   * @return  The set of controls to include with this search result
182   *          reference when it is sent to the client.
183   */
184  public List<Control> getControls()
185  {
186    return controls;
187  }
188}
189