Search in sources :

Example 46 with RequestInfo

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;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Endpoint(com.nike.riposte.server.http.Endpoint) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) RequestInfo(com.nike.riposte.server.http.RequestInfo)

Example 47 with RequestInfo

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);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) RequestTooBigException(com.nike.riposte.server.error.exception.RequestTooBigException) RequestInfo(com.nike.riposte.server.http.RequestInfo) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 48 with RequestInfo

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());
}
Also used : RequestInfoForLogging(com.nike.backstopper.handler.RequestInfoForLogging) RequestInfo(com.nike.riposte.server.http.RequestInfo) Test(org.junit.Test)

Example 49 with RequestInfo

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);
}
Also used : UnexpectedMajorExceptionHandlingError(com.nike.backstopper.handler.UnexpectedMajorExceptionHandlingError) RequestInfoForLogging(com.nike.backstopper.handler.RequestInfoForLogging) RequestInfo(com.nike.riposte.server.http.RequestInfo) Test(org.junit.Test)

Aggregations

RequestInfo (com.nike.riposte.server.http.RequestInfo)49 Test (org.junit.Test)37 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)17 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)12 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)10 HttpRequest (io.netty.handler.codec.http.HttpRequest)10 Pair (com.nike.internal.util.Pair)8 ResponseInfo (com.nike.riposte.server.http.ResponseInfo)8 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)8 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)7 Optional (java.util.Optional)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)6 RequestAndResponseFilter (com.nike.riposte.server.http.filter.RequestAndResponseFilter)5 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)5 HttpContent (io.netty.handler.codec.http.HttpContent)5 HttpMethod (io.netty.handler.codec.http.HttpMethod)5 HttpResponse (io.netty.handler.codec.http.HttpResponse)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)5