001/*
002 * Copyright 2017-2023 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2017-2023 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2017-2023 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.unboundidds.tools;
037
038
039
040import java.io.OutputStream;
041import java.io.PrintStream;
042
043import com.unboundid.ldap.sdk.ExtendedResult;
044import com.unboundid.ldap.sdk.LDAPConnection;
045import com.unboundid.ldap.sdk.LDAPResult;
046import com.unboundid.ldap.sdk.SearchResultEntry;
047import com.unboundid.ldap.sdk.SearchResultReference;
048import com.unboundid.util.InternalUseOnly;
049import com.unboundid.util.NotExtensible;
050import com.unboundid.util.NotNull;
051import com.unboundid.util.ThreadSafety;
052import com.unboundid.util.ThreadSafetyLevel;
053
054
055
056/**
057 * This class provides an API that may be implemented by classes that format
058 * and output the results for LDAP-related tools.
059 * <BR>
060 * <BLOCKQUOTE>
061 *   <B>NOTE:</B>  This class, and other classes within the
062 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
063 *   supported for use against Ping Identity, UnboundID, and
064 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
065 *   for proprietary functionality or for external specifications that are not
066 *   considered stable or mature enough to be guaranteed to work in an
067 *   interoperable way with other types of LDAP servers.
068 * </BLOCKQUOTE>
069 */
070@InternalUseOnly()
071@NotExtensible()
072@ThreadSafety(level=ThreadSafetyLevel.INTERFACE_NOT_THREADSAFE)
073public abstract class LDAPResultWriter
074{
075  // The print stream
076  @NotNull private volatile PrintStream printStream;
077
078
079
080  /**
081   * Creates a new LDAP result writer that will write to the provided output
082   * stream.
083   *
084   * @param  outputStream  The output stream to which the output will be
085   *                       written.  It must not be {@code null}.
086   */
087  protected LDAPResultWriter(@NotNull final OutputStream outputStream)
088  {
089    printStream = getPrintStream(outputStream);
090  }
091
092
093
094  /**
095   * Updates the output stream to which output will be written.
096   *
097   * @param  outputStream  The output stream to which the output will be
098   *                       written.  It must not be {@code null}.
099   */
100  public final void updateOutputStream(@NotNull final OutputStream outputStream)
101  {
102    printStream = getPrintStream(outputStream);
103  }
104
105
106
107  /**
108   * Retrieves a {@code PrintStream} that wraps the provided
109   * {@code OutputStream}.  If the given stream is already a
110   * {@code PrintStream}, then that stream will be returned without creating a
111   * new object.
112   *
113   * @param  outputStream  The output stream to be wrapped by a print stream
114   *                       (or to be returned directly if it is already a print
115   *                       stream).  It must not be {@code null}.
116   *
117   * @return  The print stream for the provided output stream.
118   */
119  @NotNull()
120  private static PrintStream getPrintStream(
121               @NotNull final OutputStream outputStream)
122  {
123    if (outputStream instanceof PrintStream)
124    {
125      return (PrintStream) outputStream;
126    }
127    else
128    {
129      return new PrintStream(outputStream);
130    }
131  }
132
133
134
135  /**
136   * Writes a blank line to the associated print stream.
137   */
138  protected void println()
139  {
140    printStream.println();
141  }
142
143
144
145  /**
146   * Writes the provided string to the associated print stream without a
147   * subsequent newline.
148   *
149   * @param  string  The string to be written.  It must not be {@code null}.
150   */
151  protected void print(@NotNull final String string)
152  {
153    printStream.print(string);
154  }
155
156
157
158  /**
159   * Writes the provided string to the associated print stream with a subsequent
160   * newline.
161   *
162   * @param  string  The string to be written.  It must not be {@code null}.
163   */
164  protected void println(@NotNull final String string)
165  {
166    printStream.println(string);
167  }
168
169
170
171  /**
172   * Retrieves the print stream that may be used to write output.
173   *
174   * @return  The print stream that may be used to write output.
175   */
176  @NotNull()
177  protected final PrintStream getPrintStream()
178  {
179    return printStream;
180  }
181
182
183
184  /**
185   * Flushes any buffered output.
186   */
187  public final void flush()
188  {
189    printStream.flush();
190  }
191
192
193
194  /**
195   * Writes the provided comment to the output.
196   *
197   * @param  comment  The comment to be written.  It must not be {@code null}.
198   */
199  public abstract void writeComment(@NotNull final String comment);
200
201
202
203  /**
204   * Formats and writes a header that describes the way in which the data will
205   * be formatted.  This will be displayed at the beginning of the output
206   * (including at the beginning of each file, if output should be spread
207   * across multiple files).
208   */
209  public abstract void writeHeader();
210
211
212
213  /**
214   * Formats and writes the provided search result entry.
215   *
216   * @param  entry  The search result entry to be processed.
217   */
218  public abstract void writeSearchResultEntry(
219              @NotNull SearchResultEntry entry);
220
221
222
223  /**
224   * Formats and writes the provided search result reference.
225   *
226   * @param  ref  The search result reference to be processed.
227   */
228  public abstract void writeSearchResultReference(
229              @NotNull SearchResultReference ref);
230
231
232
233  /**
234   * Formats and writes the provided LDAP result.
235   *
236   * @param  result  The LDAP result to be processed.  It may or may not be a
237   *                 search result.
238   */
239  public abstract void writeResult(@NotNull LDAPResult result);
240
241
242
243  /**
244   * Formats and writes the provided unsolicited notification.
245   *
246   * @param  connection    The connection on which the unsolicited notification
247   *                       was received.
248   * @param  notification  The unsolicited notification that was received.
249   */
250  public abstract void writeUnsolicitedNotification(
251              @NotNull LDAPConnection connection,
252              @NotNull ExtendedResult notification);
253}