Search in sources :

Example 6 with ResponseEntity

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

the class RestTemplate method exchange.

@Override
public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException {
    Type type = responseType.getType();
    RequestCallback requestCallback = httpEntityCallback(requestEntity, type);
    ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type);
    return execute(url, method, requestCallback, responseExtractor);
}
Also used : MediaType(org.springframework.http.MediaType) Type(java.lang.reflect.Type) ResponseEntity(org.springframework.http.ResponseEntity)

Example 7 with ResponseEntity

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

the class AsyncRestTemplateIntegrationTests method exchangePostCallbackWithLambdas.

@Test
public void exchangePostCallbackWithLambdas() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("MyHeader", "MyValue");
    requestHeaders.setContentType(MediaType.TEXT_PLAIN);
    HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld, requestHeaders);
    ListenableFuture<ResponseEntity<Void>> resultFuture = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post");
    final URI expected = new URI(baseUrl + "/post/1");
    resultFuture.addCallback(result -> {
        assertEquals("Invalid location", expected, result.getHeaders().getLocation());
        assertFalse(result.hasBody());
    }, ex -> fail(ex.getMessage()));
    waitTillDone(resultFuture);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) URI(java.net.URI) Test(org.junit.Test)

Example 8 with ResponseEntity

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

the class AsyncRestTemplateIntegrationTests method exchangeGetCallbackWithLambdas.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void exchangeGetCallbackWithLambdas() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("MyHeader", "MyValue");
    HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
    ListenableFuture<ResponseEntity<String>> responseFuture = template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get");
    responseFuture.addCallback(result -> assertEquals("Invalid content", helloWorld, result.getBody()), ex -> fail(ex.getMessage()));
    waitTillDone(responseFuture);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) Test(org.junit.Test)

Example 9 with ResponseEntity

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

the class AsyncRestTemplateIntegrationTests method exchangePostCallback.

@Test
public void exchangePostCallback() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("MyHeader", "MyValue");
    requestHeaders.setContentType(MediaType.TEXT_PLAIN);
    HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld, requestHeaders);
    ListenableFuture<ResponseEntity<Void>> resultFuture = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post");
    final URI expected = new URI(baseUrl + "/post/1");
    resultFuture.addCallback(new ListenableFutureCallback<ResponseEntity<Void>>() {

        @Override
        public void onSuccess(ResponseEntity<Void> result) {
            assertEquals("Invalid location", expected, result.getHeaders().getLocation());
            assertFalse(result.hasBody());
        }

        @Override
        public void onFailure(Throwable ex) {
            fail(ex.getMessage());
        }
    });
    waitTillDone(resultFuture);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) URI(java.net.URI) Test(org.junit.Test)

Example 10 with ResponseEntity

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

the class ResponseEntityResultHandler method handleResult.

@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
    Mono<?> returnValueMono;
    MethodParameter bodyParameter;
    ReactiveAdapter adapter = getAdapter(result);
    if (adapter != null) {
        Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
        returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
        bodyParameter = result.getReturnTypeSource().nested().nested();
    } else {
        returnValueMono = Mono.justOrEmpty(result.getReturnValue());
        bodyParameter = result.getReturnTypeSource().nested();
    }
    return returnValueMono.then(returnValue -> {
        Assert.isInstanceOf(HttpEntity.class, returnValue, "HttpEntity expected");
        HttpEntity<?> httpEntity = (HttpEntity<?>) returnValue;
        if (httpEntity instanceof ResponseEntity) {
            ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
            exchange.getResponse().setStatusCode(responseEntity.getStatusCode());
        }
        HttpHeaders entityHeaders = httpEntity.getHeaders();
        HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
        if (!entityHeaders.isEmpty()) {
            entityHeaders.entrySet().stream().filter(entry -> !responseHeaders.containsKey(entry.getKey())).forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
        }
        if (httpEntity.getBody() == null) {
            return exchange.getResponse().setComplete();
        }
        String etag = entityHeaders.getETag();
        Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
        HttpMethod httpMethod = exchange.getRequest().getMethod();
        if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
            return exchange.getResponse().setComplete();
        }
        return writeBody(httpEntity.getBody(), bodyParameter, exchange);
    });
}
Also used : RequestEntity(org.springframework.http.RequestEntity) Arrays(java.util.Arrays) ReactiveAdapter(org.springframework.core.ReactiveAdapter) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) HandlerResult(org.springframework.web.reactive.HandlerResult) ServerWebExchange(org.springframework.web.server.ServerWebExchange) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) MethodParameter(org.springframework.core.MethodParameter) HandlerResultHandler(org.springframework.web.reactive.HandlerResultHandler) ResponseEntity(org.springframework.http.ResponseEntity) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Assert(org.springframework.util.Assert) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) Instant(java.time.Instant) MethodParameter(org.springframework.core.MethodParameter) ReactiveAdapter(org.springframework.core.ReactiveAdapter) HttpMethod(org.springframework.http.HttpMethod)

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