com.unboundid.ldap.sdk.unboundidds.extensions.DeliverPasswordResetTokenExtendedResult Maven / Gradle / Ivy
                 Go to download
                
        
                    Show more of this group  Show more artifacts with this name
Show all versions of unboundid-ldapsdk-commercial-edition Show documentation
                Show all versions of unboundid-ldapsdk-commercial-edition Show documentation
      The UnboundID LDAP SDK for Java is a fast, comprehensive, and easy-to-use
      Java API for communicating with LDAP directory servers and performing
      related tasks like reading and writing LDIF, encoding and decoding data
      using base64 and ASN.1 BER, and performing secure communication.  This
      package contains the Commercial Edition of the LDAP SDK, which includes
      all of the general-purpose functionality contained in the Standard
      Edition, plus additional functionality specific to UnboundID server
      products.
    
                
            /*
 * Copyright 2015-2016 UnboundID Corp.
 * All Rights Reserved.
 */
/*
 * Copyright (C) 2015-2016 UnboundID Corp.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (GPLv2 only)
 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see .
 */
package com.unboundid.ldap.sdk.unboundidds.extensions;
import java.util.ArrayList;
import com.unboundid.asn1.ASN1Element;
import com.unboundid.asn1.ASN1OctetString;
import com.unboundid.asn1.ASN1Sequence;
import com.unboundid.ldap.sdk.Control;
import com.unboundid.ldap.sdk.ExtendedResult;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.ResultCode;
import com.unboundid.util.Debug;
import com.unboundid.util.NotMutable;
import com.unboundid.util.StaticUtils;
import com.unboundid.util.ThreadSafety;
import com.unboundid.util.ThreadSafetyLevel;
import com.unboundid.util.Validator;
import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
/**
 * 
 *   NOTE:  This class is part of the Commercial Edition of the UnboundID
 *   LDAP SDK for Java.  It is not available for use in applications that
 *   include only the Standard Edition of the LDAP SDK, and is not supported for
 *   use in conjunction with non-UnboundID products.
 * 
 * This class provides an implementation of an extended result that may be used
 * to provide information about the result of processing for a deliver password
 * reset token extended request.  If the token was delivered successfully, then
 * this result will include information about the mechanism through which the
 * token was delivered.
 * 
 * If the request was processed successfully, then the extended result will have
 * an OID of 1.3.6.1.4.1.30221.2.6.46 and a value with the following encoding:
 * 
 * 
 *   DeliverPasswordResetTokenResult ::= SEQUENCE {
 *        deliveryMechanism     OCTET STRING,
 *        recipientID           [0] OCTET STRING OPTIONAL,
 *        message               [1] OCTET STRING OPTIONAL,
 *        ... }
 * 
 *
 * @see  DeliverPasswordResetTokenExtendedRequest
 */
