private static void checkDecoderMap(Map<RequestOpCode, MessageDecoder<?>> decoderMap) { Set<RequestOpCode> opsWithoutDecoder = EnumSet.of(RequestOpCode.OP_MSG, RequestOpCode.RESERVED); for (RequestOpCode value : RequestOpCode.values()) { if (opsWithoutDecoder.contains(value)) { continue; } if (!decoderMap.containsKey(value)) { throw new AssertionError("There is no decoder for operation " + value); } } } }
@Override protected void decodeLittleEndian( ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects ) throws Exception { if (byteBuf instanceof EmptyByteBuf) { //TODO: This is a workaround. Check how to prevent calling decode on channel inactive return; } // Header final RequestBaseMessage requestBaseMessage = BaseMessageDecoder.decode( channelHandlerContext, byteBuf); byteBuf.skipBytes(Ints.BYTES); // Ignore responseTo field in header int requestOpCodeInt = byteBuf.readInt(); RequestOpCode requestOpCode = RequestOpCode.getByOpcode(requestOpCodeInt); if (null == requestOpCode) { LOGGER.warn(INVALID_OPCODE_MESSAGE + requestOpCodeInt); throw new IllegalOperationException(requestOpCodeInt); } // Body MessageDecoder<?> messageDecoder = decoderLocator.getByOpCode(requestOpCode); if (null == messageDecoder) { LOGGER.error(OPERATION_NOT_IMPLEMENTED + requestOpCode); throw new UnsupportedOperationException(OPERATION_NOT_IMPLEMENTED + requestOpCode); } objects.add(messageDecoder.decode(byteBuf, requestBaseMessage)); } }
@Override public boolean handleError(RequestOpCode requestOpCode, MessageReplier messageReplier, Throwable throwable) { Connection connection = getConnection(messageReplier); ReplyMessage handleMongodbException; if (throwable instanceof MongoException) { handleMongodbException = errorHandler.handleMongodbException(connection, messageReplier.getRequestId(), requestOpCode.canReply(), (MongoException) throwable ); } else { handleMongodbException = errorHandler.handleUnexpectedError( connection, messageReplier.getRequestId(), requestOpCode.canReply(), throwable ); } if (requestOpCode.canReply() && handleMongodbException != null) { messageReplier.replyMessage(handleMongodbException); } return true; } }