List of usage examples for org.springframework.web.client HttpClientErrorException getStatusCode
public HttpStatus getStatusCode()
From source file:org.apache.zeppelin.livy.BaseLivyInterpreter.java
private String callRestAPI(String targetURL, String method, String jsonData) throws LivyException { targetURL = livyURL + targetURL;// w w w . j a v a 2s . c om LOGGER.debug("Call rest api in {}, method: {}, jsonData: {}", targetURL, method, jsonData); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE); headers.add("X-Requested-By", "zeppelin"); for (Map.Entry<String, String> entry : customHeaders.entrySet()) { headers.add(entry.getKey(), entry.getValue()); } 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(cause.getResponseBodyAsString() + "\n" + ExceptionUtils.getFullStackTrace(ExceptionUtils.getRootCause(e))); } if (e instanceof HttpServerErrorException) { HttpServerErrorException errorException = (HttpServerErrorException) e; String errorResponse = errorException.getResponseBodyAsString(); if (errorResponse.contains("Session is in state dead")) { throw new SessionDeadException(); } throw new LivyException(errorResponse, e); } 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)); } }
From source file:com.daon.identityx.controller.SimpleController.java
/** * The a web method throws an exception with a http status, then we controller will pass this detail * back to the client.//from ww w .j a v a 2 s. com * * @param ex * @return */ @ExceptionHandler(HttpClientErrorException.class) @ResponseBody public Error handleHttpExceptions(HttpClientErrorException ex, HttpServletResponse response) { logger.error("An unexpected exception occurred while attempting to process the request. Exception: " + ex.getMessage()); response.setStatus(ex.getStatusCode().value()); return new Error(ex.getStatusCode().value(), ex.getStatusText()); }
From source file:com.thinkbiganalytics.metadata.rest.client.MetadataClient.java
private <R> Optional<R> optinal(Supplier<R> supplier) { try {/*from w w w .j a v a2s . co m*/ return Optional.ofNullable(supplier.get()); } catch (HttpClientErrorException e) { if (e.getStatusCode() == HttpStatus.NOT_FOUND) { return Optional.empty(); } else { throw e; } } }
From source file:com.thinkbiganalytics.metadata.rest.client.MetadataClient.java
private <R> R nullable(Supplier<R> supplier) { try {/*from w w w .ja v a2s. co m*/ return supplier.get(); } catch (HttpClientErrorException e) { if (e.getStatusCode() == HttpStatus.NOT_FOUND) { return null; } else { throw e; } } }
From source file:com.thinkbiganalytics.metadata.rest.client.MetadataClient.java
/** * Gets the data source with the specified id. * * @param id the data source id/*from w w w. j a v a 2s. c o m*/ * @return the data source, if found * @throws RestClientException if the data source is unavailable */ public Optional<Datasource> getDatasource(@Nonnull final String id) { try { return Optional.of(get(path("datasource", id), uri -> (uri != null) ? uri.queryParam("sensitive", true) : null, Datasource.class)); } catch (final HttpClientErrorException e) { if (e.getStatusCode() == HttpStatus.NOT_FOUND) { return Optional.empty(); } else { throw e; } } }
From source file:com.ge.predix.integration.test.PrivilegeManagementAccessControlServiceIT.java
@Test(dataProvider = "subjectProvider") public void testPutGetDeleteSubject(final BaseSubject subject) throws UnsupportedEncodingException { ResponseEntity<BaseSubject> responseEntity = null; try {/* w w w. j a v a 2 s .co m*/ this.privilegeHelper.putSubject(this.acsAdminRestTemplate, subject, this.acsUrl, this.zone1Headers, this.privilegeHelper.getDefaultAttribute()); } catch (HttpClientErrorException e) { Assert.fail("Unable to create subject."); } String encodedSubjectIdentifier = URLEncoder.encode(subject.getSubjectIdentifier(), "UTF-8"); URI uri = URI.create(this.acsUrl + PrivilegeHelper.ACS_SUBJECT_API_PATH + encodedSubjectIdentifier); try { responseEntity = this.acsAdminRestTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(this.zone1Headers), BaseSubject.class); Assert.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK); } catch (HttpClientErrorException e) { Assert.fail("Unable to get subject."); } try { this.acsAdminRestTemplate.exchange(uri, HttpMethod.DELETE, new HttpEntity<>(this.zone1Headers), ResponseEntity.class); responseEntity = this.acsAdminRestTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(this.zone1Headers), BaseSubject.class); Assert.fail("Subject " + subject.getSubjectIdentifier() + " was not properly deleted"); } catch (HttpClientErrorException e) { Assert.assertEquals(e.getStatusCode(), HttpStatus.NOT_FOUND, "Subject was not deleted."); } }
From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java
private AccessGrant refreshAccessToken() throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException { log.debug("Refreshing Access Token for " + AuthenticationUtil.getRunAsUser()); OAuth2CredentialsInfo credentialInfo = oauth2CredentialsStoreService .getPersonalOAuth2Credentials(GoogleDocsConstants.REMOTE_SYSTEM); if (credentialInfo.getOAuthRefreshToken() != null) { AccessGrant accessGrant = null;/*from ww w. j av a 2 s .c om*/ try { accessGrant = connectionFactory.getOAuthOperations() .refreshAccess(credentialInfo.getOAuthRefreshToken(), GoogleDocsConstants.SCOPE, null); } catch (HttpClientErrorException hcee) { if (hcee.getStatusCode().value() == HttpStatus.SC_BAD_REQUEST) { throw new GoogleDocsAuthenticationException(hcee.getMessage()); } else if (hcee.getStatusCode().value() == HttpStatus.SC_UNAUTHORIZED) { throw new GoogleDocsAuthenticationException("Token Refresh Failed."); } else { throw new GoogleDocsServiceException(hcee.getMessage(), hcee.getStatusCode().value()); } } if (accessGrant != null) { Date expiresIn = null; if (accessGrant.getExpireTime() != null) { if (accessGrant.getExpireTime() > 0L) { expiresIn = new Date(new Date().getTime() + accessGrant.getExpireTime()); } } try { oauth2CredentialsStoreService.storePersonalOAuth2Credentials(GoogleDocsConstants.REMOTE_SYSTEM, accessGrant.getAccessToken(), credentialInfo.getOAuthRefreshToken(), expiresIn, new Date()); } catch (NoSuchSystemException nsse) { throw nsse; } } else { throw new GoogleDocsAuthenticationException("No Access Grant Returned."); } log.debug("Access Token Refreshed"); return accessGrant; } else { throw new GoogleDocsRefreshTokenException( "No Refresh Token Provided for " + AuthenticationUtil.getRunAsUser()); } }