Search in sources :

Example 1 with AsyncWebRequest

use of cn.taketoday.web.context.async.AsyncWebRequest in project today-infrastructure by TAKETODAY.

the class ReactiveTypeHandlerTests method resetRequest.

private void resetRequest() {
    this.servletRequest = new MockHttpServletRequest();
    this.servletResponse = new MockHttpServletResponse();
    this.webRequest = new ServletRequestContext(null, this.servletRequest, this.servletResponse);
    AsyncWebRequest webRequest = new StandardServletAsyncWebRequest(this.webRequest);
    WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncRequest(webRequest);
    this.servletRequest.setAsyncSupported(true);
}
Also used : StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) AsyncWebRequest(cn.taketoday.web.context.async.AsyncWebRequest) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse)

Example 2 with AsyncWebRequest

use of cn.taketoday.web.context.async.AsyncWebRequest in project today-infrastructure by TAKETODAY.

the class RequestMappingHandlerAdapter method invokeHandlerMethod.

/**
 * Invoke the {@link RequestMapping} handler method preparing a {@link ModelAndView}
 * if view resolution is required.
 *
 * @see #createInvocableHandlerMethod(RequestContext, HandlerMethod)
 */
@Nullable
protected Object invokeHandlerMethod(RequestContext request, HandlerMethod handlerMethod) throws Throwable {
    BindingContext bindingContext = createBindingContext(handlerMethod);
    ModelFactory modelFactory = getModelFactory(handlerMethod, bindingContext);
    // ActionMappingAnnotationHandler handler = annotationHandlerMap.get(handlerMethod);
    ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(request, handlerMethod);
    invocableMethod.setReturnValueHandlerManager(returnValueHandlerManager);
    RedirectModel inputRedirectModel = RequestContextUtils.getInputRedirectModel(request, redirectModelManager);
    bindingContext.addAllAttributes(inputRedirectModel);
    modelFactory.initModel(request, bindingContext, invocableMethod);
    AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request);
    asyncWebRequest.setTimeout(asyncRequestTimeout);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    asyncManager.setTaskExecutor(taskExecutor);
    asyncManager.setAsyncRequest(asyncWebRequest);
    asyncManager.registerCallableInterceptors(callableInterceptors);
    asyncManager.registerDeferredResultInterceptors(deferredResultInterceptors);
    if (asyncManager.hasConcurrentResult()) {
        Object result = asyncManager.getConcurrentResult();
        bindingContext = asyncManager.getBindingContext();
        asyncManager.clearConcurrentResult();
        LogFormatUtils.traceDebug(log, traceOn -> {
            String formatted = LogFormatUtils.formatValue(result, !traceOn);
            return "Resume with async result [" + formatted + "]";
        });
        invocableMethod = invocableMethod.wrapConcurrentResult(result);
    }
    Object returnValue = invocableMethod.invokeAndHandle(request, bindingContext);
    if (asyncManager.isConcurrentHandlingStarted()) {
        return null;
    }
    // modelFactory.updateModel(request, bindingContext);
    return returnValue;
}
Also used : WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) RedirectModel(cn.taketoday.web.view.RedirectModel) BindingContext(cn.taketoday.web.BindingContext) AsyncWebRequest(cn.taketoday.web.context.async.AsyncWebRequest) Nullable(cn.taketoday.lang.Nullable)

Example 3 with AsyncWebRequest

use of cn.taketoday.web.context.async.AsyncWebRequest in project today-framework by TAKETODAY.

the class ResponseBodyEmitterReturnValueHandlerTests method responseBodyEmitterWithTimeoutValue.

@Test
public void responseBodyEmitterWithTimeoutValue() throws Exception {
    AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
    WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncRequest(asyncWebRequest);
    ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
    emitter.onTimeout(mock(Runnable.class));
    emitter.onCompletion(mock(Runnable.class));
    HandlerMethod handlerMethod = on(TestController.class).resolveHandlerMethod(ResponseBodyEmitter.class);
    this.handler.handleReturnValue(webRequest, handlerMethod, emitter);
    verify(asyncWebRequest).setTimeout(19000L);
    verify(asyncWebRequest).addTimeoutHandler(any(Runnable.class));
    verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
    verify(asyncWebRequest).startAsync();
}
Also used : AsyncWebRequest(cn.taketoday.web.context.async.AsyncWebRequest) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) Test(org.junit.jupiter.api.Test)

Example 4 with AsyncWebRequest

use of cn.taketoday.web.context.async.AsyncWebRequest in project today-framework by TAKETODAY.

the class ResponseBodyEmitterReturnValueHandlerTests method responseBodyEmitterWithErrorValue.

@SuppressWarnings("unchecked")
@Test
public void responseBodyEmitterWithErrorValue() throws Exception {
    AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
    WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncRequest(asyncWebRequest);
    ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
    emitter.onError(mock(Consumer.class));
    emitter.onCompletion(mock(Runnable.class));
    HandlerMethod type = on(TestController.class).resolveHandlerMethod(ResponseBodyEmitter.class);
    this.handler.handleReturnValue(webRequest, type, emitter);
    verify(asyncWebRequest).addErrorHandler(any(Consumer.class));
    verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
    verify(asyncWebRequest).startAsync();
}
Also used : Consumer(java.util.function.Consumer) AsyncWebRequest(cn.taketoday.web.context.async.AsyncWebRequest) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) Test(org.junit.jupiter.api.Test)

Example 5 with AsyncWebRequest

use of cn.taketoday.web.context.async.AsyncWebRequest in project today-framework by TAKETODAY.

the class ResponseBodyEmitterReturnValueHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    List<HttpMessageConverter<?>> converters = Collections.singletonList(new MappingJackson2HttpMessageConverter());
    this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
    this.webRequest = new ServletRequestContext(null, this.request, this.response);
    AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
    WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncRequest(asyncWebRequest);
    this.request.setAsyncSupported(true);
}
Also used : StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) AsyncWebRequest(cn.taketoday.web.context.async.AsyncWebRequest) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

AsyncWebRequest (cn.taketoday.web.context.async.AsyncWebRequest)7 StandardServletAsyncWebRequest (cn.taketoday.web.context.async.StandardServletAsyncWebRequest)5 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)3 Nullable (cn.taketoday.lang.Nullable)2 BindingContext (cn.taketoday.web.BindingContext)2 WebAsyncManager (cn.taketoday.web.context.async.WebAsyncManager)2 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)2 RedirectModel (cn.taketoday.web.view.RedirectModel)2 Test (org.junit.jupiter.api.Test)2 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)1 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)1 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)1 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)1 Consumer (java.util.function.Consumer)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1