Search in sources :

Example 41 with RestClientException

use of org.springframework.web.client.RestClientException in project yorc-a4c-plugin by ystia.

the class RestClient method sendRequest.

/**
 * Common used method to send REST request
 * @param targetUrl
 * @param httpMethod
 * @param responseType
 * @param httpEntity
 * @param <T>
 * @return ResponseEntity<T>
 * @throws Exception
 */
private <T> ResponseEntity<T> sendRequest(String targetUrl, HttpMethod httpMethod, Class<T> responseType, HttpEntity httpEntity) throws Exception {
    ResponseEntity<T> resp = null;
    try {
        log.debug("Request URI         : {}", targetUrl);
        log.debug("Method      : {}", httpMethod);
        if (httpEntity.getHeaders() != null) {
            log.debug("Headers     : {}", httpEntity.getHeaders());
        }
        if (httpEntity.getBody() != null) {
            log.debug("Request body: {}", httpEntity.getBody().toString());
        }
        resp = restTemplate.exchange(targetUrl, httpMethod, httpEntity, responseType);
        log.debug("Response Status code  : {}", resp.getStatusCode());
        if (resp.getHeaders() != null) {
            log.debug("Response Headers      : {}", resp.getHeaders());
        }
        if (resp.getBody() != null) {
            log.debug("Response Body: {}", resp.getBody().toString());
        }
        return resp;
    } catch (HttpStatusCodeException e) {
        log.debug("Status code exception: {}", e.getRawStatusCode());
        if (!isStatusCodeOk(e.getRawStatusCode())) {
            log.debug("Response Body errors: {}", e.getResponseBodyAsString());
            ErrorsResponse errors = objectMapper.readValue(e.getResponseBodyAsString(), ErrorsResponse.class);
            YorcError error = errors.getErrors().iterator().next();
            throw YorcRestException.fromYorcError(error);
        }
    } catch (RestClientException e) {
        throw new Exception("An error occurred while calling " + httpMethod + " " + targetUrl, e);
    }
    return resp;
}
Also used : RestClientException(org.springframework.web.client.RestClientException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RestClientException(org.springframework.web.client.RestClientException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) PluginConfigurationException(alien4cloud.paas.exception.PluginConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 42 with RestClientException

use of org.springframework.web.client.RestClientException in project nixmash-blog by mintster.

the class WebUI method getGitHubStats.

public GitHubDTO getGitHubStats() {
    String gitHubRepoUrl = environment.getProperty("github.repo.url");
    String gitHubUserUrl = environment.getProperty("github.user.url");
    // Load Repository JSON elements into GitHubDTO Object
    GitHubDTO gitHubDTO = new GitHubDTO();
    RestTemplate restTemplate = new RestTemplate();
    try {
        gitHubDTO = restTemplate.getForObject(gitHubRepoUrl, GitHubDTO.class);
    } catch (RestClientException e) {
        gitHubDTO.setIsEmpty(true);
        return gitHubDTO;
    }
    // Load User Followers count from GitHub User JSON Endpoint and add to GitHubDTO
    HttpEntity<String> stringUserEntity = restTemplate.getForEntity(gitHubUserUrl, String.class);
    ObjectMapper mapper = new ObjectMapper();
    try {
        ObjectNode node = mapper.readValue(stringUserEntity.getBody(), ObjectNode.class);
        gitHubDTO.setFollowers(node.get("followers").intValue());
    } catch (IOException e) {
        logger.error("Error adding follower count from GitHub API to GitHubDTO object");
    }
    return gitHubDTO;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GitHubDTO(com.nixmash.blog.jpa.dto.GitHubDTO) RestTemplate(org.springframework.web.client.RestTemplate) RestClientException(org.springframework.web.client.RestClientException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 43 with RestClientException

use of org.springframework.web.client.RestClientException in project nixmash-blog by mintster.

the class GithubJobUI method getGitHubStats.

public GitHubDTO getGitHubStats() {
    String gitHubRepoUrl = environment.getProperty("github.job.repo.url");
    String gitHubUserUrl = environment.getProperty("github.job.user.url");
    // Load Repository JSON elements into GitHubDTO Object
    GitHubDTO gitHubDTO = new GitHubDTO();
    RestTemplate restTemplate = new RestTemplate();
    try {
        gitHubDTO = restTemplate.getForObject(gitHubRepoUrl, GitHubDTO.class);
    } catch (RestClientException e) {
        gitHubDTO.setIsEmpty(true);
        return gitHubDTO;
    }
    // Load User Followers count from GitHub User JSON Endpoint and add to GitHubDTO
    HttpEntity<String> stringUserEntity = restTemplate.getForEntity(gitHubUserUrl, String.class);
    ObjectMapper mapper = new ObjectMapper();
    try {
        ObjectNode node = mapper.readValue(stringUserEntity.getBody(), ObjectNode.class);
        gitHubDTO.setFollowers(node.get("followers").intValue());
    } catch (IOException e) {
        logger.error("Error adding follower count from GitHub API to GitHubDTO object");
    }
    return gitHubDTO;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GitHubDTO(com.nixmash.blog.jpa.dto.GitHubDTO) RestTemplate(org.springframework.web.client.RestTemplate) RestClientException(org.springframework.web.client.RestClientException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

RestClientException (org.springframework.web.client.RestClientException)43 HttpHeaders (org.springframework.http.HttpHeaders)12 HttpEntity (org.springframework.http.HttpEntity)11 IOException (java.io.IOException)9 RestTemplate (org.springframework.web.client.RestTemplate)9 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)8 Test (org.junit.Test)5 JsonParseException (com.fasterxml.jackson.core.JsonParseException)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)4 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)4 URI (java.net.URI)3 List (java.util.List)3 Test (org.junit.jupiter.api.Test)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 RealNameAuthentication (com.itrus.portal.db.RealNameAuthentication)2 RealNameAuthenticationExample (com.itrus.portal.db.RealNameAuthenticationExample)2 GitHubDTO (com.nixmash.blog.jpa.dto.GitHubDTO)2