Search in sources :

Example 91 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-boot by spring-projects.

the class StandardGitHub method getRepository.

@Override
public GitHubRepository getRepository(String organization, String name) {
    RestTemplate restTemplate = new RestTemplate(Arrays.asList(new MappingJackson2HttpMessageConverter(new ObjectMapper())));
    restTemplate.getInterceptors().add(new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            request.getHeaders().add("User-Agent", StandardGitHub.this.username);
            request.getHeaders().add("Authorization", "Basic " + Base64.getEncoder().encodeToString((StandardGitHub.this.username + ":" + StandardGitHub.this.password).getBytes()));
            request.getHeaders().add("Accept", MediaType.APPLICATION_JSON_VALUE);
            return execution.execute(request, body);
        }
    });
    UriTemplateHandler uriTemplateHandler = new DefaultUriBuilderFactory("https://api.github.com/repos/" + organization + "/" + name + "/");
    restTemplate.setUriTemplateHandler(uriTemplateHandler);
    return new StandardGitHubRepository(restTemplate);
}
Also used : HttpRequest(org.springframework.http.HttpRequest) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) IOException(java.io.IOException) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) UriTemplateHandler(org.springframework.web.util.UriTemplateHandler) RestTemplate(org.springframework.web.client.RestTemplate) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 92 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project esup-papercut by EsupPortail.

the class RequestResponseLoggingInterceptor method intercept.

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    logRequest(request, body);
    ClientHttpResponse response = execution.execute(request, body);
    logResponse(response);
    return response;
}
Also used : ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 93 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project paascloud-master by paascloud.

the class UacFallbackProvider method response.

private ClientHttpResponse response(final HttpStatus status) {
    return new ClientHttpResponse() {

        @Override
        public HttpStatus getStatusCode() {
            return status;
        }

        @Override
        public int getRawStatusCode() {
            return status.value();
        }

        @Override
        public String getStatusText() {
            return status.getReasonPhrase();
        }

        @Override
        public void close() {
            log.info("close");
        }

        @Override
        public InputStream getBody() {
            String message = "{\n" + "\"code\": 200,\n" + "\"message\": \"微服务故障, 请稍后再试\"\n" + "}";
            return new ByteArrayInputStream(message.getBytes());
        }

        @Override
        public HttpHeaders getHeaders() {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            return headers;
        }
    };
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 94 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project micro-continuum by codefinity.

the class YSericeFallbackProvider method response.

private ClientHttpResponse response(final HttpStatus status) {
    return new ClientHttpResponse() {

        @Override
        public HttpStatus getStatusCode() throws IOException {
            return status;
        }

        @Override
        public int getRawStatusCode() throws IOException {
            return status.value();
        }

        @Override
        public String getStatusText() throws IOException {
            return status.getReasonPhrase();
        }

        @Override
        public void close() {
        }

        @Override
        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream("fallback".getBytes());
        }

        @Override
        public HttpHeaders getHeaders() {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            return headers;
        }
    };
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 95 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-cloud-netflix by spring-cloud.

the class RibbonRoutingFilterTests method testSetResponseWithHttpStatusCode.

@Test
public void testSetResponseWithHttpStatusCode() throws Exception {
    ClientHttpResponse response = this.createClientHttpResponse();
    this.filter.setResponse(response);
    assertThat(200).isEqualTo(this.requestContext.get("responseStatusCode"));
}
Also used : ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Aggregations

ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)142 IOException (java.io.IOException)38 Test (org.junit.Test)38 HttpHeaders (org.springframework.http.HttpHeaders)35 ByteArrayInputStream (java.io.ByteArrayInputStream)23 URI (java.net.URI)23 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)23 Test (org.junit.jupiter.api.Test)21 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)20 RestTemplate (org.springframework.web.client.RestTemplate)20 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)18 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MockClientHttpResponse (org.springframework.mock.http.client.MockClientHttpResponse)9 ResponseExtractor (org.springframework.web.client.ResponseExtractor)9 Tag (io.micrometer.core.instrument.Tag)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Invocation (org.apache.servicecomb.core.Invocation)6