Search in sources :

Example 56 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project brave by openzipkin.

the class ClientHttpResponseWrapperTest method statusCode_fromHttpStatusCodeException.

@Test
public void statusCode_fromHttpStatusCodeException() {
    HttpClientErrorException ex = new HttpClientErrorException(HttpStatus.BAD_REQUEST);
    assertThat(new ClientHttpResponseWrapper(request, null, ex).statusCode()).isEqualTo(400);
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ClientHttpResponseWrapper(brave.spring.web.TracingClientHttpRequestInterceptor.ClientHttpResponseWrapper) Test(org.junit.Test)

Example 57 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project open-kilda by telstra.

the class FlowSteps method eachFlowCanNotBeReadFromNorthbound.

@And("^each flow can not be read from Northbound$")
public void eachFlowCanNotBeReadFromNorthbound() {
    for (FlowPayload flow : flows) {
        FlowPayload result = null;
        try {
            result = Failsafe.with(retryPolicy().abortWhen(null).handleResultIf(Objects::nonNull)).get(() -> northboundService.getFlow(flow.getId()));
        } catch (HttpClientErrorException ex) {
            log.info(format("The flow '%s' doesn't exist. It is expected.", flow.getId()));
        }
        assertNull(format("The flow '%s' exists.", flow.getId()), result);
    }
}
Also used : FlowPayload(org.openkilda.messaging.payload.flow.FlowPayload) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) And(cucumber.api.java.en.And)

Example 58 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project zeppelin by apache.

the class BaseLivyInterprereter method callRestAPI.

private String callRestAPI(String targetURL, String method, String jsonData) throws LivyException {
    targetURL = livyURL + targetURL;
    LOGGER.debug("Call rest api in {}, method: {}, jsonData: {}", targetURL, method, jsonData);
    RestTemplate restTemplate = getRestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json");
    headers.add("X-Requested-By", "zeppelin");
    ResponseEntity<String> response = null;
    try {
        if (method.equals("POST")) {
            HttpEntity<String> entity = new HttpEntity<>(jsonData, headers);
            response = restTemplate.exchange(targetURL, HttpMethod.POST, entity, String.class);
        } else if (method.equals("GET")) {
            HttpEntity<String> entity = new HttpEntity<>(headers);
            response = restTemplate.exchange(targetURL, HttpMethod.GET, entity, String.class);
        } else if (method.equals("DELETE")) {
            HttpEntity<String> entity = new HttpEntity<>(headers);
            response = restTemplate.exchange(targetURL, HttpMethod.DELETE, entity, String.class);
        }
    } catch (HttpClientErrorException e) {
        response = new ResponseEntity(e.getResponseBodyAsString(), e.getStatusCode());
        LOGGER.error(String.format("Error with %s StatusCode: %s", response.getStatusCode().value(), e.getResponseBodyAsString()));
    } catch (RestClientException e) {
        // Exception happens when kerberos is enabled.
        if (e.getCause() instanceof HttpClientErrorException) {
            HttpClientErrorException cause = (HttpClientErrorException) e.getCause();
            if (cause.getResponseBodyAsString().matches(SESSION_NOT_FOUND_PATTERN)) {
                throw new SessionNotFoundException(cause.getResponseBodyAsString());
            }
        }
        throw new LivyException(e);
    }
    if (response == null) {
        throw new LivyException("No http response returned");
    }
    LOGGER.debug("Get response, StatusCode: {}, responseBody: {}", response.getStatusCode(), response.getBody());
    if (response.getStatusCode().value() == 200 || response.getStatusCode().value() == 201) {
        return response.getBody();
    } else if (response.getStatusCode().value() == 404) {
        if (response.getBody().matches(SESSION_NOT_FOUND_PATTERN)) {
            throw new SessionNotFoundException(response.getBody());
        } else {
            throw new APINotFoundException("No rest api found for " + targetURL + ", " + response.getStatusCode());
        }
    } else {
        String responseString = response.getBody();
        if (responseString.contains("CreateInteractiveRequest[\\\"master\\\"]")) {
            return responseString;
        }
        throw new LivyException(String.format("Error with %s StatusCode: %s", response.getStatusCode().value(), responseString));
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) ResponseEntity(org.springframework.http.ResponseEntity) KerberosRestTemplate(org.springframework.security.kerberos.client.KerberosRestTemplate) RestTemplate(org.springframework.web.client.RestTemplate) RestClientException(org.springframework.web.client.RestClientException)

Example 59 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project spring-framework by spring-projects.

the class SimpleUrlHandlerMappingIntegrationTests method testHandlerNotFound.

@Test
public void testHandlerNotFound() throws Exception {
    URI url = new URI("http://localhost:" + this.port + "/oops");
    RequestEntity<Void> request = RequestEntity.get(url).build();
    try {
        new RestTemplate().exchange(request, byte[].class);
    } catch (HttpClientErrorException ex) {
        assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode());
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) RestTemplate(org.springframework.web.client.RestTemplate) URI(java.net.URI) Test(org.junit.Test)

Example 60 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project spring-boot by spring-projects.

the class CloudFoundrySecurityService method getAccessLevel.

/**
	 * Return the access level that should be granted to the given token.
	 * @param token the token
	 * @param applicationId the cloud foundry application ID
	 * @return the access level that should be granted
	 * @throws CloudFoundryAuthorizationException if the token is not authorized
	 */
public AccessLevel getAccessLevel(String token, String applicationId) throws CloudFoundryAuthorizationException {
    try {
        URI uri = getPermissionsUri(applicationId);
        RequestEntity<?> request = RequestEntity.get(uri).header("Authorization", "bearer " + token).build();
        Map<?, ?> body = this.restTemplate.exchange(request, Map.class).getBody();
        if (Boolean.TRUE.equals(body.get("read_sensitive_data"))) {
            return AccessLevel.FULL;
        }
        return AccessLevel.RESTRICTED;
    } catch (HttpClientErrorException ex) {
        if (ex.getStatusCode().equals(HttpStatus.FORBIDDEN)) {
            throw new CloudFoundryAuthorizationException(Reason.ACCESS_DENIED, "Access denied");
        }
        throw new CloudFoundryAuthorizationException(Reason.INVALID_TOKEN, "Invalid token", ex);
    } catch (HttpServerErrorException ex) {
        throw new CloudFoundryAuthorizationException(Reason.SERVICE_UNAVAILABLE, "Cloud controller not reachable");
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException)

Aggregations

HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)109 HttpHeaders (org.springframework.http.HttpHeaders)31 Test (org.junit.Test)25 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 HashMap (java.util.HashMap)21 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)21 MediaType (org.springframework.http.MediaType)19 RestTemplate (org.springframework.web.client.RestTemplate)19 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)18 HttpEntity (org.springframework.http.HttpEntity)16 URI (java.net.URI)15 Map (java.util.Map)8 HttpStatus (org.springframework.http.HttpStatus)8 RestClientException (org.springframework.web.client.RestClientException)8 ResourceAccessException (org.springframework.web.client.ResourceAccessException)7 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)6 Date (java.util.Date)5 List (java.util.List)5 OpenAppNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO)4 ArrayList (java.util.ArrayList)4