Search in sources :

Example 11 with ASN1StreamReaderSequence

use of com.unboundid.asn1.ASN1StreamReaderSequence in project ldapsdk by pingidentity.

the class LDAPMessage method readFrom.

/**
 * Reads an LDAP message from the provided ASN.1 stream reader.
 *
 * @param  reader               The ASN.1 stream reader from which the LDAP
 *                              message should be read.
 * @param  ignoreSocketTimeout  Indicates whether to ignore socket timeout
 *                              exceptions caught during processing.  This
 *                              should be {@code true} when the associated
 *                              connection is operating in asynchronous mode,
 *                              and {@code false} when operating in
 *                              synchronous mode.  In either case, exceptions
 *                              will not be ignored for the first read, since
 *                              that will be handled by the connection reader.
 *
 * @return  The decoded LDAP message, or {@code null} if the end of the input
 *          stream has been reached.
 *
 * @throws  LDAPException  If an error occurs while attempting to read or
 *                         decode the LDAP message.
 */
@Nullable()
public static LDAPMessage readFrom(@NotNull final ASN1StreamReader reader, final boolean ignoreSocketTimeout) throws LDAPException {
    final ASN1StreamReaderSequence messageSequence;
    try {
        reader.setIgnoreSocketTimeout(false, ignoreSocketTimeout);
        messageSequence = reader.beginSequence();
        if (messageSequence == null) {
            return null;
        }
    } catch (final IOException ioe) {
        if (!((ioe instanceof SocketTimeoutException) || (ioe instanceof InterruptedIOException))) {
            Debug.debugException(ioe);
        }
        throw new LDAPException(ResultCode.SERVER_DOWN, ERR_MESSAGE_IO_ERROR.get(StaticUtils.getExceptionMessage(ioe)), ioe);
    } catch (final Exception e) {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MESSAGE_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
    }
    try {
        reader.setIgnoreSocketTimeout(ignoreSocketTimeout, ignoreSocketTimeout);
        final int messageID = reader.readInteger();
        final ProtocolOp protocolOp;
        final byte protocolOpType = (byte) reader.peek();
        switch(protocolOpType) {
            case PROTOCOL_OP_TYPE_BIND_REQUEST:
                protocolOp = new BindRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_BIND_RESPONSE:
                protocolOp = new BindResponseProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_UNBIND_REQUEST:
                protocolOp = new UnbindRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_SEARCH_REQUEST:
                protocolOp = new SearchRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_SEARCH_RESULT_ENTRY:
                protocolOp = new SearchResultEntryProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_SEARCH_RESULT_REFERENCE:
                protocolOp = new SearchResultReferenceProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_SEARCH_RESULT_DONE:
                protocolOp = new SearchResultDoneProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_MODIFY_REQUEST:
                protocolOp = new ModifyRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_MODIFY_RESPONSE:
                protocolOp = new ModifyResponseProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_ADD_REQUEST:
                protocolOp = new AddRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_ADD_RESPONSE:
                protocolOp = new AddResponseProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_DELETE_REQUEST:
                protocolOp = new DeleteRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_DELETE_RESPONSE:
                protocolOp = new DeleteResponseProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_MODIFY_DN_REQUEST:
                protocolOp = new ModifyDNRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_MODIFY_DN_RESPONSE:
                protocolOp = new ModifyDNResponseProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_COMPARE_REQUEST:
                protocolOp = new CompareRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_COMPARE_RESPONSE:
                protocolOp = new CompareResponseProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_ABANDON_REQUEST:
                protocolOp = new AbandonRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_EXTENDED_REQUEST:
                protocolOp = new ExtendedRequestProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_EXTENDED_RESPONSE:
                protocolOp = new ExtendedResponseProtocolOp(reader);
                break;
            case PROTOCOL_OP_TYPE_INTERMEDIATE_RESPONSE:
                protocolOp = new IntermediateResponseProtocolOp(reader);
                break;
            default:
                throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MESSAGE_INVALID_PROTOCOL_OP_TYPE.get(StaticUtils.toHex(protocolOpType)));
        }
        final ArrayList<Control> controls = new ArrayList<>(5);
        if (messageSequence.hasMoreElements()) {
            final ASN1StreamReaderSequence controlSequence = reader.beginSequence();
            while (controlSequence.hasMoreElements()) {
                controls.add(Control.readFrom(reader));
            }
        }
        return new LDAPMessage(messageID, protocolOp, controls);
    } catch (final LDAPException le) {
        Debug.debugException(le);
        throw le;
    } catch (final IOException ioe) {
        Debug.debugException(ioe);
        if ((ioe instanceof SocketTimeoutException) || (ioe instanceof InterruptedIOException)) {
            // connection to be terminated.
            throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MESSAGE_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(ioe)));
        } else {
            throw new LDAPException(ResultCode.SERVER_DOWN, ERR_MESSAGE_IO_ERROR.get(StaticUtils.getExceptionMessage(ioe)), ioe);
        }
    } catch (final Exception e) {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MESSAGE_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
    }
}
Also used : ASN1StreamReaderSequence(com.unboundid.asn1.ASN1StreamReaderSequence) InterruptedIOException(java.io.InterruptedIOException) ArrayList(java.util.ArrayList) Control(com.unboundid.ldap.sdk.Control) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) LDAPException(com.unboundid.ldap.sdk.LDAPException) Nullable(com.unboundid.util.Nullable)

Aggregations

ASN1StreamReaderSequence (com.unboundid.asn1.ASN1StreamReaderSequence)11 NotNull (com.unboundid.util.NotNull)8 ArrayList (java.util.ArrayList)8 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)5 ASN1Exception (com.unboundid.asn1.ASN1Exception)3 LDAPException (com.unboundid.ldap.sdk.LDAPException)3 Nullable (com.unboundid.util.Nullable)2 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 ASN1Element (com.unboundid.asn1.ASN1Element)1 ASN1StreamReader (com.unboundid.asn1.ASN1StreamReader)1 ASN1StreamReaderSet (com.unboundid.asn1.ASN1StreamReaderSet)1 Control (com.unboundid.ldap.sdk.Control)1 LDAPRuntimeException (com.unboundid.ldap.sdk.LDAPRuntimeException)1 ResultCode (com.unboundid.ldap.sdk.ResultCode)1 JSONObjectFilter (com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 KeyStoreException (java.security.KeyStoreException)1