Search in sources :

Example 66 with ServletWebRequest

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileList.

@Test
public void resolveMultipartFileList() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected1 = new MockMultipartFile("mfilelist", "Hello World 1".getBytes());
    MultipartFile expected2 = new MockMultipartFile("mfilelist", "Hello World 2".getBytes());
    request.addFile(expected1);
    request.addFile(expected2);
    request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof List);
    assertEquals(Arrays.asList(expected1, expected2), result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 67 with ServletWebRequest

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

the class CookieValueMethodArgumentResolverTests method setUp.

@Before
public void setUp() throws Exception {
    resolver = new TestCookieValueMethodArgumentResolver();
    Method method = getClass().getMethod("params", Cookie.class, String.class, String.class);
    paramNamedCookie = new SynthesizingMethodParameter(method, 0);
    paramNamedDefaultValueString = new SynthesizingMethodParameter(method, 1);
    paramString = new SynthesizingMethodParameter(method, 2);
    request = new MockHttpServletRequest();
    webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Method(java.lang.reflect.Method) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 68 with ServletWebRequest

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

the class ErrorsMethodHandlerArgumentResolverTests method setUp.

@Before
public void setUp() throws Exception {
    paramErrors = new MethodParameter(getClass().getDeclaredMethod("handle", Errors.class), 0);
    bindingResult = new WebDataBinder(new Object(), "attr").getBindingResult();
    webRequest = new ServletWebRequest(new MockHttpServletRequest());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Before(org.junit.Before)

Example 69 with ServletWebRequest

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

the class ContentNegotiatingViewResolver method getMediaTypes.

/**
	 * Determines the list of {@link MediaType} for the given {@link HttpServletRequest}.
	 * @param request the current servlet request
	 * @return the list of media types requested, if any
	 */
protected List<MediaType> getMediaTypes(HttpServletRequest request) {
    try {
        ServletWebRequest webRequest = new ServletWebRequest(request);
        List<MediaType> acceptableMediaTypes = this.contentNegotiationManager.resolveMediaTypes(webRequest);
        acceptableMediaTypes = (!acceptableMediaTypes.isEmpty() ? acceptableMediaTypes : Collections.singletonList(MediaType.ALL));
        List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request);
        Set<MediaType> compatibleMediaTypes = new LinkedHashSet<>();
        for (MediaType acceptable : acceptableMediaTypes) {
            for (MediaType producible : producibleMediaTypes) {
                if (acceptable.isCompatibleWith(producible)) {
                    compatibleMediaTypes.add(getMostSpecificMediaType(acceptable, producible));
                }
            }
        }
        List<MediaType> selectedMediaTypes = new ArrayList<>(compatibleMediaTypes);
        MediaType.sortBySpecificityAndQuality(selectedMediaTypes);
        if (logger.isDebugEnabled()) {
            logger.debug("Requested media types are " + selectedMediaTypes + " based on Accept header types " + "and producible media types " + producibleMediaTypes + ")");
        }
        return selectedMediaTypes;
    } catch (HttpMediaTypeNotAcceptableException ex) {
        return null;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 70 with ServletWebRequest

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

the class ResourceHttpRequestHandler method handleRequest.

/**
	 * Processes a resource request.
	 * <p>Checks for the existence of the requested resource in the configured list of locations.
	 * If the resource does not exist, a {@code 404} response will be returned to the client.
	 * If the resource exists, the request will be checked for the presence of the
	 * {@code Last-Modified} header, and its value will be compared against the last-modified
	 * timestamp of the given resource, returning a {@code 304} status code if the
	 * {@code Last-Modified} value  is greater. If the resource is newer than the
	 * {@code Last-Modified} value, or the header is not present, the content resource
	 * of the resource will be written to the response with caching headers
	 * set to expire one year in the future.
	 */
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // For very general mappings (e.g. "/") we need to check 404 first
    Resource resource = getResource(request);
    if (resource == null) {
        logger.trace("No matching resource found - returning 404");
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    if (HttpMethod.OPTIONS.matches(request.getMethod())) {
        response.setHeader("Allow", getAllowHeader());
        return;
    }
    // Supported methods and required session
    checkRequest(request);
    // Header phase
    if (new ServletWebRequest(request, response).checkNotModified(resource.lastModified())) {
        logger.trace("Resource not modified - returning 304");
        return;
    }
    // Apply cache settings, if any
    prepareResponse(response);
    // Check the media type for the resource
    MediaType mediaType = getMediaType(request, resource);
    if (mediaType != null) {
        if (logger.isTraceEnabled()) {
            logger.trace("Determined media type '" + mediaType + "' for " + resource);
        }
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("No media type found for " + resource + " - not sending a content-type header");
        }
    }
    // Content phase
    if (METHOD_HEAD.equals(request.getMethod())) {
        setHeaders(response, resource, mediaType);
        logger.trace("HEAD request - skipping content");
        return;
    }
    ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
    if (request.getHeader(HttpHeaders.RANGE) == null) {
        setHeaders(response, resource, mediaType);
        this.resourceHttpMessageConverter.write(resource, mediaType, outputMessage);
    } else {
        response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
        ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(request);
        try {
            List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
            response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
            if (httpRanges.size() == 1) {
                ResourceRegion resourceRegion = httpRanges.get(0).toResourceRegion(resource);
                this.resourceRegionHttpMessageConverter.write(resourceRegion, mediaType, outputMessage);
            } else {
                this.resourceRegionHttpMessageConverter.write(HttpRange.toResourceRegions(httpRanges, resource), mediaType, outputMessage);
            }
        } catch (IllegalArgumentException ex) {
            response.setHeader("Content-Range", "bytes */" + resource.contentLength());
            response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
        }
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ResourceRegion(org.springframework.core.io.support.ResourceRegion) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HttpRange(org.springframework.http.HttpRange)

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