@NotMutable()
@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
public final class DeliverPasswordResetTokenExtendedResult
       extends ExtendedResult
{
  /**
   * The OID (1.3.6.1.4.1.30221.2.6.46) for the deliver password reset token
   * extended result.
   */
  public static final String DELIVER_PW_RESET_TOKEN_RESULT_OID =
       "1.3.6.1.4.1.30221.2.6.46";
  /**
   * The BER type for the recipient ID element of the value sequence.
   */
  private static final byte RECIPIENT_ID_BER_TYPE = (byte) 0x80;
  /**
   * The BER type for the message element of the value sequence.
   */
  private static final byte DELIVERY_MESSAGE_BER_TYPE = (byte) 0x81;
  /**
   * The serial version UID for this serializable class.
   */
  private static final long serialVersionUID = 576599499447071902L;
  // The name of the mechanism by which the password reset token was delivered.
  private final String deliveryMechanism;
  // An message providing additional information about the delivery of the
  // password reset token.
  private final String deliveryMessage;
  // An identifier for the recipient of the password reset token.
  private final String recipientID;
  /**
   * Creates a new deliver password reset token extended result with the
   * provided information.
   *
   * @param  messageID          The message ID for the LDAP message that is
   *                            associated with this LDAP result.
   * @param  resultCode         The result code from the response.  It must not
   *                            be {@code null}.
   * @param  diagnosticMessage  The diagnostic message from the response, if
   *                            available.
   * @param  matchedDN          The matched DN from the response, if available.
   * @param  referralURLs       The set of referral URLs from the response, if
   *                            available.
   * @param  deliveryMechanism  The name of the mechanism by which the password
   *                            reset token was delivered, if available.  This
   *                            should be non-{@code null} for a success result.
   * @param  recipientID        An identifier for the user to whom the password
   *                            reset token was delivered.  It may be
   *                            {@code null} if no token was delivered or there
   *                            is no appropriate identifier, but if a value is
   *                            provided then it should appropriate for the
   *                            delivery mechanism (e.g., the user's e-mail
   *                            address if delivered via e-mail, a phone number
   *                            if delivered via SMS or voice call, etc.).
   * @param  deliveryMessage    An optional message providing additional
   *                            information about the password reset token
   *                            delivery, if available.  If this is
   *                            non-{@code null}, then the delivery mechanism
   *                            must also be non-null.
   * @param  responseControls   The set of controls for the response, if
   *                            available.
   */
  public DeliverPasswordResetTokenExtendedResult(final int messageID,
              final ResultCode resultCode, final String diagnosticMessage,
              final String matchedDN, final String[] referralURLs,
              final String deliveryMechanism, final String recipientID,
              final String deliveryMessage, final Control... responseControls)
  {
    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
         ((deliveryMechanism == null)
              ? null : DELIVER_PW_RESET_TOKEN_RESULT_OID),
         encodeValue(deliveryMechanism, recipientID, deliveryMessage),
         responseControls);
    this.deliveryMechanism = deliveryMechanism;
    this.recipientID       = recipientID;
    this.deliveryMessage   = deliveryMessage;
  }
  /**
   * Creates a new deliver password reset token result from the provided generic
   * extended result.
   *
   * @param  result  The generic extended result to be parsed as a deliver
   *                 password reset token result.
   *
   * @throws LDAPException  If the provided extended result cannot be parsed as
   *                         a deliver password reset token result.
   */
  public DeliverPasswordResetTokenExtendedResult(final ExtendedResult result)
       throws LDAPException
  {
    super(result);
    final ASN1OctetString value = result.getValue();
    if (value == null)
    {
      deliveryMechanism = null;
      recipientID       = null;
      deliveryMessage   = null;
      return;
    }
    try
    {
      final ASN1Element[] elements =
           ASN1Sequence.decodeAsSequence(value.getValue()).elements();
      deliveryMechanism =
           ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
      String id = null;
      String msg = null;
      for (int i=1; i < elements.length; i++)
      {
        switch (elements[i].getType())
        {
          case RECIPIENT_ID_BER_TYPE:
            id = ASN1OctetString.decodeAsOctetString(elements[i]).stringValue();
            break;
          case DELIVERY_MESSAGE_BER_TYPE:
            msg = ASN1OctetString.decodeAsOctetString(
                 elements[i]).stringValue();
            break;
          default:
            throw new LDAPException(ResultCode.DECODING_ERROR,
                 ERR_DELIVER_PW_RESET_TOKEN_RESULT_UNEXPECTED_TYPE.get(
                      StaticUtils.toHex(elements[i].getType())));
        }
      }
      recipientID = id;
      deliveryMessage = msg;
    }
    catch (final LDAPException le)
    {
      Debug.debugException(le);
      throw le;
    }
    catch (final Exception e)
    {
      Debug.debugException(e);
      throw new LDAPException(ResultCode.DECODING_ERROR,
           ERR_DELIVER_PW_RESET_TOKEN_RESULT_ERROR_DECODING_VALUE.get(
                StaticUtils.getExceptionMessage(e)),
           e);
    }
  }
  /**
   * Encodes the provided information into an ASN.1 octet string suitable for
   * use as the value of this extended result.
   *
   * @param  deliveryMechanism  The name of the mechanism by which the password
   *                            reset token was delivered, if available.  This
   *                            should be non-{@code null} for a success result.
   * @param  recipientID        An identifier for the user to whom the password
   *                            reset token was delivered.  It may be
   *                            {@code null} if no token was delivered or there
   *                            is no appropriate identifier, but if a value is
   *                            provided then it should appropriate for the
   *                            delivery mechanism (e.g., the user's e-mail
   *                            address if delivered via e-mail, a phone number
   *                            if delivered via SMS or voice call, etc.).
   * @param  deliveryMessage    An optional message providing additional
   *                            information about the password reset token
   *                            delivery, if available.  If this is
   *                            non-{@code null}, then the delivery mechanism
   *                            must also be non-null.
   *
   * @return  An ASN.1 octet string containing the encoded value, or
   *          {@code null} if the extended result should not have a value.
   */
  private static ASN1OctetString encodeValue(final String deliveryMechanism,
                                             final String recipientID,
                                             final String deliveryMessage)
  {
    if (deliveryMechanism == null)
    {
      Validator.ensureTrue((recipientID == null),
           "The delivery mechanism must be non-null if the recipient ID " +
                "is non-null.");
      Validator.ensureTrue((deliveryMessage == null),
           "The delivery mechanism must be non-null if the delivery message " +
                "is non-null.");
      return null;
    }
    final ArrayList elements = new ArrayList(3);
    elements.add(new ASN1OctetString(deliveryMechanism));
    if (recipientID != null)
    {
      elements.add(new ASN1OctetString(RECIPIENT_ID_BER_TYPE, recipientID));
    }
    if (deliveryMessage != null)
    {
      elements.add(new ASN1OctetString(DELIVERY_MESSAGE_BER_TYPE,
           deliveryMessage));
    }
    return new ASN1OctetString(new ASN1Sequence(elements).encode());
  }
  /**
   * Retrieves the name of the mechanism by which the password reset token was
   * delivered to the user, if available.
   *
   * @return  The name of the mechanism by which the password reset token was
   *          delivered to the user, or {@code null} if this is not available.
   */
  public String getDeliveryMechanism()
  {
    return deliveryMechanism;
  }
  /**
   * Retrieves an identifier for the user to whom the password reset token was
   * delivered, if available.  If a recipient ID is provided, then it should be
   * in a form appropriate to the delivery mechanism (e.g., an e-mail address
   * if the token was delivered by e-mail, a phone number if it was delivered
   * by SMS or a voice call, etc.).
   *
   * @return  An identifier for the user to whom the password reset token was
   *          delivered, or {@code null} if this is not available.
   */
  public String getRecipientID()
  {
    return recipientID;
  }
  /**
   * Retrieves a message providing additional information about the password
   * reset token delivery, if available.
   *
   * @return  A message providing additional information about the password
   *          reset token delivery, or {@code null} if this is not available.
   */
  public String getDeliveryMessage()
  {
    return deliveryMessage;
  }
  /**
   * {@inheritDoc}
   */
  @Override()
  public String getExtendedResultName()
  {
    return INFO_EXTENDED_RESULT_NAME_DELIVER_PW_RESET_TOKEN.get();
  }
  /**
   * Appends a string representation of this extended result to the provided
   * buffer.
   *
   * @param  buffer  The buffer to which a string representation of this
   *                 extended result will be appended.
   */
  @Override()
  public void toString(final StringBuilder buffer)
  {
    buffer.append("DeliverPasswordResetTokenExtendedResult(resultCode=");
    buffer.append(getResultCode());
    final int messageID = getMessageID();
    if (messageID >= 0)
    {
      buffer.append(", messageID=");
      buffer.append(messageID);
    }
    if (deliveryMechanism != null)
    {
      buffer.append(", deliveryMechanism='");
      buffer.append(deliveryMechanism);
      buffer.append('\'');
    }
    if (recipientID != null)
    {
      buffer.append(", recipientID='");
      buffer.append(recipientID);
      buffer.append('\'');
    }
    if (deliveryMessage != null)
    {
      buffer.append(", deliveryMessage='");
      buffer.append(deliveryMessage);
      buffer.append('\'');
    }
    final String diagnosticMessage = getDiagnosticMessage();
    if (diagnosticMessage != null)
    {
      buffer.append(", diagnosticMessage='");
      buffer.append(diagnosticMessage);
      buffer.append('\'');
    }
    final String matchedDN = getMatchedDN();
    if (matchedDN != null)
    {
      buffer.append(", matchedDN='");
      buffer.append(matchedDN);
      buffer.append('\'');
    }
    final String[] referralURLs = getReferralURLs();
    if (referralURLs.length > 0)
    {
      buffer.append(", referralURLs={");
      for (int i=0; i < referralURLs.length; i++)
      {
        if (i > 0)
        {
          buffer.append(", ");
        }
        buffer.append('\'');
        buffer.append(referralURLs[i]);
        buffer.append('\'');
      }
      buffer.append('}');
    }
    final Control[] responseControls = getResponseControls();
    if (responseControls.length > 0)
    {
      buffer.append(", responseControls={");
      for (int i=0; i < responseControls.length; i++)
      {
        if (i > 0)
        {
          buffer.append(", ");
        }
        buffer.append(responseControls[i]);
      }
      buffer.append('}');
    }
    buffer.append(')');
  }
}
       © 2015 - 2025 Weber Informatics LLC | Privacy Policy