Search in sources :

Example 1 with InterceptException

use of org.apache.pulsar.common.intercept.InterceptException in project pulsar by apache.

the class ExceptionHandlerTest method testHandle.

@Test
@SneakyThrows
public void testHandle() {
    String restriction = "Reach the max tenants [5] restriction";
    String internal = "internal exception";
    String illegal = "illegal argument exception ";
    ExceptionHandler handler = new ExceptionHandler();
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    handler.handle(response, new InterceptException(PRECONDITION_FAILED_412, restriction));
    Mockito.verify(response).sendError(PRECONDITION_FAILED_412, restriction);
    handler.handle(response, new InterceptException(INTERNAL_SERVER_ERROR_500, internal));
    Mockito.verify(response).sendError(INTERNAL_SERVER_ERROR_500, internal);
    handler.handle(response, new IllegalArgumentException(illegal));
    Mockito.verify(response).sendError(INTERNAL_SERVER_ERROR_500, illegal);
    Response response2 = Mockito.mock(Response.class);
    HttpChannel httpChannel = Mockito.mock(HttpChannel.class);
    Mockito.when(response2.getHttpChannel()).thenReturn(httpChannel);
    handler.handle(response2, new InterceptException(PRECONDITION_FAILED_412, restriction));
    Mockito.verify(httpChannel).sendResponse(Mockito.any(), Mockito.any(), Mockito.anyBoolean());
}
Also used : InterceptException(org.apache.pulsar.common.intercept.InterceptException) Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpChannel(org.eclipse.jetty.server.HttpChannel) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test) SneakyThrows(lombok.SneakyThrows)

Example 2 with InterceptException

use of org.apache.pulsar.common.intercept.InterceptException in project pulsar by apache.

