Search in sources :

Example 31 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.

the class HttpEntityMethodProcessorTests method handleReturnValueCharSequence.

// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Exception {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new StringHttpMessageConverter());
    Method method = getClass().getDeclaredMethod("handle");
    MethodParameter returnType = new MethodParameter(method, -1);
    ResponseEntity<StringBuilder> returnValue = ResponseEntity.ok(new StringBuilder("Foo"));
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null);
    processor.handleReturnValue(webRequest, new HandlerMethod(this, method), returnValue);
    assertThat(servletResponse.getHeader("Content-Type")).isEqualTo("text/plain;charset=ISO-8859-1");
    assertThat(servletResponse.getContentAsString()).isEqualTo("Foo");
}
Also used : HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 32 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.

the class ModelMethodProcessorTests method handleModelReturnValue.

@Test
public void handleModelReturnValue() throws Exception {
    BindingContext bindingContext = webRequest.getBindingContext();
    bindingContext.addAttribute("attr1", "value1");
    ModelMap returnValue = new ModelMap();
    returnValue.addAttribute("attr2", "value2");
    Method method = getClass().getDeclaredMethod("model", Model.class);
    HandlerMethod handlerMethod = new HandlerMethod(this, method);
    processor.handleReturnValue(webRequest, handlerMethod, returnValue);
    assertThat(bindingContext.getModel().get("attr1")).isEqualTo("value1");
    assertThat(bindingContext.getModel().get("attr2")).isEqualTo("value2");
    // RedirectModel
    assertThat((Object) bindingContext.getRedirectModel()).isNull();
    assertThat(webRequest.hasAttribute(RedirectModel.OUTPUT_ATTRIBUTE)).isFalse();
    assertThat((Object) RedirectModel.findOutputModel(webRequest)).isNull();
    assertThat((Object) RedirectModel.findOutputModel(webRequest)).isSameAs(bindingContext.getRedirectModel());
    processor.handleReturnValue(webRequest, redirectModelHandler, new RedirectModel("attr3", "value3"));
    assertThat(webRequest.hasAttribute(RedirectModel.OUTPUT_ATTRIBUTE)).isTrue();
    assertThat((Object) RedirectModel.findOutputModel(webRequest)).isSameAs(bindingContext.getRedirectModel());
    assertThat((Object) bindingContext.getRedirectModel()).isNotNull();
    assertThat(bindingContext.getRedirectModel().getAttribute("attr3")).isEqualTo("value3");
    processor.handleReturnValue(webRequest, redirectModelHandler, new RedirectModel("attr4", "value4"));
    assertThat(bindingContext.getRedirectModel().getAttribute("attr3")).isEqualTo("value3");
    assertThat(bindingContext.getRedirectModel().getAttribute("attr4")).isEqualTo("value4");
}
Also used : RedirectModel(cn.taketoday.web.view.RedirectModel) ModelMap(cn.taketoday.web.view.ModelMap) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Method(java.lang.reflect.Method) BindingContext(cn.taketoday.web.BindingContext) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 33 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.

the class ModelMethodProcessorTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    processor = new ModelMethodProcessor();
    Method method = getClass().getDeclaredMethod("model", Model.class);
    Method redirectModel = getClass().getDeclaredMethod("redirectModel", RedirectModel.class);
    redirectModelHandler = new HandlerMethod(this, redirectModel);
    paramModel = new MethodParameter(method, 0);
    redirectModelParam = new MethodParameter(redirectModel, 0);
    webRequest = new ServletRequestContext(null, new MockHttpServletRequest(), null);
    webRequest.setBindingContext(new BindingContext());
}
Also used : MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Method(java.lang.reflect.Method) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) BindingContext(cn.taketoday.web.BindingContext) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 34 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-infrastructure by TAKETODAY.

the class AnnotationMappingInfo method getOrder.

// order
@Override
public int getOrder() {
    // FIXME
    final HandlerMethod method = handler.getMethod();
    final int handlerOrder = OrderUtils.getOrder(method.getMethod());
    final int paramsOrder = params == null ? 0 : params.length;
    final int consumesOrder = consumes == null ? 0 : consumes.length;
    return handlerOrder + consumesOrder + paramsOrder;
}
Also used : HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod)

Example 35 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.

the class SimpleActionMappingMethodExceptionHandler method handleInternal.

@Nullable
@Override
protected Object handleInternal(RequestContext context, @Nullable ActionMappingAnnotationHandler annotationHandler, Throwable ex) throws Exception {
    if (annotationHandler != null) {
        HandlerMethod handlerMethod = annotationHandler.getMethod();
        context.setStatus(getErrorStatusValue(ex));
        if (handlerMethod.isReturnTypeAssignableTo(RenderedImage.class)) {
            ClassPathResource pathResource = new ClassPathResource(getErrorStatusValue(ex) + ".png", getClass());
            if (pathResource.exists()) {
                context.setContentType(MediaType.IMAGE_JPEG_VALUE);
                return ImageIO.read(pathResource.getInputStream());
            }
        } else if (handlerMethod.isResponseBody()) {
            writeErrorMessage(ex, context);
            return NONE_RETURN_VALUE;
        }
        // next
        return null;
    }
    // next
    return null;
}
Also used : HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Nullable(cn.taketoday.lang.Nullable)

Aggregations

HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)35 Method (java.lang.reflect.Method)30 Test (org.junit.jupiter.api.Test)28 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)26 MethodParameter (cn.taketoday.core.MethodParameter)25 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)25 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)25 ByteArrayHttpMessageConverter (cn.taketoday.http.converter.ByteArrayHttpMessageConverter)24 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)24 ArrayList (java.util.ArrayList)24 AllEncompassingFormHttpMessageConverter (cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter)19 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)19 MappingJackson2XmlHttpMessageConverter (cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter)19 HttpEntity (cn.taketoday.http.HttpEntity)4 JsonViewRequestBodyAdvice (cn.taketoday.web.handler.method.JsonViewRequestBodyAdvice)4 JsonViewResponseBodyAdvice (cn.taketoday.web.handler.method.JsonViewResponseBodyAdvice)4 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)4 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)3