Search in sources :

Example 91 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method setUp.

@Before
public void setUp() throws Exception {
    resolver = new RequestParamMethodArgumentResolver(null, true);
    request = new MockHttpServletRequest();
    webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 92 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolvePartList.

@Test
public void resolvePartList() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MockPart expected1 = new MockPart("pfilelist", "Hello World 1".getBytes());
    MockPart expected2 = new MockPart("pfilelist", "Hello World 2".getBytes());
    request.addPart(expected1);
    request.addPart(expected2);
    request.addPart(new MockPart("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof List);
    assertEquals(Arrays.asList(expected1, expected2), result);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockPart(org.springframework.mock.web.test.MockPart) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 93 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class ResponseEntityExceptionHandler method handleAsyncRequestTimeoutException.

/**
	 * Customize the response for NoHandlerFoundException.
	 * <p>This method delegates to {@link #handleExceptionInternal}.
	 * @param ex the exception
	 * @param headers the headers to be written to the response
	 * @param status the selected response status
	 * @param webRequest the current request
	 * @return a {@code ResponseEntity} instance
	 * @since 4.2.8
	 */
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {
    if (webRequest instanceof ServletWebRequest) {
        ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
        HttpServletRequest request = servletRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse response = servletRequest.getNativeResponse(HttpServletResponse.class);
        if (response.isCommitted()) {
            if (logger.isErrorEnabled()) {
                logger.error("Async timeout for " + request.getMethod() + " [" + request.getRequestURI() + "]");
            }
            return null;
        }
    }
    return handleExceptionInternal(ex, null, headers, status, webRequest);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 94 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method invokeHandlerMethod.

/**
	 * Invoke the {@link RequestMapping} handler method preparing a {@link ModelAndView}
	 * if view resolution is required.
	 * @since 4.2
	 * @see #createInvocableHandlerMethod(HandlerMethod)
	 */
protected ModelAndView invokeHandlerMethod(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
    ServletWebRequest webRequest = new ServletWebRequest(request, response);
    try {
        WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
        ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);
        ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
        invocableMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
        invocableMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
        invocableMethod.setDataBinderFactory(binderFactory);
        invocableMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
        ModelAndViewContainer mavContainer = new ModelAndViewContainer();
        mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
        modelFactory.initModel(webRequest, mavContainer, invocableMethod);
        mavContainer.setIgnoreDefaultModelOnRedirect(this.ignoreDefaultModelOnRedirect);
        AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
        asyncWebRequest.setTimeout(this.asyncRequestTimeout);
        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
        asyncManager.setTaskExecutor(this.taskExecutor);
        asyncManager.setAsyncWebRequest(asyncWebRequest);
        asyncManager.registerCallableInterceptors(this.callableInterceptors);
        asyncManager.registerDeferredResultInterceptors(this.deferredResultInterceptors);
        if (asyncManager.hasConcurrentResult()) {
            Object result = asyncManager.getConcurrentResult();
            mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
            asyncManager.clearConcurrentResult();
            if (logger.isDebugEnabled()) {
                logger.debug("Found concurrent result value [" + result + "]");
            }
            invocableMethod = invocableMethod.wrapConcurrentResult(result);
        }
        invocableMethod.invokeAndHandle(webRequest, mavContainer);
        if (asyncManager.isConcurrentHandlingStarted()) {
            return null;
        }
        return getModelAndView(mavContainer, modelFactory, webRequest);
    } finally {
        webRequest.requestCompleted();
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) ModelFactory(org.springframework.web.method.annotation.ModelFactory) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory)

Example 95 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class ExceptionHandlerExceptionResolver method doResolveHandlerMethodException.

/**
	 * Find an {@code @ExceptionHandler} method and invoke it to handle the raised exception.
	 */
@Override
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) {
    ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
    if (exceptionHandlerMethod == null) {
        return null;
    }
    exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
    exceptionHandlerMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
    ServletWebRequest webRequest = new ServletWebRequest(request, response);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Invoking @ExceptionHandler method: " + exceptionHandlerMethod);
        }
        Throwable cause = exception.getCause();
        if (cause != null) {
            // Expose cause as provided argument as well
            exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod);
        } else {
            // Otherwise, just the given exception as-is
            exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
        }
    } catch (Throwable invocationEx) {
        // probably an accident (e.g. failed assertion or the like).
        if (invocationEx != exception && logger.isWarnEnabled()) {
            logger.warn("Failed to invoke @ExceptionHandler method: " + exceptionHandlerMethod, invocationEx);
        }
        // Continue with default processing of the original exception...
        return null;
    }
    if (mavContainer.isRequestHandled()) {
        return new ModelAndView();
    } else {
        ModelMap model = mavContainer.getModel();
        HttpStatus status = mavContainer.getStatus();
        ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, status);
        mav.setViewName(mavContainer.getViewName());
        if (!mavContainer.isViewReference()) {
            mav.setView((View) mavContainer.getView());
        }
        if (model instanceof RedirectAttributes) {
            Map<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
            request = webRequest.getNativeRequest(HttpServletRequest.class);
            RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
        }
        return mav;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) HttpStatus(org.springframework.http.HttpStatus) ModelMap(org.springframework.ui.ModelMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Aggregations

ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)114 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)76 Test (org.junit.Test)49 Before (org.junit.Before)45 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)29 MethodParameter (org.springframework.core.MethodParameter)26 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)25 Method (java.lang.reflect.Method)20 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)16 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)14 ITestBean (org.springframework.tests.sample.beans.ITestBean)13 TestBean (org.springframework.tests.sample.beans.TestBean)13 RequestParam (org.springframework.web.bind.annotation.RequestParam)10 MultipartFile (org.springframework.web.multipart.MultipartFile)10 MockPart (org.springframework.mock.web.test.MockPart)9 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)8 List (java.util.List)6 Optional (java.util.Optional)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)5 IOException (java.io.IOException)4