Search in sources :

Example 11 with ResponseEntity

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

the class ResponseBodyEmitterReturnValueHandler method handleReturnValue.

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }
    HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
    ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
    if (returnValue instanceof ResponseEntity) {
        ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
        response.setStatus(responseEntity.getStatusCodeValue());
        outputMessage.getHeaders().putAll(responseEntity.getHeaders());
        returnValue = responseEntity.getBody();
        if (returnValue == null) {
            mavContainer.setRequestHandled(true);
            outputMessage.flush();
            return;
        }
    }
    ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
    ShallowEtagHeaderFilter.disableContentCaching(request);
    ResponseBodyEmitterAdapter adapter = getAdapterFor(returnValue.getClass());
    if (adapter == null) {
        throw new IllegalStateException("Could not find ResponseBodyEmitterAdapter for return value type: " + returnValue.getClass());
    }
    ResponseBodyEmitter emitter = adapter.adaptToEmitter(returnValue, outputMessage);
    emitter.extendResponse(outputMessage);
    // Commit the response and wrap to ignore further header changes
    outputMessage.getBody();
    outputMessage.flush();
    outputMessage = new StreamingServletServerHttpResponse(outputMessage);
    DeferredResult<?> deferredResult = new DeferredResult<>(emitter.getTimeout());
    WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
    HttpMessageConvertingHandler handler = new HttpMessageConvertingHandler(outputMessage, deferredResult);
    emitter.initialize(handler);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ResponseEntity(org.springframework.http.ResponseEntity) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

Example 12 with ResponseEntity

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

the class StreamingResponseBodyReturnValueHandler method handleReturnValue.

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }
    HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
    ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
    if (returnValue instanceof ResponseEntity) {
        ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
        response.setStatus(responseEntity.getStatusCodeValue());
        outputMessage.getHeaders().putAll(responseEntity.getHeaders());
        returnValue = responseEntity.getBody();
        if (returnValue == null) {
            mavContainer.setRequestHandled(true);
            outputMessage.flush();
            return;
        }
    }
    ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
    ShallowEtagHeaderFilter.disableContentCaching(request);
    Assert.isInstanceOf(StreamingResponseBody.class, returnValue, "StreamingResponseBody expected");
    StreamingResponseBody streamingBody = (StreamingResponseBody) returnValue;
    Callable<Void> callable = new StreamingResponseBodyTask(outputMessage.getBody(), streamingBody);
    WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer);
}
Also used : ServletRequest(javax.servlet.ServletRequest) ResponseEntity(org.springframework.http.ResponseEntity) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 13 with ResponseEntity

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

the class HttpEntityMethodProcessorMockTests method shouldFailHandlingWhenConverterCannotWrite.

@Test
public void shouldFailHandlingWhenConverterCannotWrite() throws Exception {
    String body = "Foo";
    ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
    MediaType accepted = MediaType.TEXT_PLAIN;
    servletRequest.addHeader("Accept", accepted.toString());
    given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true);
    given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    given(stringHttpMessageConverter.canWrite(String.class, accepted)).willReturn(false);
    this.thrown.expect(HttpMediaTypeNotAcceptableException.class);
    processor.handleReturnValue(returnValue, returnTypeResponseEntityProduces, mavContainer, webRequest);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 14 with ResponseEntity

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

the class HttpEntityMethodProcessorMockTests method shouldFailHandlingWhenContentTypeNotSupported.

@Test
public void shouldFailHandlingWhenContentTypeNotSupported() throws Exception {
    String body = "Foo";
    ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
    MediaType accepted = MediaType.APPLICATION_ATOM_XML;
    servletRequest.addHeader("Accept", accepted.toString());
    given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true);
    given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    this.thrown.expect(HttpMediaTypeNotAcceptableException.class);
    processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 15 with ResponseEntity

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

the class HttpEntityMethodProcessorMockTests method shouldHandleReturnValue.

@Test
public void shouldHandleReturnValue() throws Exception {
    String body = "Foo";
    ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
    MediaType accepted = MediaType.TEXT_PLAIN;
    servletRequest.addHeader("Accept", accepted.toString());
    initStringMessageConversion(accepted);
    processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
    assertTrue(mavContainer.isRequestHandled());
    verify(stringHttpMessageConverter).write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpOutputMessage(org.springframework.http.HttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Aggregations

ResponseEntity (org.springframework.http.ResponseEntity)165 HttpHeaders (org.springframework.http.HttpHeaders)62 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)56 Locale (java.util.Locale)32 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)29 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)24 Test (org.junit.Test)19 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)16 ApiOperation (io.swagger.annotations.ApiOperation)15 ApiResponses (io.swagger.annotations.ApiResponses)15 MediaType (org.springframework.http.MediaType)13 List (java.util.List)12 HttpEntity (org.springframework.http.HttpEntity)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 HttpStatus (org.springframework.http.HttpStatus)10 InputStream (java.io.InputStream)9 Type (java.lang.reflect.Type)8 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)8 IOException (java.io.IOException)7