use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.
the class DispatcherServlet method doDispatch.
/**
* Process the actual dispatching to the handler.
* <p>The handler will be obtained by applying the servlet's HandlerMappings in order.
* The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters
* to find the first that supports the handler class.
* <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers
* themselves to decide which methods are acceptable.
* @param request current HTTP request
* @param response current HTTP response
* @throws Exception in case of any kind of processing failure
*/
@SuppressWarnings("deprecation")
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = HttpMethod.GET.matches(method);
if (isGet || HttpMethod.HEAD.matches(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
} catch (Exception ex) {
dispatchException = ex;
} catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
} catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
} catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler, new NestedServletException("Handler processing failed", err));
} finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
} else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.
the class ModelAndViewMethodReturnValueHandlerTests method setup.
@BeforeEach
public void setup() throws Exception {
this.handler = new ModelAndViewMethodReturnValueHandler();
this.mavContainer = new ModelAndViewContainer();
this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
this.returnParamModelAndView = getReturnValueParam("modelAndView");
}
use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorTests method handleReturnValueWithETagAndETagFilter.
// SPR-13423
@Test
public void handleReturnValueWithETagAndETagFilter() throws Exception {
String eTagValue = "\"deadb33f8badf00d\"";
String content = "body";
Method method = getClass().getDeclaredMethod("handle");
MethodParameter returnType = new MethodParameter(method, -1);
FilterChain chain = (req, res) -> {
ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(eTagValue).body(content);
try {
ServletWebRequest requestToUse = new ServletWebRequest((HttpServletRequest) req, (HttpServletResponse) res);
new HttpEntityMethodProcessor(Collections.singletonList(new StringHttpMessageConverter())).handleReturnValue(returnValue, returnType, mavContainer, requestToUse);
assertThat(this.servletResponse.getContentAsString()).as("Response body was cached? It should be written directly to the raw response").isEqualTo(content);
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
};
this.servletRequest.setMethod("GET");
new ShallowEtagHeaderFilter().doFilter(this.servletRequest, this.servletResponse, chain);
assertThat(this.servletResponse.getStatus()).isEqualTo(200);
assertThat(this.servletResponse.getHeader(HttpHeaders.ETAG)).isEqualTo(eTagValue);
assertThat(this.servletResponse.getContentAsString()).isEqualTo(content);
}
use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorMockTests method setup.
@BeforeEach
@SuppressWarnings("unchecked")
public void setup() throws Exception {
stringHttpMessageConverter = mock(HttpMessageConverter.class);
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
resourceMessageConverter = mock(HttpMessageConverter.class);
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
given(resourceMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
resourceRegionMessageConverter = mock(HttpMessageConverter.class);
given(resourceRegionMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
given(resourceRegionMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
processor = new HttpEntityMethodProcessor(Arrays.asList(stringHttpMessageConverter, resourceMessageConverter, resourceRegionMessageConverter));
Method handle1 = getClass().getMethod("handle1", HttpEntity.class, ResponseEntity.class, Integer.TYPE, RequestEntity.class);
paramHttpEntity = new MethodParameter(handle1, 0);
paramRequestEntity = new MethodParameter(handle1, 3);
paramResponseEntity = new MethodParameter(handle1, 1);
paramInt = new MethodParameter(handle1, 2);
returnTypeResponseEntity = new MethodParameter(handle1, -1);
returnTypeResponseEntityProduces = new MethodParameter(getClass().getMethod("handle4"), -1);
returnTypeHttpEntity = new MethodParameter(getClass().getMethod("handle2", HttpEntity.class), -1);
returnTypeHttpEntitySubclass = new MethodParameter(getClass().getMethod("handle2x", HttpEntity.class), -1);
returnTypeInt = new MethodParameter(getClass().getMethod("handle3"), -1);
returnTypeResponseEntityResource = new MethodParameter(getClass().getMethod("handle5"), -1);
mavContainer = new ModelAndViewContainer();
servletRequest = new MockHttpServletRequest("GET", "/foo");
servletResponse = new MockHttpServletResponse();
webRequest = new ServletWebRequest(servletRequest, servletResponse);
}
use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.
the class ModelAndViewResolverMethodReturnValueHandlerTests method setup.
@BeforeEach
public void setup() {
mavResolvers = new ArrayList<>();
handler = new ModelAndViewResolverMethodReturnValueHandler(mavResolvers);
mavContainer = new ModelAndViewContainer();
request = new ServletWebRequest(new MockHttpServletRequest());
}
Aggregations