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");
}
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");
}
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());
}
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;
}
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;
}
Aggregations