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;
}
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;
}
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;
}
Aggregations