use of jakarta.servlet.http.HttpServletRequest 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 jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.
the class ServletServerHttpAsyncRequestControl method start.
@Override
public void start(long timeout) {
Assert.state(!isCompleted(), "Async processing has already completed");
if (isStarted()) {
return;
}
HttpServletRequest servletRequest = this.request.getServletRequest();
HttpServletResponse servletResponse = this.response.getServletResponse();
this.asyncContext = servletRequest.startAsync(servletRequest, servletResponse);
this.asyncContext.addListener(this);
if (timeout != NO_TIMEOUT_VALUE) {
this.asyncContext.setTimeout(timeout);
}
}
use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.
the class WebUtilsTests method testWithXForwardedHeaders.
private void testWithXForwardedHeaders(String serverName, int port, String forwardedProto, String forwardedHost, int forwardedPort, String originHeader) throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServerName(serverName);
if (port != -1) {
request.setServerPort(port);
}
if (forwardedProto != null) {
request.addHeader("X-Forwarded-Proto", forwardedProto);
}
if (forwardedHost != null) {
request.addHeader("X-Forwarded-Host", forwardedHost);
}
if (forwardedPort != -1) {
request.addHeader("X-Forwarded-Port", String.valueOf(forwardedPort));
}
request.addHeader(HttpHeaders.ORIGIN, originHeader);
HttpServletRequest requestToUse = adaptFromForwardedHeaders(request);
ServerHttpRequest httpRequest = new ServletServerHttpRequest(requestToUse);
assertThat(WebUtils.isSameOrigin(httpRequest)).isTrue();
}
use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.
the class WebUtilsTests method testWithForwardedHeader.
private void testWithForwardedHeader(String serverName, int port, String forwardedHeader, String originHeader) throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServerName(serverName);
if (port != -1) {
request.setServerPort(port);
}
request.addHeader("Forwarded", forwardedHeader);
request.addHeader(HttpHeaders.ORIGIN, originHeader);
HttpServletRequest requestToUse = adaptFromForwardedHeaders(request);
ServerHttpRequest httpRequest = new ServletServerHttpRequest(requestToUse);
assertThat(WebUtils.isSameOrigin(httpRequest)).isTrue();
}
use of jakarta.servlet.http.HttpServletRequest in project spring-framework by spring-projects.
the class WebUtilsTests method adaptFromForwardedHeaders.
// SPR-16668
private HttpServletRequest adaptFromForwardedHeaders(HttpServletRequest request) throws Exception {
MockFilterChain chain = new MockFilterChain();
new ForwardedHeaderFilter().doFilter(request, new MockHttpServletResponse(), chain);
return (HttpServletRequest) chain.getRequest();
}
Aggregations