Search in sources :

Example 11 with NativeWebRequest

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

the class MvcNamespaceTests method testDefaultConfig.

@Test
public void testDefaultConfig() throws Exception {
    loadBeanDefinitions("mvc-config.xml", 14);
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertNotNull(mapping);
    assertEquals(0, mapping.getOrder());
    assertTrue(mapping.getUrlPathHelper().shouldRemoveSemicolonContent());
    mapping.setDefaultHandler(handlerMethod);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
    NativeWebRequest webRequest = new ServletWebRequest(request);
    ContentNegotiationManager manager = mapping.getContentNegotiationManager();
    assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
    RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
    assertNotNull(adapter);
    assertEquals(false, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect"));
    List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
    assertTrue(converters.size() > 0);
    for (HttpMessageConverter<?> converter : converters) {
        if (converter instanceof AbstractJackson2HttpMessageConverter) {
            ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
            assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
            assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
            assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
            if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
                assertEquals(XmlMapper.class, objectMapper.getClass());
            }
        }
    }
    assertNotNull(appContext.getBean(FormattingConversionServiceFactoryBean.class));
    assertNotNull(appContext.getBean(ConversionService.class));
    assertNotNull(appContext.getBean(LocalValidatorFactoryBean.class));
    assertNotNull(appContext.getBean(Validator.class));
    // default web binding initializer behavior test
    request = new MockHttpServletRequest("GET", "/");
    request.addParameter("date", "2009-10-31");
    request.addParameter("percent", "99.99%");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertEquals(1, chain.getInterceptors().length);
    assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
    ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[0];
    interceptor.preHandle(request, response, handlerMethod);
    assertSame(appContext.getBean(ConversionService.class), request.getAttribute(ConversionService.class.getName()));
    adapter.handle(request, response, handlerMethod);
    assertTrue(handler.recordedValidationError);
    assertEquals(LocalDate.parse("2009-10-31").toDate(), handler.date);
    assertEquals(Double.valueOf(0.9999), handler.percent);
    CompositeUriComponentsContributor uriComponentsContributor = this.appContext.getBean(MvcUriComponentsBuilder.MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME, CompositeUriComponentsContributor.class);
    assertNotNull(uriComponentsContributor);
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) FormattingConversionServiceFactoryBean(org.springframework.format.support.FormattingConversionServiceFactoryBean) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) ConversionService(org.springframework.core.convert.ConversionService) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Validator(org.springframework.validation.Validator) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 12 with NativeWebRequest

use of org.springframework.web.context.request.NativeWebRequest in project uPortal by Jasig.

the class PortalRequestUtilsImpl method getOriginalPortalRequest.

@Override
public HttpServletRequest getOriginalPortalRequest(WebRequest request) {
    final HttpServletRequest portalRequest = (HttpServletRequest) request.getAttribute(PortalHttpServletRequestWrapper.ATTRIBUTE__HTTP_SERVLET_REQUEST, WebRequest.SCOPE_REQUEST);
    if (portalRequest != null) {
        return portalRequest;
    }
    if (request instanceof NativeWebRequest) {
        final NativeWebRequest nativeWebRequest = (NativeWebRequest) request;
        final Object nativeRequest = nativeWebRequest.getNativeRequest();
        if (nativeRequest instanceof HttpServletRequest) {
            return (HttpServletRequest) nativeRequest;
        }
    }
    throw new IllegalArgumentException("The orginal portal HttpServletRequest is not available from the WebRequest using attribute '" + PortalHttpServletRequestWrapper.ATTRIBUTE__HTTP_SERVLET_REQUEST + "'");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest)

Example 13 with NativeWebRequest

use of org.springframework.web.context.request.NativeWebRequest in project uplace.es by Uplace.

the class ExceptionTranslator method handleMethodArgumentNotValid.

@Override
public ResponseEntity<Problem> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nonnull NativeWebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldErrorVM> fieldErrors = result.getFieldErrors().stream().map(f -> new FieldErrorVM(f.getObjectName(), f.getField(), f.getCode())).collect(Collectors.toList());
    Problem problem = Problem.builder().withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE).withTitle("Method argument not valid").withStatus(defaultConstraintViolationStatus()).with("message", ErrorConstants.ERR_VALIDATION).with("fieldErrors", fieldErrors).build();
    return create(ex, problem, request);
}
Also used : ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) BindingResult(org.springframework.validation.BindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ProblemHandling(org.zalando.problem.spring.web.advice.ProblemHandling) Collectors(java.util.stream.Collectors) DefaultProblem(org.zalando.problem.DefaultProblem) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) Problem(org.zalando.problem.Problem) ProblemBuilder(org.zalando.problem.ProblemBuilder) Status(org.zalando.problem.Status) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntity(org.springframework.http.ResponseEntity) HeaderUtil(com.arnaugarcia.uplace.web.rest.util.HeaderUtil) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BindingResult(org.springframework.validation.BindingResult) DefaultProblem(org.zalando.problem.DefaultProblem) Problem(org.zalando.problem.Problem) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)

Aggregations

NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)13 Test (org.junit.Test)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 CallableProcessingInterceptorAdapter (org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter)3 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)3 AsyncEvent (javax.servlet.AsyncEvent)2 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)2 HeaderUtil (com.arnaugarcia.uplace.web.rest.util.HeaderUtil)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 ServletException (javax.servlet.ServletException)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1