Search in sources :

Example 11 with ServletServerHttpResponse

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

the class HttpSockJsSessionTests method setup.

@Before
public void setup() {
    super.setUp();
    this.frameFormat = new DefaultSockJsFrameFormat("%s");
    this.servletResponse = new MockHttpServletResponse();
    this.response = new ServletServerHttpResponse(this.servletResponse);
    this.servletRequest = new MockHttpServletRequest();
    this.servletRequest.setAsyncSupported(true);
    this.request = new ServletServerHttpRequest(this.servletRequest);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) DefaultSockJsFrameFormat(org.springframework.web.socket.sockjs.frame.DefaultSockJsFrameFormat) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 12 with ServletServerHttpResponse

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

the class SockJsServiceTests method handleInfoGetWildflyNPE.

// SPR-11919
@Test
public void handleInfoGetWildflyNPE() throws Exception {
    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    ServletOutputStream ous = mock(ServletOutputStream.class);
    given(mockResponse.getHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).willThrow(NullPointerException.class);
    given(mockResponse.getOutputStream()).willReturn(ous);
    this.response = new ServletServerHttpResponse(mockResponse);
    handleRequest("GET", "/echo/info", HttpStatus.OK);
    verify(mockResponse, times(1)).getOutputStream();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) Test(org.junit.Test)

Example 13 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse 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 14 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse 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 15 with ServletServerHttpResponse

use of org.springframework.http.server.ServletServerHttpResponse 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)

Aggregations

ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)28 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)18 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 Before (org.junit.Before)6 Test (org.junit.Test)6 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)6 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)4 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)4 ResponseEntity (org.springframework.http.ResponseEntity)3 ServletRequest (javax.servlet.ServletRequest)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpHeaders (org.springframework.http.HttpHeaders)2 MediaType (org.springframework.http.MediaType)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 HandshakeFailureException (org.springframework.web.socket.server.HandshakeFailureException)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ByteChannel (java.nio.channels.ByteChannel)1