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