use of com.nike.riposte.server.http.RequestInfo in project riposte by Nike-Inc.
the class RoutingHandler method doChannelRead.
@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
HttpRequest nettyRequest = (HttpRequest) msg;
HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
RequestInfo request = handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary(nettyRequest, state);
// If the Netty HttpRequest is invalid, we shouldn't do any endpoint routing.
handlerUtils.throwExceptionIfNotSuccessfullyDecoded(nettyRequest);
// The HttpRequest is valid, so continue with the endpoint routing.
Pair<Endpoint<?>, String> endpointForExecution = findSingleEndpointForExecution(request);
request.setPathParamsBasedOnPathTemplate(endpointForExecution.getRight());
state.setEndpointForExecution(endpointForExecution.getLeft(), endpointForExecution.getRight());
handleSpanNameUpdateForRequestWithPathTemplate(nettyRequest, request, state);
throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize(nettyRequest, endpointForExecution.getLeft());
}
return PipelineContinuationBehavior.CONTINUE;
}
use of com.nike.riposte.server.http.RequestInfo in project riposte by Nike-Inc.
the class RequestInfoSetterHandler method doChannelRead.
@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) {
try {
HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
if (state == null || state.isResponseSendingLastChunkSent() || !ctx.channel().isActive()) {
if (state == null)
logger.error("HttpProcessingState is null for this request. This should not be possible.");
// chunk and not process anything further.
return PipelineContinuationBehavior.DO_NOT_FIRE_CONTINUE_EVENT;
}
// We have a HttpProcessingState. Process the message and continue the pipeline processing.
if (msg instanceof HttpRequest) {
// This should be done by RoutingHandler already but it doesn't hurt to double check here, and it
// keeps this handler independent in case things get refactored again in the future.
handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary((HttpRequest) msg, state);
// The above method call should set RequestInfo on the state, even if the Netty HttpRequest obj was
// invalid (if it's invalid it will default to a synthetic/dummy RequestInfo that indicates an
// error). But if it *is* invalid, we want to throw an exception here to immediately invoke
// error handling behavior.
handlerUtils.throwExceptionIfNotSuccessfullyDecoded((HttpRequest) msg);
} else if (msg instanceof HttpContent) {
HttpContent httpContentMsg = (HttpContent) msg;
handlerUtils.throwExceptionIfNotSuccessfullyDecoded(httpContentMsg);
RequestInfo<?> requestInfo = state.getRequestInfo();
if (requestInfo == null) {
throw new IllegalStateException("Found a HttpContent msg without a RequestInfo stored in the HttpProcessingState. " + "This should be impossible");
}
int currentRequestLengthInBytes = requestInfo.addContentChunk(httpContentMsg);
int configuredMaxRequestSize = getConfiguredMaxRequestSize(state.getEndpointForExecution(), globalConfiguredMaxRequestSizeInBytes);
if (!isMaxRequestSizeValidationDisabled(configuredMaxRequestSize) && currentRequestLengthInBytes > configuredMaxRequestSize) {
throw new RequestTooBigException("Request raw content length exceeded configured max request size of " + configuredMaxRequestSize);
}
}
return PipelineContinuationBehavior.CONTINUE;
} finally {
// For HttpContent messages, either requestInfo.addContentChunk() has been called and the reference count
// increased (i.e. the RequestInfo is now responsible for releasing the content when
// requestInfo.releaseAllResources() is called), or an exception has been thrown. In any case, we
// are done with any message from a pipeline perspective and can reduce its reference count.
ReferenceCountUtil.release(msg);
}
}
use of com.nike.riposte.server.http.RequestInfo in project riposte by Nike-Inc.
the class RiposteApiExceptionHandlerTest method maybeHandleErrorFromNettyInterfaceReturnsNullIfBackstopperMaybeHandleExceptionReturnsNull.
@Test
public void maybeHandleErrorFromNettyInterfaceReturnsNullIfBackstopperMaybeHandleExceptionReturnsNull() throws UnexpectedMajorExceptionHandlingError, UnexpectedMajorErrorHandlingError {
doReturn(null).when(adapterSpy).maybeHandleException(any(Throwable.class), any(RequestInfoForLogging.class));
RequestInfo requestInfoMock = mock(RequestInfo.class);
assertThat(adapterSpy.maybeHandleError(new Exception(), requestInfoMock), nullValue());
}
use of com.nike.riposte.server.http.RequestInfo in project riposte by Nike-Inc.
the class RiposteApiExceptionHandlerTest method maybeHandleErrorExplosionThrowsUnexpectedMajorErrorHandlingError.
// Note the difference between UnexpectedMajor**Exception**HandlingError and UnexpectedMajor**Error**HandlingError. I know, I know, confusing and annoying. Adapters can be like that.
@Test(expected = UnexpectedMajorErrorHandlingError.class)
public void maybeHandleErrorExplosionThrowsUnexpectedMajorErrorHandlingError() throws UnexpectedMajorExceptionHandlingError, UnexpectedMajorErrorHandlingError {
UnexpectedMajorExceptionHandlingError innerExplosion = new UnexpectedMajorExceptionHandlingError("intentional kaboom", new Exception());
doThrow(innerExplosion).when(adapterSpy).maybeHandleException(any(Throwable.class), any(RequestInfoForLogging.class));
RequestInfo requestInfoMock = mock(RequestInfo.class);
adapterSpy.maybeHandleError(new Exception(), requestInfoMock);
}
Aggregations