Search in sources :

Example 16 with ResponseEntity

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

the class ServletInvocableHandlerMethodTests method wrapConcurrentResult_ResponseEntityNullBody.

// SPR-12287
@Test
public void wrapConcurrentResult_ResponseEntityNullBody() throws Exception {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new StringHttpMessageConverter());
    List<Object> advice = Collections.singletonList(mock(ResponseBodyAdvice.class));
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null, advice);
    this.returnValueHandlers.addHandler(processor);
    ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handleDeferred");
    handlerMethod = handlerMethod.wrapConcurrentResult(new ResponseEntity<>(HttpStatus.OK));
    handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
    assertEquals(200, this.response.getStatus());
    assertEquals("", this.response.getContentAsString());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ArrayList(java.util.ArrayList) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 17 with ResponseEntity

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

the class ServletInvocableHandlerMethodTests method wrapConcurrentResult_ResponseEntity.

@Test
public void wrapConcurrentResult_ResponseEntity() throws Exception {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new StringHttpMessageConverter());
    this.returnValueHandlers.addHandler(new HttpEntityMethodProcessor(converters));
    ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handleDeferred");
    handlerMethod = handlerMethod.wrapConcurrentResult(new ResponseEntity<>("bar", HttpStatus.OK));
    handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
    assertEquals("bar", this.response.getContentAsString());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ArrayList(java.util.ArrayList) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 18 with ResponseEntity

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

the class UndertowXhrTransport method executeRequest.

protected ResponseEntity<String> executeRequest(URI url, HttpString method, HttpHeaders headers, String body) {
    CountDownLatch latch = new CountDownLatch(1);
    List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    try {
        ClientConnection connection = this.httpClient.connect(url, this.worker, this.bufferPool, this.optionMap).get();
        try {
            ClientRequest request = new ClientRequest().setMethod(method).setPath(url.getPath());
            request.getRequestHeaders().add(HttpString.tryFromString(HttpHeaders.HOST), url.getHost());
            if (body != null && !body.isEmpty()) {
                HttpString headerName = HttpString.tryFromString(HttpHeaders.CONTENT_LENGTH);
                request.getRequestHeaders().add(headerName, body.length());
            }
            addHttpHeaders(request, headers);
            connection.sendRequest(request, createRequestCallback(body, responses, latch));
            latch.await();
            ClientResponse response = responses.iterator().next();
            HttpStatus status = HttpStatus.valueOf(response.getResponseCode());
            HttpHeaders responseHeaders = toHttpHeaders(response.getResponseHeaders());
            String responseBody = response.getAttachment(RESPONSE_BODY);
            return (responseBody != null ? new ResponseEntity<>(responseBody, responseHeaders, status) : new ResponseEntity<>(responseHeaders, status));
        } finally {
            IoUtils.safeClose(connection);
        }
    } catch (IOException ex) {
        throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
    } catch (InterruptedException ex) {
        throw new SockJsTransportFailureException("Interrupted while processing request to " + url, ex);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) ResponseEntity(org.springframework.http.ResponseEntity) ClientConnection(io.undertow.client.ClientConnection) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpString(io.undertow.util.HttpString)

Example 19 with ResponseEntity

use of org.springframework.http.ResponseEntity in project springside4 by springside.

the class CustomExceptionHandler method handleGeneralException.

@ExceptionHandler(value = { Exception.class })
public final ResponseEntity<ErrorResult> handleGeneralException(Exception ex, HttpServletRequest request) {
    logError(ex, request);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType(MediaTypes.JSON_UTF_8));
    ErrorResult result = new ErrorResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    return new ResponseEntity<ErrorResult>(result, headers, HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 20 with ResponseEntity

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

the class AbstractAuthorizationCodeProviderTests method setupAccessTokenProvider.

@BeforeOAuth2Context
public void setupAccessTokenProvider() {
    accessTokenProvider = new AuthorizationCodeAccessTokenProvider() {

        private ResponseExtractor<OAuth2AccessToken> extractor = super.getResponseExtractor();

        private ResponseExtractor<ResponseEntity<Void>> authExtractor = super.getAuthorizationResponseExtractor();

        private ResponseErrorHandler errorHandler = super.getResponseErrorHandler();

        @Override
        protected ResponseErrorHandler getResponseErrorHandler() {
            return new DefaultResponseErrorHandler() {

                public void handleError(ClientHttpResponse response) throws IOException {
                    response.getHeaders();
                    response.getStatusCode();
                    tokenEndpointResponse = response;
                    errorHandler.handleError(response);
                }
            };
        }

        @Override
        protected ResponseExtractor<OAuth2AccessToken> getResponseExtractor() {
            return new ResponseExtractor<OAuth2AccessToken>() {

                public OAuth2AccessToken extractData(ClientHttpResponse response) throws IOException {
                    try {
                        response.getHeaders();
                        response.getStatusCode();
                        tokenEndpointResponse = response;
                        return extractor.extractData(response);
                    } catch (ResourceAccessException e) {
                        return null;
                    }
                }
            };
        }

        @Override
        protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
            return new ResponseExtractor<ResponseEntity<Void>>() {

                public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
                    response.getHeaders();
                    response.getStatusCode();
                    tokenEndpointResponse = response;
                    return authExtractor.extractData(response);
                }
            };
        }
    };
    context.setAccessTokenProvider(accessTokenProvider);
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) ResponseExtractor(org.springframework.web.client.ResponseExtractor) IOException(java.io.IOException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) ResponseEntity(org.springframework.http.ResponseEntity) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) AuthorizationCodeAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) BeforeOAuth2Context(org.springframework.security.oauth2.client.test.BeforeOAuth2Context)

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