001/*
002 * Copyright 2016-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2016-2024 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) 2016-2024 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.util.ArrayList;
041
042import com.unboundid.ldap.sdk.BindResult;
043import com.unboundid.ldap.sdk.LDAPConnection;
044import com.unboundid.ldap.sdk.LDAPConnectionPoolHealthCheck;
045import com.unboundid.ldap.sdk.LDAPException;
046import com.unboundid.ldap.sdk.ResultCode;
047import com.unboundid.util.CommandLineTool;
048import com.unboundid.util.NotNull;
049import com.unboundid.util.StaticUtils;
050import com.unboundid.util.ThreadSafety;
051import com.unboundid.util.ThreadSafetyLevel;
052
053import static com.unboundid.ldap.sdk.unboundidds.tools.ToolMessages.*;
054
055
056
057/**
058 * This class provides an implementation of a connection pool health check that
059 * can display information about the result of a bind operation.  It will always
060 * report information about an unsuccessful bind.  It may optionally report
061 * information about a successful bind, and optionally only if the successful
062 * bind includes one or more response controls.
063 * <BR>
064 * <BLOCKQUOTE>
065 *   <B>NOTE:</B>  This class, and other classes within the
066 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
067 *   supported for use against Ping Identity, UnboundID, and
068 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
069 *   for proprietary functionality or for external specifications that are not
070 *   considered stable or mature enough to be guaranteed to work in an
071 *   interoperable way with other types of LDAP servers.
072 * </BLOCKQUOTE>
073 * <BR>
074 * Note that this health check is only intended to generate output when
075 * appropriate and will never throw an exception to indicate that a connection
076 * is unusable.  If additional health checking is required, then this health
077 * check may be combined with others via an aggregate health check in a manner
078 * that ensures this health check will be invoked before any others that may
079 * throw an exception.
080 */
081@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
082public final class ReportBindResultLDAPConnectionPoolHealthCheck
083       extends LDAPConnectionPoolHealthCheck
084{
085  // Indicates whether to display result details for successful binds that
086  // include one or more response controls.
087  private final boolean displaySuccessResultWithControls;
088
089  // Indicates whether to display result details for successful binds that do
090  // not include any response controls.
091  private final boolean displaySuccessResultWithoutControls;
092
093  // The tool whose output and error streams will be used for displaying result
094  // details.
095  @NotNull private final CommandLineTool tool;
096
097  // The column at which to wrap long lines.
098  private final int wrapColumn;
099
100
101
102  /**
103   * Creates a new instance of this health check with the provided information.
104   *
105   * @param  tool                                 The tool with which this
106   *                                              health check is associated.
107   *                                              Any success messages written
108   *                                              will be sent to the tool's
109   *                                              standard output stream.  Any
110   *                                              failure messages written will
111   *                                              be sent to the tool's standard
112   *                                              error stream.
113   * @param  displaySuccessResultWithControls     Indicates whether to display
114   *                                              information about a bind
115   *                                              result with a result code of
116   *                                              {@code SUCCESS} that has one
117   *                                              or more response controls.
118   * @param  displaySuccessResultWithoutControls  Indicates whether to display
119   *                                              information about a bind
120   *                                              result with a result code of
121   *                                              {@code SUCCESS} that has no
122   *                                              response controls.
123   */
124  public ReportBindResultLDAPConnectionPoolHealthCheck(
125              @NotNull final CommandLineTool tool,
126              final boolean displaySuccessResultWithControls,
127              final boolean displaySuccessResultWithoutControls)
128  {
129    this.tool = tool;
130    this.displaySuccessResultWithControls = displaySuccessResultWithControls;
131    this.displaySuccessResultWithoutControls =
132         displaySuccessResultWithoutControls;
133
134    wrapColumn = StaticUtils.TERMINAL_WIDTH_COLUMNS - 1;
135  }
136
137
138
139  /**
140   * {@inheritDoc}
141   */
142  @Override()
143  public void ensureConnectionValidAfterAuthentication(
144                   @NotNull final LDAPConnection connection,
145                   @NotNull final BindResult bindResult)
146         throws LDAPException
147  {
148    if (bindResult.getResultCode() == ResultCode.SUCCESS)
149    {
150      final boolean displayResult;
151      if (bindResult.hasResponseControl())
152      {
153        displayResult = displaySuccessResultWithControls;
154      }
155      else
156      {
157        displayResult = displaySuccessResultWithoutControls;
158      }
159
160      if (displayResult)
161      {
162        final ArrayList<String> lines = new ArrayList<>(10);
163        lines.add("# " + INFO_REPORT_BIND_RESULT_HEADER.get());
164
165        ResultUtils.formatResult(lines, bindResult, true, false, 5, wrapColumn);
166        for (final String line : lines)
167        {
168          tool.out(line);
169        }
170        tool.out();
171      }
172    }
173    else
174    {
175      final ArrayList<String> lines = new ArrayList<>(10);
176      lines.add("# " + INFO_REPORT_BIND_RESULT_HEADER.get());
177
178      ResultUtils.formatResult(lines, bindResult, true, false, 0, wrapColumn);
179      for (final String line : lines)
180      {
181        tool.err(line);
182      }
183      tool.err();
184    }
185  }
186
187
188
189  /**
190   * {@inheritDoc}
191   */
192  @Override()
193  public void toString(@NotNull final StringBuilder buffer)
194  {
195    buffer.append("ReportBindResultLDAPConnectionPoolHealthCheck(" +
196         "displaySuccessResultWithControls=");
197    buffer.append(displaySuccessResultWithControls);
198    buffer.append(", displaySuccessResultWithoutControls=");
199    buffer.append(displaySuccessResultWithoutControls);
200    buffer.append(')');
201  }
202}