Search in sources :

Example 1 with ChunkedOutboundMessage

use of com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage in project riposte by Nike-Inc.

the class RequestHasBeenHandledVerificationHandler method doChannelRead.

@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg == null) {
        throw new InvalidRipostePipelineException("msg cannot be null at this stage of the pipeline. An endpoint handler should have fired a valid " + "OutboundMessage. invalid_riposte_pipeline=true");
    }
    if (!(msg instanceof OutboundMessage)) {
        throw new InvalidRipostePipelineException("Expected msg to be a OutboundMessage, but instead found: " + msg.getClass().getName() + ". invalid_riposte_pipeline=true");
    }
    HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
    if (state == null) {
        throw new InvalidRipostePipelineException("Found null HttpProcessingState in the channel, which is not allowed at this point. " + "invalid_riposte_pipeline=true");
    }
    ResponseInfo<?> responseInfo = state.getResponseInfo();
    if (responseInfo == null) {
        throw new InvalidRipostePipelineException("Found null ResponseInfo in the channel state, which is not allowed at this point. " + "An endpoint handler should have set a ResponseInfo on the state. invalid_riposte_pipeline=true");
    }
    if (responseInfo.isChunkedResponse() && !(msg instanceof ChunkedOutboundMessage)) {
        throw new InvalidRipostePipelineException("ResponseInfo.isChunkedResponse() indicates a chunked response, but the message was not a " + "ChunkedOutboundMessage. msg_type=" + msg.getClass().getName() + ", invalid_riposte_pipeline=true");
    }
    if (!responseInfo.isChunkedResponse() && !(msg instanceof LastOutboundMessageSendFullResponseInfo)) {
        throw new InvalidRipostePipelineException("ResponseInfo.isChunkedResponse() indicates a full response, but the message was not a " + "LastOutboundMessageSendFullResponseInfo. msg_type=" + msg.getClass().getName() + ", invalid_riposte_pipeline=true");
    }
    return CONTINUE;
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ChunkedOutboundMessage(com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage) OutboundMessage(com.nike.riposte.server.channelpipeline.message.OutboundMessage) InvalidRipostePipelineException(com.nike.riposte.server.error.exception.InvalidRipostePipelineException) ChunkedOutboundMessage(com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage) LastOutboundMessageSendFullResponseInfo(com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendFullResponseInfo)

Example 2 with ChunkedOutboundMessage

use of com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage in project riposte by Nike-Inc.

the class ResponseSenderHandler method doSendResponse.

protected void doSendResponse(ChannelHandlerContext ctx, Object msg) throws JsonProcessingException {
    // Only bother trying to send the response if the channel is active.
    if (!ctx.channel().isActive()) {
        if (logger.isDebugEnabled()) {
            runnableWithTracingAndMdc(() -> logger.debug("The channel is closed. Ignoring this method call to send response."), ctx).run();
        }
        return;
    }
    // The HttpProcessingState will never be null thanks to ExceptionHandlingHandler
    HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
    if (state.isResponseSendingLastChunkSent()) {
        if (logger.isDebugEnabled()) {
            runnableWithTracingAndMdc(() -> logger.debug("A response has already been sent. " + "Ignoring this method call to send response."), ctx).run();
        }
        return;
    }
    RequestInfo<?> requestInfo = state.getRequestInfo();
    if (requestInfo == null)
        requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    ResponseInfo<?> responseInfo = state.getResponseInfo();
    Endpoint<?> endpointExecuted = state.getEndpointForExecution();
    ObjectMapper customSerializer = (endpointExecuted == null) ? null : endpointExecuted.customResponseContentSerializer(requestInfo);
    if (msg instanceof ChunkedOutboundMessage) {
        // Chunked message. Stream it out.
        responseSender.sendResponseChunk(ctx, requestInfo, responseInfo, (ChunkedOutboundMessage) msg);
    } else {
        // Full message. Send it.
        if (getErrorResponseBodyIfPossible(responseInfo) != null) {
            // noinspection unchecked
            responseSender.sendErrorResponse(ctx, requestInfo, (ResponseInfo<ErrorResponseBody>) responseInfo);
        } else {
            responseSender.sendFullResponse(ctx, requestInfo, responseInfo, customSerializer);
        }
    }
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) ChunkedOutboundMessage(com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ChunkedOutboundMessage

use of com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage in project riposte by Nike-Inc.

the class ResponseSenderHandler method sendResponse.

protected void sendResponse(ChannelHandlerContext ctx, Object msg) throws JsonProcessingException {
    try {
        HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        if (state.isResponseSendingLastChunkSent()) {
            if (logger.isDebugEnabled()) {
                runnableWithTracingAndMdc(() -> logger.debug("A response has already been sent. " + "Ignoring this method call to send response."), ctx).run();
            }
            return;
        }
        RequestInfo<?> requestInfo = state.getRequestInfo();
        if (requestInfo == null)
            requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
        ResponseInfo<?> responseInfo = state.getResponseInfo();
        Endpoint<?> endpointExecuted = state.getEndpointForExecution();
        ObjectMapper customSerializer = (endpointExecuted == null) ? null : endpointExecuted.customResponseContentSerializer(requestInfo);
        if (msg != null && msg instanceof ChunkedOutboundMessage) {
            // Chunked message. Stream it out.
            responseSender.sendResponseChunk(ctx, requestInfo, responseInfo, (ChunkedOutboundMessage) msg);
        } else {
            // Full message. Send it.
            if (containsErrorResponseBody(responseInfo)) {
                // noinspection unchecked
                responseSender.sendErrorResponse(ctx, requestInfo, (ResponseInfo<ErrorResponseBody>) responseInfo);
            } else
                responseSender.sendFullResponse(ctx, requestInfo, responseInfo, customSerializer);
        }
    } catch (Throwable t) {
        runnableWithTracingAndMdc(() -> logger.error("An unexpected error occurred while attempting to send a response.", t), ctx).run();
        throw t;
    }
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) ChunkedOutboundMessage(com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with ChunkedOutboundMessage

use of com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage in project riposte by Nike-Inc.

the class ResponseSenderHandlerTest method doSendResponse_calls_responseSender_sendResponseChunk_when_msg_is_a_ChunkedOutboundMessage.

@Test
public void doSendResponse_calls_responseSender_sendResponseChunk_when_msg_is_a_ChunkedOutboundMessage() throws JsonProcessingException {
    // given
    ChunkedOutboundMessage chunkedMsgMock = mock(ChunkedOutboundMessage.class);
    RequestInfo<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    doReturn(requestInfo).when(stateMock).getRequestInfo();
    // when
    handlerSpy.doSendResponse(ctxMock, chunkedMsgMock);
    // then
    verify(responseSenderMock).sendResponseChunk(ctxMock, requestInfo, responseInfo, chunkedMsgMock);
}
Also used : ChunkedOutboundMessage(com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage) Test(org.junit.Test)

Aggregations

ChunkedOutboundMessage (com.nike.riposte.server.channelpipeline.message.ChunkedOutboundMessage)4 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ErrorResponseBody (com.nike.riposte.server.error.handler.ErrorResponseBody)2 LastOutboundMessageSendFullResponseInfo (com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendFullResponseInfo)1 OutboundMessage (com.nike.riposte.server.channelpipeline.message.OutboundMessage)1 InvalidRipostePipelineException (com.nike.riposte.server.error.exception.InvalidRipostePipelineException)1 Test (org.junit.Test)1