Polish
This commit is contained in:
parent
956b66bbd4
commit
a3dc5e0491
|
@ -81,10 +81,10 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
|
|||
}
|
||||
|
||||
private void initResponseStatus() {
|
||||
ResponseStatus annot = getMethodAnnotation(ResponseStatus.class);
|
||||
if (annot != null) {
|
||||
this.responseStatus = annot.value();
|
||||
this.responseReason = annot.reason();
|
||||
ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class);
|
||||
if (annotation != null) {
|
||||
this.responseStatus = annotation.value();
|
||||
this.responseReason = annotation.reason();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,8 +97,8 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
|
|||
}
|
||||
|
||||
/**
|
||||
* Invokes the method and handles the return value through a registered
|
||||
* {@link HandlerMethodReturnValueHandler}.
|
||||
* Invokes the method and handles the return value through one of the
|
||||
* configured {@link HandlerMethodReturnValueHandler}s.
|
||||
*
|
||||
* @param webRequest the current request
|
||||
* @param mavContainer the ModelAndViewContainer for this request
|
||||
|
@ -142,14 +142,12 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
|
|||
if (this.responseStatus == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(this.responseReason)) {
|
||||
webRequest.getResponse().sendError(this.responseStatus.value(), this.responseReason);
|
||||
}
|
||||
else {
|
||||
webRequest.getResponse().setStatus(this.responseStatus.value());
|
||||
}
|
||||
|
||||
// to be picked up by the RedirectView
|
||||
webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, this.responseStatus);
|
||||
}
|
||||
|
@ -167,7 +165,7 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
|
|||
* Does this method have the response status instruction?
|
||||
*/
|
||||
private boolean hasResponseStatus() {
|
||||
return responseStatus != null;
|
||||
return this.responseStatus != null;
|
||||
}
|
||||
|
||||
private String getReturnValueHandlingErrorMessage(String message, Object returnValue) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -47,7 +47,6 @@ import org.springframework.web.servlet.view.RedirectView;
|
|||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link ServletInvocableHandlerMethod}.
|
||||
|
@ -68,48 +67,50 @@ public class ServletInvocableHandlerMethodTests {
|
|||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
|
||||
argumentResolvers = new HandlerMethodArgumentResolverComposite();
|
||||
mavContainer = new ModelAndViewContainer();
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
webRequest = new ServletWebRequest(request, response);
|
||||
this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
|
||||
this.argumentResolvers = new HandlerMethodArgumentResolverComposite();
|
||||
this.mavContainer = new ModelAndViewContainer();
|
||||
this.request = new MockHttpServletRequest();
|
||||
this.response = new MockHttpServletResponse();
|
||||
this.webRequest = new ServletWebRequest(this.request, this.response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeAndHandle_VoidWithResponseStatus() throws Exception {
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "responseStatus");
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertTrue("Null return value + @ResponseStatus should result in 'request handled'",
|
||||
mavContainer.isRequestHandled());
|
||||
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus());
|
||||
this.mavContainer.isRequestHandled());
|
||||
assertEquals(HttpStatus.BAD_REQUEST.value(), this.response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeAndHandle_VoidWithHttpServletResponseArgument() throws Exception {
|
||||
argumentResolvers.addResolver(new ServletResponseMethodArgumentResolver());
|
||||
this.argumentResolvers.addResolver(new ServletResponseMethodArgumentResolver());
|
||||
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "httpServletResponse", HttpServletResponse.class);
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
ServletInvocableHandlerMethod handlerMethod =
|
||||
getHandlerMethod(new Handler(), "httpServletResponse", HttpServletResponse.class);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertTrue("Null return value + HttpServletResponse arg should result in 'request handled'",
|
||||
mavContainer.isRequestHandled());
|
||||
this.mavContainer.isRequestHandled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeAndHandle_VoidRequestNotModified() throws Exception {
|
||||
webRequest.getNativeRequest(MockHttpServletRequest.class).addHeader("If-Modified-Since", 10 * 1000 * 1000);
|
||||
this.request.addHeader("If-Modified-Since", 10 * 1000 * 1000);
|
||||
int lastModifiedTimestamp = 1000 * 1000;
|
||||
webRequest.checkNotModified(lastModifiedTimestamp);
|
||||
this.webRequest.checkNotModified(lastModifiedTimestamp);
|
||||
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "notModified");
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertTrue("Null return value + 'not modified' request should result in 'request handled'",
|
||||
mavContainer.isRequestHandled());
|
||||
this.mavContainer.isRequestHandled());
|
||||
}
|
||||
|
||||
// SPR-9159
|
||||
|
@ -117,40 +118,40 @@ public class ServletInvocableHandlerMethodTests {
|
|||
@Test
|
||||
public void invokeAndHandle_NotVoidWithResponseStatusAndReason() throws Exception {
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "responseStatusWithReason");
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertTrue("When a phrase is used, the response should not be used any more", mavContainer.isRequestHandled());
|
||||
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus());
|
||||
assertEquals("400 Bad Request", response.getErrorMessage());
|
||||
assertTrue("When a status reason w/ used, the the request is handled", this.mavContainer.isRequestHandled());
|
||||
assertEquals(HttpStatus.BAD_REQUEST.value(), this.response.getStatus());
|
||||
assertEquals("400 Bad Request", this.response.getErrorMessage());
|
||||
}
|
||||
|
||||
@Test(expected=HttpMessageNotWritableException.class)
|
||||
public void invokeAndHandle_Exception() throws Exception {
|
||||
returnValueHandlers.addHandler(new ExceptionRaisingReturnValueHandler());
|
||||
this.returnValueHandlers.addHandler(new ExceptionRaisingReturnValueHandler());
|
||||
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "handle");
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
fail("Expected exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeAndHandle_DynamicReturnValue() throws Exception {
|
||||
argumentResolvers.addResolver(new RequestParamMethodArgumentResolver(null, false));
|
||||
returnValueHandlers.addHandler(new ViewMethodReturnValueHandler());
|
||||
returnValueHandlers.addHandler(new ViewNameMethodReturnValueHandler());
|
||||
this.argumentResolvers.addResolver(new RequestParamMethodArgumentResolver(null, false));
|
||||
this.returnValueHandlers.addHandler(new ViewMethodReturnValueHandler());
|
||||
this.returnValueHandlers.addHandler(new ViewNameMethodReturnValueHandler());
|
||||
|
||||
// Invoke without a request parameter (String return value)
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "dynamicReturnValue", String.class);
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertNotNull(mavContainer.getView());
|
||||
assertEquals(RedirectView.class, mavContainer.getView().getClass());
|
||||
assertNotNull(this.mavContainer.getView());
|
||||
assertEquals(RedirectView.class, this.mavContainer.getView().getClass());
|
||||
|
||||
// Invoke with a request parameter (RedirectView return value)
|
||||
request.setParameter("param", "value");
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
this.request.setParameter("param", "value");
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertEquals("view", mavContainer.getViewName());
|
||||
assertEquals("view", this.mavContainer.getViewName());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -166,24 +167,24 @@ public class ServletInvocableHandlerMethodTests {
|
|||
private void wrapConcurrentResult_ResponseBody(Object handler) throws Exception {
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(new StringHttpMessageConverter());
|
||||
returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters));
|
||||
this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters));
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(handler, "handle");
|
||||
handlerMethod = handlerMethod.wrapConcurrentResult("bar");
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertEquals("bar", response.getContentAsString());
|
||||
assertEquals("bar", this.response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapConcurrentResult_ResponseEntity() throws Exception {
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(new StringHttpMessageConverter());
|
||||
returnValueHandlers.addHandler(new HttpEntityMethodProcessor(converters));
|
||||
this.returnValueHandlers.addHandler(new HttpEntityMethodProcessor(converters));
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handle");
|
||||
handlerMethod = handlerMethod.wrapConcurrentResult(new ResponseEntity<>("bar", HttpStatus.OK));
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertEquals("bar", response.getContentAsString());
|
||||
assertEquals("bar", this.response.getContentAsString());
|
||||
}
|
||||
|
||||
// SPR-12287
|
||||
|
@ -194,13 +195,13 @@ public class ServletInvocableHandlerMethodTests {
|
|||
converters.add(new StringHttpMessageConverter());
|
||||
List<Object> advice = Arrays.asList(mock(ResponseBodyAdvice.class));
|
||||
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null, advice);
|
||||
returnValueHandlers.addHandler(processor);
|
||||
this.returnValueHandlers.addHandler(processor);
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handle");
|
||||
handlerMethod = handlerMethod.wrapConcurrentResult(new ResponseEntity<>(HttpStatus.OK));
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals("", response.getContentAsString());
|
||||
assertEquals(200, this.response.getStatus());
|
||||
assertEquals("", this.response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -209,13 +210,13 @@ public class ServletInvocableHandlerMethodTests {
|
|||
converters.add(new StringHttpMessageConverter());
|
||||
List<Object> advice = Arrays.asList(mock(ResponseBodyAdvice.class));
|
||||
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null, advice);
|
||||
returnValueHandlers.addHandler(processor);
|
||||
this.returnValueHandlers.addHandler(processor);
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handle");
|
||||
handlerMethod = handlerMethod.wrapConcurrentResult(null);
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals("", response.getContentAsString());
|
||||
assertEquals(200, this.response.getStatus());
|
||||
assertEquals("", this.response.getContentAsString());
|
||||
}
|
||||
|
||||
private ServletInvocableHandlerMethod getHandlerMethod(Object controller,
|
||||
|
@ -228,6 +229,7 @@ public class ServletInvocableHandlerMethodTests {
|
|||
return handlerMethod;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class Handler {
|
||||
|
||||
|
@ -280,7 +282,6 @@ public class ServletInvocableHandlerMethodTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private static class ExceptionRaisingReturnValueHandler implements HandlerMethodReturnValueHandler {
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue