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
010 * docs/licenses/cddl.txt
011 * or http://www.opensource.org/licenses/cddl1.php.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * docs/licenses/cddl.txt.  If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 *      Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 *      Copyright 2016-2017 Ping Identity Corporation
026 */
027
028package com.unboundid.directory.sdk.broker.types;
029
030import com.unboundid.util.NotExtensible;
031
032/**
033 * The result of the status request of an Identity Authenticator. Use
034 * StatusResult.Builder to create instances of this class.
035 */
036@NotExtensible()
037public final class StatusResult
038{
039  /**
040   * Whether the authenticator is ready to process authentication requests.
041   */
042  private final boolean ready;
043
044  /**
045   * The error code if an error occurred while processing the status
046   * request.
047   */
048  private final String error;
049
050  /**
051   * The message describing the error that occurred while processing the
052   * status request.
053   */
054  private final String errorDetail;
055
056  /**
057   * Parameters to return that may be used to initiate the authentication
058   * process. May be {@code null}.
059   */
060  private final String responseParams;
061
062  /**
063   * The message that will be only be included in the trace log for diagnostic
064   * purposes.
065   */
066  private final String diagnosticMessage;
067
068
069
070  /**
071   * Builder class to build an instance of StatusResult.
072   */
073  public static class Builder
074  {
075    private boolean ready;
076    private String error;
077    private String errorDetail;
078    private String responseParams;
079    private String diagnosticMessage;
080
081
082
083    /**
084     * Specifies the JSON object specifying the parameters to return that may be
085     * used to initiate the authentication process.
086     * <p>
087     * Since this response is visible to the client in a potentially
088     * unauthenticated context, the authenticator should not expose any
089     * sensitive user or server information in the response.
090     * @param responseParams  The parameters to return.
091     * @return  this builder.
092     */
093    public Builder setResponseParams(final String responseParams)
094    {
095      this.responseParams = responseParams;
096      return this;
097    }
098
099    /**
100     * Specifies the message that will be only be included in the trace log
101     * for diagnostic purposes.
102     * @param diagnosticMessage  The diagnostic message
103     * @return  this builder.
104     */
105    public Builder setDiagnosticMessage(final String diagnosticMessage)
106    {
107      this.diagnosticMessage = diagnosticMessage;
108      return this;
109    }
110
111
112
113    /**
114     * Builds a new AuthenticationResult.
115     *
116     * @return  A new AuthenticationResult.
117     */
118    public StatusResult build()
119    {
120      return new StatusResult(
121          ready,
122          error,
123          errorDetail,
124          responseParams,
125          diagnosticMessage);
126    }
127  }
128
129
130
131  /**
132   * Create a new builder for a ready response.
133   * @return  A new builder for a ready response.
134   */
135  public static Builder ready()
136  {
137    final Builder b = new Builder();
138    b.ready = true;
139    return b;
140  }
141
142
143
144  /**
145   * Create a new builder for a not ready response. An authenticator
146   * should return a not ready response if it may not be used to authenticate
147   * the user/client/etc... at this time.
148   * @return  A new builder for a not ready response.
149   */
150  public static Builder notReady()
151  {
152    final Builder b = new Builder();
153    b.ready = false;
154    return b;
155  }
156
157
158
159  /**
160   * Create a new builder for an error response. An authenticator
161   * should return an error response if an error occurred while determining
162   * whether the authenticator is ready.
163   * <p>
164   * Since this response is visible to the client in a potentially
165   * unauthenticated context, the authenticator should not expose any
166   * sensitive user or server information in the error response.
167   * @param error The error code.
168   * @param errorDetail The details about the error.
169   *
170   * @return  A new builder for an error response.
171   */
172  public static Builder notReady(final String error, final String errorDetail)
173  {
174    final Builder b = new Builder();
175    b.ready = false;
176    b.error = error;
177    b.errorDetail = errorDetail;
178    return b;
179  }
180
181
182
183  /**
184   * Create a new StatusResult from the provided information.
185   * @param ready           Whether the authenticator is ready.
186   * @param error           The error code.
187   * @param errorDetail     The error detail.
188   * @param responseParams  The response parameters.
189   * @param diagnosticMessage  The diagnostic message.
190   */
191  private StatusResult(
192      final boolean ready,
193      final String error,
194      final String errorDetail,
195      final String responseParams,
196      final String diagnosticMessage)
197  {
198    this.ready = ready;
199    this.error = error;
200    this.errorDetail = errorDetail;
201    this.responseParams = responseParams;
202    this.diagnosticMessage = diagnosticMessage;
203  }
204
205
206
207  /**
208   * Indicates whether this authenticator is ready to process authentication
209   * requests.
210   *
211   * @return {@code true} if this authenticator is ready, or
212   *         {@code false} otherwise.
213   */
214  public boolean isReady()
215  {
216    return ready;
217  }
218
219
220
221  /**
222   * Retrieve the error code to return if an error occurred while processing the
223   * authentication request or {@code null} if an error did not occur.
224   *
225   * @return The error code.
226   */
227  public String getError()
228  {
229    return error;
230  }
231
232
233
234  /**
235   * Retrieve the message to return if an error occurred while processing
236   * the authentication request describing the details of the error
237   * or {@code null} if an error did not occur.
238   *
239   * @return The error details.
240   */
241  public String getErrorDetail()
242  {
243    return errorDetail;
244  }
245
246
247
248  /**
249   * Retrieve the JSON object specifying the parameters to return that may be
250   * used to initiate the authentication process. This string can be parsed
251   * using any JSON library. For example, Jackson's ObjectMapper.readTree() or
252   * the UnboundID LDAP SDK's JSONObject constructor.
253   * @return  The parameters to return, may be {@code null}.
254   */
255  public String getResponseParams()
256  {
257    return responseParams;
258  }
259
260  /**
261   * Gets the message that will be only be included in the trace log
262   * for diagnostic purposes.
263   *
264   * @return The message that will be only be included in the trace log
265   */
266  public String getDiagnosticMessage()
267  {
268    return diagnosticMessage;
269  }
270
271
272
273  /**
274   * {@inheritDoc}
275   */
276  @Override
277  public boolean equals(final Object o)
278  {
279    if (this == o)
280    {
281      return true;
282    }
283    if (o == null || getClass() != o.getClass())
284    {
285      return false;
286    }
287
288    StatusResult that = (StatusResult) o;
289
290    if (ready != that.ready)
291    {
292      return false;
293    }
294    if (error != null ? !error.equals(that.error) : that.error != null)
295    {
296      return false;
297    }
298    if (errorDetail != null ?
299        !errorDetail.equals(that.errorDetail) : that.errorDetail != null)
300    {
301      return false;
302    }
303    return responseParams != null ?
304        !responseParams.equals(that.responseParams) :
305        that.responseParams != null;
306
307  }
308
309
310
311  /**
312   * {@inheritDoc}
313   */
314  @Override
315  public int hashCode()
316  {
317    int result = (ready ? 1 : 0);
318    result = 31 * result + (error != null ? error.hashCode() : 0);
319    result = 31 * result + (errorDetail != null ? errorDetail.hashCode() : 0);
320    result = 31 * result +
321        (responseParams != null ? responseParams.hashCode() : 0);
322    return result;
323  }
324
325
326
327  /**
328   * {@inheritDoc}
329   */
330  @Override
331  public String toString()
332  {
333    final StringBuilder sb = new StringBuilder("StatusResult{");
334    sb.append("ready=").append(ready);
335    sb.append(", error='").append(error).append('\'');
336    sb.append(", errorDetail='").append(errorDetail).append('\'');
337    sb.append(", responseParams='").append(responseParams).append('\'');
338    sb.append('}');
339    return sb.toString();
340  }
341}