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);
}
}
Aggregations