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));
}
}
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());
}
}
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");
}
}
use of org.springframework.web.client.HttpClientErrorException in project goci by EBISPOT.
the class EnsemblController method getAncestors.
public Map<String, String> getAncestors(String efoTerm) throws IOException {
Map<String, String> result = new HashMap<>();
String uri = olsServer.concat(olsEfoTerms);
if (efoTerm.contains("http")) {
uri = uri.concat("?").concat(efoTerm);
} else {
uri = uri.concat(olsShortForm).concat(efoTerm);
}
RestTemplate restTemplate = new RestTemplate();
String efoObject = null;
try {
efoObject = restTemplate.getForObject(uri, String.class);
} catch (HttpClientErrorException ex) {
result.put("error", "Term ".concat(efoTerm).concat(" not found in EFO"));
}
if (efoObject != null) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(efoObject);
JsonNode responseNode = node.get("_embedded").get("terms").get(0);
String ancestors_link = responseNode.get("_links").get("hierarchicalAncestors").get("href").asText().trim();
ancestors_link = java.net.URLDecoder.decode(ancestors_link, "UTF-8");
String label = responseNode.get("label").asText().trim();
String iri = responseNode.get("iri").asText().trim();
String ancestorsObject = restTemplate.getForObject(ancestors_link, String.class);
result.put("iri", iri);
result.put("label", label);
result.put("ancestors", ancestorsObject);
}
return result;
}
use of org.springframework.web.client.HttpClientErrorException in project spring-security-oauth by spring-projects.
the class AbstractAuthorizationCodeProviderTests method testUnauthenticatedAuthorizationRespondsUnauthorized.
@Test
@OAuth2ContextConfiguration(resource = MyTrustedClient.class, initialize = false)
public void testUnauthenticatedAuthorizationRespondsUnauthorized() throws Exception {
AccessTokenRequest request = context.getAccessTokenRequest();
request.setCurrentUri("http://anywhere");
request.add(OAuth2Utils.USER_OAUTH_APPROVAL, "true");
try {
String code = accessTokenProvider.obtainAuthorizationCode(context.getResource(), request);
assertNotNull(code);
fail("Expected UserRedirectRequiredException");
} catch (HttpClientErrorException e) {
assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
}
}
Aggregations