/** * Tries to extract the {@link RequestInfo} associated with the current request using the given arguments. First it * will try to get it from the given state. If that fails, it will try to create a new one based on the given msg * (which only works if the msg is a {@link HttpRequest}). If that also fails then a new dummy instance for an * unknown request will be created via {@link RequestInfoImpl#dummyInstanceForUnknownRequests()} and returned. */ RequestInfo<?> getRequestInfo(HttpProcessingState state, Object msg) { // Try to get the RequestInfo from the state variable first. RequestInfo requestInfo = state.getRequestInfo(); if (requestInfo != null) { return requestInfo; } // The state did not have a request info. See if we can build one from the msg. if (msg instanceof HttpRequest) { try { return handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest) msg, state ); } catch (Throwable t) { logger.error( "Unable to generate RequestInfo from HttpRequest. Defaulting to a synthetic RequestInfo.", t ); } } // Something major blew up if we reach here, so we just need to create a dummy RequestInfo for an unknown // request. requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); state.setRequestInfo(requestInfo); return requestInfo; }
/** * Tries to extract the {@link RequestInfo} associated with the current request using the given arguments. First it * will try to get it from the given state. If that fails, it will try to create a new one based on the given msg * (which only works if the msg is a {@link HttpRequest}). If that also fails then a new dummy instance for an * unknown request will be created via {@link RequestInfoImpl#dummyInstanceForUnknownRequests()} and returned. */ RequestInfo<?> getRequestInfo(HttpProcessingState state, Object msg) { // Try to get the RequestInfo from the state variable first. RequestInfo requestInfo = state.getRequestInfo(); if (requestInfo != null) { return requestInfo; } // The state did not have a request info. See if we can build one from the msg. if (msg instanceof HttpRequest) { try { return handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest) msg, state ); } catch (Throwable t) { logger.error( "Unable to generate RequestInfo from HttpRequest. Defaulting to a synthetic RequestInfo.", t ); } } // Something major blew up if we reach here, so we just need to create a dummy RequestInfo for an unknown // request. requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); state.setRequestInfo(requestInfo); return requestInfo; }
? handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest) msg, httpProcessingState
? handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest) msg, httpProcessingState
@Test public void getRequestInfo_uses_dummy_instance_if_an_exception_occurs_while_creating_RequestInfo_from_HttpRequest_msg() { // given assertThat(state.getRequestInfo(), nullValue()); RiposteHandlerInternalUtil explodingHandlerUtilMock = mock(RiposteHandlerInternalUtil.class); doThrow(new RuntimeException("intentional exception")) .when(explodingHandlerUtilMock) .createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary(any(), any()); Whitebox.setInternalState(handler, "handlerUtils", explodingHandlerUtilMock); HttpRequest httpRequest = new DefaultHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, "/some/uri" ); // when RequestInfo<?> result = handler.getRequestInfo(state, httpRequest); // then assertThat(result.getUri(), is(RequestInfo.NONE_OR_UNKNOWN_TAG)); verify(explodingHandlerUtilMock) .createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary(httpRequest, state); }
@Test public void createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary_uses_RequestInfo_from_state_if_it_already_exists() { // given RequestInfo<?> alreadyExistingRequestInfoMock = mock(RequestInfo.class); doReturn(alreadyExistingRequestInfoMock).when(stateSpy).getRequestInfo(); assertThat(stateSpy.getRequestInfo()).isSameAs(alreadyExistingRequestInfoMock); // when RequestInfo<?> result = implSpy.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary(nettyRequest, stateSpy); // then assertThat(result).isSameAs(alreadyExistingRequestInfoMock); assertThat(stateSpy.getRequestInfo()).isSameAs(alreadyExistingRequestInfoMock); verify(stateSpy, never()).setRequestInfo(any(RequestInfo.class)); }
@Test public void createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary_works_as_expected_when_no_decoder_failure() { // given assertThat(stateSpy.getRequestInfo()).isNull(); // when RequestInfo<?> result = implSpy.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary(nettyRequest, stateSpy); // then assertThat(result).isNotNull(); assertThat(result.getMethod()).isEqualTo(nettyRequest.method()); assertThat(result.getUri()).isEqualTo(nettyRequest.uri()); verify(stateSpy).setRequestInfo(result); assertThat(stateSpy.getRequestInfo()).isSameAs(result); }
handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest) msg, state );
if (msg instanceof HttpRequest) { HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest)msg, state
if (msg instanceof HttpRequest) { HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest)msg, state
handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest) msg, state );
@Override public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); RequestInfo request = handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest)msg, state ); // If the Netty HttpRequest is invalid, we shouldn't do any endpoint routing. handlerUtils.throwExceptionIfNotSuccessfullyDecoded((HttpRequest) msg); // 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()); throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize( (HttpRequest) msg, endpointForExecution.getLeft() ); } return PipelineContinuationBehavior.CONTINUE; }
@Override public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); RequestInfo request = handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( (HttpRequest)msg, state ); // If the Netty HttpRequest is invalid, we shouldn't do any endpoint routing. handlerUtils.throwExceptionIfNotSuccessfullyDecoded((HttpRequest) msg); // 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()); throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize( (HttpRequest) msg, endpointForExecution.getLeft() ); } return PipelineContinuationBehavior.CONTINUE; }
@Test public void createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary_creates_dummy_ResultInfo_when_exception_occurs_during_ResultInfo_instantiation() { // given String brokenUri = "%notARealEscapeSequence"; nettyRequest.setUri(brokenUri); // Sanity check that trying to create a new RequestInfo using the netty HttpRequest results in an exception. Throwable sanityCheckEx = catchThrowable(() -> new RequestInfoImpl<>(nettyRequest)); assertThat(sanityCheckEx).isNotNull(); RequestInfo<?> dummyRequestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); assertThat(stateSpy.getRequestInfo()).isNull(); // when RequestInfo<?> result = implSpy.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary(nettyRequest, stateSpy); // then assertThat(result).isNotNull(); assertThat(result.getUri()).isEqualTo(dummyRequestInfo.getUri()); verify(stateSpy).setRequestInfo(result); assertThat(stateSpy.getRequestInfo()).isSameAs(result); }
(httpProcessingState == null) ? null : handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( nettyRequest, httpProcessingState );
(httpProcessingState == null) ? null : handlerUtils.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( nettyRequest, httpProcessingState );
implSpy.createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary(nettyRequest, stateSpy);