the class PulsarDecoder method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HAProxyMessage) {
        HAProxyMessage proxyMessage = (HAProxyMessage) msg;
        this.proxyMessage = proxyMessage;
        proxyMessage.release();
        return;
    }
    // Get a buffer that contains the full frame
    ByteBuf buffer = (ByteBuf) msg;
    try {
        // De-serialize the command
        int cmdSize = (int) buffer.readUnsignedInt();
        cmd.parseFrom(buffer, cmdSize);
        if (log.isDebugEnabled()) {
            log.debug("[{}] Received cmd {}", ctx.channel().remoteAddress(), cmd.getType());
        }
        messageReceived();
        switch(cmd.getType()) {
            case PARTITIONED_METADATA:
                checkArgument(cmd.hasPartitionMetadata());
                try {
                    interceptCommand(cmd);
                    handlePartitionMetadataRequest(cmd.getPartitionMetadata());
                } catch (InterceptException e) {
                    ctx.writeAndFlush(Commands.newPartitionMetadataResponse(getServerError(e.getErrorCode()), e.getMessage(), cmd.getPartitionMetadata().getRequestId()));
                }
                break;
            case PARTITIONED_METADATA_RESPONSE:
                checkArgument(cmd.hasPartitionMetadataResponse());
                handlePartitionResponse(cmd.getPartitionMetadataResponse());
                break;
            case LOOKUP:
                checkArgument(cmd.hasLookupTopic());
                handleLookup(cmd.getLookupTopic());
                break;
            case LOOKUP_RESPONSE:
                checkArgument(cmd.hasLookupTopicResponse());
                handleLookupResponse(cmd.getLookupTopicResponse());
                break;
            case ACK:
                checkArgument(cmd.hasAck());
                handleAck(cmd.getAck());
                break;
            case ACK_RESPONSE:
                checkArgument(cmd.hasAckResponse());
                handleAckResponse(cmd.getAckResponse());
                break;
            case CLOSE_CONSUMER:
                checkArgument(cmd.hasCloseConsumer());
                safeInterceptCommand(cmd);
                handleCloseConsumer(cmd.getCloseConsumer());
                break;
            case CLOSE_PRODUCER:
                checkArgument(cmd.hasCloseProducer());
                safeInterceptCommand(cmd);
                handleCloseProducer(cmd.getCloseProducer());
                break;
            case CONNECT:
                checkArgument(cmd.hasConnect());
                handleConnect(cmd.getConnect());
                break;
            case CONNECTED:
                checkArgument(cmd.hasConnected());
                handleConnected(cmd.getConnected());
                break;
            case ERROR:
                checkArgument(cmd.hasError());
                handleError(cmd.getError());
                break;
            case FLOW:
                checkArgument(cmd.hasFlow());
                handleFlow(cmd.getFlow());
                break;
            case MESSAGE:
                {
                    checkArgument(cmd.hasMessage());
                    handleMessage(cmd.getMessage(), buffer);
                    break;
                }
            case PRODUCER:
                checkArgument(cmd.hasProducer());
                try {
                    interceptCommand(cmd);
                    handleProducer(cmd.getProducer());
                } catch (InterceptException e) {
                    ctx.writeAndFlush(Commands.newError(cmd.getProducer().getRequestId(), getServerError(e.getErrorCode()), e.getMessage()));
                }
                break;
            case SEND:
                {
                    checkArgument(cmd.hasSend());
                    try {
                        interceptCommand(cmd);
                        // Store a buffer marking the content + headers
                        ByteBuf headersAndPayload = buffer.markReaderIndex();
                        handleSend(cmd.getSend(), headersAndPayload);
                    } catch (InterceptException e) {
                        ctx.writeAndFlush(Commands.newSendError(cmd.getSend().getProducerId(), cmd.getSend().getSequenceId(), getServerError(e.getErrorCode()), e.getMessage()));
                    }
                    break;
                }
            case SEND_ERROR:
                checkArgument(cmd.hasSendError());
                handleSendError(cmd.getSendError());
                break;
            case SEND_RECEIPT:
                checkArgument(cmd.hasSendReceipt());
                handleSendReceipt(cmd.getSendReceipt());
                break;
            case SUBSCRIBE:
                checkArgument(cmd.hasSubscribe());
                try {
                    interceptCommand(cmd);
                    handleSubscribe(cmd.getSubscribe());
                } catch (InterceptException e) {
                    ctx.writeAndFlush(Commands.newError(cmd.getSubscribe().getRequestId(), getServerError(e.getErrorCode()), e.getMessage()));
                }
                break;
            case SUCCESS:
                checkArgument(cmd.hasSuccess());
                handleSuccess(cmd.getSuccess());
                break;
            case PRODUCER_SUCCESS:
                checkArgument(cmd.hasProducerSuccess());
                handleProducerSuccess(cmd.getProducerSuccess());
                break;
            case UNSUBSCRIBE:
                checkArgument(cmd.hasUnsubscribe());
                safeInterceptCommand(cmd);
                handleUnsubscribe(cmd.getUnsubscribe());
                break;
            case SEEK:
                checkArgument(cmd.hasSeek());
                try {
                    interceptCommand(cmd);
                    handleSeek(cmd.getSeek());
                } catch (InterceptException e) {
                    ctx.writeAndFlush(Commands.newError(cmd.getSeek().getRequestId(), getServerError(e.getErrorCode()), e.getMessage()));
                }
                break;
            case PING:
                checkArgument(cmd.hasPing());
                handlePing(cmd.getPing());
                break;
            case PONG:
                checkArgument(cmd.hasPong());
                handlePong(cmd.getPong());
                break;
            case REDELIVER_UNACKNOWLEDGED_MESSAGES:
                checkArgument(cmd.hasRedeliverUnacknowledgedMessages());
                handleRedeliverUnacknowledged(cmd.getRedeliverUnacknowledgedMessages());
                break;
            case CONSUMER_STATS:
                checkArgument(cmd.hasConsumerStats());
                handleConsumerStats(cmd.getConsumerStats());
                break;
            case CONSUMER_STATS_RESPONSE:
                checkArgument(cmd.hasConsumerStatsResponse());
                handleConsumerStatsResponse(cmd.getConsumerStatsResponse());
                break;
            case REACHED_END_OF_TOPIC:
                checkArgument(cmd.hasReachedEndOfTopic());
                handleReachedEndOfTopic(cmd.getReachedEndOfTopic());
                break;
            case GET_LAST_MESSAGE_ID:
                checkArgument(cmd.hasGetLastMessageId());
                handleGetLastMessageId(cmd.getGetLastMessageId());
                break;
            case GET_LAST_MESSAGE_ID_RESPONSE:
                checkArgument(cmd.hasGetLastMessageIdResponse());
                handleGetLastMessageIdSuccess(cmd.getGetLastMessageIdResponse());
                break;
            case ACTIVE_CONSUMER_CHANGE:
                handleActiveConsumerChange(cmd.getActiveConsumerChange());
                break;
            case GET_TOPICS_OF_NAMESPACE:
                checkArgument(cmd.hasGetTopicsOfNamespace());
                try {
                    interceptCommand(cmd);
                    handleGetTopicsOfNamespace(cmd.getGetTopicsOfNamespace());
                } catch (InterceptException e) {
                    ctx.writeAndFlush(Commands.newError(cmd.getGetTopicsOfNamespace().getRequestId(), getServerError(e.getErrorCode()), e.getMessage()));
                }
                break;
            case GET_TOPICS_OF_NAMESPACE_RESPONSE:
                checkArgument(cmd.hasGetTopicsOfNamespaceResponse());
                handleGetTopicsOfNamespaceSuccess(cmd.getGetTopicsOfNamespaceResponse());
                break;
            case GET_SCHEMA:
                checkArgument(cmd.hasGetSchema());
                try {
                    interceptCommand(cmd);
                    handleGetSchema(cmd.getGetSchema());
                } catch (InterceptException e) {
                    ctx.writeAndFlush(Commands.newGetSchemaResponseError(cmd.getGetSchema().getRequestId(), getServerError(e.getErrorCode()), e.getMessage()));
                }
                break;
            case GET_SCHEMA_RESPONSE:
                checkArgument(cmd.hasGetSchemaResponse());
                handleGetSchemaResponse(cmd.getGetSchemaResponse());
                break;
            case GET_OR_CREATE_SCHEMA:
                checkArgument(cmd.hasGetOrCreateSchema());
                try {
                    interceptCommand(cmd);
                    handleGetOrCreateSchema(cmd.getGetOrCreateSchema());
                } catch (InterceptException e) {
                    ctx.writeAndFlush(Commands.newGetOrCreateSchemaResponseError(cmd.getGetOrCreateSchema().getRequestId(), getServerError(e.getErrorCode()), e.getMessage()));
                }
                break;
            case GET_OR_CREATE_SCHEMA_RESPONSE:
                checkArgument(cmd.hasGetOrCreateSchemaResponse());
                handleGetOrCreateSchemaResponse(cmd.getGetOrCreateSchemaResponse());
                break;
            case AUTH_CHALLENGE:
                checkArgument(cmd.hasAuthChallenge());
                handleAuthChallenge(cmd.getAuthChallenge());
                break;
            case AUTH_RESPONSE:
                checkArgument(cmd.hasAuthResponse());
                handleAuthResponse(cmd.getAuthResponse());
                break;
            case TC_CLIENT_CONNECT_REQUEST:
                checkArgument(cmd.hasTcClientConnectRequest());
                handleTcClientConnectRequest(cmd.getTcClientConnectRequest());
                break;
            case TC_CLIENT_CONNECT_RESPONSE:
                checkArgument(cmd.hasTcClientConnectResponse());
                handleTcClientConnectResponse(cmd.getTcClientConnectResponse());
                break;
            case NEW_TXN:
                checkArgument(cmd.hasNewTxn());
                handleNewTxn(cmd.getNewTxn());
                break;
            case NEW_TXN_RESPONSE:
                checkArgument(cmd.hasNewTxnResponse());
                handleNewTxnResponse(cmd.getNewTxnResponse());
                break;
            case ADD_PARTITION_TO_TXN:
                checkArgument(cmd.hasAddPartitionToTxn());
                handleAddPartitionToTxn(cmd.getAddPartitionToTxn());
                break;
            case ADD_PARTITION_TO_TXN_RESPONSE:
                checkArgument(cmd.hasAddPartitionToTxnResponse());
                handleAddPartitionToTxnResponse(cmd.getAddPartitionToTxnResponse());
                break;
            case ADD_SUBSCRIPTION_TO_TXN:
                checkArgument(cmd.hasAddSubscriptionToTxn());
                handleAddSubscriptionToTxn(cmd.getAddSubscriptionToTxn());
                break;
            case ADD_SUBSCRIPTION_TO_TXN_RESPONSE:
                checkArgument(cmd.hasAddSubscriptionToTxnResponse());
                handleAddSubscriptionToTxnResponse(cmd.getAddSubscriptionToTxnResponse());
                break;
            case END_TXN:
                checkArgument(cmd.hasEndTxn());
                handleEndTxn(cmd.getEndTxn());
                break;
            case END_TXN_RESPONSE:
                checkArgument(cmd.hasEndTxnResponse());
                handleEndTxnResponse(cmd.getEndTxnResponse());
                break;
            case END_TXN_ON_PARTITION:
                checkArgument(cmd.hasEndTxnOnPartition());
                handleEndTxnOnPartition(cmd.getEndTxnOnPartition());
                break;
            case END_TXN_ON_PARTITION_RESPONSE:
                checkArgument(cmd.hasEndTxnOnPartitionResponse());
                handleEndTxnOnPartitionResponse(cmd.getEndTxnOnPartitionResponse());
                break;
            case END_TXN_ON_SUBSCRIPTION:
                checkArgument(cmd.hasEndTxnOnSubscription());
                handleEndTxnOnSubscription(cmd.getEndTxnOnSubscription());
                break;
            case END_TXN_ON_SUBSCRIPTION_RESPONSE:
                checkArgument(cmd.hasEndTxnOnSubscriptionResponse());
                handleEndTxnOnSubscriptionResponse(cmd.getEndTxnOnSubscriptionResponse());
                break;
            default:
                break;
        }
    } finally {
        buffer.release();
    }
}
Also used : InterceptException(org.apache.pulsar.common.intercept.InterceptException) HAProxyMessage(io.netty.handler.codec.haproxy.HAProxyMessage) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

InterceptException (org.apache.pulsar.common.intercept.InterceptException)2 ByteBuf (io.netty.buffer.ByteBuf)1 HAProxyMessage (io.netty.handler.codec.haproxy.HAProxyMessage)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 SneakyThrows (lombok.SneakyThrows)1 HttpChannel (org.eclipse.jetty.server.HttpChannel)1 Response (org.eclipse.jetty.server.Response)1 Test (org.testng.annotations.Test)1