Search in sources :

Example 21 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project com.revolsys.open by revolsys.

the class RecordReaderHttpMessageConverter method read.

@Override
public RecordReader read(final Class<? extends RecordReader> clazz, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    try {
        final HttpHeaders headers = inputMessage.getHeaders();
        final MediaType mediaType = headers.getContentType();
        Charset charset = mediaType.getCharSet();
        if (charset == null) {
            charset = StandardCharsets.UTF_8;
        }
        final InputStream body = inputMessage.getBody();
        final String mediaTypeString = mediaType.getType() + "/" + mediaType.getSubtype();
        final RecordReaderFactory readerFactory = IoFactory.factoryByMediaType(RecordReaderFactory.class, mediaTypeString);
        if (readerFactory == null) {
            throw new HttpMessageNotReadableException("Cannot read data in format" + mediaType);
        } else {
            final Reader<Record> reader = readerFactory.newRecordReader(new InputStreamResource("recordInput", body));
            GeometryFactory factory = this.geometryFactory;
            final ServletWebRequest requestAttributes = (ServletWebRequest) RequestContextHolder.getRequestAttributes();
            final String srid = requestAttributes.getParameter("srid");
            if (srid != null && srid.trim().length() > 0) {
                factory = GeometryFactory.floating3d(Integer.parseInt(srid));
            }
            reader.setProperty(IoConstants.GEOMETRY_FACTORY, factory);
            return (RecordReader) reader;
        }
    } catch (final IOException e) {
        throw new HttpMessageNotReadableException("Error reading data", e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStream(java.io.InputStream) RecordReader(com.revolsys.record.io.RecordReader) Charset(java.nio.charset.Charset) IOException(java.io.IOException) RecordReaderFactory(com.revolsys.record.io.RecordReaderFactory) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MediaType(org.springframework.http.MediaType) Record(com.revolsys.record.Record) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) InputStreamResource(com.revolsys.spring.resource.InputStreamResource)

Example 22 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project com.revolsys.open by revolsys.

the class WebAnnotationMethodHandlerAdapter method invokeHandlerMethod.

protected ModelAndView invokeHandlerMethod(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
    final AnnotationHandlerMethodResolver methodResolver = getMethodResolver(handler);
    final WebMethodHandler handlerMethod = methodResolver.resolveHandlerMethod(request);
    final ServletWebRequest webRequest = new ServletWebRequest(request, response);
    final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    try {
        RequestContextHolder.setRequestAttributes(webRequest);
        final ExtendedModelMap implicitModel = new BindingAwareModelMap();
        final Object result = handlerMethod.invokeMethod(handler, request, response);
        if (result == null) {
            return null;
        } else {
            final ModelAndView mav = getModelAndView(handlerMethod.getMethod(), handler.getClass(), result, implicitModel, webRequest);
            return mav;
        }
    } finally {
        RequestContextHolder.setRequestAttributes(requestAttributes);
    }
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) ModelAndView(org.springframework.web.servlet.ModelAndView) BindingAwareModelMap(org.springframework.validation.support.BindingAwareModelMap) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 23 with ServletWebRequest

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

the class PageableHandlerMethodArgumentResolverUnitTests method returnsCorrectPageSizeForOneIndexParameters.

// DATACMNS-761
@Test
public void returnsCorrectPageSizeForOneIndexParameters() {
    PageableHandlerMethodArgumentResolver resolver = getResolver();
    resolver.setOneIndexedParameters(true);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("size", "10");
    Pageable result = resolver.resolveArgument(supportedMethodParameter, null, new ServletWebRequest(request), null);
    assertThat(result.getPageSize()).isEqualTo(10);
}
Also used : Pageable(org.springframework.data.domain.Pageable) PageableHandlerMethodArgumentResolver(org.springframework.data.web.PageableHandlerMethodArgumentResolver) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 24 with ServletWebRequest

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

the class PageableHandlerMethodArgumentResolverUnitTests method considersOneIndexedParametersSetting.

// DATACMNS-563
@Test
public void considersOneIndexedParametersSetting() {
    PageableHandlerMethodArgumentResolver resolver = getResolver();
    resolver.setOneIndexedParameters(true);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("page", "1");
    assertThat(resolver.resolveArgument(supportedMethodParameter, null, new ServletWebRequest(request), null).getPageNumber()).isEqualTo(0);
}
Also used : PageableHandlerMethodArgumentResolver(org.springframework.data.web.PageableHandlerMethodArgumentResolver) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 25 with ServletWebRequest

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

the class SortHandlerMethodArgumentResolverUnitTests method returnsNullForSortParameterSetToNothing.

@Test
public void returnsNullForSortParameterSetToNothing() throws Exception {
    MethodParameter parameter = getParameterOfMethod("supportedMethod");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("sort", (String) null);
    SortHandlerMethodArgumentResolver resolver = new SortHandlerMethodArgumentResolver();
    Sort result = resolver.resolveArgument(parameter, null, new ServletWebRequest(request), null);
    assertThat(result.isSorted()).isFalse();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Sort(org.springframework.data.domain.Sort) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Aggregations

ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)224 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)80 Test (org.junit.jupiter.api.Test)79 MethodParameter (org.springframework.core.MethodParameter)50 BeforeEach (org.junit.jupiter.api.BeforeEach)41 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)32 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)30 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)28 Method (java.lang.reflect.Method)21 Test (org.junit.Test)21 MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)21 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)18 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)14 TestBean (org.springframework.beans.testfixture.beans.TestBean)14 RequestParam (org.springframework.web.bind.annotation.RequestParam)14 MockPart (org.springframework.web.testfixture.servlet.MockPart)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 MultipartFile (org.springframework.web.multipart.MultipartFile)13 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)11 IOException (java.io.IOException)10