Search in sources :

Example 51 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequest.

@Test
public void fromHttpRequest() throws URISyntaxException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.setRequestURI("/path");
    request.setQueryString("a=1");
    UriComponents result = UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)).build();
    assertEquals("http", result.getScheme());
    assertEquals("localhost", result.getHost());
    assertEquals(-1, result.getPort());
    assertEquals("/path", result.getPath());
    assertEquals("a=1", result.getQuery());
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 52 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessor method handleReturnValue.

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
    mavContainer.setRequestHandled(true);
    ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
    ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);
    // Try even with null return value. ResponseBodyAdvice could get involved.
    writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 53 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class AbstractMessageConverterMethodProcessor method writeWithMessageConverters.

/**
	 * Writes the given return value to the given web request. Delegates to
	 * {@link #writeWithMessageConverters(Object, MethodParameter, ServletServerHttpRequest, ServletServerHttpResponse)}
	 */
protected <T> void writeWithMessageConverters(T value, MethodParameter returnType, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
    ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
    ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);
    writeWithMessageConverters(value, returnType, inputMessage, outputMessage);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 54 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class HttpEntityMethodProcessor method handleReturnValue.

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    mavContainer.setRequestHandled(true);
    if (returnValue == null) {
        return;
    }
    ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
    ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);
    Assert.isInstanceOf(HttpEntity.class, returnValue);
    HttpEntity<?> responseEntity = (HttpEntity<?>) returnValue;
    HttpHeaders outputHeaders = outputMessage.getHeaders();
    HttpHeaders entityHeaders = responseEntity.getHeaders();
    if (outputHeaders.containsKey(HttpHeaders.VARY) && entityHeaders.containsKey(HttpHeaders.VARY)) {
        List<String> values = getVaryRequestHeadersToAdd(outputHeaders, entityHeaders);
        if (!values.isEmpty()) {
            outputHeaders.setVary(values);
        }
    }
    if (!entityHeaders.isEmpty()) {
        for (Map.Entry<String, List<String>> entry : entityHeaders.entrySet()) {
            if (!outputHeaders.containsKey(entry.getKey())) {
                outputHeaders.put(entry.getKey(), entry.getValue());
            }
        }
    }
    if (responseEntity instanceof ResponseEntity) {
        int returnStatus = ((ResponseEntity<?>) responseEntity).getStatusCodeValue();
        outputMessage.getServletResponse().setStatus(returnStatus);
        if (returnStatus == 200) {
            if (isResourceNotModified(inputMessage, outputMessage)) {
                // Ensure headers are flushed, no body should be written.
                outputMessage.flush();
                // Skip call to converters, as they may update the body.
                return;
            }
        } else if (returnStatus / 100 == 3) {
            String location = outputHeaders.getFirst("location");
            if (location != null) {
                saveFlashAttributes(mavContainer, webRequest, location);
            }
        }
    }
    // Try even with null body. ResponseBodyAdvice could get involved.
    writeWithMessageConverters(responseEntity.getBody(), returnType, inputMessage, outputMessage);
    // Ensure headers are flushed even if no body was written.
    outputMessage.flush();
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) ModelMap(org.springframework.ui.ModelMap) Map(java.util.Map)

Example 55 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class HttpEntityMethodProcessor method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws IOException, HttpMediaTypeNotSupportedException {
    ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
    Type paramType = getHttpEntityType(parameter);
    if (paramType == null) {
        throw new IllegalArgumentException("HttpEntity parameter '" + parameter.getParameterName() + "' in method " + parameter.getMethod() + " is not parameterized");
    }
    Object body = readWithMessageConverters(webRequest, parameter, paramType);
    if (RequestEntity.class == parameter.getParameterType()) {
        return new RequestEntity<>(body, inputMessage.getHeaders(), inputMessage.getMethod(), inputMessage.getURI());
    } else {
        return new HttpEntity<>(body, inputMessage.getHeaders());
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ResolvableType(org.springframework.core.ResolvableType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) HttpEntity(org.springframework.http.HttpEntity) RequestEntity(org.springframework.http.RequestEntity)

Aggregations

ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)65 Test (org.junit.Test)33 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)29 HttpRequest (org.springframework.http.HttpRequest)20 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Before (org.junit.Before)8 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 HttpInputMessage (org.springframework.http.HttpInputMessage)5 MediaType (org.springframework.http.MediaType)4 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)4 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpEntity (org.springframework.http.HttpEntity)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1