/** * Returns the value of calling {@link ServletRuntime#isAsyncRequest(HttpServletRequest)} on the {@link * ServletRuntime} returned by {@link #getServletRuntime(ServletRequest)}. This method is here to allow * easy overriding by subclasses if needed, where {@link ServletRuntime} is not in scope. * * @param request The request to inspect to see if it's part of an async servlet request or not. * * @return the value of calling {@link ServletRuntime#isAsyncRequest(HttpServletRequest)} on the {@link * ServletRuntime} returned by {@link #getServletRuntime(ServletRequest)}. */ protected boolean isAsyncRequest(HttpServletRequest request) { return getServletRuntime(request).isAsyncRequest(request); }
/** * The dispatcher type {@code javax.servlet.DispatcherType.ASYNC} introduced in Servlet 3.0 means a filter can be * invoked in more than one thread over the course of a single request. This method should return {@code true} if * the filter is currently executing within an asynchronous dispatch. * * @param request the current request * * @deprecated This method is no longer used to determine whether this filter should execute, and will be removed * in a future update. It remains here only to prevent breaking impls that overrode the method. */ @Deprecated protected boolean isAsyncDispatch(HttpServletRequest request) { return getServletRuntime(request).isAsyncDispatch(request); }
/** * Delegates to {@link * ServletRuntime#setupTracingCompletionWhenAsyncRequestCompletes(HttpServletRequest, HttpServletResponse, * TracingState, HttpTagAndSpanNamingStrategy, HttpTagAndSpanNamingAdapter)}, with the {@link ServletRuntime} * retrieved via {@link #getServletRuntime(ServletRequest)}. This method is here to allow easy overriding by * subclasses if needed, where {@link ServletRuntime} is not in scope. * * @param asyncRequest The async servlet request (guaranteed to be async since this method will only be called when * {@link #isAsyncRequest(HttpServletRequest)} returns true). * @param asyncResponse The servlet response object - needed for span tagging. * @param originalRequestTracingState The {@link TracingState} that was generated when this request started, and * which should be completed when the given async servlet request finishes. * @param tagAndNamingStrategy The {@link HttpTagAndSpanNamingStrategy} that should be used for final span name * and tagging. * @param tagAndNamingAdapter The {@link HttpTagAndSpanNamingAdapter} that should be used by * {@code tagAndNamingStrategy} for final span name and tagging. */ protected void setupTracingCompletionWhenAsyncRequestCompletes( HttpServletRequest asyncRequest, HttpServletResponse asyncResponse, TracingState originalRequestTracingState, HttpTagAndSpanNamingStrategy<HttpServletRequest, HttpServletResponse> tagAndNamingStrategy, HttpTagAndSpanNamingAdapter<HttpServletRequest,HttpServletResponse> tagAndNamingAdapter ) { getServletRuntime(asyncRequest).setupTracingCompletionWhenAsyncRequestCompletes( asyncRequest, asyncResponse, originalRequestTracingState, tagAndNamingStrategy, tagAndNamingAdapter ); }
@Test public void getServletRuntime_uses_cached_value_if_possible() { // given RequestTracingFilter filterSpy = spy(getBasicFilter()); ServletRuntime servletRuntimeMock = mock(ServletRuntime.class); filterSpy.servletRuntime = servletRuntimeMock; // when ServletRuntime result = filterSpy.getServletRuntime(mock(HttpServletRequest.class)); // then assertThat(result).isSameAs(servletRuntimeMock); }
@DataProvider(value = { "true", "false" }, splitBy = "\\|") @Test public void isAsyncRequest_delegates_to_ServletRuntime(boolean servletRuntimeResult) { // given RequestTracingFilter filterSpy = spy(getBasicFilter()); doReturn(servletRuntimeMock).when(filterSpy).getServletRuntime(any(HttpServletRequest.class)); doReturn(servletRuntimeResult).when(servletRuntimeMock).isAsyncRequest(any(HttpServletRequest.class)); // when boolean result = filterSpy.isAsyncRequest(requestMock); // then assertThat(result).isEqualTo(servletRuntimeResult); verify(filterSpy).getServletRuntime(requestMock); verify(servletRuntimeMock).isAsyncRequest(requestMock); }
@Test public void setupTracingCompletionWhenAsyncRequestCompletes_delegates_to_ServletRuntime() { // given RequestTracingFilter filterSpy = spy(getBasicFilter()); doReturn(servletRuntimeMock).when(filterSpy).getServletRuntime(any(HttpServletRequest.class)); TracingState tracingStateMock = mock(TracingState.class); // when filterSpy.setupTracingCompletionWhenAsyncRequestCompletes( requestMock, responseMock, tracingStateMock, tagAndNamingStrategy, tagAndNamingAdapterMock ); // then verify(filterSpy).setupTracingCompletionWhenAsyncRequestCompletes( requestMock, responseMock, tracingStateMock, tagAndNamingStrategy, tagAndNamingAdapterMock ); verify(filterSpy).getServletRuntime(requestMock); verify(servletRuntimeMock).setupTracingCompletionWhenAsyncRequestCompletes( requestMock, responseMock, tracingStateMock, tagAndNamingStrategy, tagAndNamingAdapterMock ); verifyNoMoreInteractions(filterSpy, servletRuntimeMock, requestMock, tracingStateMock); }
@DataProvider(value = { "true", "false" }) @Test @SuppressWarnings("deprecation") public void isAsyncDispatch_delegates_to_ServletRuntime(boolean servletRuntimeResult) { // given RequestTracingFilter filterSpy = spy(getBasicFilter()); doReturn(servletRuntimeMock).when(filterSpy).getServletRuntime(any(HttpServletRequest.class)); doReturn(servletRuntimeResult).when(servletRuntimeMock).isAsyncDispatch(any(HttpServletRequest.class)); // when boolean result = filterSpy.isAsyncDispatch(requestMock); // then assertThat(result).isEqualTo(servletRuntimeResult); verify(filterSpy).getServletRuntime(requestMock); verify(servletRuntimeMock).isAsyncDispatch(requestMock); }
@Test public void getServletRuntime_returns_value_of_ServletRuntime_determineServletRuntime_method_and_caches_result() { // given Class<? extends ServletRuntime> expectedServletRuntimeClass = ServletRuntime.determineServletRuntime(requestMock.getClass(), ASYNC_LISTENER_CLASSNAME).getClass(); RequestTracingFilter filter = getBasicFilter(); assertThat(filter.servletRuntime).isNull(); // when ServletRuntime result = filter.getServletRuntime(requestMock); // then assertThat(result.getClass()).isEqualTo(expectedServletRuntimeClass); assertThat(filter.servletRuntime).isSameAs(result); }